blob: b4064aa09a46431a33533a516d5b389950d99d60 [file] [log] [blame]
Jian Li2dc9f002017-03-03 04:26:31 +09001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2017-present Open Networking Foundation
Jian Li2dc9f002017-03-03 04:26:31 +09003 *
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 */
16package org.onosproject.mapping.impl;
17
18import com.google.common.collect.ImmutableList;
19import com.google.common.collect.Lists;
20import com.google.common.collect.Sets;
21import org.junit.After;
22import org.junit.Before;
23import org.junit.Test;
24import org.onosproject.common.event.impl.TestEventDispatcher;
25import org.onosproject.core.ApplicationId;
26import org.onosproject.core.DefaultApplicationId;
27import org.onosproject.mapping.DefaultMapping;
28import org.onosproject.mapping.DefaultMappingEntry;
29import org.onosproject.mapping.Mapping;
Jian Licdd1bfd2017-03-12 22:42:56 +090030import org.onosproject.mapping.MappingAdminService;
Jian Li2dc9f002017-03-03 04:26:31 +090031import org.onosproject.mapping.MappingEntry;
32import org.onosproject.mapping.MappingEvent;
33import org.onosproject.mapping.MappingKey;
34import org.onosproject.mapping.MappingListener;
35import org.onosproject.mapping.MappingProvider;
36import org.onosproject.mapping.MappingProviderRegistry;
37import org.onosproject.mapping.MappingProviderService;
38import org.onosproject.mapping.MappingService;
39import org.onosproject.mapping.MappingStore.Type;
40import org.onosproject.mapping.MappingTreatment;
41import org.onosproject.mapping.MappingValue;
42import org.onosproject.mapping.actions.MappingAction;
43import org.onosproject.mapping.addresses.MappingAddress;
44import org.onosproject.net.AnnotationKeys;
45import org.onosproject.net.DefaultAnnotations;
46import org.onosproject.net.DefaultDevice;
47import org.onosproject.net.Device;
48import org.onosproject.net.DeviceId;
49import org.onosproject.net.device.DeviceServiceAdapter;
50import org.onosproject.net.provider.AbstractProvider;
51import org.onosproject.net.provider.ProviderId;
52
53import java.util.ArrayList;
54import java.util.List;
55
56import static org.junit.Assert.assertEquals;
57import static org.junit.Assert.assertFalse;
58import static org.junit.Assert.assertNotNull;
59import static org.junit.Assert.assertTrue;
60import static org.onosproject.mapping.MappingStore.Type.MAP_DATABASE;
61import static org.onosproject.net.NetTestTools.injectEventDispatcher;
62
63/**
64 * Unit tests for mapping manager.
65 */
66public 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 Licdd1bfd2017-03-12 22:42:56 +090086 private MappingAdminService adminService;
Jian Li2dc9f002017-03-03 04:26:31 +090087 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 Licdd1bfd2017-03-12 22:42:56 +0900101 adminService = manager;
Jian Li2dc9f002017-03-03 04:26:31 +0900102 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 Licdd1bfd2017-03-12 22:42:56 +0900166 adminService.storeMappingEntry(type, entry);
Jian Li2dc9f002017-03-03 04:26:31 +0900167
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 Licdd1bfd2017-03-12 22:42:56 +0900215 adminService.storeMappingEntry(MAP_DATABASE, me1);
216 adminService.storeMappingEntry(MAP_DATABASE, me2);
217 adminService.storeMappingEntry(MAP_DATABASE, me3);
Jian Li2dc9f002017-03-03 04:26:31 +0900218 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 Licdd1bfd2017-03-12 22:42:56 +0900235 adminService.removeMappingEntries(MAP_DATABASE, me1, me2);
Jian Li2dc9f002017-03-03 04:26:31 +0900236 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 Licdd1bfd2017-03-12 22:42:56 +0900250 adminService.purgeMappings(MAP_DATABASE, LISP_DID);
Jian Li2dc9f002017-03-03 04:26:31 +0900251 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 Li9552f172017-04-20 00:19:56 +0900264 service.getMappingEntriesByAppId(MAP_DATABASE, appId)).size() == 2);
Jian Li2dc9f002017-03-03 04:26:31 +0900265 }
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 Licdd1bfd2017-03-12 22:42:56 +0900275 adminService.removeMappingEntriesByAppId(MAP_DATABASE, appId);
Jian Li2dc9f002017-03-03 04:26:31 +0900276
277 assertTrue("should not have any mappings",
278 Lists.newLinkedList(
Jian Li9552f172017-04-20 00:19:56 +0900279 service.getMappingEntriesByAppId(MAP_DATABASE, appId)).size() == 0);
Jian Li2dc9f002017-03-03 04:26:31 +0900280 }
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}