Kea 2.7.6
classify.cc
Go to the documentation of this file.
1// Copyright (C) 2014-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 <cc/data.h>
10#include <dhcp/classify.h>
11#include <util/str.h>
12
13#include <boost/algorithm/string/classification.hpp>
14#include <boost/algorithm/string/constants.hpp>
15#include <boost/algorithm/string/split.hpp>
16
17#include <sstream>
18#include <vector>
19
20namespace isc {
21namespace dhcp {
22
23using namespace isc::data;
24
25ClientClasses::ClientClasses(const std::string& class_names)
26 : container_() {
27 std::vector<std::string> split_text;
28 boost::split(split_text, class_names, boost::is_any_of(","),
29 boost::algorithm::token_compress_off);
30 for (size_t i = 0; i < split_text.size(); ++i) {
31 std::string trimmed = util::str::trim(split_text[i]);
32 // Ignore empty class names.
33 if (!trimmed.empty()) {
34 insert(ClientClass(trimmed));
35 }
36 }
37}
38
40 for (auto const& cclass : other) {
41 insert(cclass);
42 }
43}
44
45void
47 auto& idx = container_.get<ClassNameTag>();
48 auto it = idx.find(class_name);
49 if (it != idx.end()) {
50 static_cast<void>(idx.erase(it));
51 }
52}
53
54bool
56 auto const& idx = container_.get<ClassNameTag>();
57 return (idx.count(x) != 0);
58}
59
60bool
62 if (cclasses.size() > size()) {
63 for (const auto& cclass : *this) {
64 if (cclasses.contains(cclass)) {
65 return (true);
66 }
67 }
68 } else {
69 for (const auto& cclass : cclasses) {
70 if (contains(cclass)) {
71 return (true);
72 }
73 }
74 }
75
76 return (false);
77}
78
79std::string
80ClientClasses::toText(const std::string& separator) const {
81 std::stringstream s;
82 bool first = true;
83 for (auto const& class_it : *this) {
84 if (!first) {
85 s << separator;
86 } else {
87 first = false;
88 }
89 s << class_it;
90 }
91 return (s.str());
92}
93
97 for (auto const& c : container_) {
98 result->add(Element::create(c));
99 }
100 return (result);
101}
102
103void
105 if (cc_list) {
106 clear();
107 if (cc_list->getType() != Element::list) {
108 isc_throw(BadValue, "not a List element");
109 }
110
111 for (auto i = 0; i < cc_list->size(); ++i) {
112 auto cclass = cc_list->get(i);
113 if (cclass->getType() != Element::string) {
114 isc_throw(BadValue, "elements of list must be valid strings");
115 }
116
117 static_cast<void>(insert(cclass->stringValue()));
118 }
119 }
120}
121
122bool
124 return ((size() == other.size()) && std::equal(cbegin(), cend(), other.cbegin()));
125}
126
129 clear();
130 for (auto const& cclass : other) {
131 insert(cclass);
132 }
133
134 return (*this);
135}
136
137} // end of namespace isc::dhcp
138} // end of namespace isc
Defines elements for storing the names of client classes.
A generic exception that is thrown if a parameter given to a method is considered invalid in that con...
static ElementPtr create(const Position &pos=ZERO_POSITION())
Definition data.cc:249
static ElementPtr createList(const Position &pos=ZERO_POSITION())
Creates an empty ListElement type ElementPtr.
Definition data.cc:299
Container for storing client class names.
Definition classify.h:109
bool equals(const ClientClasses &other) const
Compares two ClientClasses container for equality.
Definition classify.cc:123
void clear()
Clears containers.
Definition classify.h:226
bool contains(const ClientClass &x) const
returns if class x belongs to the defined classes
Definition classify.cc:55
void insert(const ClientClass &class_name)
Insert an element.
Definition classify.h:159
void erase(const ClientClass &class_name)
Erase element by name.
Definition classify.cc:46
ClientClasses & operator=(const ClientClasses &other)
Assigns the contents of on container to another.
Definition classify.cc:128
virtual isc::data::ElementPtr toElement() const
Returns all class names as an ElementPtr of type ListElement.
Definition classify.cc:95
void fromElement(isc::data::ConstElementPtr list)
Sets contents from a ListElement.
Definition classify.cc:104
bool intersects(const ClientClasses &cclasses) const
returns whether this container has at least one class in common with a given container.
Definition classify.cc:61
ClientClasses()
Default constructor.
Definition classify.h:117
std::string toText(const std::string &separator=", ") const
Returns all class names as text.
Definition classify.cc:80
const_iterator cbegin() const
Iterators to the first element.
Definition classify.h:183
const_iterator cend() const
Iterators to the past the end element.
Definition classify.h:198
size_t size() const
Returns the number of classes.
Definition classify.h:177
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
boost::shared_ptr< const Element > ConstElementPtr
Definition data.h:29
boost::shared_ptr< Element > ElementPtr
Definition data.h:28
std::string ClientClass
Defines a single class name.
Definition classify.h:43
string trim(const string &input)
Trim leading and trailing spaces.
Definition str.cc:32
Defines the logger used by the top-level component of kea-lfc.
Tag for the name index.
Definition classify.h:49