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