summaryrefslogtreecommitdiffstats
path: root/src/lib/dhcpsrv/cfg_hosts_util.cc
blob: 11c9e87aa2f5d7e2f2c2f541257bc880a70aceba (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
// Copyright (C) 2017-2018 Internet Systems Consortium, Inc. ("ISC")
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.

#include <config.h>

#include <cc/data.h>
#include <dhcpsrv/subnet_id.h>
#include <dhcpsrv/cfg_hosts_util.h>
#include <exceptions/exceptions.h>
#include <boost/pointer_cast.hpp>

using namespace isc::data;

namespace isc {
namespace dhcp {

void CfgHostsList::internalize(ConstElementPtr list) {
    if (!list) {
        isc_throw(BadValue, "internal error: CfgHostsList::internalize: "
                  "argument is NULL");
    }
    if (list->getType() != Element::list) {
        isc_throw(BadValue, "internal error: CfgHostsList::internalize: "
                  "argument is not a list Element");
    }
    for (size_t i = 0; i < list->size(); ++i) {
        ConstElementPtr item = list->get(i);
        if (!item) {
            isc_throw(BadValue, "internal error: CfgHostsList::internalize: "
                      "null pointer from the list at " << i);
        }
        if (item->getType() != Element::map) {
            isc_throw(BadValue, "internal error: CfgHostsList::internalize: "
                      "not a map from the list at " << i);
        }
        if (item->size() != 2) {
            isc_throw(BadValue, "internal error: CfgHostsList::internalize: "
                      "bad map size from the list at " << i);
        }
        ConstElementPtr id = item->get("id");
        if (!id) {
            isc_throw(BadValue, "internal error: CfgHostsList::internalize: "
                      "no id from a map at " << i);
        }
        if (id->getType() != Element::integer) {
            isc_throw(BadValue, "internal error: CfgHostsList::internalize: "
                      "not integer id from a map at " <<i);
        }
        SubnetID subnet_id = static_cast<SubnetID>(id->intValue());
        ConstElementPtr resvs = item->get("reservations");
        if (!resvs) {
            isc_throw(BadValue, "internal error: CfgHostsList::internalize: "
                      "no reservations for subnet ID " << subnet_id);
        }
        map_.insert(std::make_pair(subnet_id, 
                                   boost::const_pointer_cast<Element>(resvs)));
    }
}

ElementPtr CfgHostsList::externalize() const {
    ElementPtr result = Element::createList();
    for (CfgHostsMap::const_iterator item = map_.begin();
         item != map_.end(); ++item) {
        ElementPtr pair = Element::createMap();
        pair->set("id", Element::create(static_cast<int64_t>(item->first)));
        pair->set("reservations", item->second);
        result->add(pair);
    }
    return (result);
}

void CfgHostsList::add(SubnetID id, isc::data::ElementPtr resv) {
    CfgHostsMap::iterator item = map_.find(id);
    if (item != map_.end()) {
        item->second->add(resv);
    } else {
        ElementPtr resvs = Element::createList();
        resvs->add(resv);
        map_.insert(std::make_pair(id, resvs));
    }
}

ConstElementPtr CfgHostsList::get(SubnetID id) const {
    CfgHostsMap::const_iterator item = map_.find(id);
    if (item != map_.end()) {
        return (item->second);
    } else {
        return (Element::createList());
    }
}

}
}