Ray Milkey | ae9faf1 | 2015-08-03 15:52:26 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
Ray Milkey | ae9faf1 | 2015-08-03 15:52:26 -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; |
Ray Milkey | ae9faf1 | 2015-08-03 15:52:26 -0700 | [diff] [blame] | 17 | |
| 18 | import java.util.Set; |
| 19 | |
| 20 | import org.junit.After; |
| 21 | import org.junit.Before; |
| 22 | import org.junit.Test; |
| 23 | import org.onlab.junit.TestUtils; |
Andrea Campanella | e81e941 | 2018-01-16 11:12:20 +0100 | [diff] [blame^] | 24 | import org.onosproject.cluster.ClusterService; |
| 25 | import org.onosproject.cluster.ClusterServiceAdapter; |
Ray Milkey | ae9faf1 | 2015-08-03 15:52:26 -0700 | [diff] [blame] | 26 | import org.onosproject.event.EventDeliveryServiceAdapter; |
Ray Milkey | a412236 | 2015-08-18 15:19:08 -0700 | [diff] [blame] | 27 | import org.onosproject.net.config.Config; |
| 28 | import org.onosproject.net.config.ConfigFactory; |
| 29 | import org.onosproject.net.config.NetworkConfigRegistry; |
| 30 | import org.onosproject.net.config.NetworkConfigService; |
| 31 | import org.onosproject.net.config.SubjectFactory; |
Ray Milkey | ae9faf1 | 2015-08-03 15:52:26 -0700 | [diff] [blame] | 32 | import org.onosproject.net.NetTestTools; |
Thomas Vachuska | 4998caa | 2015-08-26 13:28:38 -0700 | [diff] [blame] | 33 | import org.onosproject.store.config.impl.DistributedNetworkConfigStore; |
Ray Milkey | ae9faf1 | 2015-08-03 15:52:26 -0700 | [diff] [blame] | 34 | import org.onosproject.store.service.TestStorageService; |
| 35 | |
| 36 | import static org.hamcrest.Matchers.hasSize; |
| 37 | import static org.hamcrest.Matchers.instanceOf; |
| 38 | import static org.hamcrest.Matchers.is; |
| 39 | import static org.hamcrest.Matchers.notNullValue; |
| 40 | import static org.hamcrest.Matchers.nullValue; |
| 41 | import static org.junit.Assert.assertThat; |
| 42 | |
| 43 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 44 | import com.google.common.testing.EqualsTester; |
| 45 | |
| 46 | /** |
| 47 | * Unit tests for network config registry. |
| 48 | */ |
| 49 | public class NetworkConfigManagerTest { |
Andrea Campanella | e81e941 | 2018-01-16 11:12:20 +0100 | [diff] [blame^] | 50 | |
| 51 | private final ClusterService clusterService = new ClusterServiceAdapter(); |
| 52 | |
Ray Milkey | ae9faf1 | 2015-08-03 15:52:26 -0700 | [diff] [blame] | 53 | private NetworkConfigManager manager; |
| 54 | private NetworkConfigRegistry registry; |
| 55 | private NetworkConfigService configService; |
| 56 | private DistributedNetworkConfigStore configStore; |
| 57 | |
| 58 | /** |
| 59 | * Config classes for testing. |
| 60 | */ |
| 61 | public class BasicConfig1 extends Config<String> { } |
| 62 | public class BasicConfig2 extends Config<String> { } |
| 63 | |
| 64 | public class MockSubjectFactory extends SubjectFactory<String> { |
| 65 | protected MockSubjectFactory(Class<String> subjectClass, String subjectKey) { |
| 66 | super(subjectClass, subjectKey); |
| 67 | } |
| 68 | |
| 69 | @Override |
| 70 | public String createSubject(String subjectKey) { |
| 71 | return subjectKey + "-subject"; |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * Config factory classes for testing. |
| 77 | */ |
| 78 | public class MockConfigFactory1 extends ConfigFactory<String, BasicConfig1> { |
| 79 | protected MockConfigFactory1(SubjectFactory<String> subjectFactory, |
| 80 | Class<BasicConfig1> configClass, String configKey) { |
| 81 | super(subjectFactory, configClass, configKey); |
| 82 | } |
| 83 | @Override |
| 84 | public BasicConfig1 createConfig() { |
| 85 | return new BasicConfig1(); |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | public class MockConfigFactory2 extends ConfigFactory<String, BasicConfig2> { |
| 90 | protected MockConfigFactory2(SubjectFactory<String> subjectFactory, |
| 91 | Class<BasicConfig2> configClass, String configKey) { |
| 92 | super(subjectFactory, configClass, configKey); |
| 93 | } |
| 94 | @Override |
| 95 | public BasicConfig2 createConfig() { |
| 96 | return new BasicConfig2(); |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | MockSubjectFactory factory1 = new MockSubjectFactory(String.class, |
| 101 | "key1"); |
| 102 | MockSubjectFactory factory2 = new MockSubjectFactory(String.class, |
| 103 | "key2"); |
| 104 | |
| 105 | MockConfigFactory1 config1Factory = new MockConfigFactory1(factory1, |
| 106 | BasicConfig1.class, "config1"); |
| 107 | MockConfigFactory2 config2Factory = new MockConfigFactory2(factory2, |
| 108 | BasicConfig2.class, "config2"); |
| 109 | |
| 110 | |
| 111 | @Before |
| 112 | public void setUp() throws Exception { |
| 113 | configStore = new DistributedNetworkConfigStore(); |
| 114 | TestUtils.setField(configStore, "storageService", new TestStorageService()); |
| 115 | configStore.activate(); |
| 116 | manager = new NetworkConfigManager(); |
| 117 | manager.store = configStore; |
| 118 | NetTestTools.injectEventDispatcher(manager, new EventDeliveryServiceAdapter()); |
Andrea Campanella | e81e941 | 2018-01-16 11:12:20 +0100 | [diff] [blame^] | 119 | manager.clusterService = clusterService; |
Ray Milkey | ae9faf1 | 2015-08-03 15:52:26 -0700 | [diff] [blame] | 120 | manager.activate(); |
| 121 | registry = manager; |
| 122 | configService = manager; |
| 123 | } |
| 124 | |
| 125 | @After |
| 126 | public void tearDown() { |
| 127 | configStore.deactivate(); |
| 128 | manager.deactivate(); |
| 129 | } |
| 130 | |
| 131 | @Test |
| 132 | public void testRegistry() { |
| 133 | assertThat(registry.getConfigFactories(), hasSize(0)); |
| 134 | assertThat(registry.getConfigFactories(String.class), hasSize(0)); |
| 135 | assertThat(registry.getConfigFactory(BasicConfig1.class), nullValue()); |
| 136 | |
| 137 | registry.registerConfigFactory(config1Factory); |
| 138 | registry.registerConfigFactory(config2Factory); |
| 139 | |
| 140 | assertThat(registry.getConfigFactories(), hasSize(2)); |
| 141 | assertThat(registry.getConfigFactories(String.class), hasSize(2)); |
| 142 | |
| 143 | ConfigFactory queried = registry.getConfigFactory(BasicConfig1.class); |
| 144 | assertThat(queried, is(config1Factory)); |
| 145 | |
| 146 | registry.unregisterConfigFactory(queried); |
| 147 | // Factory associations are not removed according to code documentation |
| 148 | assertThat(registry.getConfigFactories(), hasSize(1)); |
| 149 | assertThat(registry.getConfigFactories(String.class), hasSize(1)); |
| 150 | assertThat(registry.getConfigFactory(BasicConfig1.class), nullValue()); |
| 151 | } |
| 152 | |
| 153 | @Test |
| 154 | public void configIdEquals() { |
| 155 | NetworkConfigManager.ConfigIdentifier id1 = |
| 156 | new NetworkConfigManager.ConfigIdentifier("s1", "c1"); |
| 157 | NetworkConfigManager.ConfigIdentifier likeId1 = |
| 158 | new NetworkConfigManager.ConfigIdentifier("s1", "c1"); |
| 159 | NetworkConfigManager.ConfigIdentifier id2 = |
| 160 | new NetworkConfigManager.ConfigIdentifier("s1", "c2"); |
| 161 | NetworkConfigManager.ConfigIdentifier id3 = |
| 162 | new NetworkConfigManager.ConfigIdentifier("s2", "c1"); |
| 163 | |
| 164 | new EqualsTester().addEqualityGroup(id1, likeId1) |
| 165 | .addEqualityGroup(id2) |
| 166 | .addEqualityGroup(id3) |
| 167 | .testEquals(); |
| 168 | } |
| 169 | |
| 170 | @Test |
| 171 | public void configKeyEquals() { |
| 172 | NetworkConfigManager.ConfigKey key1 = |
| 173 | new NetworkConfigManager.ConfigKey(String.class, String.class); |
| 174 | NetworkConfigManager.ConfigKey likeKey1 = |
| 175 | new NetworkConfigManager.ConfigKey(String.class, String.class); |
| 176 | NetworkConfigManager.ConfigKey key2 = |
| 177 | new NetworkConfigManager.ConfigKey(String.class, Integer.class); |
| 178 | NetworkConfigManager.ConfigKey key3 = |
| 179 | new NetworkConfigManager.ConfigKey(Integer.class, String.class); |
| 180 | |
| 181 | new EqualsTester().addEqualityGroup(key1, likeKey1) |
| 182 | .addEqualityGroup(key2) |
| 183 | .addEqualityGroup(key3) |
| 184 | .testEquals(); |
| 185 | } |
| 186 | |
| 187 | /** |
| 188 | * Tests creation, query and removal of a factory. |
| 189 | */ |
| 190 | @Test |
| 191 | public void testAddConfig() { |
| 192 | |
| 193 | assertThat(configService.getSubjectFactory(String.class), nullValue()); |
| 194 | assertThat(configService.getSubjectFactory("key"), nullValue()); |
| 195 | |
| 196 | registry.registerConfigFactory(config1Factory); |
| 197 | registry.registerConfigFactory(config2Factory); |
| 198 | configService.addConfig("configKey", BasicConfig1.class); |
| 199 | |
| 200 | Config newConfig = configService.getConfig("configKey", BasicConfig1.class); |
| 201 | assertThat(newConfig, notNullValue()); |
| 202 | |
| 203 | assertThat(configService.getSubjectFactory(String.class), notNullValue()); |
| 204 | assertThat(configService.getSubjectFactory("key1"), notNullValue()); |
| 205 | |
| 206 | Set<Class> classes = configService.getSubjectClasses(); |
| 207 | assertThat(classes, hasSize(1)); |
| 208 | |
| 209 | Set<String> subjectsForClass = |
| 210 | configService.getSubjects(String.class); |
| 211 | assertThat(subjectsForClass, hasSize(1)); |
| 212 | |
| 213 | Set<String> subjectsForConfig = |
| 214 | configService.getSubjects(String.class, BasicConfig1.class); |
| 215 | assertThat(subjectsForConfig, hasSize(1)); |
| 216 | |
| 217 | Class queriedConfigClass = configService.getConfigClass("key1", "config1"); |
| 218 | assertThat(queriedConfigClass == BasicConfig1.class, is(true)); |
| 219 | |
| 220 | Set<? extends Config> configs = configService.getConfigs("configKey"); |
| 221 | assertThat(configs.size(), is(1)); |
| 222 | configs.forEach(c -> assertThat(c, instanceOf(BasicConfig1.class))); |
| 223 | |
| 224 | configService.removeConfig("configKey", BasicConfig1.class); |
| 225 | Config newConfigAfterRemove = configService.getConfig("configKey", BasicConfig1.class); |
| 226 | assertThat(newConfigAfterRemove, nullValue()); |
| 227 | } |
| 228 | |
| 229 | /** |
| 230 | * Tests creation, query and removal of a factory. |
| 231 | */ |
| 232 | @Test |
| 233 | public void testApplyConfig() { |
| 234 | |
| 235 | assertThat(configService.getSubjectFactory(String.class), nullValue()); |
| 236 | assertThat(configService.getSubjectFactory("key"), nullValue()); |
| 237 | |
| 238 | registry.registerConfigFactory(config1Factory); |
| 239 | registry.registerConfigFactory(config2Factory); |
| 240 | configService.applyConfig("configKey", BasicConfig1.class, new ObjectMapper().createObjectNode()); |
| 241 | |
| 242 | Config newConfig = configService.getConfig("configKey", BasicConfig1.class); |
| 243 | assertThat(newConfig, notNullValue()); |
| 244 | |
| 245 | assertThat(configService.getSubjectFactory(String.class), notNullValue()); |
| 246 | assertThat(configService.getSubjectFactory("key1"), notNullValue()); |
| 247 | } |
Deepa Vaddireddy | 0c49b60 | 2016-06-02 12:19:07 +0530 | [diff] [blame] | 248 | |
| 249 | /** |
| 250 | * Tests creation, query and removal of a configuration including queued. |
| 251 | */ |
| 252 | @Test |
| 253 | public void testRemoveConfig() { |
| 254 | |
| 255 | assertThat(configService.getSubjectFactory(String.class), nullValue()); |
| 256 | assertThat(configService.getSubjectFactory("key"), nullValue()); |
| 257 | |
| 258 | registry.registerConfigFactory(config1Factory); |
| 259 | registry.registerConfigFactory(config2Factory); |
| 260 | configService.applyConfig("configKey", BasicConfig1.class, new ObjectMapper().createObjectNode()); |
| 261 | |
| 262 | configService.applyConfig("key1", "key", "config1", new ObjectMapper().createObjectNode()); |
| 263 | configService.applyConfig("key1", "keyxx", "config3", new ObjectMapper().createObjectNode()); |
| 264 | configService.applyConfig("key2", "key1", "config4", new ObjectMapper().createObjectNode()); |
| 265 | |
| 266 | configService.removeConfig(); |
| 267 | |
| 268 | Set<String> subjects = configService.getSubjects(factory1.subjectClass()); |
| 269 | assertThat(subjects.size(), is(0)); |
| 270 | |
| 271 | Set<String> subjects2 = configService.getSubjects(factory2.subjectClass()); |
| 272 | assertThat(subjects2.size(), is(0)); |
| 273 | |
| 274 | configService.applyConfig("key1", "key", "config1", new ObjectMapper().createObjectNode()); |
| 275 | configService.applyConfig("key1", "keyxx", "config3", new ObjectMapper().createObjectNode()); |
| 276 | configService.applyConfig("key1", "key1", "config4", new ObjectMapper().createObjectNode()); |
| 277 | |
| 278 | @SuppressWarnings("unchecked") |
| 279 | Set<String> configs = configService.getSubjects( |
| 280 | configService.getSubjectFactory("key1").subjectClass()); |
| 281 | |
| 282 | configs.forEach(c -> configService.removeConfig(c)); |
| 283 | Set<String> newConfig1 = configService.getSubjects(factory1.subjectClass()); |
| 284 | |
| 285 | assertThat(newConfig1, notNullValue()); |
| 286 | } |
Ray Milkey | ae9faf1 | 2015-08-03 15:52:26 -0700 | [diff] [blame] | 287 | } |