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