blob: 609a030b27e9f66a63a7b48062c899f1e43fc87b [file] [log] [blame]
Sonal Kasliwalee230262020-01-06 10:46:30 +00001/*
2 * Copyright 2016-present Open Networking Foundation
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.opencord.cordmcast;
17
18import static org.junit.Assert.assertNotNull;
19import static org.junit.Assert.assertTrue;
20
21import java.util.Arrays;
22import java.util.Collection;
23import java.util.Dictionary;
24import java.util.HashSet;
25import java.util.Hashtable;
26import java.util.Map;
27import java.util.Set;
28import static org.easymock.EasyMock.expect;
29import static org.easymock.EasyMock.replay;
30import org.easymock.EasyMock;
31import org.junit.After;
32import org.junit.Before;
33import org.junit.Test;
34import org.onosproject.cfg.ComponentConfigService;
35import org.onosproject.mcast.api.McastEvent;
36import org.onosproject.mcast.api.McastRouteUpdate;
37import org.onosproject.mcast.api.MulticastRouteService;
38import org.onosproject.net.ConnectPoint;
39import org.onosproject.net.DeviceId;
40import org.onosproject.net.HostId;
41import org.onosproject.net.config.NetworkConfigRegistryAdapter;
42import org.onosproject.net.device.DeviceServiceAdapter;
43import org.onosproject.net.flow.FlowRuleServiceAdapter;
44import org.onosproject.net.flow.TrafficSelector;
45import org.onosproject.net.flow.TrafficTreatment;
46import org.onosproject.net.flow.criteria.IPCriterion;
47import org.onosproject.net.flow.instructions.Instructions.OutputInstruction;
48import org.onosproject.net.flowobjective.Objective;
49import org.onosproject.store.service.StorageServiceAdapter;
50import org.onosproject.store.service.TestConsistentMap;
51import org.osgi.service.component.ComponentContext;
52import com.google.common.collect.ImmutableMap;
53import com.google.common.collect.Sets;
54import static org.onlab.junit.TestTools.assertAfter;
55
56public class McastTest extends McastTestBase {
57
58 private CordMcast cordMcast;
59
60 private static final int WAIT_TIMEOUT = 1000;
61 private static final int WAIT = 250;
62 McastRouteUpdate previousSubject, currentSubject;
63
64 @Before
65 public void setUp() {
66 cordMcast = new CordMcast();
67
68 cordMcast.coreService = new MockCoreService();
69 cordMcast.flowObjectiveService = new MockFlowObjectiveService();
70 cordMcast.mastershipService = new TestMastershipService();
71 cordMcast.flowRuleService = new FlowRuleServiceAdapter();
72 cordMcast.deviceService = new DeviceServiceAdapter();
73 cordMcast.networkConfig = new NetworkConfigRegistryAdapter();
74 cordMcast.cordConfigService = new MockCordConfigService();
75
76 cordMcast.storageService =
77 EasyMock.createMock(StorageServiceAdapter.class);
78 expect(cordMcast.storageService.consistentMapBuilder()).andReturn(new TestConsistentMap.Builder<>());
79 replay(cordMcast.storageService);
80
81 Dictionary<String, Object> cfgDict = new Hashtable<String, Object>();
82 cfgDict.put("vlanEnabled", false);
83 cordMcast.componentConfigService =
84 EasyMock.createNiceMock(ComponentConfigService.class);
85 replay(cordMcast.componentConfigService);
86
87 cordMcast.mcastService = EasyMock.createNiceMock(MulticastRouteService.class);
88 expect(cordMcast.mcastService.getRoutes()).andReturn(Sets.newHashSet());
89 replay(cordMcast.mcastService);
90
91 ComponentContext componentContext = EasyMock.createMock(ComponentContext.class);
92 expect(componentContext.getProperties()).andReturn(cfgDict);
93 replay(componentContext);
94
95 cordMcast.activate(componentContext);
96
97 }
98
99 @After
100 public void tearDown() {
101 cordMcast.deactivate();
102 forwardMap.clear();
103 nextMap.clear();
104 }
105
106 @Test
107 public void testAddingSinkEvent() throws InterruptedException {
108
109 Set<ConnectPoint> sinks2Cp = new HashSet<ConnectPoint>(Arrays.asList(CONNECT_POINT_B));
110 Map<HostId, Set<ConnectPoint>> sinks2 = ImmutableMap.of(HOST_ID_NONE, sinks2Cp);
111
112 //Adding the details to create different routes
113 previousSubject = McastRouteUpdate.mcastRouteUpdate(route1, sources, sinks);
114 currentSubject = McastRouteUpdate.mcastRouteUpdate(route1, sources, sinks2);
115 // Creating new mcast event for adding sink
116 McastEvent event = new McastEvent(McastEvent.Type.SINKS_ADDED, previousSubject, currentSubject);
117 cordMcast.listener.event(event);
118 synchronized (forwardMap) {
119 forwardMap.wait(WAIT_TIMEOUT);
120 }
121
122 // ForwardMap will contain the operation "Add" in the flowObjective. None -> CP_B
123 assertNotNull(forwardMap.get(DEVICE_ID_OF_A));
124 assertTrue(forwardMap.get(DEVICE_ID_OF_A).op() == Objective.Operation.ADD);
125
126 // Output port number will be PORT_B i.e. 16
127 Collection<TrafficTreatment> traffictreatMentCollection =
128 nextMap.get(DEVICE_ID_OF_A).next();
129 assertTrue(1 == traffictreatMentCollection.size());
130 OutputInstruction output = null;
131 for (TrafficTreatment trafficTreatment : traffictreatMentCollection) {
132 output = outputPort(trafficTreatment);
133 }
134 assertNotNull(output);
135 assertTrue(PORT_B == output.port());
136 // Checking the group ip address
137 TrafficSelector trafficSelector = forwardMap.get(DEVICE_ID_OF_A).selector();
138 IPCriterion ipCriterion = ipAddress(trafficSelector);
139 assertNotNull(ipCriterion);
140 assertTrue(MULTICAST_IP.equals(ipCriterion.ip().address()));
141
142 }
143
144 @Test
145 public void testAddToExistingSinkEvent() throws InterruptedException {
146
147 // Adding first sink (none --> CP_B)
148 testAddingSinkEvent();
149
150 Set<ConnectPoint> sinksCp = new HashSet<ConnectPoint>(Arrays.asList(CONNECT_POINT_B));
151 Map<HostId, Set<ConnectPoint>> sinks = ImmutableMap.of(HOST_ID_NONE, sinksCp);
152 previousSubject = McastRouteUpdate.mcastRouteUpdate(route1, sources, sinks);
153 sinksCp = new HashSet<ConnectPoint>(Arrays.asList(CONNECT_POINT_B, CONNECT_POINT_C));
154 sinks = ImmutableMap.of(HOST_ID_NONE, sinksCp);
155 currentSubject = McastRouteUpdate.mcastRouteUpdate(route1, sources, sinks);
156 // Again listening the mcast event with different output port ( none --> CP_B, CP_C)
157 McastEvent event = new McastEvent(McastEvent.Type.SINKS_ADDED, previousSubject, currentSubject);
158 cordMcast.listener.event(event);
159
160 // NextMap will contain the operation "ADD_TO_EXISTING" in the DefaultNextObjective.
161 assertAfter(WAIT_TIMEOUT, WAIT_TIMEOUT * 2, () ->
162 assertTrue(nextMap.get(DEVICE_ID_OF_A).op() == Objective.Operation.ADD_TO_EXISTING));
163 // Output port number will be changed to 24 i.e. PORT_C
164 Collection<TrafficTreatment> traffictreatMentCollection = nextMap.get(DEVICE_ID_OF_A).next();
165 assertTrue(1 == traffictreatMentCollection.size());
166 OutputInstruction output = null;
167 for (TrafficTreatment trafficTreatment : traffictreatMentCollection) {
168 output = outputPort(trafficTreatment);
169 }
170 assertNotNull(output);
171 assertTrue(PORT_C == output.port());
172 }
173
174 @Test
175 public void testRemoveSinkEvent() throws InterruptedException {
176
177 testAddToExistingSinkEvent();
178 // Handling the mcast event for removing sink.
179 Set<ConnectPoint> sinksCp = new HashSet<ConnectPoint>(Arrays.asList(CONNECT_POINT_B, CONNECT_POINT_C));
180 Map<HostId, Set<ConnectPoint>> sinks = ImmutableMap.of(HOST_ID_NONE, sinksCp);
181 previousSubject = McastRouteUpdate.mcastRouteUpdate(route1, sources, sinks);
182 sinksCp = new HashSet<ConnectPoint>(Arrays.asList(CONNECT_POINT_C));
183 sinks = ImmutableMap.of(HOST_ID_NONE, sinksCp);
184 currentSubject = McastRouteUpdate.mcastRouteUpdate(route1, sources, sinks);
185 McastEvent event = new McastEvent(McastEvent.Type.SINKS_REMOVED, previousSubject, currentSubject);
186 cordMcast.listener.event(event);
187 // Operation will be REMOVE_FROM_EXISTING and nextMap will be updated. ( None --> CP_C)
188 assertAfter(WAIT_TIMEOUT, WAIT_TIMEOUT * 2, () ->
189 assertTrue(nextMap.get(DEVICE_ID_OF_A).op() == Objective.Operation.REMOVE_FROM_EXISTING));
190
191 // Output port number will be PORT_B i.e. 16
192 // Port_B is removed from the group.
193 Collection<TrafficTreatment> traffictreatMentCollection =
194 nextMap.get(DEVICE_ID_OF_A).next();
195 assertTrue(1 == traffictreatMentCollection.size());
196 OutputInstruction output = null;
197 for (TrafficTreatment trafficTreatment : traffictreatMentCollection) {
198 output = outputPort(trafficTreatment);
199 }
200 assertNotNull(output);
201 assertTrue(PORT_B == output.port());
202
203 }
204
205 @Test
206 public void testRemoveLastSinkEvent() throws InterruptedException {
207
208 testRemoveSinkEvent();
209 // Handling the mcast event for removing sink.
210 Set<ConnectPoint> sinksCp = new HashSet<ConnectPoint>(Arrays.asList(CONNECT_POINT_C));
211 Map<HostId, Set<ConnectPoint>> sinks = ImmutableMap.of(HOST_ID_NONE, sinksCp);
212 previousSubject = McastRouteUpdate.mcastRouteUpdate(route1, sources, sinks);
213 sinksCp = new HashSet<ConnectPoint>(Arrays.asList());
214 sinks = ImmutableMap.of(HOST_ID_NONE, sinksCp);
215 currentSubject = McastRouteUpdate.mcastRouteUpdate(route1, sources, sinks);
216 McastEvent event = new McastEvent(McastEvent.Type.SINKS_REMOVED, previousSubject, currentSubject);
217 cordMcast.listener.event(event);
218
219 // Operation will be REMOVE_FROM_EXISTING and nextMap will be updated. None --> { }
220 assertAfter(WAIT_TIMEOUT, WAIT_TIMEOUT * 2, () ->
221 assertTrue(nextMap.get(DEVICE_ID_OF_A).op() == Objective.Operation.REMOVE_FROM_EXISTING));
222
223 // Output port number will be changed to 24 i.e. PORT_C
224 Collection<TrafficTreatment> traffictreatMentCollection = nextMap.get(DEVICE_ID_OF_A).next();
225 assertTrue(1 == traffictreatMentCollection.size());
226 OutputInstruction output = null;
227 for (TrafficTreatment trafficTreatment : traffictreatMentCollection) {
228 output = outputPort(trafficTreatment);
229 }
230 assertNotNull(output);
231 assertTrue(PORT_C == output.port());
232 }
233
234 @Test
235 public void testUnkownOltDevice() throws InterruptedException {
236
237 // Configuration of mcast event for unknown olt device
238 final DeviceId deviceIdOfB = DeviceId.deviceId("of:1");
239
240 ConnectPoint connectPointA = new ConnectPoint(deviceIdOfB, PORT_A);
241 ConnectPoint connectPointB = new ConnectPoint(deviceIdOfB, PORT_B);
242 Set<ConnectPoint> sourcesCp = new HashSet<ConnectPoint>(Arrays.asList(connectPointA));
243 Set<ConnectPoint> sinksCp = new HashSet<ConnectPoint>(Arrays.asList());
244 Set<ConnectPoint> sinks2Cp = new HashSet<ConnectPoint>(Arrays.asList(connectPointB));
245 Map<HostId, Set<ConnectPoint>> sources = ImmutableMap.of(HOST_ID_NONE, sourcesCp);
246
247 Map<HostId, Set<ConnectPoint>> sinks = ImmutableMap.of(HOST_ID_NONE, sinksCp);
248 Map<HostId, Set<ConnectPoint>> sinks2 = ImmutableMap.of(HOST_ID_NONE, sinks2Cp);
249 //Adding the details to create different routes
250 McastRouteUpdate previousSubject = McastRouteUpdate.mcastRouteUpdate(route1, sources, sinks);
251 McastRouteUpdate currentSubject = McastRouteUpdate.mcastRouteUpdate(route1, sources, sinks2);
252 // Creating new mcast event for adding sink
253 McastEvent event = new McastEvent(McastEvent.Type.SINKS_ADDED, previousSubject, currentSubject);
254 cordMcast.listener.event(event);
255 // OltInfo flag is set to true when olt device is unkown
256 assertAfter(WAIT, WAIT * 2, () -> assertTrue(knownOltFlag));
257 assertTrue(0 == forwardMap.size());
258 assertTrue(0 == nextMap.size());
259
260 }
261
262 @Test
263 public void testRouteAddedEvent() throws InterruptedException {
264
265 //Adding the details to create different routes
266 previousSubject = McastRouteUpdate.mcastRouteUpdate(route1, emptySource, sinks);
267 currentSubject = McastRouteUpdate.mcastRouteUpdate(route1, emptySource, sinks);
268 // Creating new mcast event for route adding
269 McastEvent event = new McastEvent(McastEvent.Type.ROUTE_ADDED, previousSubject, currentSubject);
270 cordMcast.listener.event(event);
271 // There will be no forwarding objective
272 assertAfter(WAIT, WAIT * 2, () -> assertTrue(0 == forwardMap.size()));
273 assertTrue(0 == nextMap.size());
274
275 }
276
277
278 @Test
279 public void testRouteRemovedEvent() throws InterruptedException {
280
281 testRouteAddedEvent();
282
283 //Adding the details to create different routes
284 previousSubject = McastRouteUpdate.mcastRouteUpdate(route1, emptySource, sinks);
285 currentSubject = McastRouteUpdate.mcastRouteUpdate(route1, emptySource, sinks);
286 // Creating new mcast event for route removing
287 McastEvent event = new McastEvent(McastEvent.Type.ROUTE_REMOVED, previousSubject, currentSubject);
288 cordMcast.listener.event(event);
289 // There will be no forwarding objective
290 assertAfter(WAIT, WAIT * 2, () -> assertTrue(0 == forwardMap.size()));
291 assertTrue(0 == nextMap.size());
292
293 }
294
295
296 @Test
297 public void testSourceAddedEvent() throws InterruptedException {
298
299 // Adding route before adding source.
300 testRouteAddedEvent();
301
302 //Adding the details to create different routes
303 previousSubject = McastRouteUpdate.mcastRouteUpdate(route1, emptySource, sinks);
304 currentSubject = McastRouteUpdate.mcastRouteUpdate(route1, sources, sinks);
305 // Creating new mcast event for source adding
306 McastEvent event = new McastEvent(McastEvent.Type.SOURCES_ADDED, previousSubject, currentSubject);
307 cordMcast.listener.event(event);
308 // There will be no forwarding objective
309 assertAfter(WAIT, WAIT * 2, () -> assertTrue(0 == forwardMap.size()));
310 assertTrue(0 == nextMap.size());
311
312 }
313
314 @Test
315 public void testSourcesRemovedEvent() throws InterruptedException {
316
317 testSourceAddedEvent();
318
319 //Adding the details to create different routes
320 previousSubject = McastRouteUpdate.mcastRouteUpdate(route1, sources, sinks);
321 currentSubject = McastRouteUpdate.mcastRouteUpdate(route1, emptySource, sinks);
322 // Creating new mcast event for removing source
323 // Warning message of unknown event will be displayed.
324 McastEvent event = new McastEvent(McastEvent.Type.SOURCES_REMOVED, previousSubject, currentSubject);
325 cordMcast.listener.event(event);
326 assertAfter(WAIT, WAIT * 2, () -> assertTrue(0 == forwardMap.size()));
327 assertTrue(0 == nextMap.size());
328 }
329
330}