浏览代码

FD3-800 when the status or the host type of a host or subnet has changed, the host reservations are free

Guillermo Espinoza 6 年之前
父节点
当前提交
8c516ad240

+ 1 - 1
src/HostBundle/Entity/Host.php

@@ -134,7 +134,7 @@ class Host implements WorkflowInterface
      */
     public function __toString()
     {
-        return $this->mac ? strval($this->mac) : sprintf('%s - %s', $this->id, $this->hostType->__toString());
+        return sprintf('%s - %s', $this->mac ? strval($this->mac) : $this->id, $this->hostType->__toString());
     }
 
     /**

+ 12 - 2
src/HostBundle/EventListener/HostStatusChangeSubscriber.php

@@ -40,7 +40,7 @@ class HostStatusChangeSubscriber implements EventSubscriber
      */
     public function postUpdate(LifecycleEventArgs $args)
     {
-        $this->execute($args);
+        $this->execute($args, true);
     }
 
     /**
@@ -55,7 +55,7 @@ class HostStatusChangeSubscriber implements EventSubscriber
      * @param LifecycleEventArgs $args
      * @param boolean $remove
      */
-    public function execute(LifecycleEventArgs $args)
+    public function execute(LifecycleEventArgs $args, $updateFixedAddress = false)
     {
         $flashbag = $this->serviceContainer->get('session')->getFlashBag();
         $translator = $this->serviceContainer->get('translator');
@@ -71,6 +71,16 @@ class HostStatusChangeSubscriber implements EventSubscriber
                 // invalidating the host lease
                 $lease4 = $leaseService->invalidate($entity->getMac());
                 $flashbag->add("success", $translator->trans('host.lease.invalidate.ok', ['%hwaddr%' => $entity->getMac()], 'KeaBundle'));
+
+                // setting fixed address to null
+                if ($updateFixedAddress == true && $entity->getFixedAddress() != '') {
+                    $entity->setFixedAddress(null);
+                    $entity->setIpv4Address(null);
+                    $entity->setFixedIP(false);
+
+                    $em->persist($entity);
+                    $em->flush($entity);
+                }
             }
         } catch (\Exception $ex) {
             $flashbag->add("error", $ex->getMessage());

+ 4 - 3
src/HostBundle/Resources/translations/HostBundle.es.yml

@@ -65,7 +65,7 @@ list:
     label_extra_data: Extra Data
     label_status: Estado
     label_host: Host
-    label_associated_hosts: Hosts asociados ( ID - Tipo de host )
+    label_associated_hosts: Hosts asociados ( ID | MAC - Tipo de host )
     label_options_fixed_address: IP Fija
 
 show:
@@ -106,7 +106,7 @@ show:
     label_status: Estado
     label_fixed_address: IP Fija
     label_host: Host
-    label_associated_hosts: Hosts asociados ( ID - Tipo de host )
+    label_associated_hosts: Hosts asociados ( ID | MAC - Tipo de host )
 
 filter:
     label_mac: Mac
@@ -147,4 +147,5 @@ State: Estado
 active: Activo
 suspended: Suspendido
 disabled: Inactivo
-options.fixed_address.not_unique: La IP fija seleccionada ya esta asociada a otro host
+options.fixed_address.not_unique: La IP fija seleccionada ya esta asociada a otro host
+DHCP Option: DHCP Options

+ 105 - 0
src/IPv4Bundle/EventListener/SubnetStatusOrHostTypeChangeSubscriber.php

@@ -0,0 +1,105 @@
+<?php
+
+namespace IPv4Bundle\EventListener;
+
+use Doctrine\Common\EventSubscriber;
+use Doctrine\ORM\Event\LifecycleEventArgs;
+use HostBundle\Entity\Host;
+use IPv4Bundle\Entity\SubNet;
+use KeaBundle\Services\LeaseService;
+
+class SubnetStatusOrHostTypeChangeSubscriber implements EventSubscriber
+{
+
+    /**
+     * @var ContainerInterface
+     */
+    private $serviceContainer;
+        
+    
+    /**
+     * @param ContainerInterface $serviceContainer
+     */
+    public function __construct($serviceContainer)
+    {
+        $this->serviceContainer = $serviceContainer;
+    }
+
+    /**
+     * @return array
+     */
+    public function getSubscribedEvents()
+    {
+        return array(
+            'preUpdate',
+            'preRemove',
+        );
+    }
+
+    /**
+     * @param LifecycleEventArgs $args
+     */
+    public function preUpdate(LifecycleEventArgs $args)
+    {
+        $this->execute($args);
+    }
+
+    /**
+     * @param LifecycleEventArgs $args
+     */
+    public function preRemove(LifecycleEventArgs $args)
+    {
+        $this->execute($args);
+    }
+
+    /**
+     * @param LifecycleEventArgs $args
+     * @param boolean $remove
+     */
+    public function execute(LifecycleEventArgs $args)
+    {
+        $flashbag = $this->serviceContainer->get('session')->getFlashBag();
+        $translator = $this->serviceContainer->get('translator');
+        try {
+            // checking if the entity is a Host and the status has changed
+            $em = $args->getEntityManager();
+            $uow = $em->getUnitOfWork();
+            $uow->computeChangeSets();
+            $entity = $args->getEntity();
+            $changeset = $uow->getEntityChangeSet($entity);
+            if ($entity instanceof SubNet && (isset($changeset['status']) || isset($changeset['allowedHostType']))) {
+                // $leaseService = $this->serviceContainer->get('kea.lease.service');
+                // // invalidating the host lease
+                // $lease4 = $leaseService->invalidate($entity->getMac());
+                // $flashbag->add("success", $translator->trans('host.lease.invalidate.ok', ['%hwaddr%' => $entity->getMac()], 'KeaBundle'));
+
+                // setting fixed address to null to all hosts in the pools static associated to the subnet
+                $ipRange = $this->serviceContainer->get('pool_ipv4_service')->getStaticPoolIPRangeBySubnet($entity);
+                $hosts = [];
+                foreach ($ipRange as $ip) {
+                    $host = $em->getRepository('HostBundle:Host')->findOneBy([
+                        'ipv4Address' => $ip,
+                    ]);
+                    if ($host) {
+                        $hosts[] = $host;
+                    }
+                }
+                foreach ($hosts as $host) {
+                    if ($host->getFixedAddress() != '') {
+                        $host->setFixedAddress(null);
+                        $host->setIpv4Address(null);
+                        $host->setFixedIP(false);
+    
+                        $em->persist($host);
+                        $em->flush($host);
+                    }
+                }
+            }
+        } catch (\Exception $ex) {
+            $flashbag->add("error", $ex->getMessage());
+        } catch (\Throwable $ex) {
+            $flashbag->add("error", $ex->getMessage());
+        }
+    }
+
+}

+ 6 - 0
src/IPv4Bundle/Resources/config/services.yml

@@ -32,3 +32,9 @@ services:
         class: IPv4Bundle\Services\PoolService
         arguments: [ '@doctrine.orm.entity_manager' ]
         public: true
+
+    ipv4.subnet_status_host_type_change.subscriber:
+        class: IPv4Bundle\EventListener\SubnetStatusOrHostTypeChangeSubscriber
+        arguments: [ "@service_container" ]
+        tags:
+            - { name: doctrine.event_subscriber, connection: default }

+ 31 - 1
src/IPv4Bundle/Services/PoolService.php

@@ -6,6 +6,7 @@ use Doctrine\ORM\EntityManager;
 use Doctrine\ORM\EntityRepository;
 use HostBundle\Entity\HostType;
 use IPv4Bundle\Entity\Pool;
+use IPv4Bundle\Entity\SubNet;
 use KeaBundle\Entity\Lease4;
 
 class PoolService
@@ -80,7 +81,6 @@ class PoolService
      */
     public function getStaticPoolIPRange(HostType $hostType = null, $status = null)
     {
-        $count = 1;
         $range = [];
         $pools = $this->poolRepository->findAllStaticByHostTypeAndStatus($hostType, $status);
         foreach ($pools as $pool) {
@@ -98,4 +98,34 @@ class PoolService
         return $range;
     }
     
+    /**
+     * This function return an array that contains an ip range
+     * based on the statics pools on the subnet
+     * 
+     * @param SubNet $subnet
+     *
+     * @return array
+     */
+    public function getStaticPoolIPRangeBySubnet(SubNet $subnet)
+    {
+        $range = [];
+        $pools = $subnet->getIpPool();
+        foreach ($pools as $pool) {
+            if (!$pool->getIsStatic()) {
+                continue;
+            }
+            $firstIp = ip2long($pool->getFirstIp());
+            $range[] = $firstIp;
+            $lastIp =  ip2long($pool->getLastIp());
+            $currentIP = $firstIp + 1;
+            while ($currentIP <= $lastIp) {
+                $range[] = $currentIP;
+                $currentIP++;
+            }
+        }
+        sort($range);
+        
+        return $range;
+    }
+    
 }