Jian Li | 2dc9f00 | 2017-03-03 04:26:31 +0900 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2017-present Open Networking Foundation |
Jian Li | 2dc9f00 | 2017-03-03 04:26:31 +0900 | [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 | */ |
| 16 | package org.onosproject.mapping.impl; |
| 17 | |
| 18 | import com.google.common.collect.ImmutableList; |
| 19 | import com.google.common.collect.Lists; |
| 20 | import com.google.common.collect.Sets; |
| 21 | import org.junit.After; |
| 22 | import org.junit.Before; |
| 23 | import org.junit.Test; |
| 24 | import org.onosproject.common.event.impl.TestEventDispatcher; |
| 25 | import org.onosproject.core.ApplicationId; |
| 26 | import org.onosproject.core.DefaultApplicationId; |
| 27 | import org.onosproject.mapping.DefaultMapping; |
| 28 | import org.onosproject.mapping.DefaultMappingEntry; |
| 29 | import org.onosproject.mapping.Mapping; |
Jian Li | cdd1bfd | 2017-03-12 22:42:56 +0900 | [diff] [blame] | 30 | import org.onosproject.mapping.MappingAdminService; |
Jian Li | 2dc9f00 | 2017-03-03 04:26:31 +0900 | [diff] [blame] | 31 | import org.onosproject.mapping.MappingEntry; |
| 32 | import org.onosproject.mapping.MappingEvent; |
| 33 | import org.onosproject.mapping.MappingKey; |
| 34 | import org.onosproject.mapping.MappingListener; |
| 35 | import org.onosproject.mapping.MappingProvider; |
| 36 | import org.onosproject.mapping.MappingProviderRegistry; |
| 37 | import org.onosproject.mapping.MappingProviderService; |
| 38 | import org.onosproject.mapping.MappingService; |
| 39 | import org.onosproject.mapping.MappingStore.Type; |
| 40 | import org.onosproject.mapping.MappingTreatment; |
| 41 | import org.onosproject.mapping.MappingValue; |
| 42 | import org.onosproject.mapping.actions.MappingAction; |
| 43 | import org.onosproject.mapping.addresses.MappingAddress; |
| 44 | import org.onosproject.net.AnnotationKeys; |
| 45 | import org.onosproject.net.DefaultAnnotations; |
| 46 | import org.onosproject.net.DefaultDevice; |
| 47 | import org.onosproject.net.Device; |
| 48 | import org.onosproject.net.DeviceId; |
| 49 | import org.onosproject.net.device.DeviceServiceAdapter; |
| 50 | import org.onosproject.net.provider.AbstractProvider; |
| 51 | import org.onosproject.net.provider.ProviderId; |
| 52 | |
| 53 | import java.util.ArrayList; |
| 54 | import java.util.List; |
| 55 | |
| 56 | import static org.junit.Assert.assertEquals; |
| 57 | import static org.junit.Assert.assertFalse; |
| 58 | import static org.junit.Assert.assertNotNull; |
| 59 | import static org.junit.Assert.assertTrue; |
| 60 | import static org.onosproject.mapping.MappingStore.Type.MAP_DATABASE; |
| 61 | import static org.onosproject.net.NetTestTools.injectEventDispatcher; |
| 62 | |
| 63 | /** |
| 64 | * Unit tests for mapping manager. |
| 65 | */ |
| 66 | public class MappingManagerTest { |
| 67 | |
| 68 | private static final ProviderId LISP_PID = new ProviderId("lisp", "lisp"); |
| 69 | private static final ProviderId BAR_PID = new ProviderId("bar", "bar"); |
| 70 | |
| 71 | private static final DeviceId LISP_DID = DeviceId.deviceId("lisp:001"); |
| 72 | private static final DeviceId BAR_DID = DeviceId.deviceId("bar:002"); |
| 73 | |
| 74 | private static final DefaultAnnotations ANNOTATIONS = |
| 75 | DefaultAnnotations.builder().set(AnnotationKeys.DRIVER, "bar").build(); |
| 76 | |
| 77 | private static final Device LISP_DEV = |
| 78 | new DefaultDevice(LISP_PID, LISP_DID, Device.Type.SWITCH, "", "", "", "", null); |
| 79 | private static final Device BAR_DEV = |
| 80 | new DefaultDevice(BAR_PID, BAR_DID, Device.Type.SWITCH, "", "", "", "", null, ANNOTATIONS); |
| 81 | |
| 82 | |
| 83 | private MappingManager manager; |
| 84 | |
| 85 | private MappingService service; |
Jian Li | cdd1bfd | 2017-03-12 22:42:56 +0900 | [diff] [blame] | 86 | private MappingAdminService adminService; |
Jian Li | 2dc9f00 | 2017-03-03 04:26:31 +0900 | [diff] [blame] | 87 | private MappingProviderRegistry registry; |
| 88 | private MappingProviderService providerService; |
| 89 | private TestProvider provider; |
| 90 | private TestListener listener = new TestListener(); |
| 91 | private ApplicationId appId; |
| 92 | |
| 93 | @Before |
| 94 | public void setUp() { |
| 95 | manager = new MappingManager(); |
| 96 | manager.store = new SimpleMappingStore(); |
| 97 | injectEventDispatcher(manager, new TestEventDispatcher()); |
| 98 | manager.deviceService = new TestDeviceService(); |
| 99 | |
| 100 | service = manager; |
Jian Li | cdd1bfd | 2017-03-12 22:42:56 +0900 | [diff] [blame] | 101 | adminService = manager; |
Jian Li | 2dc9f00 | 2017-03-03 04:26:31 +0900 | [diff] [blame] | 102 | registry = manager; |
| 103 | |
| 104 | manager.activate(); |
| 105 | manager.addListener(listener); |
| 106 | provider = new TestProvider(LISP_PID); |
| 107 | providerService = registry.register(provider); |
| 108 | appId = new TestApplicationId(0, "MappingManagerTest"); |
| 109 | assertTrue("provider should be registered", |
| 110 | registry.getProviders().contains(provider.id())); |
| 111 | } |
| 112 | |
| 113 | @After |
| 114 | public void tearDown() { |
| 115 | registry.unregister(provider); |
| 116 | assertFalse("provider should not be registered", |
| 117 | registry.getProviders().contains(provider.id())); |
| 118 | service.removeListener(listener); |
| 119 | manager.deactivate(); |
| 120 | injectEventDispatcher(manager, null); |
| 121 | manager.deviceService = null; |
| 122 | } |
| 123 | |
| 124 | /** |
| 125 | * Creates a mapping from a specified deviceID, key and value. |
| 126 | * |
| 127 | * @param did device identifier |
| 128 | * @param key test value for mapping key |
| 129 | * @param value test value for mapping value |
| 130 | * @return mapping instance |
| 131 | */ |
| 132 | private Mapping mapping(DeviceId did, int key, int value) { |
| 133 | TestMappingKey mappingKey = new TestMappingKey(key); |
| 134 | TestMappingValue mappingValue = new TestMappingValue(value); |
| 135 | |
| 136 | return DefaultMapping.builder() |
| 137 | .forDevice(did) |
| 138 | .withKey(mappingKey) |
| 139 | .withValue(mappingValue) |
| 140 | .fromApp(appId) |
| 141 | .withId(key + value) |
| 142 | .build(); |
| 143 | } |
| 144 | |
| 145 | /** |
| 146 | * Creates a mapping from a specified key and value. |
| 147 | * |
| 148 | * @param key test value for mapping key |
| 149 | * @param value test value for mapping value |
| 150 | * @return mapping instance |
| 151 | */ |
| 152 | private Mapping mapping(int key, int value) { |
| 153 | return mapping(LISP_DID, key, value); |
| 154 | } |
| 155 | |
| 156 | /** |
| 157 | * Adds a new mapping into the mapping store. |
| 158 | * |
| 159 | * @param type mapping store type |
| 160 | * @param tval test value |
| 161 | * @return a mapping that has been added to the store |
| 162 | */ |
| 163 | private Mapping addMapping(Type type, int tval) { |
| 164 | Mapping mapping = mapping(tval, tval); |
| 165 | MappingEntry entry = new DefaultMappingEntry(mapping); |
Jian Li | cdd1bfd | 2017-03-12 22:42:56 +0900 | [diff] [blame] | 166 | adminService.storeMappingEntry(type, entry); |
Jian Li | 2dc9f00 | 2017-03-03 04:26:31 +0900 | [diff] [blame] | 167 | |
| 168 | assertNotNull("mapping should be found", |
| 169 | service.getMappingEntries(type, LISP_DID)); |
| 170 | return mapping; |
| 171 | } |
| 172 | |
| 173 | /** |
| 174 | * Obtains the number of mappings. |
| 175 | * |
| 176 | * @param type mapping store type |
| 177 | * @return number of mappings |
| 178 | */ |
| 179 | private int mappingCount(Type type) { |
| 180 | return Sets.newHashSet(service.getMappingEntries(type, LISP_DID)).size(); |
| 181 | } |
| 182 | |
| 183 | /** |
| 184 | * Tests retrieving mapping entries method. |
| 185 | */ |
| 186 | @Test |
| 187 | public void getMappingEntries() { |
| 188 | |
| 189 | assertTrue("Store should be empty", Sets.newHashSet( |
| 190 | service.getMappingEntries(MAP_DATABASE, LISP_DID)).isEmpty()); |
| 191 | addMapping(MAP_DATABASE, 1); |
| 192 | addMapping(MAP_DATABASE, 2); |
| 193 | assertEquals("2 mappings should exist", 2, mappingCount(MAP_DATABASE)); |
| 194 | |
| 195 | addMapping(MAP_DATABASE, 1); |
| 196 | assertEquals("should still be 2 mappings", 2, mappingCount(MAP_DATABASE)); |
| 197 | } |
| 198 | |
| 199 | /** |
| 200 | * Tests storing mapping entry method. |
| 201 | */ |
| 202 | @Test |
| 203 | public void storeMappingEntry() { |
| 204 | |
| 205 | Mapping m1 = mapping(1, 1); |
| 206 | Mapping m2 = mapping(2, 2); |
| 207 | Mapping m3 = mapping(3, 3); |
| 208 | |
| 209 | MappingEntry me1 = new DefaultMappingEntry(m1); |
| 210 | MappingEntry me2 = new DefaultMappingEntry(m2); |
| 211 | MappingEntry me3 = new DefaultMappingEntry(m3); |
| 212 | |
| 213 | assertTrue("store should be empty", Sets.newHashSet( |
| 214 | service.getMappingEntries(MAP_DATABASE, LISP_DID)).isEmpty()); |
Jian Li | cdd1bfd | 2017-03-12 22:42:56 +0900 | [diff] [blame] | 215 | adminService.storeMappingEntry(MAP_DATABASE, me1); |
| 216 | adminService.storeMappingEntry(MAP_DATABASE, me2); |
| 217 | adminService.storeMappingEntry(MAP_DATABASE, me3); |
Jian Li | 2dc9f00 | 2017-03-03 04:26:31 +0900 | [diff] [blame] | 218 | assertEquals("3 mappings should exist", 3, mappingCount(MAP_DATABASE)); |
| 219 | } |
| 220 | |
| 221 | /** |
| 222 | * Tests removing mapping entries method. |
| 223 | */ |
| 224 | @Test |
| 225 | public void removeMappingEntries() { |
| 226 | |
| 227 | Mapping m1 = addMapping(MAP_DATABASE, 1); |
| 228 | Mapping m2 = addMapping(MAP_DATABASE, 2); |
| 229 | addMapping(MAP_DATABASE, 3); |
| 230 | assertEquals("3 mappings should exist", 3, mappingCount(MAP_DATABASE)); |
| 231 | |
| 232 | MappingEntry me1 = new DefaultMappingEntry(m1); |
| 233 | MappingEntry me2 = new DefaultMappingEntry(m2); |
| 234 | |
Jian Li | cdd1bfd | 2017-03-12 22:42:56 +0900 | [diff] [blame] | 235 | adminService.removeMappingEntries(MAP_DATABASE, me1, me2); |
Jian Li | 2dc9f00 | 2017-03-03 04:26:31 +0900 | [diff] [blame] | 236 | assertEquals("1 mappings should exist", 1, mappingCount(MAP_DATABASE)); |
| 237 | } |
| 238 | |
| 239 | /** |
| 240 | * Tests purging all mappings. |
| 241 | */ |
| 242 | @Test |
| 243 | public void purgeMappings() { |
| 244 | |
| 245 | addMapping(MAP_DATABASE, 1); |
| 246 | addMapping(MAP_DATABASE, 2); |
| 247 | addMapping(MAP_DATABASE, 3); |
| 248 | assertEquals("3 mappings should exist", 3, mappingCount(MAP_DATABASE)); |
| 249 | |
Jian Li | cdd1bfd | 2017-03-12 22:42:56 +0900 | [diff] [blame] | 250 | adminService.purgeMappings(MAP_DATABASE, LISP_DID); |
Jian Li | 2dc9f00 | 2017-03-03 04:26:31 +0900 | [diff] [blame] | 251 | assertEquals("0 mappings should exist", 0, mappingCount(MAP_DATABASE)); |
| 252 | } |
| 253 | |
| 254 | /** |
| 255 | * Tests obtaining mapping entries by application ID. |
| 256 | */ |
| 257 | @Test |
| 258 | public void getMappingEntriesByAddId() { |
| 259 | addMapping(MAP_DATABASE, 1); |
| 260 | addMapping(MAP_DATABASE, 2); |
| 261 | |
| 262 | assertTrue("should have two mappings", |
| 263 | Lists.newLinkedList( |
Jian Li | 9552f17 | 2017-04-20 00:19:56 +0900 | [diff] [blame] | 264 | service.getMappingEntriesByAppId(MAP_DATABASE, appId)).size() == 2); |
Jian Li | 2dc9f00 | 2017-03-03 04:26:31 +0900 | [diff] [blame] | 265 | } |
| 266 | |
| 267 | /** |
| 268 | * Tests removing mapping entries by application ID. |
| 269 | */ |
| 270 | @Test |
| 271 | public void removeMappingEntriesByAppId() { |
| 272 | addMapping(MAP_DATABASE, 1); |
| 273 | addMapping(MAP_DATABASE, 2); |
| 274 | |
Jian Li | cdd1bfd | 2017-03-12 22:42:56 +0900 | [diff] [blame] | 275 | adminService.removeMappingEntriesByAppId(MAP_DATABASE, appId); |
Jian Li | 2dc9f00 | 2017-03-03 04:26:31 +0900 | [diff] [blame] | 276 | |
| 277 | assertTrue("should not have any mappings", |
| 278 | Lists.newLinkedList( |
Jian Li | 9552f17 | 2017-04-20 00:19:56 +0900 | [diff] [blame] | 279 | service.getMappingEntriesByAppId(MAP_DATABASE, appId)).size() == 0); |
Jian Li | 2dc9f00 | 2017-03-03 04:26:31 +0900 | [diff] [blame] | 280 | } |
| 281 | |
| 282 | private static class TestDeviceService extends DeviceServiceAdapter { |
| 283 | @Override |
| 284 | public int getDeviceCount() { |
| 285 | return 2; |
| 286 | } |
| 287 | |
| 288 | @Override |
| 289 | public Iterable<Device> getDevices() { |
| 290 | return ImmutableList.of(LISP_DEV, BAR_DEV); |
| 291 | } |
| 292 | |
| 293 | @Override |
| 294 | public Iterable<Device> getAvailableDevices() { |
| 295 | return getDevices(); |
| 296 | } |
| 297 | |
| 298 | @Override |
| 299 | public Device getDevice(DeviceId deviceId) { |
| 300 | return deviceId.equals(BAR_DID) ? BAR_DEV : LISP_DEV; |
| 301 | } |
| 302 | } |
| 303 | |
| 304 | private static class TestListener implements MappingListener { |
| 305 | final List<MappingEvent> events = new ArrayList<>(); |
| 306 | |
| 307 | @Override |
| 308 | public void event(MappingEvent event) { |
| 309 | events.add(event); |
| 310 | } |
| 311 | } |
| 312 | |
| 313 | private class TestProvider extends AbstractProvider implements MappingProvider { |
| 314 | |
| 315 | /** |
| 316 | * Creates a provider with the supplied identifier. |
| 317 | * |
| 318 | * @param id provider id |
| 319 | */ |
| 320 | TestProvider(ProviderId id) { |
| 321 | super(id); |
| 322 | } |
| 323 | } |
| 324 | |
| 325 | private class TestApplicationId extends DefaultApplicationId { |
| 326 | TestApplicationId(int id, String name) { |
| 327 | super(id, name); |
| 328 | } |
| 329 | } |
| 330 | |
| 331 | private class TestMappingKey implements MappingKey { |
| 332 | |
| 333 | private final int val; |
| 334 | |
| 335 | TestMappingKey(int val) { |
| 336 | this.val = val; |
| 337 | } |
| 338 | |
| 339 | @Override |
| 340 | public MappingAddress address() { |
| 341 | return null; |
| 342 | } |
| 343 | |
| 344 | @Override |
| 345 | public int hashCode() { |
| 346 | return val; |
| 347 | } |
| 348 | |
| 349 | @Override |
| 350 | public boolean equals(Object o) { |
| 351 | return o instanceof TestMappingKey && this.val == ((TestMappingKey) o).val; |
| 352 | } |
| 353 | } |
| 354 | |
| 355 | private class TestMappingValue implements MappingValue { |
| 356 | |
| 357 | private final int val; |
| 358 | |
| 359 | TestMappingValue(int val) { |
| 360 | this.val = val; |
| 361 | } |
| 362 | |
| 363 | @Override |
| 364 | public MappingAction action() { |
| 365 | return null; |
| 366 | } |
| 367 | |
| 368 | @Override |
| 369 | public List<MappingTreatment> treatments() { |
| 370 | return null; |
| 371 | } |
| 372 | |
| 373 | @Override |
| 374 | public int hashCode() { |
| 375 | return val; |
| 376 | } |
| 377 | |
| 378 | @Override |
| 379 | public boolean equals(Object o) { |
| 380 | return o instanceof TestMappingValue && this.val == ((TestMappingValue) o).val; |
| 381 | } |
| 382 | } |
| 383 | } |