|
@@ -66,68 +66,90 @@ class KEAHostReservationSubscriber implements EventSubscriber
|
|
|
*/
|
|
|
public function execute(LifecycleEventArgs $args, $remove = false)
|
|
|
{
|
|
|
- $entity = $args->getEntity();
|
|
|
- if ($entity instanceof Host) {
|
|
|
- $query = "SELECT COUNT(*) AS `exists`
|
|
|
- FROM information_schema.tables
|
|
|
- WHERE table_schema = 'kea'
|
|
|
- AND table_name = 'hosts';";
|
|
|
-
|
|
|
- $databaseConnection = $this->serviceContainer->get('database_connection');
|
|
|
- $stmt = $databaseConnection->prepare($query);
|
|
|
- $stmt->execute();
|
|
|
- $tableExists = $stmt->fetchAll();
|
|
|
- if (!isset($tableExists[0]['exists']) || $tableExists[0]['exists'] == '0') {
|
|
|
- return;
|
|
|
- }
|
|
|
-
|
|
|
- if ($entity->getFixedAddress() && $remove == false) {
|
|
|
- $query = "
|
|
|
- INSERT INTO kea.hosts (dhcp_identifier,
|
|
|
- dhcp_identifier_type,
|
|
|
- ipv4_address,
|
|
|
- hostname)
|
|
|
- VALUES (:mac,
|
|
|
- (SELECT type FROM kea.host_identifier_type WHERE name=:type),
|
|
|
- INET_ATON(:ip),
|
|
|
- :hostname);
|
|
|
- ";
|
|
|
- } else {
|
|
|
- $query = "
|
|
|
- DELETE FROM kea.hosts
|
|
|
- WHERE dhcp_identifier = :mac;
|
|
|
- ";
|
|
|
- }
|
|
|
-
|
|
|
- $databaseConnection = $this->serviceContainer->get('database_connection');
|
|
|
- $stmt = $databaseConnection->prepare($query);
|
|
|
- $hostType = $entity->getHostType();
|
|
|
- $shortname = $hostType->getShortname();
|
|
|
- // calculo el campo dhcp_identifier_type de kea.hosts
|
|
|
- if ($shortname == 'cablemodem' || $shortname == 'cm') {
|
|
|
- $stmt->bindValue("mac", hex2bin(str_replace(':', '', $entity->getMac())));
|
|
|
- } else {
|
|
|
- // mta|cpe
|
|
|
- $hex = '';
|
|
|
- $prefix = $shortname . '-';
|
|
|
- for ($i = 0; $i < strlen($prefix); $i++) {
|
|
|
- $ord = ord($prefix[$i]);
|
|
|
- $hexCode = dechex($ord);
|
|
|
- $hex .= substr('0'.$hexCode, -2);
|
|
|
+ try {
|
|
|
+ $entity = $args->getEntity();
|
|
|
+ if ($entity instanceof Host) {
|
|
|
+ $query = "SELECT COUNT(*) AS `exists`
|
|
|
+ FROM information_schema.tables
|
|
|
+ WHERE table_schema = 'kea'
|
|
|
+ AND table_name = 'hosts';";
|
|
|
+
|
|
|
+ $databaseConnection = $this->serviceContainer->get('database_connection');
|
|
|
+ $stmt = $databaseConnection->prepare($query);
|
|
|
+ $stmt->execute();
|
|
|
+ $tableExists = $stmt->fetchAll();
|
|
|
+ if (!isset($tableExists[0]['exists']) || $tableExists[0]['exists'] == '0') {
|
|
|
+ return;
|
|
|
}
|
|
|
- $stmt->bindValue("mac", hex2bin($hex . str_replace(':', '', $entity->getHost()->getMac())));
|
|
|
- }
|
|
|
- if ($entity->getFixedAddress() && $remove == false) {
|
|
|
+
|
|
|
+ if ($entity->getFixedAddress() && $remove == false) {
|
|
|
+ $query = "
|
|
|
+ DELETE FROM kea.hosts
|
|
|
+ WHERE dhcp_identifier = :mac
|
|
|
+ ;
|
|
|
+ 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)
|
|
|
+ ON DUPLICATE KEY UPDATE
|
|
|
+ dhcp_identifier_type=(SELECT type FROM kea.host_identifier_type WHERE name=:type),
|
|
|
+ dhcp4_subnet_id=:dhcp4_subnet_id, ipv4_address=INET_ATON(:ip),
|
|
|
+ hostname=:hostname
|
|
|
+ ;
|
|
|
+ ";
|
|
|
+ } else {
|
|
|
+ $query = "
|
|
|
+ DELETE FROM kea.hosts
|
|
|
+ WHERE dhcp_identifier = :mac;
|
|
|
+ ";
|
|
|
+ }
|
|
|
+
|
|
|
+ $databaseConnection = $this->serviceContainer->get('database_connection');
|
|
|
+ $stmt = $databaseConnection->prepare($query);
|
|
|
+ $hostType = $entity->getHostType();
|
|
|
+ $shortname = $hostType->getShortname();
|
|
|
+ // calculo el campo dhcp_identifier_type de kea.hosts
|
|
|
if ($shortname == 'cablemodem' || $shortname == 'cm') {
|
|
|
- $stmt->bindValue("type", 'hw-address');
|
|
|
+ $stmt->bindValue("mac", hex2bin(str_replace(':', '', $entity->getMac())));
|
|
|
} else {
|
|
|
- $stmt->bindValue("type", 'flex-id');
|
|
|
+ // mta|cpe
|
|
|
+ $hex = '';
|
|
|
+ $prefix = $shortname . '-';
|
|
|
+ for ($i = 0; $i < strlen($prefix); $i++) {
|
|
|
+ $ord = ord($prefix[$i]);
|
|
|
+ $hexCode = dechex($ord);
|
|
|
+ $hex .= substr('0'.$hexCode, -2);
|
|
|
+ }
|
|
|
+ $stmt->bindValue("mac", hex2bin($hex . str_replace(':', '', $entity->getHost()->getMac())));
|
|
|
+ }
|
|
|
+ if ($entity->getFixedAddress() && $remove == false) {
|
|
|
+ if ($shortname == 'cablemodem' || $shortname == 'cm') {
|
|
|
+ $stmt->bindValue("type", 'hw-address');
|
|
|
+ } else {
|
|
|
+ $stmt->bindValue("type", 'flex-id');
|
|
|
+ }
|
|
|
+ $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->bindValue("hostname", $entity->getHostName());
|
|
|
- $stmt->bindValue("ip", $entity->getFixedAddress());
|
|
|
+ $stmt->execute();
|
|
|
}
|
|
|
- $stmt->execute();
|
|
|
- }
|
|
|
+ } catch (\Exception $ex) {
|
|
|
+ $flashbag = $this->serviceContainer->get('session')->getFlashBag();
|
|
|
+ $flashbag->add("error", $ex->getMessage());
|
|
|
+ } catch (\Throwable $ex) {
|
|
|
+ $flashbag = $this->serviceContainer->get('session')->getFlashBag();
|
|
|
+ $flashbag->add("error", $ex->getMessage());
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
}
|