Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 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 | 4998caa | 2015-08-26 13:28:38 -0700 | [diff] [blame] | 16 | package org.onosproject.net.config.impl; |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 17 | |
Thomas Vachuska | 0a400ea | 2015-09-04 11:25:03 -0700 | [diff] [blame] | 18 | import com.fasterxml.jackson.databind.JsonNode; |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 19 | import com.google.common.collect.ImmutableSet; |
| 20 | import com.google.common.collect.Maps; |
| 21 | import org.apache.felix.scr.annotations.Activate; |
| 22 | import org.apache.felix.scr.annotations.Component; |
| 23 | import org.apache.felix.scr.annotations.Deactivate; |
| 24 | import org.apache.felix.scr.annotations.Reference; |
| 25 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 26 | import org.apache.felix.scr.annotations.Service; |
Andrea Campanella | e81e941 | 2018-01-16 11:12:20 +0100 | [diff] [blame] | 27 | import org.onosproject.cluster.ClusterService; |
Thomas Vachuska | 42e8cce | 2015-07-29 19:25:18 -0700 | [diff] [blame] | 28 | import org.onosproject.event.AbstractListenerManager; |
Ray Milkey | a412236 | 2015-08-18 15:19:08 -0700 | [diff] [blame] | 29 | import org.onosproject.net.config.Config; |
| 30 | import org.onosproject.net.config.ConfigFactory; |
| 31 | import org.onosproject.net.config.NetworkConfigEvent; |
| 32 | import org.onosproject.net.config.NetworkConfigListener; |
| 33 | import org.onosproject.net.config.NetworkConfigRegistry; |
| 34 | import org.onosproject.net.config.NetworkConfigService; |
| 35 | import org.onosproject.net.config.NetworkConfigStore; |
| 36 | import org.onosproject.net.config.NetworkConfigStoreDelegate; |
| 37 | import org.onosproject.net.config.SubjectFactory; |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 38 | import org.slf4j.Logger; |
| 39 | import org.slf4j.LoggerFactory; |
| 40 | |
| 41 | import java.util.Map; |
| 42 | import java.util.Objects; |
| 43 | import java.util.Set; |
| 44 | |
| 45 | import static com.google.common.base.Preconditions.checkNotNull; |
Heedo Kang | 4a47a30 | 2016-02-29 17:40:23 +0900 | [diff] [blame] | 46 | import static org.onosproject.security.AppGuard.checkPermission; |
| 47 | import static org.onosproject.security.AppPermission.Type.*; |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 48 | |
| 49 | /** |
| 50 | * Implementation of the network configuration subsystem. |
| 51 | */ |
| 52 | @Component(immediate = true) |
| 53 | @Service |
Thomas Vachuska | 42e8cce | 2015-07-29 19:25:18 -0700 | [diff] [blame] | 54 | public class NetworkConfigManager |
| 55 | extends AbstractListenerManager<NetworkConfigEvent, NetworkConfigListener> |
| 56 | implements NetworkConfigRegistry, NetworkConfigService { |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 57 | |
| 58 | private final Logger log = LoggerFactory.getLogger(getClass()); |
| 59 | |
| 60 | private static final String NULL_FACTORY_MSG = "Factory cannot be null"; |
| 61 | private static final String NULL_SCLASS_MSG = "Subject class cannot be null"; |
Simon Hunt | 3da1a18 | 2016-02-08 16:42:54 -0800 | [diff] [blame] | 62 | private static final String NULL_SCKEY_MSG = "Subject class key cannot be null"; |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 63 | private static final String NULL_CCLASS_MSG = "Config class cannot be null"; |
Thomas Vachuska | 6f350ed | 2016-01-08 09:53:03 -0800 | [diff] [blame] | 64 | private static final String NULL_CKEY_MSG = "Config key cannot be null"; |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 65 | private static final String NULL_SUBJECT_MSG = "Subject cannot be null"; |
Thomas Vachuska | 6f350ed | 2016-01-08 09:53:03 -0800 | [diff] [blame] | 66 | private static final String NULL_JSON_MSG = "JSON cannot be null"; |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 67 | |
| 68 | // Inventory of configuration factories |
| 69 | private final Map<ConfigKey, ConfigFactory> factories = Maps.newConcurrentMap(); |
ke han | 31dd5b4 | 2017-06-23 13:35:09 +0800 | [diff] [blame] | 70 | private final Map<ConfigKey, Integer> factoryCounters = Maps.newConcurrentMap(); |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 71 | |
Ray Milkey | 0a4f6c3 | 2015-08-03 11:22:01 -0700 | [diff] [blame] | 72 | // Secondary indices to retrieve subject and config classes by keys |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 73 | private final Map<String, SubjectFactory> subjectClasses = Maps.newConcurrentMap(); |
| 74 | private final Map<Class, SubjectFactory> subjectClassKeys = Maps.newConcurrentMap(); |
Jonathan Hart | 111b42b | 2015-07-14 13:28:05 -0700 | [diff] [blame] | 75 | private final Map<ConfigIdentifier, Class<? extends Config>> configClasses = Maps.newConcurrentMap(); |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 76 | |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 77 | private final NetworkConfigStoreDelegate storeDelegate = new InternalStoreDelegate(); |
| 78 | |
| 79 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 80 | protected NetworkConfigStore store; |
| 81 | |
Andrea Campanella | e81e941 | 2018-01-16 11:12:20 +0100 | [diff] [blame] | 82 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 83 | protected ClusterService clusterService; |
| 84 | |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 85 | |
| 86 | @Activate |
| 87 | public void activate() { |
| 88 | eventDispatcher.addSink(NetworkConfigEvent.class, listenerRegistry); |
| 89 | store.setDelegate(storeDelegate); |
| 90 | log.info("Started"); |
| 91 | } |
| 92 | |
| 93 | @Deactivate |
| 94 | public void deactivate() { |
| 95 | eventDispatcher.removeSink(NetworkConfigEvent.class); |
| 96 | store.unsetDelegate(storeDelegate); |
| 97 | log.info("Stopped"); |
| 98 | } |
| 99 | |
| 100 | |
| 101 | @Override |
| 102 | @SuppressWarnings("unchecked") |
| 103 | public void registerConfigFactory(ConfigFactory configFactory) { |
| 104 | checkNotNull(configFactory, NULL_FACTORY_MSG); |
ke han | 31dd5b4 | 2017-06-23 13:35:09 +0800 | [diff] [blame] | 105 | if (factoryCounters.containsKey(key(configFactory))) { |
| 106 | factoryCounters.replace(key(configFactory), (factoryCounters.get(key(configFactory)) + 1)); |
| 107 | } else { |
| 108 | factories.put(key(configFactory), configFactory); |
| 109 | factoryCounters.put(key(configFactory), 1); |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 110 | |
ke han | 31dd5b4 | 2017-06-23 13:35:09 +0800 | [diff] [blame] | 111 | configClasses.put(identifier(configFactory), configFactory.configClass()); |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 112 | |
ke han | 31dd5b4 | 2017-06-23 13:35:09 +0800 | [diff] [blame] | 113 | SubjectFactory subjectFactory = configFactory.subjectFactory(); |
| 114 | subjectClasses.putIfAbsent(subjectFactory.subjectClassKey(), subjectFactory); |
| 115 | subjectClassKeys.putIfAbsent(subjectFactory.subjectClass(), subjectFactory); |
| 116 | |
| 117 | store.addConfigFactory(configFactory); |
| 118 | } |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | @Override |
| 122 | public void unregisterConfigFactory(ConfigFactory configFactory) { |
| 123 | checkNotNull(configFactory, NULL_FACTORY_MSG); |
ke han | 31dd5b4 | 2017-06-23 13:35:09 +0800 | [diff] [blame] | 124 | Integer factoryCounter = factoryCounters.get(key(configFactory)); |
| 125 | if (factoryCounter > 1) { |
| 126 | factoryCounters.replace(key(configFactory), (factoryCounter - 1)); |
| 127 | } else { |
| 128 | factoryCounters.remove(key(configFactory)); |
| 129 | factories.remove(key(configFactory)); |
| 130 | configClasses.remove(identifier(configFactory)); |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 131 | |
Andrea Campanella | e81e941 | 2018-01-16 11:12:20 +0100 | [diff] [blame] | 132 | // Removing the config factory only if this is the only ONOS instance or if it's the last |
| 133 | // instance active in a cluster. otherwise the other instances lose access to the config factory |
| 134 | // and can't use the associated net-cfgs. |
| 135 | if (clusterService.getNodes().size() == 1) { |
| 136 | // Note that we are deliberately not removing subject factory key bindings. |
| 137 | store.removeConfigFactory(configFactory); |
| 138 | } |
ke han | 31dd5b4 | 2017-06-23 13:35:09 +0800 | [diff] [blame] | 139 | } |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | @Override |
| 143 | public Set<ConfigFactory> getConfigFactories() { |
| 144 | return ImmutableSet.copyOf(factories.values()); |
| 145 | } |
| 146 | |
| 147 | |
| 148 | @Override |
| 149 | @SuppressWarnings("unchecked") |
| 150 | public <S, C extends Config<S>> Set<ConfigFactory<S, C>> getConfigFactories(Class<S> subjectClass) { |
| 151 | ImmutableSet.Builder<ConfigFactory<S, C>> builder = ImmutableSet.builder(); |
| 152 | factories.forEach((key, factory) -> { |
| 153 | if (factory.subjectFactory().subjectClass().equals(subjectClass)) { |
| 154 | builder.add(factory); |
| 155 | } |
| 156 | }); |
| 157 | return builder.build(); |
| 158 | } |
| 159 | |
| 160 | @Override |
| 161 | public <S, C extends Config<S>> ConfigFactory<S, C> getConfigFactory(Class<C> configClass) { |
| 162 | checkNotNull(configClass, NULL_CCLASS_MSG); |
| 163 | return store.getConfigFactory(configClass); |
| 164 | } |
| 165 | |
| 166 | |
| 167 | @Override |
| 168 | public Set<Class> getSubjectClasses() { |
Heedo Kang | 4a47a30 | 2016-02-29 17:40:23 +0900 | [diff] [blame] | 169 | checkPermission(CONFIG_READ); |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 170 | ImmutableSet.Builder<Class> builder = ImmutableSet.builder(); |
| 171 | factories.forEach((k, v) -> builder.add(k.subjectClass)); |
| 172 | return builder.build(); |
| 173 | } |
| 174 | |
| 175 | @Override |
Thomas Vachuska | ea5adc6 | 2015-10-07 11:52:30 -0700 | [diff] [blame] | 176 | public SubjectFactory getSubjectFactory(String subjectClassKey) { |
Heedo Kang | 4a47a30 | 2016-02-29 17:40:23 +0900 | [diff] [blame] | 177 | checkPermission(CONFIG_READ); |
Thomas Vachuska | ea5adc6 | 2015-10-07 11:52:30 -0700 | [diff] [blame] | 178 | return subjectClasses.get(subjectClassKey); |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | @Override |
| 182 | public SubjectFactory getSubjectFactory(Class subjectClass) { |
Heedo Kang | 4a47a30 | 2016-02-29 17:40:23 +0900 | [diff] [blame] | 183 | checkPermission(CONFIG_READ); |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 184 | return subjectClassKeys.get(subjectClass); |
| 185 | } |
| 186 | |
| 187 | @Override |
Thomas Vachuska | ea5adc6 | 2015-10-07 11:52:30 -0700 | [diff] [blame] | 188 | public Class<? extends Config> getConfigClass(String subjectClassKey, String configKey) { |
Heedo Kang | 4a47a30 | 2016-02-29 17:40:23 +0900 | [diff] [blame] | 189 | checkPermission(CONFIG_READ); |
Simon Hunt | 3da1a18 | 2016-02-08 16:42:54 -0800 | [diff] [blame] | 190 | checkNotNull(subjectClassKey, NULL_SCKEY_MSG); |
Thomas Vachuska | 6f350ed | 2016-01-08 09:53:03 -0800 | [diff] [blame] | 191 | checkNotNull(configKey, NULL_CKEY_MSG); |
Thomas Vachuska | ea5adc6 | 2015-10-07 11:52:30 -0700 | [diff] [blame] | 192 | return configClasses.get(new ConfigIdentifier(subjectClassKey, configKey)); |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 193 | } |
| 194 | |
| 195 | @Override |
| 196 | public <S> Set<S> getSubjects(Class<S> subjectClass) { |
Heedo Kang | 4a47a30 | 2016-02-29 17:40:23 +0900 | [diff] [blame] | 197 | checkPermission(CONFIG_READ); |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 198 | checkNotNull(subjectClass, NULL_SCLASS_MSG); |
| 199 | return store.getSubjects(subjectClass); |
| 200 | } |
| 201 | |
| 202 | @Override |
| 203 | public <S, C extends Config<S>> Set<S> getSubjects(Class<S> subjectClass, Class<C> configClass) { |
Heedo Kang | 4a47a30 | 2016-02-29 17:40:23 +0900 | [diff] [blame] | 204 | checkPermission(CONFIG_READ); |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 205 | checkNotNull(subjectClass, NULL_SCLASS_MSG); |
| 206 | checkNotNull(configClass, NULL_CCLASS_MSG); |
| 207 | return store.getSubjects(subjectClass, configClass); |
| 208 | } |
| 209 | |
| 210 | @Override |
| 211 | public <S> Set<Config<S>> getConfigs(S subject) { |
Heedo Kang | 4a47a30 | 2016-02-29 17:40:23 +0900 | [diff] [blame] | 212 | checkPermission(CONFIG_READ); |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 213 | checkNotNull(subject, NULL_SUBJECT_MSG); |
| 214 | Set<Class<? extends Config<S>>> configClasses = store.getConfigClasses(subject); |
| 215 | ImmutableSet.Builder<Config<S>> cfg = ImmutableSet.builder(); |
| 216 | configClasses.forEach(cc -> cfg.add(store.getConfig(subject, cc))); |
| 217 | return cfg.build(); |
| 218 | } |
| 219 | |
| 220 | @Override |
Thomas Vachuska | 6f350ed | 2016-01-08 09:53:03 -0800 | [diff] [blame] | 221 | public <S, C extends Config<S>> C getConfig(S subject, Class<C> configClass) { |
Heedo Kang | 4a47a30 | 2016-02-29 17:40:23 +0900 | [diff] [blame] | 222 | checkPermission(CONFIG_READ); |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 223 | checkNotNull(subject, NULL_SUBJECT_MSG); |
| 224 | checkNotNull(configClass, NULL_CCLASS_MSG); |
| 225 | return store.getConfig(subject, configClass); |
| 226 | } |
| 227 | |
| 228 | |
| 229 | @Override |
| 230 | public <S, C extends Config<S>> C addConfig(S subject, Class<C> configClass) { |
Heedo Kang | 4a47a30 | 2016-02-29 17:40:23 +0900 | [diff] [blame] | 231 | checkPermission(CONFIG_WRITE); |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 232 | checkNotNull(subject, NULL_SUBJECT_MSG); |
| 233 | checkNotNull(configClass, NULL_CCLASS_MSG); |
| 234 | return store.createConfig(subject, configClass); |
| 235 | } |
| 236 | |
| 237 | @Override |
Thomas Vachuska | 0a400ea | 2015-09-04 11:25:03 -0700 | [diff] [blame] | 238 | public <S, C extends Config<S>> C applyConfig(S subject, Class<C> configClass, JsonNode json) { |
Heedo Kang | 4a47a30 | 2016-02-29 17:40:23 +0900 | [diff] [blame] | 239 | checkPermission(CONFIG_WRITE); |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 240 | checkNotNull(subject, NULL_SUBJECT_MSG); |
| 241 | checkNotNull(configClass, NULL_CCLASS_MSG); |
Simon Hunt | 3da1a18 | 2016-02-08 16:42:54 -0800 | [diff] [blame] | 242 | checkNotNull(json, NULL_JSON_MSG); |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 243 | return store.applyConfig(subject, configClass, json); |
| 244 | } |
| 245 | |
| 246 | @Override |
Thomas Vachuska | 6f350ed | 2016-01-08 09:53:03 -0800 | [diff] [blame] | 247 | @SuppressWarnings("unchecked") |
| 248 | public <S, C extends Config<S>> C applyConfig(String subjectClassKey, S subject, |
| 249 | String configKey, JsonNode json) { |
Heedo Kang | 4a47a30 | 2016-02-29 17:40:23 +0900 | [diff] [blame] | 250 | checkPermission(CONFIG_WRITE); |
Simon Hunt | 3da1a18 | 2016-02-08 16:42:54 -0800 | [diff] [blame] | 251 | checkNotNull(subjectClassKey, NULL_SCKEY_MSG); |
Thomas Vachuska | 6f350ed | 2016-01-08 09:53:03 -0800 | [diff] [blame] | 252 | checkNotNull(subject, NULL_SUBJECT_MSG); |
| 253 | checkNotNull(configKey, NULL_CKEY_MSG); |
Simon Hunt | 3da1a18 | 2016-02-08 16:42:54 -0800 | [diff] [blame] | 254 | checkNotNull(json, NULL_JSON_MSG); |
Thomas Vachuska | 6f350ed | 2016-01-08 09:53:03 -0800 | [diff] [blame] | 255 | Class<? extends Config> configClass = configClasses.get(new ConfigIdentifier(subjectClassKey, configKey)); |
| 256 | if (configClass != null) { |
| 257 | return store.applyConfig(subject, (Class<C>) configClass, json); |
| 258 | } else { |
Andrea Campanella | dcb5e93 | 2016-01-11 17:32:23 -0800 | [diff] [blame] | 259 | log.info("Configuration \'{}\' queued for subject {}", configKey, subject); |
Thomas Vachuska | 6f350ed | 2016-01-08 09:53:03 -0800 | [diff] [blame] | 260 | store.queueConfig(subject, configKey, json); |
| 261 | return null; |
| 262 | } |
| 263 | } |
| 264 | |
| 265 | @Override |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 266 | public <S, C extends Config<S>> void removeConfig(S subject, Class<C> configClass) { |
Heedo Kang | 4a47a30 | 2016-02-29 17:40:23 +0900 | [diff] [blame] | 267 | checkPermission(CONFIG_WRITE); |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 268 | checkNotNull(subject, NULL_SUBJECT_MSG); |
| 269 | checkNotNull(configClass, NULL_CCLASS_MSG); |
| 270 | store.clearConfig(subject, configClass); |
| 271 | } |
| 272 | |
Thomas Vachuska | 6f350ed | 2016-01-08 09:53:03 -0800 | [diff] [blame] | 273 | @Override |
| 274 | public <S> void removeConfig(String subjectClassKey, S subject, String configKey) { |
Simon Hunt | 3da1a18 | 2016-02-08 16:42:54 -0800 | [diff] [blame] | 275 | checkNotNull(subjectClassKey, NULL_SCKEY_MSG); |
Thomas Vachuska | 6f350ed | 2016-01-08 09:53:03 -0800 | [diff] [blame] | 276 | checkNotNull(subject, NULL_SUBJECT_MSG); |
Simon Hunt | 3da1a18 | 2016-02-08 16:42:54 -0800 | [diff] [blame] | 277 | checkNotNull(configKey, NULL_CKEY_MSG); |
Thomas Vachuska | 6f350ed | 2016-01-08 09:53:03 -0800 | [diff] [blame] | 278 | Class<? extends Config> configClass = configClasses.get(new ConfigIdentifier(subjectClassKey, configKey)); |
| 279 | if (configClass != null) { |
| 280 | store.clearConfig(subject, configClass); |
| 281 | } else { |
| 282 | store.clearQueuedConfig(subject, configKey); |
Andrea Campanella | e81e941 | 2018-01-16 11:12:20 +0100 | [diff] [blame] | 283 | } |
Thomas Vachuska | 6f350ed | 2016-01-08 09:53:03 -0800 | [diff] [blame] | 284 | } |
| 285 | |
Andrea Campanella | e81e941 | 2018-01-16 11:12:20 +0100 | [diff] [blame] | 286 | @Override |
| 287 | public <S> void removeConfig(S subject) { |
Deepa Vaddireddy | 0c49b60 | 2016-06-02 12:19:07 +0530 | [diff] [blame] | 288 | checkPermission(CONFIG_WRITE); |
| 289 | store.clearConfig(subject); |
Andrea Campanella | e81e941 | 2018-01-16 11:12:20 +0100 | [diff] [blame] | 290 | } |
Deepa Vaddireddy | 0c49b60 | 2016-06-02 12:19:07 +0530 | [diff] [blame] | 291 | |
Andrea Campanella | e81e941 | 2018-01-16 11:12:20 +0100 | [diff] [blame] | 292 | @Override |
| 293 | public <S> void removeConfig() { |
| 294 | checkPermission(CONFIG_WRITE); |
| 295 | store.clearConfig(); |
| 296 | } |
Deepa Vaddireddy | 0c49b60 | 2016-06-02 12:19:07 +0530 | [diff] [blame] | 297 | |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 298 | // Auxiliary store delegate to receive notification about changes in |
| 299 | // the network configuration store state - by the store itself. |
| 300 | private class InternalStoreDelegate implements NetworkConfigStoreDelegate { |
| 301 | @Override |
| 302 | public void notify(NetworkConfigEvent event) { |
Thomas Vachuska | 42e8cce | 2015-07-29 19:25:18 -0700 | [diff] [blame] | 303 | post(event); |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 304 | } |
| 305 | } |
| 306 | |
| 307 | |
| 308 | // Produces a key for uniquely tracking a config factory. |
| 309 | private static ConfigKey key(ConfigFactory factory) { |
| 310 | return new ConfigKey(factory.subjectFactory().subjectClass(), factory.configClass()); |
| 311 | } |
| 312 | |
| 313 | // Auxiliary key to track config factories. |
Ray Milkey | ae9faf1 | 2015-08-03 15:52:26 -0700 | [diff] [blame] | 314 | protected static final class ConfigKey { |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 315 | final Class subjectClass; |
| 316 | final Class configClass; |
| 317 | |
Ray Milkey | ae9faf1 | 2015-08-03 15:52:26 -0700 | [diff] [blame] | 318 | protected ConfigKey(Class subjectClass, Class configClass) { |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 319 | this.subjectClass = subjectClass; |
| 320 | this.configClass = configClass; |
| 321 | } |
| 322 | |
| 323 | @Override |
| 324 | public int hashCode() { |
| 325 | return Objects.hash(subjectClass, configClass); |
| 326 | } |
| 327 | |
| 328 | @Override |
| 329 | public boolean equals(Object obj) { |
| 330 | if (this == obj) { |
| 331 | return true; |
| 332 | } |
| 333 | if (obj instanceof ConfigKey) { |
| 334 | final ConfigKey other = (ConfigKey) obj; |
| 335 | return Objects.equals(this.subjectClass, other.subjectClass) |
| 336 | && Objects.equals(this.configClass, other.configClass); |
| 337 | } |
| 338 | return false; |
| 339 | } |
| 340 | } |
| 341 | |
Jonathan Hart | 111b42b | 2015-07-14 13:28:05 -0700 | [diff] [blame] | 342 | private static ConfigIdentifier identifier(ConfigFactory factory) { |
Thomas Vachuska | ea5adc6 | 2015-10-07 11:52:30 -0700 | [diff] [blame] | 343 | return new ConfigIdentifier(factory.subjectFactory().subjectClassKey(), factory.configKey()); |
Jonathan Hart | 111b42b | 2015-07-14 13:28:05 -0700 | [diff] [blame] | 344 | } |
| 345 | |
Thomas Vachuska | 4998caa | 2015-08-26 13:28:38 -0700 | [diff] [blame] | 346 | static final class ConfigIdentifier { |
Thomas Vachuska | 6f350ed | 2016-01-08 09:53:03 -0800 | [diff] [blame] | 347 | final String subjectClassKey; |
Jonathan Hart | 111b42b | 2015-07-14 13:28:05 -0700 | [diff] [blame] | 348 | final String configKey; |
| 349 | |
Thomas Vachuska | 6f350ed | 2016-01-08 09:53:03 -0800 | [diff] [blame] | 350 | protected ConfigIdentifier(String subjectClassKey, String configKey) { |
| 351 | this.subjectClassKey = subjectClassKey; |
Jonathan Hart | 111b42b | 2015-07-14 13:28:05 -0700 | [diff] [blame] | 352 | this.configKey = configKey; |
| 353 | } |
| 354 | |
| 355 | @Override |
| 356 | public int hashCode() { |
Thomas Vachuska | 6f350ed | 2016-01-08 09:53:03 -0800 | [diff] [blame] | 357 | return Objects.hash(subjectClassKey, configKey); |
Jonathan Hart | 111b42b | 2015-07-14 13:28:05 -0700 | [diff] [blame] | 358 | } |
| 359 | |
| 360 | @Override |
| 361 | public boolean equals(Object obj) { |
| 362 | if (this == obj) { |
| 363 | return true; |
| 364 | } |
| 365 | if (obj instanceof ConfigIdentifier) { |
| 366 | final ConfigIdentifier other = (ConfigIdentifier) obj; |
Thomas Vachuska | 6f350ed | 2016-01-08 09:53:03 -0800 | [diff] [blame] | 367 | return Objects.equals(this.subjectClassKey, other.subjectClassKey) |
Jonathan Hart | 111b42b | 2015-07-14 13:28:05 -0700 | [diff] [blame] | 368 | && Objects.equals(this.configKey, other.configKey); |
| 369 | } |
| 370 | return false; |
| 371 | } |
| 372 | } |
Thomas Vachuska | 6f350ed | 2016-01-08 09:53:03 -0800 | [diff] [blame] | 373 | |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 374 | } |