| Kea 3.0.2-git

user_context_utils.cc
Go to the documentation of this file.
1// Copyright (C) 2017-2024 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 <testutils/user_context_utils.h>
10
11 using namespace isc::data;
12
13 namespace {
14
17 template<typename EP>
18 class Value {
19 public:
21 static Value mkCopy(EP value) { return (Value(value, false)); }
22
24 static Value mkShare(EP value) { return (Value(value, true)); }
25
28 EP get() const { return (value_); }
29
32 bool isShared() const { return (shared_); }
33
34 private:
38 Value(EP value, bool shared) : value_(value), shared_(shared) { }
39
41 EP value_;
42
44 bool shared_;
45};
46
53 template<typename EP>
54Value<EP> moveComments1(EP element) {
55 bool modified = false;
56
57 // On lists recurse on items
58 if (element->getType() == Element::list) {
59 ElementPtr result = ElementPtr(new ListElement());
60 typedef std::vector<ElementPtr> ListType;
61 const ListType& list = element->listValue();
62 for (auto const& it : list) {
63 Value<ElementPtr> item = moveComments1(it);
64 result->add(item.get());
65 if (!item.isShared()) {
66 modified = true;
67 }
68 }
69 if (!modified) {
70 return (Value<EP>::mkShare(element));
71 } else {
72 return (Value<EP>::mkCopy(result));
73 }
74 } else if (element->getType() != Element::map) {
75 return (Value<EP>::mkShare(element));
76 }
77
78 // Process maps: recurse on items
79 ElementPtr result = ElementPtr(new MapElement());
80 bool has_comment = false;
81 typedef std::map<std::string, ConstElementPtr> map_type;
82 const map_type& map = element->mapValue();
83 for (auto const& it : map) {
84 if (it.first == "comment") {
85 // Note there is a comment entry to move
86 has_comment = true;
87 } else if (it.first == "user-context") {
88 // Do not traverse user-context entries
89 result->set("user-context", it.second);
90 } else {
91 // Not comment or user-context
92 Value<ConstElementPtr> item = moveComments1(it.second);
93 result->set(it.first, item.get());
94 if (!item.isShared()) {
95 modified = true;
96 }
97 }
98 }
99 // Check if the value should be not modified
100 if (!has_comment && !modified) {
101 return (Value<EP>::mkShare(element));
102 }
103
104 if (has_comment) {
105 // Move the comment entry
106 ConstElementPtr comment = element->get("comment");
107 ElementPtr moved = Element::createMap();
108 moved->set("comment", comment);
109 ConstElementPtr previous = element->get("user-context");
110 // If there is already a user context merge it
111 if (previous) {
112 merge(moved, previous);
113 }
114 result->set("user-context", moved);
115 }
116
117 return (Value<EP>::mkCopy(result));
118}
119
120} // anonymous namespace
121
122 namespace isc {
123 namespace test {
124
125 ElementPtr moveComments(ElementPtr element) {
126 Value<ElementPtr> result = moveComments1(element);
127 return (result.get());
128}
129
130 ConstElementPtr moveComments(ConstElementPtr element) {
131 Value<ConstElementPtr> result = moveComments1(element);
132 return (result.get());
133}
134
135} // end of isc::test namespace
136} // end of isc namespace
Element::map
@ map
Definition data.h:147
Element::list
@ list
Definition data.h:146
Element::createMap
static ElementPtr createMap(const Position &pos=ZERO_POSITION())
Creates an empty MapElement type ElementPtr.
Definition data.cc:304
isc::data::ListElement
Definition data.h:724
isc::data::MapElement
Definition data.h:768
get
int get(CalloutHandle &handle)
The gss-tsig-get command.
Definition gss_tsig_callouts.cc:47
isc::data
Definition base_stamped_element.cc:12
isc::data::merge
void merge(ElementPtr element, ConstElementPtr other)
Merges the data from other into element.
Definition data.cc:1199
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::test
Definition env_var_wrapper.cc:12
isc::test::moveComments
ElementPtr moveComments(ElementPtr element)
Move comment entries to user-context.
Definition user_context_utils.cc:125
isc
Defines the logger used by the top-level component of kea-lfc.
Definition agent_parser.cc:148
user_context_utils.h