summaryrefslogtreecommitdiffstats
path: root/src/lib/yang/translator_option_def.cc
blob: a552191aa835069c14c0d3f6b84a8274747f197a (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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
// Copyright (C) 2018-2021 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 <yang/translator_option_def.h>
#include <yang/adaptor.h>
#include <yang/yang_models.h>
#include <sstream>

using namespace std;
using namespace isc::data;
using namespace sysrepo;

namespace isc {
namespace yang {

TranslatorOptionDef::TranslatorOptionDef(S_Session session,
                                         const string& model)
    : TranslatorBasic(session, model) {
}

TranslatorOptionDef::~TranslatorOptionDef() {
}

ElementPtr
TranslatorOptionDef::getOptionDef(const string& xpath) {
    try {
        if ((model_ == KEA_DHCP4_SERVER) ||
            (model_ == KEA_DHCP6_SERVER)) {
            return (getOptionDefKea(xpath));
        }
    } catch (const sysrepo_exception& ex) {
        isc_throw(SysrepoError,
                  "sysrepo error getting option definition at '" << xpath
                  << "': " << ex.what());
    }
    isc_throw(NotImplemented,
              "getOptionDef not implemented for the model: " << model_);
}

ElementPtr
TranslatorOptionDef::getOptionDefKea(const string& xpath) {
    ConstElementPtr code = getItem(xpath + "/code");
    ConstElementPtr name = getItem(xpath + "/name");
    ConstElementPtr type = getItem(xpath + "/type");
    ConstElementPtr space = getItem(xpath + "/space");
    if (!code || !space) {
        // Can't happen as code and space are the keys.
        isc_throw(Unexpected, "getOptionDefKea requires code and space: "
                  << xpath);
    }
    if (!name || !type) {
        isc_throw(BadValue, "getOptionDefKea requires name and type: "
                  << xpath);
    }
    ElementPtr result = Element::createMap();
    result->set("code", code);
    result->set("name", name);
    result->set("type", type);
    result->set("space", getItem(xpath + "/space"));
    ConstElementPtr record = getItem(xpath + "/record-types");
    if (record) {
        result->set("record-types", record);
    }
    ConstElementPtr array = getItem(xpath + "/array");
    if (array) {
        result->set("array", array);
    }
    ConstElementPtr encapsulate = getItem(xpath + "/encapsulate");
    if (encapsulate) {
        result->set("encapsulate", encapsulate);
    }
    ConstElementPtr context = getItem(xpath + "/user-context");
    if (context) {
        result->set("user-context", Element::fromJSON(context->stringValue()));
    }
    return (result);
}

void
TranslatorOptionDef::setOptionDef(const string& xpath, ConstElementPtr elem) {
    try {
        if ((model_ == KEA_DHCP4_SERVER) ||
            (model_ == KEA_DHCP6_SERVER)) {
            setOptionDefKea(xpath, elem);
        } else {
            isc_throw(NotImplemented,
                      "setOptionDef not implemented for the model: "
                      << model_);
        }
    } catch (const sysrepo_exception& ex) {
        isc_throw(SysrepoError,
                  "sysrepo error setting option definition '" << elem->str()
                  << "' at '" << xpath << "': " << ex.what());
    }
}

void
TranslatorOptionDef::setOptionDefKea(const string& xpath,
                                     ConstElementPtr elem) {
    // Skip code and space as they are the keys.
    ConstElementPtr name = elem->get("name");
    if (!name) {
        isc_throw(BadValue, "option definition with name: " << elem->str());
    }
    setItem(xpath + "/name", name, SR_STRING_T);
    ConstElementPtr type = elem->get("type");
    if (!type) {
        isc_throw(BadValue, "option definition with type: " << elem->str());
    }
    setItem(xpath + "/type", type, SR_STRING_T);
    ConstElementPtr record = elem->get("record-types");
    if (record) {
        setItem(xpath + "/record-types", record, SR_STRING_T);
    }
    ConstElementPtr array = elem->get("array");
    if (array) {
        setItem(xpath + "/array", array, SR_BOOL_T);
    }
    ConstElementPtr encapsulate = elem->get("encapsulate");
    if (encapsulate) {
        setItem(xpath + "/encapsulate", encapsulate, SR_STRING_T);
    }
    ConstElementPtr context = Adaptor::getContext(elem);
    if (context) {
        setItem(xpath + "/user-context", Element::create(context->str()),
                SR_STRING_T);
    }
}

TranslatorOptionDefList::TranslatorOptionDefList(S_Session session,
                                                 const string& model)
    : TranslatorBasic(session, model),
      TranslatorOptionDef(session, model) {
}

TranslatorOptionDefList::~TranslatorOptionDefList() {
}

ConstElementPtr
TranslatorOptionDefList::getOptionDefList(const string& xpath) {
    try {
        if ((model_ == KEA_DHCP4_SERVER) ||
            (model_ == KEA_DHCP6_SERVER)) {
            return (getOptionDefListKea(xpath));
        }
    } catch (const sysrepo_exception& ex) {
        isc_throw(SysrepoError,
                  "sysrepo error getting option definition list at '" << xpath
                  << "': " << ex.what());
    }
    isc_throw(NotImplemented,
              "getOptionDefList not implemented for the model: " << model_);
}

ConstElementPtr
TranslatorOptionDefList::getOptionDefListKea(const string& xpath) {
    return getList<TranslatorOptionDef>(xpath + "/option-def", *this,
                                        &TranslatorOptionDefList::getOptionDef);
}

void
TranslatorOptionDefList::setOptionDefList(const string& xpath,
                                          ConstElementPtr elem) {
    try {
        if ((model_ == KEA_DHCP4_SERVER) ||
            (model_ == KEA_DHCP6_SERVER)) {
            setOptionDefListKea(xpath, elem);
        } else {
            isc_throw(NotImplemented,
                      "setOptionDefList not implemented for the model: "
                      << model_);
        }
    } catch (const sysrepo_exception& ex) {
        isc_throw(SysrepoError,
                  "sysrepo error setting option definition list '"
                  << elem->str() << "' at '" << xpath << "': " << ex.what());
    }
}

void
TranslatorOptionDefList::setOptionDefListKea(const string& xpath,
                                             ConstElementPtr elem) {
    for (size_t i = 0; i < elem->size(); ++i) {
        ConstElementPtr def = elem->get(i);
        if (!def->contains("code")) {
            isc_throw(BadValue,
                      "option definition without code: " << def->str());
        }
        unsigned code = static_cast<unsigned>(def->get("code")->intValue());
        if (!def->contains("space")) {
            isc_throw(BadValue,
                      "option definition without space: " << def->str());
        }
        string space = def->get("space")->stringValue();
        ostringstream keys;
        keys << xpath << "/option-def[code='" << code
            << "'][space='" << space << "']";
        setOptionDef(keys.str(), def);
    }
}

}  // namespace yang
}  // namespace isc