1 // Copyright (C) 2018-2021 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 #ifndef ISC_TRANSLATOR_OPTION_DEF_H
8 #define ISC_TRANSLATOR_OPTION_DEF_H 1
9 
10 #include <yang/translator.h>
11 #include <list>
12 
13 namespace isc {
14 namespace yang {
15 
16 /// @brief Option definition translation between YANG and JSON
17 ///
18 /// JSON syntax for Kea DHCP servers is:
19 /// @code
20 /// {
21 ///     "code": <code>,
22 ///     "name": <name>,
23 ///     "space": <space>,
24 ///     "type": <type>,
25 ///     "array": <array flag>,
26 ///     "encapsulate": <encapsulated space>,
27 ///     "record-types": <record types>,
28 ///     "user-context": { <json map> },
29 ///     "comment": "<comment>"
30 /// }
31 /// @endcode
32 ///
33 /// YANG syntax for kea-dhcp[46] with code and space as keys is:
34 /// @code
35 ///    +--rw name           string
36 ///    +--rw code           uint8 / uint16
37 ///    +--rw type           string
38 ///    +--rw record-types?  string
39 ///    +--rw space          string
40 ///    +--rw encapsulate?   string
41 ///    +--rw array?         boolean
42 ///    +--rw user-context?  string
43 /// @endcode
44 ///
45 /// An example in JSON and YANG formats:
46 /// @code
47 /// [
48 ///     {
49 ///         "code": 100,
50 ///         "name": "foo",
51 ///         "space": "isc",
52 ///         "type": "string",
53 ///         "array": false
54 ///     }
55 /// ]
56 /// @endcode
57 /// @code
58 ///  /kea-dhcp6-server:config (container)
59 ///  /kea-dhcp6-server:config/
60 ///     option-def[code='100'][space='isc'] (list instance)
61 ///  /kea-dhcp6-server:config/
62 ///     option-def[code='100'][space='isc']/code = 100
63 ///  /kea-dhcp6-server:config/
64 ///     option-def[code='100'][space='isc']/space = isc
65 ///  /kea-dhcp6-server:config/
66 ///     option-def[code='100'][space='isc']/name = foo
67 ///  /kea-dhcp6-server:config/
68 ///     option-def[code='100'][space='isc']/type = string
69 ///  /kea-dhcp6-server:config/
70 ///     option-def[code='100'][space='isc']/array = false
71 /// @endcode
72 
73 /// @brief A translator class for converting an option definition between
74 /// YANG and JSON.
75 ///
76 /// Currently supports kea-dhcp[46]-server models.
77 /// @todo: Support for ietf-dhcpv6-server model.
78 class TranslatorOptionDef : virtual public TranslatorBasic {
79 public:
80 
81     /// @brief Constructor.
82     ///
83     /// @param session Sysrepo session.
84     /// @param model Model name.
85     TranslatorOptionDef(sysrepo::S_Session session, const std::string& model);
86 
87     /// @brief Destructor.
88     virtual ~TranslatorOptionDef();
89 
90     /// @brief Get and translate an option definition from YANG to JSON.
91     ///
92     /// @param xpath The xpath of the option definition.
93     /// @return JSON representation of the option definition.
94     /// @throw SysrepoError when sysrepo raises an error.
95     isc::data::ElementPtr getOptionDef(const std::string& xpath);
96 
97     /// @brief Translate and set option definition from JSON to YANG.
98     ///
99     /// @param xpath The xpath of the option definition..
100     /// @param elem The JSON element.
101     void setOptionDef(const std::string& xpath,
102                       isc::data::ConstElementPtr elem);
103 
104 protected:
105     /// @brief getOptionDef implementation specific to kea-dhcp[46]-server models.
106     ///
107     /// @param xpath The xpath of the option definition.
108     /// @return JSON representation of the option definition.
109     /// @throw SysrepoError when sysrepo raises an error.
110     /// @throw BadValue on option definition without name or type.
111     isc::data::ElementPtr getOptionDefKea(const std::string& xpath);
112 
113     /// @brief setOptionDef implementation specific to kea-dhcp[46]-server models.
114     ///
115     /// @param xpath The xpath of the option definition.
116     /// @param elem The JSON element.
117     /// @throw BadValue on option definition without name or type.
118     void setOptionDefKea(const std::string& xpath,
119                          isc::data::ConstElementPtr elem);
120 };
121 
122 // @brief A translator class for converting an option definition list
123 // between YANG and JSON.
124 //
125 /// Currently supports kea-dhcp[46]-server models.
126 /// @todo: Support for ietf-dhcpv6-server model.
127 class TranslatorOptionDefList : virtual public TranslatorOptionDef {
128 public:
129 
130     /// @brief Constructor.
131     ///
132     /// @param session Sysrepo session.
133     /// @param model Model name.
134     TranslatorOptionDefList(sysrepo::S_Session session,
135                             const std::string& model);
136 
137     /// @brief Destructor.
138     virtual ~TranslatorOptionDefList();
139 
140     /// @brief Get and translate option definition list from YANG to JSON.
141     ///
142     /// @param xpath The xpath of the option definition list.
143     /// @throw SysrepoError when sysrepo raises an error.
144     isc::data::ConstElementPtr getOptionDefList(const std::string& xpath);
145 
146     /// @brief Translate and set option definition list from JSON to YANG.
147     ///
148     /// @param xpath The xpath of the option definition list.
149     /// @param elem The JSON element.
150     void setOptionDefList(const std::string& xpath,
151                           isc::data::ConstElementPtr elem);
152 
153 protected:
154     /// @brief getOptionDefList implementation specific to kea-dhcp[46]-server
155     ///        models.
156     ///
157     /// @param xpath The xpath of the option definition list.
158     /// @throw SysrepoError when sysrepo raises an error.
159     isc::data::ConstElementPtr getOptionDefListKea(const std::string& xpath);
160 
161     /// @brief setOptionDefList implementation specific to kea-dhcp[46]-server
162     ///        models.
163     ///
164     /// @param xpath The xpath of the option definition list.
165     /// @param elem The JSON element.
166     /// @throw BadValue on option definition without code or space.
167     void setOptionDefListKea(const std::string& xpath,
168                              isc::data::ConstElementPtr elem);
169 };
170 
171 }  // namespace yang
172 }  // namespace isc
173 
174 #endif // ISC_TRANSLATOR_OPTION_DEF_H
175