MINIFICPP-31 Added UpdateAttribute processor
[nifi-minifi-cpp.git] / libminifi / src / core / ConfigurableComponent.cpp
1 /**
2 *
3 * Licensed to the Apache Software Foundation (ASF) under one or more
4 * contributor license agreements. See the NOTICE file distributed with
5 * this work for additional information regarding copyright ownership.
6 * The ASF licenses this file to You under the Apache License, Version 2.0
7 * (the "License"); you may not use this file except in compliance with
8 * the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18
19 #include <utility>
20 #include <string>
21 #include <vector>
22 #include <set>
23
24 #include "core/ConfigurableComponent.h"
25 #include "core/logging/LoggerConfiguration.h"
26
27 namespace org {
28 namespace apache {
29 namespace nifi {
30 namespace minifi {
31 namespace core {
32
33 ConfigurableComponent::ConfigurableComponent()
34 : logger_(logging::LoggerFactory<ConfigurableComponent>::getLogger()) {
35 }
36
37 ConfigurableComponent::ConfigurableComponent(const ConfigurableComponent &&other)
38 : properties_(std::move(other.properties_)),
39 dynamic_properties_(std::move(other.dynamic_properties_)),
40 logger_(logging::LoggerFactory<ConfigurableComponent>::getLogger()) {
41 }
42
43 ConfigurableComponent::~ConfigurableComponent() {
44 }
45
46 bool ConfigurableComponent::getProperty(const std::string &name, Property &prop) {
47 std::lock_guard<std::mutex> lock(configuration_mutex_);
48
49 auto &&it = properties_.find(name);
50
51 if (it != properties_.end()) {
52 prop = it->second;
53 return true;
54 } else {
55 return false;
56 }
57 }
58
59 /**
60 * Get property using the provided name.
61 * @param name property name.
62 * @param value value passed in by reference
63 * @return result of getting property.
64 */
65 bool ConfigurableComponent::getProperty(const std::string name, std::string &value) {
66 std::lock_guard<std::mutex> lock(configuration_mutex_);
67
68 auto &&it = properties_.find(name);
69 if (it != properties_.end()) {
70 Property item = it->second;
71 value = item.getValue();
72 logger_->log_debug("Component %s property name %s value %s", name, item.getName(), value);
73 return true;
74 } else {
75 return false;
76 }
77 }
78 /**
79 * Sets the property using the provided name
80 * @param property name
81 * @param value property value.
82 * @return result of setting property.
83 */
84 bool ConfigurableComponent::setProperty(const std::string name, std::string value) {
85 std::lock_guard<std::mutex> lock(configuration_mutex_);
86 auto &&it = properties_.find(name);
87
88 if (it != properties_.end()) {
89 Property item = it->second;
90 item.setValue(value);
91 properties_[item.getName()] = item;
92 logger_->log_debug("Component %s property name %s value %s", name, item.getName(), value);
93 return true;
94 } else {
95 return false;
96 }
97 }
98
99 /**
100 * Sets the property using the provided name
101 * @param property name
102 * @param value property value.
103 * @return result of setting property.
104 */
105 bool ConfigurableComponent::updateProperty(const std::string &name, const std::string &value) {
106 std::lock_guard<std::mutex> lock(configuration_mutex_);
107 auto &&it = properties_.find(name);
108
109 if (it != properties_.end()) {
110 Property item = it->second;
111 item.addValue(value);
112 properties_[item.getName()] = item;
113 logger_->log_debug("Component %s property name %s value %s", name, item.getName(), value);
114 return true;
115 } else {
116 return false;
117 }
118 }
119
120 /**
121 * Sets the property using the provided name
122 * @param property name
123 * @param value property value.
124 * @return whether property was set or not
125 */
126 bool ConfigurableComponent::setProperty(Property &prop, std::string value) {
127 std::lock_guard<std::mutex> lock(configuration_mutex_);
128 auto it = properties_.find(prop.getName());
129
130 if (it != properties_.end()) {
131 Property item = it->second;
132 item.setValue(value);
133 properties_[item.getName()] = item;
134 logger_->log_debug("property name %s value %s", prop.getName(), item.getName(), value);
135 return true;
136 } else {
137 Property newProp(prop);
138 newProp.setValue(value);
139 properties_.insert(std::pair<std::string, Property>(prop.getName(), newProp));
140 return true;
141 }
142 return false;
143 }
144
145 /**
146 * Sets supported properties for the ConfigurableComponent
147 * @param supported properties
148 * @return result of set operation.
149 */
150 bool ConfigurableComponent::setSupportedProperties(std::set<Property> properties) {
151 if (!canEdit()) {
152 return false;
153 }
154
155 std::lock_guard<std::mutex> lock(configuration_mutex_);
156
157 properties_.clear();
158 for (auto item : properties) {
159 properties_[item.getName()] = item;
160 }
161 return true;
162 }
163
164 bool ConfigurableComponent::getDynamicProperty(const std::string name, std::string &value) {
165 std::lock_guard<std::mutex> lock(configuration_mutex_);
166
167 auto &&it = dynamic_properties_.find(name);
168 if (it != dynamic_properties_.end()) {
169 Property item = it->second;
170 value = item.getValue();
171 logger_->log_debug("Component %s dynamic property name %s value %s", name, item.getName(), value);
172 return true;
173 } else {
174 return false;
175 }
176 }
177
178 bool ConfigurableComponent::createDynamicProperty(const std::string &name, const std::string &value) {
179 if (!supportsDynamicProperties()) {
180 logger_->log_debug("Attempted to create dynamic property %s, but this component does not support creation."
181 "of dynamic properties.", name);
182 return false;
183 }
184
185 Property dyn(name, DEFAULT_DYNAMIC_PROPERTY_DESC, value);
186 logger_->log_info("Processor %s dynamic property '%s' value '%s'",
187 name.c_str(),
188 dyn.getName().c_str(),
189 value.c_str());
190 dynamic_properties_[dyn.getName()] = dyn;
191 return true;
192 }
193
194 bool ConfigurableComponent::setDynamicProperty(const std::string name, std::string value) {
195 std::lock_guard<std::mutex> lock(configuration_mutex_);
196 auto &&it = dynamic_properties_.find(name);
197
198 if (it != dynamic_properties_.end()) {
199 Property item = it->second;
200 item.setValue(value);
201 dynamic_properties_[item.getName()] = item;
202 logger_->log_debug("Component %s dynamic property name %s value %s", name, item.getName(), value);
203 return true;
204 } else {
205 return createDynamicProperty(name, value);
206 }
207 }
208
209 bool ConfigurableComponent::updateDynamicProperty(const std::string &name, const std::string &value) {
210 std::lock_guard<std::mutex> lock(configuration_mutex_);
211 auto &&it = dynamic_properties_.find(name);
212
213 if (it != dynamic_properties_.end()) {
214 Property item = it->second;
215 item.addValue(value);
216 dynamic_properties_[item.getName()] = item;
217 logger_->log_debug("Component %s dynamic property name %s value %s", name, item.getName(), value);
218 return true;
219 } else {
220 return createDynamicProperty(name, value);
221 }
222 }
223
224 std::vector<std::string> ConfigurableComponent::getDynamicPropertyKeys() {
225 std::lock_guard<std::mutex> lock(configuration_mutex_);
226
227 std::vector<std::string> result;
228
229 for (const auto &pair : dynamic_properties_) {
230 result.emplace_back(pair.first);
231 }
232
233 return result;
234 }
235
236 } /* namespace core */
237 } /* namespace minifi */
238 } /* namespace nifi */
239 } /* namespace apache */
240 } /* namespace org */