فهرست منبع

FD3-498 comando kea:config option122 y remote-id-map

Guillermo Espinoza 7 سال پیش
والد
کامیت
76249e5f97

+ 1 - 0
app/DoctrineMigrations/Version20180315120345.php

@@ -16,6 +16,7 @@ class Version20180315120345 extends AbstractMigration
             'parameters' => [
                 'option122' => true,
                 'remote-id-map' => true,
+                'ip' => '127.0.0.1',
             ],
         ]);
         $this->addsql(

+ 1 - 0
src/IPv4Bundle/Entity/Host.php

@@ -14,6 +14,7 @@ class Host
 {
 
     const STATE_ACTIVE = 'active';
+    const STATE_SUSPENDED = 'suspended';
 
     /**
      * @var bigint $id

+ 5 - 1
src/KeaBundle/Command/KeaConfigCommand.php

@@ -17,6 +17,8 @@ class KeaConfigCommand extends ContainerAwareCommand
             ->setDescription('Kea DHCP Config')
             ->setHelp('Create Kea DHCP Config')
             ->addOption('class', null, InputOption::VALUE_OPTIONAL, 'Kea Class Config in KeaBundle\Services. e.g. BaseKea', 'BaseKea')
+            ->addOption('id', null, InputOption::VALUE_OPTIONAL, 'DHCP ID')
+            ->addOption('library', null, InputOption::VALUE_OPTIONAL, 'Hook library', '/home/iksop/kea-hook-cm/kea-hook-flowdat.so')
             ;
     }
 
@@ -28,9 +30,11 @@ class KeaConfigCommand extends ContainerAwareCommand
     {
         $this->output = $output;
         $class = $input->getOption('class');
+        $id = $input->getOption('id');
+        $library = $input->getOption('library');
         $keaConfigService = $this->getContainer()->get('kea.config');
 
-        $output->writeln($keaConfigService->getConfig($class));
+        $output->writeln($keaConfigService->getConfig($id, $class, $library));
     }
 
 }

+ 72 - 2
src/KeaBundle/Services/BaseKea.php

@@ -2,14 +2,27 @@
 
 namespace KeaBundle\Services;
 
+use IPv4Bundle\Entity\Host;
+
 class BaseKea
 {
 
+    /**
+     * @var array
+     */
     public $subnet4 = [];
 
+    /**
+    * @var array
+    */
+    public $hooks_libraries = [];
+
 
     /**
      * @param array $data
+     * @param boolean $logging
+     *
+     * @return string
      */
     public function getConfig($data = array(), $logging = false)
     {
@@ -17,9 +30,16 @@ class BaseKea
             $this->subnetConfig($data['subnets']);
         }
 
+        if (isset($data['dhcp']) && isset($data['library'])) {
+            $this->hooksLibrariesConfig($data);
+        }
+
         return json_encode([
-            'Dhcp4' => $this,
-            'Logging' => $this->loggingConfig()
+            'Dhcp4' => [
+                'subnet4' => $this->subnet4,
+                'hooks-libraries' => $this->hooks_libraries,
+            ],
+            'Logging' => $this->loggingConfig(),
         ]);
     }
 
@@ -42,6 +62,56 @@ class BaseKea
         }
     }
 
+    /**
+     * @param array $data
+     */
+    private function hooksLibrariesConfig($data)
+    {
+        $dhcp = $data['dhcp'];
+        $hosts = $data['hosts'];
+
+        $hook = [
+            'library' => $data['library'],
+        ];
+
+        $dhcpModelParams = $dhcp->getDhcpModel()->getData('parameters');
+
+        $option122 = isset($dhcpModelParams['option122']) ? $dhcpModelParams['option122'] : null;
+        $ip = isset($dhcpModelParams['ip']) ? $dhcpModelParams['ip'] : null;
+        if ($option122 && $ip) {
+            $macs = [];
+            foreach ($hosts as $host) {
+                $macs[] = $host->getMac();
+            }
+
+            $hook['parameters']['option122'] = [
+                $ip => $macs
+            ];
+        }
+
+        $remote_id_map = isset($dhcpModelParams['remote-id-map']) ? $dhcpModelParams['remote-id-map'] : null;
+        if ($remote_id_map) {
+            $active = [];
+            $suspended = [];
+            foreach ($hosts as $host) {
+                $mac = $host->getMac();
+                $state = $host->getState();
+                if ($state == Host::STATE_ACTIVE) {
+                    $active[] = $mac;
+                } elseif ($state == Host::STATE_SUSPENDED) {
+                    $suspended[] = $mac;
+                }
+            }
+
+            $hook['parameters']['remote-id-map'] = [
+                'active' => $active,
+                'suspended' => $suspended,
+            ];
+        }
+
+        $this->hooks_libraries[] = $hook;
+    }
+
     /**
      * @return string
      */

+ 18 - 1
src/KeaBundle/Services/KeaConfigService.php

@@ -5,32 +5,49 @@ namespace KeaBundle\Services;
 class KeaConfigService
 {
 
+    /**
+     * @var Repository
+     */
+    private $dhcpRepository;
+
     /**
      * @var Repository
      */
     private $subnetRepository;
 
+    /**
+     * @var Repository
+     */
+    private $hostRepository;
+
 
     /**
      * @param EntityManager $em
      */
     public function __construct($em)
     {
+        $this->dhcpRepository = $em->getRepository('DHCPBundle:DHCP');
         $this->subnetRepository = $em->getRepository('IPv4Bundle:SubNet');
+        $this->hostRepository = $em->getRepository('IPv4Bundle:Host');
     }
 
     /**
+     * @param int $id
      * @param string $className
+     * @param string $library
      *
      * @return string
      */
-    public function getConfig($className = 'BaseKea')
+    public function getConfig($id, $className = 'BaseKea', $library = '/home/iksop/kea-hook-cm/kea-hook-flowdat.so')
     {
         $config = '';
         $fullClass = 'KeaBundle\\Services\\' . $className;
         if (class_exists($fullClass)) {
             $data = [
+                'dhcp' => $this->dhcpRepository->find($id),
+                'hosts' => $this->hostRepository->findAll(),
                 'subnets' => $this->subnetRepository->findAll(),
+                'library' => $library,
             ];
             $keaConfig = new $fullClass;
             $config = $keaConfig->getConfig($data);