Kea 2.7.5
cfg_hosts_util.cc
Go to the documentation of this file.
1// Copyright (C) 2017-2024 Internet Systems Consortium, Inc. ("ISC")
2//
3// This Source Code Form is subject to the terms of the Mozilla Public
4// License, v. 2.0. If a copy of the MPL was not distributed with this
5// file, You can obtain one at http://mozilla.org/MPL/2.0/.
6
7#include <config.h>
8
9#include <cc/data.h>
10#include <dhcpsrv/subnet_id.h>
13#include <boost/pointer_cast.hpp>
14
15using namespace isc::data;
16
17namespace isc {
18namespace dhcp {
19
21 if (!list) {
22 isc_throw(BadValue, "internal error: CfgHostsList::internalize: "
23 "argument is NULL");
24 }
25 if (list->getType() != Element::list) {
26 isc_throw(BadValue, "internal error: CfgHostsList::internalize: "
27 "argument is not a list Element");
28 }
29 for (size_t i = 0; i < list->size(); ++i) {
30 ConstElementPtr item = list->get(i);
31 if (!item) {
32 isc_throw(BadValue, "internal error: CfgHostsList::internalize: "
33 "null pointer from the list at " << i);
34 }
35 if (item->getType() != Element::map) {
36 isc_throw(BadValue, "internal error: CfgHostsList::internalize: "
37 "not a map from the list at " << i);
38 }
39 if (item->size() != 2) {
40 isc_throw(BadValue, "internal error: CfgHostsList::internalize: "
41 "bad map size from the list at " << i);
42 }
43 ConstElementPtr id = item->get("id");
44 if (!id) {
45 isc_throw(BadValue, "internal error: CfgHostsList::internalize: "
46 "no id from a map at " << i);
47 }
48 if (id->getType() != Element::integer) {
49 isc_throw(BadValue, "internal error: CfgHostsList::internalize: "
50 "not integer id from a map at " <<i);
51 }
52 SubnetID subnet_id = static_cast<SubnetID>(id->intValue());
53 ConstElementPtr resvs = item->get("reservations");
54 if (!resvs) {
55 isc_throw(BadValue, "internal error: CfgHostsList::internalize: "
56 "no reservations for subnet ID " << subnet_id);
57 }
58 map_.insert(std::make_pair(subnet_id,
59 boost::const_pointer_cast<Element>(resvs)));
60 }
61}
62
65 for (auto const& item : map_) {
67 pair->set("id", Element::create(static_cast<int64_t>(item.first)));
68 pair->set("reservations", item.second);
69 result->add(pair);
70 }
71 return (result);
72}
73
75 CfgHostsMap::iterator item = map_.find(id);
76 if (item != map_.end()) {
77 item->second->add(resv);
78 } else {
80 resvs->add(resv);
81 map_.insert(std::make_pair(id, resvs));
82 }
83}
84
86 CfgHostsMap::const_iterator item = map_.find(id);
87 if (item != map_.end()) {
88 return (item->second);
89 } else {
90 return (Element::createList());
91 }
92}
93
94}
95}
A generic exception that is thrown if a parameter given to a method is considered invalid in that con...
static ElementPtr create(const Position &pos=ZERO_POSITION())
Definition data.cc:249
static ElementPtr createMap(const Position &pos=ZERO_POSITION())
Creates an empty MapElement type ElementPtr.
Definition data.cc:304
static ElementPtr createList(const Position &pos=ZERO_POSITION())
Creates an empty ListElement type ElementPtr.
Definition data.cc:299
isc::data::ConstElementPtr get(SubnetID id) const
Return the host reservations for a subnet ID.
void internalize(isc::data::ConstElementPtr list)
Internalize a list Element.
void add(SubnetID id, isc::data::ElementPtr resv)
Add a host reservation to the map.
isc::data::ElementPtr externalize() const
Externalize the map to a list Element.
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
boost::shared_ptr< const Element > ConstElementPtr
Definition data.h:29
boost::shared_ptr< Element > ElementPtr
Definition data.h:28
uint32_t SubnetID
Defines unique IPv4 or IPv6 subnet identifier.
Definition subnet_id.h:25
Defines the logger used by the top-level component of kea-lfc.