Kea  1.9.9-git
cfg_hosts_util.cc
Go to the documentation of this file.
1 // Copyright (C) 2017-2018 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>
11 #include <dhcpsrv/cfg_hosts_util.h>
12 #include <exceptions/exceptions.h>
13 #include <boost/pointer_cast.hpp>
14 
15 using namespace isc::data;
16 
17 namespace isc {
18 namespace dhcp {
19 
20 void CfgHostsList::internalize(ConstElementPtr list) {
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 
63 ElementPtr CfgHostsList::externalize() const {
65  for (CfgHostsMap::const_iterator item = map_.begin();
66  item != map_.end(); ++item) {
68  pair->set("id", Element::create(static_cast<int64_t>(item->first)));
69  pair->set("reservations", item->second);
70  result->add(pair);
71  }
72  return (result);
73 }
74 
75 void CfgHostsList::add(SubnetID id, isc::data::ElementPtr resv) {
76  CfgHostsMap::iterator item = map_.find(id);
77  if (item != map_.end()) {
78  item->second->add(resv);
79  } else {
81  resvs->add(resv);
82  map_.insert(std::make_pair(id, resvs));
83  }
84 }
85 
86 ConstElementPtr CfgHostsList::get(SubnetID id) const {
87  CfgHostsMap::const_iterator item = map_.find(id);
88  if (item != map_.end()) {
89  return (item->second);
90  } else {
91  return (Element::createList());
92  }
93 }
94 
95 }
96 }
boost::shared_ptr< Element > ElementPtr
Definition: data.h:20
static ElementPtr createMap(const Position &pos=ZERO_POSITION())
Creates an empty MapElement type ElementPtr.
Definition: data.cc:267
static ElementPtr createList(const Position &pos=ZERO_POSITION())
Creates an empty ListElement type ElementPtr.
Definition: data.cc:262
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
A generic exception that is thrown if a parameter given to a method is considered invalid in that con...
boost::shared_ptr< const Element > ConstElementPtr
Definition: data.h:23
Defines the logger used by the top-level component of kea-dhcp-ddns.
static ElementPtr create(const Position &pos=ZERO_POSITION())
Definition: data.cc:222
uint32_t SubnetID
Unique identifier for a subnet (both v4 and v6)
Definition: lease.h:24