Thomas Vachuska | f0e1fae | 2015-04-24 00:51:51 -0700 | [diff] [blame] | 1 | /* |
| 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 Vachuska | d24aa7f | 2015-05-14 18:37:54 -0700 | [diff] [blame] | 16 | package org.onosproject.incubator.net.config; |
Thomas Vachuska | f0e1fae | 2015-04-24 00:51:51 -0700 | [diff] [blame] | 17 | |
| 18 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 19 | import com.fasterxml.jackson.databind.node.ObjectNode; |
Thomas Vachuska | e2b7e7e | 2015-05-20 11:11:31 -0700 | [diff] [blame] | 20 | import com.google.common.annotations.Beta; |
Thomas Vachuska | f0e1fae | 2015-04-24 00:51:51 -0700 | [diff] [blame] | 21 | |
| 22 | import static com.google.common.base.Preconditions.checkNotNull; |
| 23 | |
| 24 | /** |
| 25 | * Base abstraction of a configuration facade for a specific subject. Derived |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame^] | 26 | * 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 Vachuska | f0e1fae | 2015-04-24 00:51:51 -0700 | [diff] [blame] | 30 | * |
| 31 | * @param <S> type of subject |
| 32 | */ |
Thomas Vachuska | e2b7e7e | 2015-05-20 11:11:31 -0700 | [diff] [blame] | 33 | @Beta |
Thomas Vachuska | f0e1fae | 2015-04-24 00:51:51 -0700 | [diff] [blame] | 34 | public abstract class Config<S> { |
| 35 | |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame^] | 36 | protected S subject; |
| 37 | protected String key; |
Thomas Vachuska | f0e1fae | 2015-04-24 00:51:51 -0700 | [diff] [blame] | 38 | protected ObjectNode node; |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame^] | 39 | 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 Vachuska | f0e1fae | 2015-04-24 00:51:51 -0700 | [diff] [blame] | 59 | |
| 60 | /** |
| 61 | * Returns the specific subject to which this configuration pertains. |
| 62 | * |
| 63 | * @return configuration subject |
| 64 | */ |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame^] | 65 | public S subject() { |
Thomas Vachuska | f0e1fae | 2015-04-24 00:51:51 -0700 | [diff] [blame] | 66 | return subject; |
| 67 | } |
| 68 | |
| 69 | /** |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame^] | 70 | * 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 Vachuska | f0e1fae | 2015-04-24 00:51:51 -0700 | [diff] [blame] | 73 | * |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame^] | 74 | * @return configuration key |
Thomas Vachuska | f0e1fae | 2015-04-24 00:51:51 -0700 | [diff] [blame] | 75 | */ |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame^] | 76 | 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 Vachuska | f0e1fae | 2015-04-24 00:51:51 -0700 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | /** |
| 90 | * Applies any configuration changes made via this configuration. |
| 91 | */ |
| 92 | public void apply() { |
| 93 | delegate.onApply(this); |
| 94 | } |
| 95 | |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame^] | 96 | |
| 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 | /** |
| 154 | * Gets the specified property as a long. |
| 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 long get(String name, long defaultValue) { |
| 161 | return node.path(name).asLong(defaultValue); |
| 162 | } |
| 163 | |
| 164 | /** |
| 165 | * Sets the specified property as a long 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, Long value) { |
| 172 | if (value != null) { |
| 173 | node.put(name, value.longValue()); |
| 174 | } else { |
| 175 | node.remove(name); |
| 176 | } |
| 177 | return this; |
| 178 | } |
| 179 | |
| 180 | /** |
| 181 | * Gets the specified property as a double. |
| 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 double get(String name, double defaultValue) { |
| 188 | return node.path(name).asDouble(defaultValue); |
| 189 | } |
| 190 | |
| 191 | /** |
| 192 | * Sets the specified property as a double 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, Double value) { |
| 199 | if (value != null) { |
| 200 | node.put(name, value.doubleValue()); |
| 201 | } else { |
| 202 | node.remove(name); |
| 203 | } |
| 204 | return this; |
| 205 | } |
| 206 | |
| 207 | /** |
| 208 | * Gets the specified property as an enum. |
| 209 | * |
| 210 | * @param name property name |
| 211 | * @param defaultValue default value if property not set |
| 212 | * @param enumClass the enum class |
| 213 | * @return property value or default value |
| 214 | */ |
| 215 | protected <E extends Enum<E>> E get(String name, E defaultValue, Class<E> enumClass) { |
| 216 | return Enum.valueOf(enumClass, node.path(name).asText(defaultValue.toString())); |
| 217 | } |
| 218 | |
| 219 | /** |
| 220 | * Sets the specified property as a double or clears it if null value given. |
| 221 | * |
| 222 | * @param name property name |
| 223 | * @param value new value or null to clear the property |
| 224 | * @return self |
| 225 | */ |
| 226 | protected <E extends Enum> Config<S> setOrClear(String name, E value) { |
| 227 | if (value != null) { |
| 228 | node.put(name, value.toString()); |
| 229 | } else { |
| 230 | node.remove(name); |
| 231 | } |
| 232 | return this; |
| 233 | } |
Thomas Vachuska | f0e1fae | 2015-04-24 00:51:51 -0700 | [diff] [blame] | 234 | } |