Browse Source

[5208a] toElement() moved from CfgHosts to Host

Tomek Mrugalski 8 years ago
parent
commit
7d00bc89e7
3 changed files with 145 additions and 108 deletions
  1. 10 108
      src/lib/dhcpsrv/cfg_hosts.cc
  2. 124 0
      src/lib/dhcpsrv/host.cc
  3. 11 0
      src/lib/dhcpsrv/host.h

+ 10 - 108
src/lib/dhcpsrv/cfg_hosts.cc

@@ -708,60 +708,12 @@ CfgHosts::toElement4() const {
     const HostContainerIndex0& idx = hosts_.get<0>();
     for (HostContainerIndex0::const_iterator host = idx.begin();
          host != idx.end(); ++host) {
-        // Get the subnet ID
+
+        // Convert host to element representation
+        ElementPtr map = (*host)->toElement4();
+
+        // Push it on the list
         SubnetID subnet_id = (*host)->getIPv4SubnetID();
-        // Prepare the map
-        ElementPtr map = Element::createMap();
-        // Set the identifier
-        Host::IdentifierType id_type = (*host)->getIdentifierType();
-        if (id_type == Host::IDENT_HWADDR) {
-            HWAddrPtr hwaddr = (*host)->getHWAddress();
-            map->set("hw-address", Element::create(hwaddr->toText(false)));
-        } else if (id_type == Host::IDENT_DUID) {
-            DuidPtr duid = (*host)->getDuid();
-            map->set("duid", Element::create(duid->toText()));
-        } else if (id_type == Host::IDENT_CIRCUIT_ID) {
-            const std::vector<uint8_t>& bin = (*host)->getIdentifier();
-            std::string circuit_id = util::encode::encodeHex(bin);
-            map->set("circuit-id", Element::create(circuit_id));
-        } else if (id_type == Host::IDENT_CLIENT_ID) {
-            const std::vector<uint8_t>& bin = (*host)->getIdentifier();
-            std::string client_id = util::encode::encodeHex(bin);
-            map->set("client-id", Element::create(client_id));
-        } else if (id_type == Host::IDENT_FLEX) {
-            const std::vector<uint8_t>& bin = (*host)->getIdentifier();
-            std::string flex = util::encode::encodeHex(bin);
-            map->set("flex-id", Element::create(flex));
-        } else {
-            isc_throw(ToElementError, "invalid identifier type: " << id_type);
-        }
-        // Set the reservation
-        const IOAddress& address = (*host)->getIPv4Reservation();
-        map->set("ip-address", Element::create(address.toText()));
-        // Set the hostname
-        const std::string& hostname = (*host)->getHostname();
-        map->set("hostname", Element::create(hostname));
-        // Set next-server
-        const IOAddress& next_server = (*host)->getNextServer();
-        map->set("next-server", Element::create(next_server.toText()));
-        // Set server-hostname
-        const std::string& server_hostname = (*host)->getServerHostname();
-        map->set("server-hostname", Element::create(server_hostname));
-        // Set boot-file-name
-        const std::string& boot_file_name = (*host)->getBootFileName();
-        map->set("boot-file-name", Element::create(boot_file_name));
-        // Set client-classes
-        const ClientClasses& cclasses = (*host)->getClientClasses4();
-        ElementPtr classes = Element::createList();
-        for (ClientClasses::const_iterator cclass = cclasses.cbegin();
-             cclass != cclasses.end(); ++cclass) {
-            classes->add(Element::create(*cclass));
-        }
-        map->set("client-classes", classes);
-        // Set option-data
-        ConstCfgOptionPtr opts = (*host)->getCfgOption4();
-        map->set("option-data", opts->toElement());
-        // Push the map on the list
         result.add(subnet_id, map);
     }
     return (result.externalize());
@@ -774,62 +726,12 @@ CfgHosts::toElement6() const {
     const HostContainerIndex0& idx = hosts_.get<0>();
     for (HostContainerIndex0::const_iterator host = idx.begin();
          host != idx.end(); ++host) {
-        // Get the subnet ID
+
+        // Convert host to Element representation
+        ElementPtr map = (*host)->toElement6();
+
+        // Push it on the list
         SubnetID subnet_id = (*host)->getIPv6SubnetID();
-        // Prepare the map
-        ElementPtr map = Element::createMap();
-        // Set the identifier
-        Host::IdentifierType id_type = (*host)->getIdentifierType();
-        if (id_type == Host::IDENT_HWADDR) {
-            HWAddrPtr hwaddr = (*host)->getHWAddress();
-            map->set("hw-address", Element::create(hwaddr->toText(false)));
-        } else if (id_type == Host::IDENT_DUID) {
-            DuidPtr duid = (*host)->getDuid();
-            map->set("duid", Element::create(duid->toText()));
-        } else if (id_type == Host::IDENT_CIRCUIT_ID) {
-            isc_throw(ToElementError, "unexpected circuit-id DUID type");
-        } else if (id_type == Host::IDENT_CLIENT_ID) {
-            isc_throw(ToElementError, "unexpected client-id DUID type");
-        } else if (id_type == Host::IDENT_FLEX) {
-            const std::vector<uint8_t>& bin = (*host)->getIdentifier();
-            std::string flex = util::encode::encodeHex(bin);
-            map->set("flex-id", Element::create(flex));
-        } else {
-            isc_throw(ToElementError, "invalid DUID type: " << id_type);
-        }
-        // Set reservations (ip-addresses)
-        IPv6ResrvRange na_resv =
-            (*host)->getIPv6Reservations(IPv6Resrv::TYPE_NA);
-        ElementPtr resvs = Element::createList();
-        for (IPv6ResrvIterator resv = na_resv.first;
-             resv != na_resv.second; ++resv) {
-            resvs->add(Element::create(resv->second.toText()));
-        }
-        map->set("ip-addresses", resvs);
-        // Set reservations (prefixes)
-        IPv6ResrvRange pd_resv =
-                (*host)->getIPv6Reservations(IPv6Resrv::TYPE_PD);
-        resvs = Element::createList();
-        for (IPv6ResrvIterator resv = pd_resv.first;
-             resv != pd_resv.second; ++resv) {
-            resvs->add(Element::create(resv->second.toText()));
-        }
-        map->set("prefixes", resvs);
-        // Set the hostname
-        const std::string& hostname = (*host)->getHostname();
-        map->set("hostname", Element::create(hostname));
-        // Set client-classes
-        const ClientClasses& cclasses = (*host)->getClientClasses6();
-        ElementPtr classes = Element::createList();
-        for (ClientClasses::const_iterator cclass = cclasses.cbegin();
-             cclass != cclasses.end(); ++cclass) {
-            classes->add(Element::create(*cclass));
-        }
-        map->set("client-classes", classes);
-        // Set option-data
-        ConstCfgOptionPtr opts = (*host)->getCfgOption6();
-        map->set("option-data", opts->toElement());
-        // Push the map on the list
         result.add(subnet_id, map);
     }
     return (result.externalize());

+ 124 - 0
src/lib/dhcpsrv/host.cc

@@ -9,9 +9,13 @@
 #include <dhcpsrv/host.h>
 #include <util/encode/hex.h>
 #include <util/strutil.h>
+#include <asiolink/io_address.h>
 #include <exceptions/exceptions.h>
 #include <sstream>
 
+using namespace isc::data;
+using namespace isc::asiolink;
+
 namespace isc {
 namespace dhcp {
 
@@ -400,6 +404,126 @@ Host::setBootFileName(const std::string& boot_file_name) {
     boot_file_name_ = boot_file_name;
 }
 
+ElementPtr
+Host::toElement4() const {
+
+    // Get the subnet ID
+    SubnetID subnet_id = getIPv4SubnetID();
+    // Prepare the map
+    ElementPtr map = Element::createMap();
+    // Set the identifier
+    Host::IdentifierType id_type = getIdentifierType();
+    if (id_type == Host::IDENT_HWADDR) {
+        HWAddrPtr hwaddr = getHWAddress();
+        map->set("hw-address", Element::create(hwaddr->toText(false)));
+    } else if (id_type == Host::IDENT_DUID) {
+        DuidPtr duid = getDuid();
+        map->set("duid", Element::create(duid->toText()));
+    } else if (id_type == Host::IDENT_CIRCUIT_ID) {
+        const std::vector<uint8_t>& bin = getIdentifier();
+        std::string circuit_id = util::encode::encodeHex(bin);
+        map->set("circuit-id", Element::create(circuit_id));
+    } else if (id_type == Host::IDENT_CLIENT_ID) {
+        const std::vector<uint8_t>& bin = getIdentifier();
+        std::string client_id = util::encode::encodeHex(bin);
+        map->set("client-id", Element::create(client_id));
+    } else if (id_type == Host::IDENT_FLEX) {
+        const std::vector<uint8_t>& bin = getIdentifier();
+        std::string flex = util::encode::encodeHex(bin);
+        map->set("flex-id", Element::create(flex));
+    } else {
+        isc_throw(ToElementError, "invalid identifier type: " << id_type);
+    }
+    // Set the reservation
+    const IOAddress& address = getIPv4Reservation();
+    map->set("ip-address", Element::create(address.toText()));
+    // Set the hostname
+    const std::string& hostname = getHostname();
+    map->set("hostname", Element::create(hostname));
+    // Set next-server
+    const IOAddress& next_server = getNextServer();
+    map->set("next-server", Element::create(next_server.toText()));
+    // Set server-hostname
+    const std::string& server_hostname = getServerHostname();
+    map->set("server-hostname", Element::create(server_hostname));
+    // Set boot-file-name
+    const std::string& boot_file_name = getBootFileName();
+    map->set("boot-file-name", Element::create(boot_file_name));
+    // Set client-classes
+    const ClientClasses& cclasses = getClientClasses4();
+    ElementPtr classes = Element::createList();
+    for (ClientClasses::const_iterator cclass = cclasses.cbegin();
+         cclass != cclasses.end(); ++cclass) {
+        classes->add(Element::create(*cclass));
+    }
+    map->set("client-classes", classes);
+    // Set option-data
+    ConstCfgOptionPtr opts = getCfgOption4();
+    map->set("option-data", opts->toElement());
+
+    return (map);
+}
+
+ElementPtr
+Host::toElement6() const {
+    // Get the subnet ID
+    SubnetID subnet_id = getIPv6SubnetID();
+    // Prepare the map
+    ElementPtr map = Element::createMap();
+    // Set the identifier
+    Host::IdentifierType id_type = getIdentifierType();
+    if (id_type == Host::IDENT_HWADDR) {
+        HWAddrPtr hwaddr = getHWAddress();
+        map->set("hw-address", Element::create(hwaddr->toText(false)));
+    } else if (id_type == Host::IDENT_DUID) {
+        DuidPtr duid = getDuid();
+        map->set("duid", Element::create(duid->toText()));
+    } else if (id_type == Host::IDENT_CIRCUIT_ID) {
+        isc_throw(ToElementError, "unexpected circuit-id DUID type");
+    } else if (id_type == Host::IDENT_CLIENT_ID) {
+        isc_throw(ToElementError, "unexpected client-id DUID type");
+    } else if (id_type == Host::IDENT_FLEX) {
+        const std::vector<uint8_t>& bin = getIdentifier();
+        std::string flex = util::encode::encodeHex(bin);
+        map->set("flex-id", Element::create(flex));
+    } else {
+        isc_throw(ToElementError, "invalid DUID type: " << id_type);
+    }
+    // Set reservations (ip-addresses)
+    IPv6ResrvRange na_resv = getIPv6Reservations(IPv6Resrv::TYPE_NA);
+    ElementPtr resvs = Element::createList();
+    for (IPv6ResrvIterator resv = na_resv.first;
+         resv != na_resv.second; ++resv) {
+        resvs->add(Element::create(resv->second.toText()));
+    }
+    map->set("ip-addresses", resvs);
+    // Set reservations (prefixes)
+    IPv6ResrvRange pd_resv = getIPv6Reservations(IPv6Resrv::TYPE_PD);
+    resvs = Element::createList();
+    for (IPv6ResrvIterator resv = pd_resv.first;
+         resv != pd_resv.second; ++resv) {
+        resvs->add(Element::create(resv->second.toText()));
+    }
+    map->set("prefixes", resvs);
+    // Set the hostname
+    const std::string& hostname = getHostname();
+    map->set("hostname", Element::create(hostname));
+    // Set client-classes
+    const ClientClasses& cclasses = getClientClasses6();
+    ElementPtr classes = Element::createList();
+    for (ClientClasses::const_iterator cclass = cclasses.cbegin();
+         cclass != cclasses.end(); ++cclass) {
+        classes->add(Element::create(*cclass));
+    }
+    map->set("client-classes", classes);
+
+    // Set option-data
+    ConstCfgOptionPtr opts = getCfgOption6();
+    map->set("option-data", opts->toElement());
+
+    return (map);
+}
+
 std::string
 Host::toText() const {
     std::ostringstream s;

+ 11 - 0
src/lib/dhcpsrv/host.h

@@ -8,6 +8,7 @@
 #define HOST_H
 
 #include <asiolink/io_address.h>
+#include <cc/data.h>
 #include <dhcp/classify.h>
 #include <dhcp/duid.h>
 #include <dhcp/hwaddr.h>
@@ -544,6 +545,16 @@ public:
         return (host_id_);
     }
 
+    /// @brief Unparses (converts to Element representation) IPv4 host
+    ///
+    /// @return Element representation of the host
+    isc::data::ElementPtr toElement4() const;
+
+    /// @brief Unparses (converts to Element representation) IPv4 host
+    ///
+    /// @return Element representation of the host
+    isc::data::ElementPtr toElement6() const;
+
 private:
 
     /// @brief Adds new client class for DHCPv4 or DHCPv6.