浏览代码

FD3-755 se corrige insert en tabla hosts de kea

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

+ 1 - 0
src/HostBundle/Admin/HostAdmin.php

@@ -181,6 +181,7 @@ class HostAdmin extends BaseAdmin
         if (!$object->getFixedAddress() && $object->getFixedIP() == true) {
             $object->setFixedIP(false);
         }
+        $object->setOptions(json_encode($object->getDHCPOption()));
     
         return parent::preUpdate($object);
     }

+ 10 - 1
src/HostBundle/EventListener/KEAHostReservationSubscriber.php

@@ -85,12 +85,17 @@ class KEAHostReservationSubscriber implements EventSubscriber
                 $query = "
                 INSERT INTO kea.hosts (dhcp_identifier,
                 dhcp_identifier_type,
+                dhcp4_subnet_id,
                 ipv4_address, 
                 hostname)
                 VALUES (:mac,
                 (SELECT type FROM kea.host_identifier_type WHERE name=:type),
+                :dhcp4_subnet_id,
                 INET_ATON(:ip),
-                :hostname);
+                :hostname)
+                ON DUPLICATE KEY UPDATE
+                ipv4_address=INET_ATON(:ip)
+                ;
                 ";
             } else {
                 $query = "
@@ -125,6 +130,10 @@ class KEAHostReservationSubscriber implements EventSubscriber
                 }
                 $stmt->bindValue("hostname", $entity->getHostName());
                 $stmt->bindValue("ip", $entity->getFixedAddress());
+                
+                // calculo el valor de la subnet según la config de KEA
+                $subnet = $this->serviceContainer->get('dhcp.host_service')->getSubnet($entity->getFixedAddress());
+                $stmt->bindValue("dhcp4_subnet_id", $this->serviceContainer->get('kea.config')->getSubnetInConfig($subnet));
             }
             $stmt->execute();
         }

+ 16 - 0
src/HostBundle/Services/HostService.php

@@ -92,4 +92,20 @@ class HostService
         
         return $hostsFixedIP;
     }
+    
+    /**
+     * @param string $fixedIP
+     *
+     * @return int
+     */
+    public function getSubnet($fixedIP)
+    {
+        $pool = $this->poolService->getPoolFromFixedIP($fixedIP);
+        if ($pool) {
+            return $pool->getSubnet() ? $pool->getSubnet() : null;
+        }
+        
+        return null;
+    }
+    
 }

+ 24 - 19
src/HostBundle/Traits/DHCPOptionTrait.php

@@ -39,7 +39,7 @@ trait DHCPOptionTrait
 
     function setFilename($value)
     {
-        $this->json_dhcp_option_config['filename'] = $value;
+        $this->config($key, 'filename');
     }
 
     function getFilename()
@@ -49,7 +49,7 @@ trait DHCPOptionTrait
 
     function setSubnetMask($value)
     {
-        $this->json_dhcp_option_config['subnet_mask'] = $value;
+        $this->config('subnet_mask', $value);
     }
 
     function getSubnetMask()
@@ -59,7 +59,7 @@ trait DHCPOptionTrait
 
     function setTimeOffset($value)
     {
-        $this->json_dhcp_option_config['time_offset'] = $value;
+        $this->config('time_offset', $value);
     }
 
     function getTimeOffset()
@@ -69,7 +69,7 @@ trait DHCPOptionTrait
 
     function setRouters($value)
     {
-        $this->json_dhcp_option_config['routers'] = $value;
+        $this->config('routers', $value);
     }
 
     function getRouters()
@@ -79,7 +79,7 @@ trait DHCPOptionTrait
 
     function setDomainNameServers($value)
     {
-        $this->json_dhcp_option_config['domain_name_servers'] = $value;
+        $this->config('domain_name_servers', $value);
     }
 
     function getDomainNameServers()
@@ -89,7 +89,7 @@ trait DHCPOptionTrait
 
     function setHostName($value)
     {
-        $this->json_dhcp_option_config['host_name'] = $value;
+        $this->config('host_name', $value);
     }
 
     function getHostName()
@@ -99,7 +99,7 @@ trait DHCPOptionTrait
 
     function setDomainName($value)
     {
-        $this->json_dhcp_option_config['domain_name'] = $value;
+        $this->config('domain_name', $value);
     }
 
     function getDomainName()
@@ -109,7 +109,7 @@ trait DHCPOptionTrait
 
     function setBroadcastAddress($value)
     {
-        $this->json_dhcp_option_config['broadcast_address'] = $value;
+        $this->config('broadcast_address', $value);
     }
 
     function getBroadcastAddress()
@@ -119,7 +119,7 @@ trait DHCPOptionTrait
 
     function setDefaultLeaseTime($value)
     {
-        $this->json_dhcp_option_config['default_lease_time'] = $value;
+        $this->config('default_lease_time', $value);
     }
 
     function getDefaultLeaseTime()
@@ -129,7 +129,7 @@ trait DHCPOptionTrait
 
     function setMaxLeaseTime($value)
     {
-        $this->json_dhcp_option_config['max_lease_time'] = $value;
+        $this->config('max_lease_time', $value);
     }
 
     function getMaxLeaseTime()
@@ -139,7 +139,7 @@ trait DHCPOptionTrait
 
     function setNextServer($value)
     {
-        $this->json_dhcp_option_config['next_server'] = $value;
+        $this->config('next_server', $value);
     }
 
     function getNextServer()
@@ -149,7 +149,7 @@ trait DHCPOptionTrait
 
     function setTftpServerName($value)
     {
-        $this->json_dhcp_option_config['tftp_server'] = $value;
+        $this->config('tftp_server', $value);
     }
 
     function getTftpServerName()
@@ -159,7 +159,7 @@ trait DHCPOptionTrait
 
     function setOption122DhcpServer($value)
     {
-        $this->json_dhcp_option_config['option122.dhcp-server'] = $value;
+        $this->config('option122.dhcp-server', $value);
     }
 
     function getOption122DhcpServer()
@@ -169,7 +169,7 @@ trait DHCPOptionTrait
 
     function setOption122DhcpServerSecondary($value)
     {
-        $this->json_dhcp_option_config['option122.dhcp-server-secondary'] = $value;
+        $this->config('option122.dhcp-server-secondary', $value);
     }
 
     function getOption122DhcpServerSecondary()
@@ -179,7 +179,7 @@ trait DHCPOptionTrait
 
     function setOption122ProvisioningServer($value)
     {
-        $this->json_dhcp_option_config['option122.provisioning-server'] = $value;
+        $this->config('option122.provisioning-server', $value);
     }
 
     function getOption122ProvisioningServer()
@@ -189,7 +189,7 @@ trait DHCPOptionTrait
 
     function setOption122ProvisioningType($value)
     {
-        $this->json_dhcp_option_config['option122.provisioning-type'] = $value;
+        $this->config('option122.provisioning-type', $value);
     }
 
     function getOption122ProvisioningType()
@@ -199,7 +199,7 @@ trait DHCPOptionTrait
 
     function setLogServers($value)
     {
-        $this->json_dhcp_option_config['log_servers'] = $value;
+        $this->config('log_servers', $value);
     }
 
     function getLogServers()
@@ -209,7 +209,7 @@ trait DHCPOptionTrait
 
     function setTimeServers($value)
     {
-        $this->json_dhcp_option_config['time_servers'] = $value;
+        $this->config('time_servers', $value);
     }
 
     function getTimeServers()
@@ -219,12 +219,17 @@ trait DHCPOptionTrait
 
     function setFixedAddress($value)
     {
-        $this->json_dhcp_option_config['fixed_address'] = $value;
+        $this->config('fixed_address', $value);
     }
 
     function getFixedAddress()
     {
         return @$this->json_dhcp_option_config['fixed_address'];
     }
+    
+    function config($key, $value)
+    {
+        $this->json_dhcp_option_config[$key] = $value ?: json_decode($this->getOptions(), true)[$key];
+    }
 
 }

+ 24 - 0
src/IPv4Bundle/Services/PoolService.php

@@ -48,6 +48,30 @@ class PoolService
         return null;
     }
     
+    
+    /**
+     * Retorna el primer pool donde se encuentra la $fixedIP
+     *
+     * @param string $fixedIP
+     *
+     * @return Pool
+     */
+    public function getPoolFromFixedIP($fixedIP)
+    {
+        $address = ip2long($fixedIP);
+        $pools = $this->poolRepository->findAll();
+        foreach ($pools as $pool) {
+            $firstIp = ip2long($pool->getFirstIp());
+            $lastIp =  ip2long($pool->getLastIp());
+            
+            if ($firstIp <= $address && $address <= $lastIp) {
+                return $pool;
+            }
+        }
+        
+        return null;
+    }
+    
     /**
      * @param HostType $hostType
      *

+ 23 - 0
src/KeaBundle/Services/KeaConfigService.php

@@ -233,5 +233,28 @@ class KeaConfigService
 
         return $return;
     }
+    
+    /**
+     * @param SubNet $subnet
+     *
+     * @return int
+     */
+    public function getSubnetInConfig($subnet)
+    {
+        $position = 1;
+        if ($subnet) {
+            // se usan valores por defecto
+            $config = $this->getConfig(1, 'BaseKea', '/kea-cm-hook/kea-hook-flowdat.so');
+            $configDecode = json_decode($config, true);
+            foreach ($configDecode[0]['arguments']['Dhcp4']['subnet4'] as $subnetConfig) {
+                if ($subnetConfig['subnet'] == $subnet->getAddress()) {
+                    break;
+                }
+                $position++;
+            }
+        }
+        
+        return $position;
+    }
 
 }