From c604855fc2282c5ef6c151cb4ba63b59e2079a40 Mon Sep 17 00:00:00 2001 From: Tomek Mrugalski Date: Tue, 23 Oct 2018 13:12:27 +0200 Subject: [PATCH] [#65,!88] Remaining changes after review: - Remaining methods renamed - comments expanded - size() > 0 replaced with !empty() checks. --- src/lib/yang/adaptor_config.cc | 169 ++++++++++++++++++--------------- src/lib/yang/adaptor_config.h | 73 +++++++------- 2 files changed, 129 insertions(+), 113 deletions(-) diff --git a/src/lib/yang/adaptor_config.cc b/src/lib/yang/adaptor_config.cc index 1216d4c081..729b982c29 100644 --- a/src/lib/yang/adaptor_config.cc +++ b/src/lib/yang/adaptor_config.cc @@ -104,11 +104,13 @@ AdaptorConfig::sharedNetworksAssignID(ConstElementPtr networks, for (ConstElementPtr network : networks->listValue()) { ConstElementPtr subnets = network->get(subsel); - if (subnets && (subnets->size() > 0)) { - for (size_t i = 0; i < subnets->size(); ++i) { - ElementPtr subnet = subnets->getNonConst(i); - assignID(subnet, set, next); - } + if (!subnets || subnets->empty()) { + continue; + } + + for (size_t i = 0; i < subnets->size(); ++i) { + ElementPtr subnet = subnets->getNonConst(i); + assignID(subnet, set, next); } } } @@ -154,9 +156,9 @@ AdaptorConfig::sanitizePoolsInSharedNetworks(ConstElementPtr networks, } void -AdaptorConfig::optionDefList(ConstElementPtr defs, - const string& space, - OptionCodes& codes) { +AdaptorConfig::sanitizeOptionDefList(ConstElementPtr defs, + const string& space, + OptionCodes& codes) { if (!defs || defs->empty()) { // nothing to do here. return; @@ -174,8 +176,9 @@ AdaptorConfig::optionDefList(ConstElementPtr defs, } void -AdaptorConfig::optionDataList(ConstElementPtr options,const string& space, - const OptionCodes& codes) { +AdaptorConfig::sanitizeOptionDataList(ConstElementPtr options, + const string& space, + const OptionCodes& codes) { if (!options || options->empty()) { // nothing to do here. return; @@ -191,30 +194,39 @@ AdaptorConfig::optionDataList(ConstElementPtr options,const string& space, } void -AdaptorConfig::optionClasses(ConstElementPtr classes, const string& space, - OptionCodes& codes) { +AdaptorConfig::sanitizeOptionClasses(ConstElementPtr classes, + const string& space, + OptionCodes& codes) { if (!classes || classes->empty()) { // nothing to do here. return; } + // For every client class defined... for (size_t i = 0; i < classes->size(); ++i) { ElementPtr cclass = classes->getNonConst(i); + if (space == DHCP4_SPACE) { ConstElementPtr options = cclass->get("option-def"); if (options) { - if (options->size() > 0) { - optionDefList(options, space, codes); + if (!options->empty()) { + // If present, sanitize it. + sanitizeOptionDefList(options, space, codes); } else { + // If empty, remove it. cclass->remove("option-def"); } } } + + // also sanitize option data. ConstElementPtr options = cclass->get("option-data"); if (options) { - if (options->size() > 0) { - optionDataList(options, space, codes); + if (!options->empty()) { + // If present, sanitize it. + sanitizeOptionDataList(options, space, codes); } else { + // If empty, remove it. cclass->remove("option-data"); } } @@ -222,8 +234,8 @@ AdaptorConfig::optionClasses(ConstElementPtr classes, const string& space, } void -AdaptorConfig::optionPools(ConstElementPtr pools, const string& space, - const OptionCodes& codes) { +AdaptorConfig::sanitizeOptionPools(ConstElementPtr pools, const string& space, + const OptionCodes& codes) { if (!pools || pools->empty()) { // nothing to do here. return; @@ -233,8 +245,8 @@ AdaptorConfig::optionPools(ConstElementPtr pools, const string& space, ElementPtr pool = pools->getNonConst(i); ConstElementPtr options = pool->get("option-data"); if (options) { - if (options->size() > 0) { - optionDataList(options, space, codes); + if (!options->empty()) { + sanitizeOptionDataList(options, space, codes); } else { pool->remove("option-data"); } @@ -243,8 +255,8 @@ AdaptorConfig::optionPools(ConstElementPtr pools, const string& space, } void -AdaptorConfig::optionHosts(ConstElementPtr hosts, const string& space, - const OptionCodes& codes) { +AdaptorConfig::sanitizeOptionHosts(ConstElementPtr hosts, const string& space, + const OptionCodes& codes) { if (!hosts || hosts->empty()) { // nothing to do here. return; @@ -254,8 +266,8 @@ AdaptorConfig::optionHosts(ConstElementPtr hosts, const string& space, ElementPtr host = hosts->getNonConst(i); ConstElementPtr options = host->get("option-data"); if (options) { - if (options->size() > 0) { - optionDataList(options, space, codes); + if (!options->empty()) { + sanitizeOptionDataList(options, space, codes); } else { host->remove("option-data"); } @@ -264,8 +276,9 @@ AdaptorConfig::optionHosts(ConstElementPtr hosts, const string& space, } void -AdaptorConfig::optionSubnets(ConstElementPtr subnets, const string& space, - const OptionCodes& codes) { +AdaptorConfig::sanitizeOptionSubnets(ConstElementPtr subnets, + const string& space, + const OptionCodes& codes) { if (!subnets || subnets->empty()) { // nothing to do here. return; @@ -277,8 +290,8 @@ AdaptorConfig::optionSubnets(ConstElementPtr subnets, const string& space, // Let's try to sanitize option-data first. ConstElementPtr options = subnet->get("option-data"); if (options) { - if (options->size() > 0) { - optionDataList(options, space, codes); + if (!options->empty()) { + sanitizeOptionDataList(options, space, codes); } else { subnet->remove("option-data"); } @@ -287,8 +300,8 @@ AdaptorConfig::optionSubnets(ConstElementPtr subnets, const string& space, // Then try to sanitize pools. ConstElementPtr pools = subnet->get("pools"); if (pools) { - if (pools->size() > 0) { - optionPools(pools, space, codes); + if (!pools->empty()) { + sanitizeOptionPools(pools, space, codes); } else { subnet->remove("pools"); } @@ -298,8 +311,8 @@ AdaptorConfig::optionSubnets(ConstElementPtr subnets, const string& space, if (space == DHCP6_SPACE) { ConstElementPtr pools = subnet->get("pd-pools"); if (pools) { - if (pools->size() > 0) { - optionPools(pools, space, codes); + if (!pools->empty()) { + sanitizeOptionPools(pools, space, codes); } else { subnet->remove("pd-pools"); } @@ -309,8 +322,8 @@ AdaptorConfig::optionSubnets(ConstElementPtr subnets, const string& space, // Finally, sanitize host reservations. ConstElementPtr hosts = subnet->get("reservations"); if (hosts) { - if (hosts->size() > 0) { - optionHosts(hosts, space, codes); + if (!hosts->empty()) { + sanitizeOptionHosts(hosts, space, codes); } else { subnet->remove("reservations"); } @@ -319,9 +332,9 @@ AdaptorConfig::optionSubnets(ConstElementPtr subnets, const string& space, } void -AdaptorConfig::optionSharedNetworks(ConstElementPtr networks, - const string& space, - const OptionCodes& codes) { +AdaptorConfig::sanitizeOptionSharedNetworks(ConstElementPtr networks, + const string& space, + const OptionCodes& codes) { if (!networks || networks->empty()) { // nothing to do here. return; @@ -334,8 +347,8 @@ AdaptorConfig::optionSharedNetworks(ConstElementPtr networks, // try to sanitize shared network options first. ConstElementPtr options = network->get("option-data"); if (options) { - if (options->size() > 0) { - optionDataList(options, space, codes); + if (!options->empty()) { + sanitizeOptionDataList(options, space, codes); } else { network->remove("option-data"); } @@ -350,8 +363,8 @@ AdaptorConfig::optionSharedNetworks(ConstElementPtr networks, // Now try to sanitize subnets. ConstElementPtr subnets = network->get(subnet); if (subnets) { - if (subnets->size() > 0) { - optionSubnets(subnets, space, codes); + if (!subnets->empty()) { + sanitizeOptionSubnets(subnets, space, codes); } else { network->remove(subnet); } @@ -360,7 +373,7 @@ AdaptorConfig::optionSharedNetworks(ConstElementPtr networks, } void -AdaptorConfig::requireClassesPools(ConstElementPtr pools) { +AdaptorConfig::sanitizeRequireClassesPools(ConstElementPtr pools) { if (!pools || pools->empty()) { // nothing to do here. return; @@ -376,7 +389,7 @@ AdaptorConfig::requireClassesPools(ConstElementPtr pools) { } void -AdaptorConfig::requireClassesSubnets(ConstElementPtr subnets) { +AdaptorConfig::sanitizeRequireClassesSubnets(ConstElementPtr subnets) { if (!subnets || subnets->empty()) { // nothing to do here. return; @@ -384,10 +397,10 @@ AdaptorConfig::requireClassesSubnets(ConstElementPtr subnets) { for (size_t i = 0; i < subnets->size(); ++i) { ElementPtr subnet = subnets->getNonConst(i); - requireClassesPools(subnet->get("pools")); - requireClassesPools(subnet->get("pd-pools")); + sanitizeRequireClassesPools(subnet->get("pools")); + sanitizeRequireClassesPools(subnet->get("pd-pools")); ConstElementPtr requires = subnet->get("require-client-classes"); - if (requires && (requires->size() == 0)) { + if (requires && requires->empty()) { subnet->remove("require-client-classes"); } } @@ -403,16 +416,16 @@ AdaptorConfig::requireClassesSharedNetworks(ConstElementPtr networks, for (size_t i = 0; i < networks->size(); ++i) { ElementPtr network = networks->getNonConst(i); - requireClassesSubnets(network->get(subsel)); + sanitizeRequireClassesSubnets(network->get(subsel)); ConstElementPtr requires = network->get("require-client-classes"); - if (requires && (requires->size() == 0)) { + if (requires && requires->empty()) { network->remove("require-client-classes"); } } } void -AdaptorConfig::hostList(ConstElementPtr hosts) { +AdaptorConfig::sanitizeHostList(ConstElementPtr hosts) { if (!hosts || hosts->empty()) { // nothing to do here. @@ -426,7 +439,7 @@ AdaptorConfig::hostList(ConstElementPtr hosts) { } void -AdaptorConfig::hostSubnets(ConstElementPtr subnets) { +AdaptorConfig::sanitizeHostSubnets(ConstElementPtr subnets) { if (!subnets || subnets->empty()) { // nothing to do here. @@ -434,12 +447,12 @@ AdaptorConfig::hostSubnets(ConstElementPtr subnets) { } for (ConstElementPtr subnet : subnets->listValue()) { - hostList(subnet->get("reservations")); + sanitizeHostList(subnet->get("reservations")); } } void -AdaptorConfig::hostSharedNetworks(ConstElementPtr networks, +AdaptorConfig::SanitizeHostsInSharedNetworks(ConstElementPtr networks, const string& space) { if (!networks || networks->empty()) { // nothing to do here. @@ -448,15 +461,15 @@ AdaptorConfig::hostSharedNetworks(ConstElementPtr networks, for (ConstElementPtr network : networks->listValue()) { if (space == DHCP4_SPACE) { - hostSubnets(network->get("subnet4")); + sanitizeHostSubnets(network->get("subnet4")); } else { - hostSubnets(network->get("subnet6")); + sanitizeHostSubnets(network->get("subnet6")); } } } void -AdaptorConfig::relaySubnets(ConstElementPtr subnets) { +AdaptorConfig::sanitizeRelaySubnets(ConstElementPtr subnets) { if (!subnets || subnets->empty()) { // nothing to do here. return; @@ -469,7 +482,7 @@ AdaptorConfig::relaySubnets(ConstElementPtr subnets) { } void -AdaptorConfig::relaySharedNetworks(ConstElementPtr networks, +AdaptorConfig::sanitizeRelayInSharedNetworks(ConstElementPtr networks, const string& subsel) { if (!networks || networks->empty()) { // nothing to do here. @@ -479,12 +492,12 @@ AdaptorConfig::relaySharedNetworks(ConstElementPtr networks, for (size_t i = 0; i < networks->size(); ++i) { ElementPtr network = networks->getNonConst(i); updateRelay(network); - relaySubnets(network->get(subsel)); + sanitizeRelaySubnets(network->get(subsel)); } } void -AdaptorConfig::updateDatabase(ConstElementPtr dhcp) { +AdaptorConfig::sanitizeDatabase(ConstElementPtr dhcp) { ConstElementPtr database = dhcp->get("hosts-database"); if (!database) { return; @@ -498,7 +511,7 @@ AdaptorConfig::updateDatabase(ConstElementPtr dhcp) { } void -AdaptorConfig::relaySuppliedOptions(ConstElementPtr dhcp) { +AdaptorConfig::sanitizeRelaySuppliedOptions(ConstElementPtr dhcp) { ConstElementPtr options = dhcp->get("relay-supplied-options"); if (!options || options->empty()) { return; @@ -517,7 +530,7 @@ AdaptorConfig::preProcess(ElementPtr dhcp, const string& subsel, SubnetIDSet set; ConstElementPtr subnets = dhcp->get(subsel); if (subnets) { - if (subnets->size() > 0) { + if (!subnets->empty()) { if (!subnetsCollectID(subnets, set)) { have_ids = false; } @@ -527,7 +540,7 @@ AdaptorConfig::preProcess(ElementPtr dhcp, const string& subsel, } ConstElementPtr networks = dhcp->get("shared-networks"); if (networks) { - if (networks->size() > 0) { + if (!networks->empty()) { if (!sharedNetworksCollectID(networks, set, subsel)) { have_ids = false; } @@ -546,55 +559,55 @@ AdaptorConfig::preProcess(ElementPtr dhcp, const string& subsel, initCodes(codes, space);; ConstElementPtr defs = dhcp->get("option-def"); if (defs) { - if (defs->size() > 0) { - optionDefList(defs, space, codes); + if (!defs->empty()) { + sanitizeOptionDefList(defs, space, codes); } else { dhcp->remove("option-def"); } } ConstElementPtr options = dhcp->get("option-data"); if (options) { - if (options->size() > 0) { - optionDataList(options, space, codes); + if (!options->empty()) { + sanitizeOptionDataList(options, space, codes); } else { dhcp->remove("option-data"); } } ConstElementPtr classes = dhcp->get("client-classes"); if (classes) { - if (classes->size() > 0) { - optionClasses(classes, space, codes); + if (!classes->empty()) { + sanitizeOptionClasses(classes, space, codes); } else { dhcp->remove("client-classes"); } } ConstElementPtr hosts = dhcp->get("reservations"); if (hosts) { - if (hosts->size() > 0) { - optionHosts(hosts, space, codes); + if (!hosts->empty()) { + sanitizeOptionHosts(hosts, space, codes); } else { dhcp->remove("reservations"); } } - optionSubnets(subnets, space, codes); - optionSharedNetworks(networks, space, codes); + sanitizeOptionSubnets(subnets, space, codes); + sanitizeOptionSharedNetworks(networks, space, codes); sanitizePoolsInSubnets(subnets); sanitizePoolsInSharedNetworks(networks, subsel); - hostSubnets(subnets); - hostSharedNetworks(networks, space); + sanitizeHostSubnets(subnets); + SanitizeHostsInSharedNetworks(networks, space); - relaySubnets(subnets); - relaySharedNetworks(networks, subsel); + sanitizeRelaySubnets(subnets); + sanitizeRelayInSharedNetworks(networks, subsel); - requireClassesSubnets(subnets); + sanitizeRequireClassesSubnets(subnets); requireClassesSharedNetworks(networks, subsel); - updateDatabase(dhcp); + sanitizeDatabase(dhcp); if (space == DHCP6_SPACE) { - relaySuppliedOptions(dhcp); + sanitizeRelaySuppliedOptions(dhcp); } } diff --git a/src/lib/yang/adaptor_config.h b/src/lib/yang/adaptor_config.h index 1ec1baed26..22c23f15ad 100644 --- a/src/lib/yang/adaptor_config.h +++ b/src/lib/yang/adaptor_config.h @@ -134,21 +134,24 @@ protected: /// @brief Collect option definitions from an option definition list. /// + /// Collects options definitions, but also sets missing option space + /// with default. + /// /// @param defs The option definition list. - /// @param space The default space name. + /// @param space The default space name (missing will be filled with this) /// @param codes Option definitions. - static void optionDefList(isc::data::ConstElementPtr defs, - const std::string& space, - OptionCodes& codes); + static void sanitizeOptionDefList(isc::data::ConstElementPtr defs, + const std::string& space, + OptionCodes& codes); /// @brief Set missing option codes to an option data list. /// /// @param options The option data list. /// @param space The default space name. /// @param codes Option definitions. - static void optionDataList(isc::data::ConstElementPtr options, - const std::string& space, - const OptionCodes& codes); + static void sanitizeOptionDataList(isc::data::ConstElementPtr options, + const std::string& space, + const OptionCodes& codes); /// @brief Collect option definitions from a client class list /// and set missing option codes. @@ -156,59 +159,59 @@ protected: /// @param classes The client class list. /// @param space The default space name. /// @param codes Option definitions. - static void optionClasses(isc::data::ConstElementPtr classes, - const std::string& space, - OptionCodes& codes); + static void sanitizeOptionClasses(isc::data::ConstElementPtr classes, + const std::string& space, + OptionCodes& codes); /// @brief Set missing option codes to a pool list. /// /// @param pools The pool list. /// @param space The default space name. /// @param codes Option definitions. - static void optionPools(isc::data::ConstElementPtr pools, - const std::string& space, - const OptionCodes& codes); + static void sanitizeOptionPools(isc::data::ConstElementPtr pools, + const std::string& space, + const OptionCodes& codes); /// @brief Set missing option codes to a host reservation list. /// /// @param hosts The host reservation list. /// @param space The default space name. /// @param codes Option definitions. - static void optionHosts(isc::data::ConstElementPtr hosts, - const std::string& space, - const OptionCodes& codes); + static void sanitizeOptionHosts(isc::data::ConstElementPtr hosts, + const std::string& space, + const OptionCodes& codes); /// @brief Set missing option codes to a subnet list. /// /// @param subnets The subnet list. /// @param space The default space name. /// @param codes Option definitions. - static void optionSubnets(isc::data::ConstElementPtr subnets, - const std::string& space, - const OptionCodes& codes); + static void sanitizeOptionSubnets(isc::data::ConstElementPtr subnets, + const std::string& space, + const OptionCodes& codes); /// @brief Set missing option codes to a shared network list. /// /// @param networks The shared network list. /// @param space The default space name. /// @param codes Option definitions. - static void optionSharedNetworks(isc::data::ConstElementPtr networks, - const std::string& space, - const OptionCodes& codes); + static void sanitizeOptionSharedNetworks(isc::data::ConstElementPtr networks, + const std::string& space, + const OptionCodes& codes); /// @brief Process require client classes in a pool list. /// /// Remove empty require client class list. /// /// @param pools The pool list. - static void requireClassesPools(isc::data::ConstElementPtr pools); + static void sanitizeRequireClassesPools(isc::data::ConstElementPtr pools); /// @brief Process require client classes in a subnet list. /// /// Remove empty require client class lists. /// /// @param subnets The subnet list. - static void requireClassesSubnets(isc::data::ConstElementPtr subnets); + static void sanitizeRequireClassesSubnets(isc::data::ConstElementPtr subnets); /// @brief Process require client classes in a shared network list. /// @@ -224,14 +227,14 @@ protected: /// Quote when needed flex-id identifiers. /// /// @param hosts The host reservation list. - static void hostList(isc::data::ConstElementPtr hosts); + static void sanitizeHostList(isc::data::ConstElementPtr hosts); /// @brief Process host reservations in a subnet list. /// /// Quote when needed flex-id identifiers. /// /// @param subnets The subnet list. - static void hostSubnets(isc::data::ConstElementPtr subnets); + static void sanitizeHostSubnets(isc::data::ConstElementPtr subnets); /// @brief Process host reservations in a shared network list. /// @@ -239,38 +242,38 @@ protected: /// /// @param networks The shared network list. /// @param space The default space name. - static void hostSharedNetworks(isc::data::ConstElementPtr networks, - const std::string& space); + static void SanitizeHostsInSharedNetworks(isc::data::ConstElementPtr networks, + const std::string& space); - /// @brief updateRelay in a subnet list. + /// @brief Sanitizes relay information in subnets in a subnet list. /// /// Force the use of ip-addresses when it finds an ip-address entry. /// /// @param subnets The subnet list. - static void relaySubnets(isc::data::ConstElementPtr subnets); + static void sanitizeRelaySubnets(isc::data::ConstElementPtr subnets); - /// @brief updateRelay in a shared network list. + /// @brief Sanitizes relay information in a shared network list. /// /// Force the use of ip-addresses when it finds an ip-address entry. /// /// @param networks The shared network list. /// @param subsel The subnet list name. - static void relaySharedNetworks(isc::data::ConstElementPtr networks, - const std::string& subsel); + static void sanitizeRelayInSharedNetworks(isc::data::ConstElementPtr networks, + const std::string& subsel); /// @brief Update (hosts) database. /// /// Force the use of hosts-databases vs. hosts-database. /// /// @param dhcp The DHCP server. - static void updateDatabase(isc::data::ConstElementPtr dhcp); + static void sanitizeDatabase(isc::data::ConstElementPtr dhcp); /// @brief Update relay supplied options. /// /// Remove empty relay supplied option list. /// /// @param dhcp The DHCPv6 server. - static void relaySuppliedOptions(isc::data::ConstElementPtr dhcp); + static void sanitizeRelaySuppliedOptions(isc::data::ConstElementPtr dhcp); /// @brief Pre process a configuration. /// -- 2.47.2