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