blob: 2be2fba915c8235815786b29f8da1a7012a9d3ac [file] [log] [blame]
Thomas Vachuskaf0e1fae2015-04-24 00:51:51 -07001/*
2 * Copyright 2015 Open Networking Laboratory
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Thomas Vachuskad24aa7f2015-05-14 18:37:54 -070016package org.onosproject.incubator.net.config;
Thomas Vachuskaf0e1fae2015-04-24 00:51:51 -070017
18import com.fasterxml.jackson.databind.ObjectMapper;
19import com.fasterxml.jackson.databind.node.ObjectNode;
Thomas Vachuskae2b7e7e2015-05-20 11:11:31 -070020import com.google.common.annotations.Beta;
Thomas Vachuskaf0e1fae2015-04-24 00:51:51 -070021
22import static com.google.common.base.Preconditions.checkNotNull;
23
24/**
25 * Base abstraction of a configuration facade for a specific subject. Derived
Thomas Vachuska96d55b12015-05-11 08:52:03 -070026 * classes should keep all state in the specified JSON tree as that is the
27 * only state that will be distributed or persisted; this class is merely
28 * a facade for interacting with a particular facet of configuration on a
29 * given subject.
Thomas Vachuskaf0e1fae2015-04-24 00:51:51 -070030 *
31 * @param <S> type of subject
32 */
Thomas Vachuskae2b7e7e2015-05-20 11:11:31 -070033@Beta
Thomas Vachuskaf0e1fae2015-04-24 00:51:51 -070034public abstract class Config<S> {
35
Thomas Vachuska96d55b12015-05-11 08:52:03 -070036 protected S subject;
37 protected String key;
Thomas Vachuskaf0e1fae2015-04-24 00:51:51 -070038 protected ObjectNode node;
Thomas Vachuska96d55b12015-05-11 08:52:03 -070039 protected ObjectMapper mapper;
40 protected ConfigApplyDelegate delegate;
41
42 /**
43 * Initializes the configuration behaviour with necessary context.
44 *
45 * @param subject configuration subject
46 * @param key configuration key
47 * @param node JSON object node where configuration data is stored
48 * @param mapper JSON object mapper
49 * @param delegate delegate context
50 */
51 public void init(S subject, String key, ObjectNode node, ObjectMapper mapper,
52 ConfigApplyDelegate delegate) {
53 this.subject = checkNotNull(subject);
54 this.key = key;
55 this.node = checkNotNull(node);
56 this.mapper = checkNotNull(mapper);
57 this.delegate = checkNotNull(delegate);
58 }
Thomas Vachuskaf0e1fae2015-04-24 00:51:51 -070059
60 /**
61 * Returns the specific subject to which this configuration pertains.
62 *
63 * @return configuration subject
64 */
Thomas Vachuska96d55b12015-05-11 08:52:03 -070065 public S subject() {
Thomas Vachuskaf0e1fae2015-04-24 00:51:51 -070066 return subject;
67 }
68
69 /**
Thomas Vachuska96d55b12015-05-11 08:52:03 -070070 * Returns the configuration key. This is primarily aimed for use in
71 * composite JSON trees in external representations and has no bearing on
72 * the internal behaviours.
Thomas Vachuskaf0e1fae2015-04-24 00:51:51 -070073 *
Thomas Vachuska96d55b12015-05-11 08:52:03 -070074 * @return configuration key
Thomas Vachuskaf0e1fae2015-04-24 00:51:51 -070075 */
Thomas Vachuska96d55b12015-05-11 08:52:03 -070076 public String key() {
77 return key;
78 }
79
80 /**
81 * Returns the JSON node that contains the configuration data.
82 *
83 * @return JSON node backing the configuration
84 */
85 public ObjectNode node() {
86 return node;
Thomas Vachuskaf0e1fae2015-04-24 00:51:51 -070087 }
88
89 /**
90 * Applies any configuration changes made via this configuration.
91 */
92 public void apply() {
93 delegate.onApply(this);
94 }
95
Thomas Vachuska96d55b12015-05-11 08:52:03 -070096
97 // Miscellaneous helpers for interacting with JSON
98
99 /**
100 * Gets the specified property as a string.
101 *
102 * @param name property name
103 * @param defaultValue default value if property not set
104 * @return property value or default value
105 */
106 protected String get(String name, String defaultValue) {
107 return node.path(name).asText(defaultValue);
108 }
109
110 /**
111 * Sets the specified property as a string or clears it if null value given.
112 *
113 * @param name property name
114 * @param value new value or null to clear the property
115 * @return self
116 */
117 protected Config<S> setOrClear(String name, String value) {
118 if (value != null) {
119 node.put(name, value);
120 } else {
121 node.remove(name);
122 }
123 return this;
124 }
125
126 /**
127 * Gets the specified property as a boolean.
128 *
129 * @param name property name
130 * @param defaultValue default value if property not set
131 * @return property value or default value
132 */
133 protected boolean get(String name, boolean defaultValue) {
134 return node.path(name).asBoolean(defaultValue);
135 }
136
137 /**
138 * Sets the specified property as a boolean or clears it if null value given.
139 *
140 * @param name property name
141 * @param value new value or null to clear the property
142 * @return self
143 */
144 protected Config<S> setOrClear(String name, Boolean value) {
145 if (value != null) {
146 node.put(name, value.booleanValue());
147 } else {
148 node.remove(name);
149 }
150 return this;
151 }
152
153 /**
Jonathan Hart111b42b2015-07-14 13:28:05 -0700154 * Gets the specified property as an integer.
155 *
156 * @param name property name
157 * @param defaultValue default value if property not set
158 * @return property value or default value
159 */
160 protected int get(String name, int defaultValue) {
161 return node.path(name).asInt(defaultValue);
162 }
163
164 /**
165 * Sets the specified property as an integer or clears it if null value given.
166 *
167 * @param name property name
168 * @param value new value or null to clear the property
169 * @return self
170 */
171 protected Config<S> setOrClear(String name, Integer value) {
172 if (value != null) {
173 node.put(name, value.intValue());
174 } else {
175 node.remove(name);
176 }
177 return this;
178 }
179
180 /**
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700181 * Gets the specified property as a long.
182 *
183 * @param name property name
184 * @param defaultValue default value if property not set
185 * @return property value or default value
186 */
187 protected long get(String name, long defaultValue) {
188 return node.path(name).asLong(defaultValue);
189 }
190
191 /**
192 * Sets the specified property as a long or clears it if null value given.
193 *
194 * @param name property name
195 * @param value new value or null to clear the property
196 * @return self
197 */
198 protected Config<S> setOrClear(String name, Long value) {
199 if (value != null) {
200 node.put(name, value.longValue());
201 } else {
202 node.remove(name);
203 }
204 return this;
205 }
206
207 /**
208 * Gets the specified property as a double.
209 *
210 * @param name property name
211 * @param defaultValue default value if property not set
212 * @return property value or default value
213 */
214 protected double get(String name, double defaultValue) {
215 return node.path(name).asDouble(defaultValue);
216 }
217
218 /**
219 * Sets the specified property as a double or clears it if null value given.
220 *
221 * @param name property name
222 * @param value new value or null to clear the property
223 * @return self
224 */
225 protected Config<S> setOrClear(String name, Double value) {
226 if (value != null) {
227 node.put(name, value.doubleValue());
228 } else {
229 node.remove(name);
230 }
231 return this;
232 }
233
234 /**
235 * Gets the specified property as an enum.
236 *
237 * @param name property name
238 * @param defaultValue default value if property not set
239 * @param enumClass the enum class
240 * @return property value or default value
241 */
242 protected <E extends Enum<E>> E get(String name, E defaultValue, Class<E> enumClass) {
243 return Enum.valueOf(enumClass, node.path(name).asText(defaultValue.toString()));
244 }
245
246 /**
247 * Sets the specified property as a double or clears it if null value given.
248 *
249 * @param name property name
250 * @param value new value or null to clear the property
251 * @return self
252 */
253 protected <E extends Enum> Config<S> setOrClear(String name, E value) {
254 if (value != null) {
255 node.put(name, value.toString());
256 } else {
257 node.remove(name);
258 }
259 return this;
260 }
Jonathan Hart111b42b2015-07-14 13:28:05 -0700261
Thomas Vachuskaf0e1fae2015-04-24 00:51:51 -0700262}