| Kea 3.0.2-git

adaptor_option.cc
Go to the documentation of this file.
1// Copyright (C) 2018-2022 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 <dhcp/docsis3_option_defs.h>
10#include <dhcp/std_option_defs.h>
11#include <yang/adaptor_option.h>
12#include <yang/netconf_error.h>
13
14 using namespace std;
15 using namespace isc::data;
16 using namespace isc::dhcp;
17
18 namespace isc {
19 namespace yang {
20
21 void
22 AdaptorOption::setSpace(ElementPtr option, const string& space) {
23 if (!option->contains("space")) {
24 option->set("space", Element::create(space));
25 }
26}
27
28 void
29 AdaptorOption::checkType(ConstElementPtr option) {
30 if (!option->contains("type")) {
31 isc_throw(MissingKey, "missing type in option definition "
32 << option->str());
33 }
34}
35
36 void
37 AdaptorOption::checkCode(ConstElementPtr option) {
38 if (!option->contains("code")) {
39 isc_throw(MissingKey, "missing code in option " << option->str());
40 }
41}
42
43 void
44 AdaptorOption::collect(ConstElementPtr option, OptionCodes& codes) {
45 ConstElementPtr name = option->get("name");
46 if (name) {
47 ConstElementPtr space = option->get("space");
48 ConstElementPtr code = option->get("code");
49 string index = space->stringValue() + "@" + name->stringValue();
50 uint16_t val = static_cast<uint16_t>(code->intValue());
51 codes.insert(pair<string, uint16_t>(index, val));
52 }
53}
54
55 void
56 AdaptorOption::setCode(ElementPtr option, const OptionCodes& codes) {
57 ConstElementPtr code = option->get("code");
58 if (!code) {
59 ConstElementPtr name = option->get("name");
60 if (!name) {
61 isc_throw(MissingKey, "missing name and code in option "
62 << option->str());
63 }
64 ConstElementPtr space = option->get("space");
65 string index = space->stringValue() + "@" + name->stringValue();
66 OptionCodes::const_iterator it = codes.find(index);
67 if (it == codes.end()) {
68 isc_throw(MissingKey, "can't get code from option "
69 << option->str());
70 }
71 option->set("code", Element::create(static_cast<int>(it->second)));
72 }
73}
74
75 void
76 AdaptorOption::initCodes(OptionCodes& codes, const string& space) {
77 if (space == DHCP4_OPTION_SPACE) {
78 initCodesInternal(codes, space, STANDARD_V4_OPTION_DEFINITIONS,
79 STANDARD_V4_OPTION_DEFINITIONS_SIZE);
80 initCodesInternal(codes, space, LAST_RESORT_V4_OPTION_DEFINITIONS,
81 LAST_RESORT_V4_OPTION_DEFINITIONS_SIZE);
82 initCodesInternal(codes, "vendor-4491",
83 DOCSIS3_V4_OPTION_DEFINITIONS,
84 DOCSIS3_V4_OPTION_DEFINITIONS_SIZE);
85 } else if (space == DHCP6_OPTION_SPACE) {
86 initCodesInternal(codes, space, STANDARD_V6_OPTION_DEFINITIONS,
87 STANDARD_V6_OPTION_DEFINITIONS_SIZE);
88 initCodesInternal(codes, "vendor-4491",
89 DOCSIS3_V6_OPTION_DEFINITIONS,
90 DOCSIS3_V6_OPTION_DEFINITIONS_SIZE);
91 initCodesInternal(codes, MAPE_V6_OPTION_SPACE,
92 MAPE_V6_OPTION_DEFINITIONS,
93 MAPE_V6_OPTION_DEFINITIONS_SIZE);
94 initCodesInternal(codes, MAPT_V6_OPTION_SPACE,
95 MAPT_V6_OPTION_DEFINITIONS,
96 MAPT_V6_OPTION_DEFINITIONS_SIZE);
97 initCodesInternal(codes, LW_V6_OPTION_SPACE,
98 LW_V6_OPTION_DEFINITIONS,
99 LW_V6_OPTION_DEFINITIONS_SIZE);
100 initCodesInternal(codes, V4V6_RULE_OPTION_SPACE,
101 V4V6_RULE_OPTION_DEFINITIONS,
102 V4V6_RULE_OPTION_DEFINITIONS_SIZE);
103 initCodesInternal(codes, V4V6_BIND_OPTION_SPACE,
104 V4V6_BIND_OPTION_DEFINITIONS,
105 V4V6_BIND_OPTION_DEFINITIONS_SIZE);
106 initCodesInternal(codes, "vendor-2495",
107 ISC_V6_OPTION_DEFINITIONS,
108 ISC_V6_OPTION_DEFINITIONS_SIZE);
109 }
110}
111
112 void
113 AdaptorOption::initCodesInternal(OptionCodes& codes, const string& space,
114 const OptionDefParams* params,
115 size_t params_size) {
116 for (size_t i = 0; i < params_size; ++i) {
117 string index = space + "@" + params[i].name;
118 codes.insert(pair<string, uint16_t>(index, params[i].code));
119 }
120}
121
122} // namespace yang
123} // namespace isc
adaptor_option.h
Element::create
static ElementPtr create(const Position &pos=ZERO_POSITION())
Definition data.cc:249
isc::yang::AdaptorOption::setCode
static void setCode(isc::data::ElementPtr option, const OptionCodes &codes)
Set code from name and definitions.
Definition adaptor_option.cc:56
isc::yang::AdaptorOption::initCodes
static void initCodes(OptionCodes &codes, const std::string &space)
Initialize code map.
Definition adaptor_option.cc:76
isc::yang::AdaptorOption::setSpace
static void setSpace(isc::data::ElementPtr option, const std::string &space)
Set space.
Definition adaptor_option.cc:22
isc::yang::AdaptorOption::checkType
static void checkType(isc::data::ConstElementPtr option)
Checks if type is specified in option definition.
Definition adaptor_option.cc:29
isc::yang::AdaptorOption::initCodesInternal
static void initCodesInternal(OptionCodes &codes, const std::string &space, const isc::dhcp::OptionDefParams *params, size_t params_size)
Initialize code map from option definition parameters.
Definition adaptor_option.cc:113
isc::yang::AdaptorOption::collect
static void collect(isc::data::ConstElementPtr option, OptionCodes &codes)
Collect definition.
Definition adaptor_option.cc:44
isc::yang::AdaptorOption::checkCode
static void checkCode(isc::data::ConstElementPtr option)
Check if code is specified in option defintion.
Definition adaptor_option.cc:37
docsis3_option_defs.h
isc_throw
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
Definition exceptions/exceptions.h:210
isc::data
Definition base_stamped_element.cc:12
isc::data::ConstElementPtr
boost::shared_ptr< const Element > ConstElementPtr
Definition data.h:29
isc::data::ElementPtr
boost::shared_ptr< Element > ElementPtr
Definition data.h:28
isc::dhcp
Definition dhcp4/client_handler.cc:20
isc::dhcp::DOCSIS3_V4_OPTION_DEFINITIONS
const OptionDefParams DOCSIS3_V4_OPTION_DEFINITIONS[]
Definitions of standard DHCPv4 options.
Definition docsis3_option_defs.h:26
isc::dhcp::DOCSIS3_V6_OPTION_DEFINITIONS_SIZE
const int DOCSIS3_V6_OPTION_DEFINITIONS_SIZE
Number of option definitions defined.
Definition docsis3_option_defs.h:79
isc::dhcp::DOCSIS3_V4_OPTION_DEFINITIONS_SIZE
const int DOCSIS3_V4_OPTION_DEFINITIONS_SIZE
Number of option definitions defined.
Definition docsis3_option_defs.h:34
isc::dhcp::DOCSIS3_V6_OPTION_DEFINITIONS
const OptionDefParams DOCSIS3_V6_OPTION_DEFINITIONS[]
Definitions of standard DHCPv6 options.
Definition docsis3_option_defs.h:54
isc::yang
Definition adaptor.cc:17
isc::yang::OptionCodes
std::unordered_map< std::string, uint16_t > OptionCodes
Map for DHCP option definitions handling code and an index built from space and name.
Definition adaptor_option.h:23
isc
Defines the logger used by the top-level component of kea-lfc.
Definition agent_parser.cc:148
netconf_error.h
std_option_defs.h
V4V6_BIND_OPTION_SPACE
#define V4V6_BIND_OPTION_SPACE
Definition std_option_defs.h:23
DHCP4_OPTION_SPACE
#define DHCP4_OPTION_SPACE
global std option spaces
Definition std_option_defs.h:16
V4V6_RULE_OPTION_SPACE
#define V4V6_RULE_OPTION_SPACE
Definition std_option_defs.h:22
MAPE_V6_OPTION_SPACE
#define MAPE_V6_OPTION_SPACE
Definition std_option_defs.h:19
LW_V6_OPTION_SPACE
#define LW_V6_OPTION_SPACE
Definition std_option_defs.h:21
DHCP6_OPTION_SPACE
#define DHCP6_OPTION_SPACE
Definition std_option_defs.h:17
MAPT_V6_OPTION_SPACE
#define MAPT_V6_OPTION_SPACE
Definition std_option_defs.h:20
isc::dhcp::OptionDefParams
Parameters being used to make up an option definition.
Definition option_data_types.h:70
isc::dhcp::OptionDefParams::name
const char * name
Definition option_data_types.h:71
isc::yang::MissingKey
Missing key error.
Definition netconf_error.h:23