Pārlūkot izejas kodu

FD3-667 kea mysql host reservation

Espinoza Guillermo 6 gadi atpakaļ
vecāks
revīzija
1457162ae2

+ 89 - 0
src/HostBundle/EventListener/KEAHostReservationSubscriber.php

@@ -0,0 +1,89 @@
+<?php
+
+namespace HostBundle\EventListener;
+
+use Doctrine\Common\EventSubscriber;
+use Doctrine\ORM\Event\LifecycleEventArgs;
+use HostBundle\Entity\Host;
+use Symfony\Component\DependencyInjection\ContainerInterface;
+
+class KEAHostReservationSubscriber implements EventSubscriber
+{
+    
+    /**
+     * @var ContainerInterface
+     */
+    private $serviceContainer;
+    
+    
+    /**
+     * @param ContainerInterface $serviceContainer
+     */
+    public function __construct(ContainerInterface $serviceContainer)
+    {
+        $this->serviceContainer = $serviceContainer;
+    }
+
+    /**
+     * @return array
+     */
+    public function getSubscribedEvents()
+    {
+        return array(
+            'postPersist',
+            'postUpdate',
+        );
+    }
+
+    /**
+     * @param LifecycleEventArgs $args
+     */
+    public function postPersist(LifecycleEventArgs $args)
+    {
+        $this->execute($args);
+    }
+
+    /**
+     * @param LifecycleEventArgs $args
+     */
+    public function postUpdate(LifecycleEventArgs $args)
+    {
+        $this->execute($args);
+    }
+
+    /**
+     * @param LifecycleEventArgs $args
+     */
+    public function execute(LifecycleEventArgs $args)
+    {
+        $entity = $args->getEntity();
+        if ($entity instanceof Host) {
+            
+            if ($entity->getFixedAddress()) {
+                $query = "
+                INSERT INTO kea.hosts (dhcp_identifier,
+                dhcp_identifier_type,
+                ipv4_address)
+                VALUES :mac,
+                (SELECT type FROM kea.host_identifier_type WHERE name=:type),
+                INET_ATON(:ip));
+                ";
+            } else {
+                $query = "
+                DELETE FROM kea.hosts
+                WHERE dhcp_identifier = :mac;
+                ";
+            }
+            
+            $databaseConnection = $this->serviceContainer->get('database_connection');
+            $stmt = $databaseConnection->prepare($query);
+            $stmt->bindValue("mac", hex2bin(str_replace(':', '', $entity->getMac())));
+            if ($entity->getFixedAddress()) {
+                $stmt->bindValue("type", 'hw-address');
+                $stmt->bindValue("ip", $entity->getFixedAddress());
+            }
+            $stmt->execute();
+        }
+    }
+
+}

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

@@ -36,3 +36,9 @@ services:
         tags:
             - { name: doctrine.event_subscriber, connection: default }    
         
+    dhcp.kea_host_reservation.subscriber:
+        class: HostBundle\EventListener\KEAHostReservationSubscriber
+        arguments: [ "@service_container" ]
+        tags:
+            - { name: doctrine.event_subscriber, connection: default }    
+