|
@@ -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();
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+}
|