blob: 4ae2825235318237fee530b2aca7cd79e39d78bb [file] [log] [blame]
Sonal Kasliwala0bbe6c2020-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 */
Daniele Moro8ea9e102020-03-24 18:56:52 +010016package org.opencord.cordmcast.impl;
Sonal Kasliwala0bbe6c2020-01-06 10:46:30 +000017
Sonal Kasliwala0bbe6c2020-01-06 10:46:30 +000018import java.util.List;
19import java.util.Map;
Arjun E Kabf9e6e2020-03-02 10:15:21 +000020import java.util.HashMap;
Sonal Kasliwala0bbe6c2020-01-06 10:46:30 +000021import java.util.Set;
Arjun E Kabf9e6e2020-03-02 10:15:21 +000022import java.util.HashSet;
23import java.util.Arrays;
24import java.util.ArrayList;
25import java.util.Iterator;
Sonal Kasliwala0bbe6c2020-01-06 10:46:30 +000026
Esin Karaman996177c2020-03-05 13:21:09 +000027import org.onlab.packet.Ip4Address;
Sonal Kasliwala0bbe6c2020-01-06 10:46:30 +000028import org.onlab.packet.IpAddress;
Arjun E Kabf9e6e2020-03-02 10:15:21 +000029import org.onlab.packet.VlanId;
Sonal Kasliwala0bbe6c2020-01-06 10:46:30 +000030import org.onosproject.TestApplicationId;
31import org.onosproject.core.ApplicationId;
32import org.onosproject.core.CoreServiceAdapter;
Arjun E Kabf9e6e2020-03-02 10:15:21 +000033import org.onosproject.event.DefaultEventSinkRegistry;
34import org.onosproject.event.Event;
35import org.onosproject.event.EventDeliveryService;
36import org.onosproject.event.EventSink;
Sonal Kasliwala0bbe6c2020-01-06 10:46:30 +000037import org.onosproject.mastership.MastershipServiceAdapter;
38import org.onosproject.mcast.api.McastRoute;
Esin Karaman996177c2020-03-05 13:21:09 +000039import org.onosproject.net.AnnotationKeys;
40import org.onosproject.net.Annotations;
Sonal Kasliwala0bbe6c2020-01-06 10:46:30 +000041import org.onosproject.net.ConnectPoint;
Esin Karaman996177c2020-03-05 13:21:09 +000042import org.onosproject.net.DefaultAnnotations;
43import org.onosproject.net.DefaultDevice;
44import org.onosproject.net.Device;
Sonal Kasliwala0bbe6c2020-01-06 10:46:30 +000045import org.onosproject.net.DeviceId;
46import org.onosproject.net.HostId;
47import org.onosproject.net.PortNumber;
Esin Karaman996177c2020-03-05 13:21:09 +000048import org.onosproject.net.SparseAnnotations;
Arjun E Kabf9e6e2020-03-02 10:15:21 +000049import org.onosproject.net.config.Config;
50import org.onosproject.net.config.NetworkConfigRegistryAdapter;
51import org.onosproject.net.config.basics.McastConfig;
Esin Karaman996177c2020-03-05 13:21:09 +000052import org.onosproject.net.device.DeviceServiceAdapter;
Sonal Kasliwala0bbe6c2020-01-06 10:46:30 +000053import org.onosproject.net.flow.TrafficSelector;
54import org.onosproject.net.flow.TrafficTreatment;
55import org.onosproject.net.flow.criteria.Criterion;
56import org.onosproject.net.flow.criteria.IPCriterion;
Esin Karamane4890012020-04-19 11:58:54 +000057import org.onosproject.net.flow.criteria.VlanIdCriterion;
Sonal Kasliwala0bbe6c2020-01-06 10:46:30 +000058import org.onosproject.net.flow.instructions.Instruction;
59import org.onosproject.net.flow.instructions.Instructions.OutputInstruction;
60import org.onosproject.net.flowobjective.FlowObjectiveServiceAdapter;
61import org.onosproject.net.flowobjective.ForwardingObjective;
62import org.onosproject.net.flowobjective.NextObjective;
Sonal Kasliwala0bbe6c2020-01-06 10:46:30 +000063
Sonal Kasliwala0bbe6c2020-01-06 10:46:30 +000064import com.google.common.collect.ImmutableMap;
Daniele Moro8ea9e102020-03-24 18:56:52 +010065import org.opencord.cordmcast.CordMcastStatisticsEvent;
66import org.opencord.cordmcast.CordMcastStatisticsEventListener;
Esin Karaman996177c2020-03-05 13:21:09 +000067import org.opencord.sadis.BandwidthProfileInformation;
68import org.opencord.sadis.BaseInformationService;
69import org.opencord.sadis.SadisService;
70import org.opencord.sadis.SubscriberAndDeviceInformation;
Sonal Kasliwala0bbe6c2020-01-06 10:46:30 +000071
Arjun E Kabf9e6e2020-03-02 10:15:21 +000072import static com.google.common.base.Preconditions.checkState;
73
Sonal Kasliwala0bbe6c2020-01-06 10:46:30 +000074public class McastTestBase {
75
76 // Map to store the forwardingObjective in flowObjectiveService.forward()
77 Map<DeviceId, ForwardingObjective> forwardMap = new HashMap<>();
78 // Map to store the nextObjective in flowObjectiveService.next()
79 Map<DeviceId, NextObjective> nextMap = new HashMap<>();
80 // Device configuration
81 protected static final DeviceId DEVICE_ID_OF_A = DeviceId.deviceId("of:00000a0a0a0a0a00");
82 // Port number
83 protected static final PortNumber PORT_A = PortNumber.portNumber(1048576);
84 protected static final PortNumber PORT_B = PortNumber.portNumber(16);
85 protected static final PortNumber PORT_C = PortNumber.portNumber(24);
86
87 // Connect Point for creating source and sink
88 protected static final ConnectPoint CONNECT_POINT_A = new ConnectPoint(DEVICE_ID_OF_A, PORT_A);
89 protected static final ConnectPoint CONNECT_POINT_B = new ConnectPoint(DEVICE_ID_OF_A, PORT_B);
90 protected static final ConnectPoint CONNECT_POINT_C = new ConnectPoint(DEVICE_ID_OF_A, PORT_C);
91
Esin Karaman996177c2020-03-05 13:21:09 +000092 // serial number of the device A
93 protected static final String SERIAL_NUMBER_OF_DEVICE_A = "serialNumberOfDevA";
94 // Management ip address of the device A
95 protected static final Ip4Address MANAGEMENT_IP_OF_A = Ip4Address.valueOf("10.177.125.4");
Sonal Kasliwala0bbe6c2020-01-06 10:46:30 +000096 //Host id configuration
97 protected static final HostId HOST_ID_NONE = HostId.NONE;
98 // Source connect point
99 protected static final Set<ConnectPoint> SOURCES_CP = new HashSet<ConnectPoint>(Arrays.asList(CONNECT_POINT_A));
100 Map<HostId, Set<ConnectPoint>> sources = ImmutableMap.of(HOST_ID_NONE, SOURCES_CP);
101
102 protected static final IpAddress MULTICAST_IP = IpAddress.valueOf("224.0.0.22");
Arjun E Kabf9e6e2020-03-02 10:15:21 +0000103 protected static final IpAddress SOURCE_IP = IpAddress.valueOf("192.168.1.1");
Sonal Kasliwala0bbe6c2020-01-06 10:46:30 +0000104 // Creating dummy route with IGMP type.
Arjun E Kabf9e6e2020-03-02 10:15:21 +0000105 McastRoute route1 = new McastRoute(SOURCE_IP, MULTICAST_IP, McastRoute.Type.IGMP);
Sonal Kasliwala0bbe6c2020-01-06 10:46:30 +0000106
107 // Creating empty sink used in prevRoute
108 Set<ConnectPoint> sinksCp = new HashSet<ConnectPoint>(Arrays.asList());
109 Map<HostId, Set<ConnectPoint>> sinks = ImmutableMap.of(HOST_ID_NONE, sinksCp);
110
111 // Creating empty source
112 Set<ConnectPoint> sourceCp = new HashSet<ConnectPoint>(Arrays.asList());
113 Map<HostId, Set<ConnectPoint>> emptySource = ImmutableMap.of(HOST_ID_NONE, sourceCp);
114
115 // Flag to check unknown olt device
116 boolean knownOltFlag = false;
117
Arjun E Kabf9e6e2020-03-02 10:15:21 +0000118 // For the tests reduce events period to 1s
119 protected static final int EVENT_GENERATION_PERIOD = 1;
120
Sonal Kasliwala0bbe6c2020-01-06 10:46:30 +0000121 class MockCoreService extends CoreServiceAdapter {
122 @Override
123 public ApplicationId registerApplication(String name) {
124 ApplicationId testApplicationId = TestApplicationId.create("org.opencord.cordmcast");
125 return testApplicationId;
126 }
127 }
128
129 class MockFlowObjectiveService extends FlowObjectiveServiceAdapter {
130 @Override
131 public void forward(DeviceId deviceId, ForwardingObjective forwardingObjective) {
132 synchronized (forwardMap) {
133 forwardMap.put(deviceId, forwardingObjective);
134 forwardMap.notify();
135 }
136 }
137
138 @Override
139 public void next(DeviceId deviceId, NextObjective nextObjective) {
140 nextMap.put(deviceId, nextObjective);
141 }
142 }
143
144 class TestMastershipService extends MastershipServiceAdapter {
145 @Override
146 public boolean isLocalMaster(DeviceId deviceId) {
147 return true;
148 }
149 }
150
Esin Karaman996177c2020-03-05 13:21:09 +0000151 protected class MockSadisService implements SadisService {
Sonal Kasliwala0bbe6c2020-01-06 10:46:30 +0000152
Esin Karaman996177c2020-03-05 13:21:09 +0000153 @Override
154 public BaseInformationService<SubscriberAndDeviceInformation> getSubscriberInfoService() {
155 return new MockSubService();
156 }
Sonal Kasliwala0bbe6c2020-01-06 10:46:30 +0000157
Esin Karaman996177c2020-03-05 13:21:09 +0000158 @Override
159 public BaseInformationService<BandwidthProfileInformation> getBandwidthProfileService() {
160 return null;
161 }
162 }
Sonal Kasliwala0bbe6c2020-01-06 10:46:30 +0000163
Arjun E Kabf9e6e2020-03-02 10:15:21 +0000164 /**
165 * Mocks the McastConfig class to return vlan id value.
166 */
167 static class MockMcastConfig extends McastConfig {
Esin Karamane4890012020-04-19 11:58:54 +0000168 private VlanId egressVlan;
169 private VlanId egressInnerVlan;
170
171 public MockMcastConfig(VlanId egressVlan, VlanId egressInnerVlan) {
172 this.egressVlan = egressVlan;
173 this.egressInnerVlan = egressInnerVlan;
174 }
175
Arjun E Kabf9e6e2020-03-02 10:15:21 +0000176 @Override
177 public VlanId egressVlan() {
Esin Karamane4890012020-04-19 11:58:54 +0000178 return egressVlan;
Arjun E Kabf9e6e2020-03-02 10:15:21 +0000179 }
Esin Karamanbb35a3b2020-03-18 13:53:24 +0000180
181 @Override
182 public VlanId egressInnerVlan() {
Esin Karamane4890012020-04-19 11:58:54 +0000183 return egressInnerVlan;
Esin Karamanbb35a3b2020-03-18 13:53:24 +0000184 }
Arjun E Kabf9e6e2020-03-02 10:15:21 +0000185 }
186
187 /**
188 * Mocks the network config registry.
189 */
190 @SuppressWarnings("unchecked")
191 static final class TestNetworkConfigRegistry
192 extends NetworkConfigRegistryAdapter {
Esin Karamane4890012020-04-19 11:58:54 +0000193
194 private VlanId egressVlan = VlanId.vlanId("4000");
195 private VlanId egressInnerVlan = VlanId.NONE;
196
197 public void setEgressVlan(VlanId egressVlan) {
198 this.egressVlan = egressVlan;
199 }
200
201 public void setEgressInnerVlan(VlanId egressInnerVlan) {
202 this.egressInnerVlan = egressInnerVlan;
203 }
204
Arjun E Kabf9e6e2020-03-02 10:15:21 +0000205 @Override
206 public <S, C extends Config<S>> C getConfig(S subject, Class<C> configClass) {
Esin Karamane4890012020-04-19 11:58:54 +0000207 McastConfig mcastConfig = new MockMcastConfig(egressVlan, egressInnerVlan);
Arjun E Kabf9e6e2020-03-02 10:15:21 +0000208 return (C) mcastConfig;
209 }
210 }
211
212 public static class TestEventDispatcher extends DefaultEventSinkRegistry
213 implements EventDeliveryService {
214
215 @Override
216 @SuppressWarnings("unchecked")
217 public synchronized void post(Event event) {
218 EventSink sink = getSink(event.getClass());
219 checkState(sink != null, "No sink for event %s", event);
220 sink.process(event);
221 }
222
223 @Override
224 public void setDispatchTimeLimit(long millis) {
225
226 }
227
228 @Override
229 public long getDispatchTimeLimit() {
230 return 0;
231 }
232 }
233
234 public static class MockCordMcastStatisticsEventListener implements CordMcastStatisticsEventListener {
235 protected List<CordMcastStatisticsEvent> mcastEventList = new ArrayList<CordMcastStatisticsEvent>();
236
237 @Override
238 public void event(CordMcastStatisticsEvent event) {
239 mcastEventList.add(event);
240 }
241 }
242
Esin Karaman996177c2020-03-05 13:21:09 +0000243 private class MockSubService implements BaseInformationService<SubscriberAndDeviceInformation> {
244 MockSubscriberAndDeviceInformation deviceA =
245 new MockSubscriberAndDeviceInformation(SERIAL_NUMBER_OF_DEVICE_A, MANAGEMENT_IP_OF_A);
Sonal Kasliwala0bbe6c2020-01-06 10:46:30 +0000246
Esin Karaman996177c2020-03-05 13:21:09 +0000247 @Override
248 public SubscriberAndDeviceInformation get(String id) {
249 return SERIAL_NUMBER_OF_DEVICE_A.equals(id) ? deviceA : null;
250 }
Sonal Kasliwala0bbe6c2020-01-06 10:46:30 +0000251
Esin Karaman996177c2020-03-05 13:21:09 +0000252 @Override
253 public void invalidateAll() {
254 }
Sonal Kasliwala0bbe6c2020-01-06 10:46:30 +0000255
Esin Karaman996177c2020-03-05 13:21:09 +0000256 @Override
257 public void invalidateId(String id) {
258 }
Sonal Kasliwala0bbe6c2020-01-06 10:46:30 +0000259
Esin Karaman996177c2020-03-05 13:21:09 +0000260 @Override
261 public SubscriberAndDeviceInformation getfromCache(String id) {
262 return null;
263 }
264 }
Sonal Kasliwala0bbe6c2020-01-06 10:46:30 +0000265
Esin Karaman996177c2020-03-05 13:21:09 +0000266 private class MockSubscriberAndDeviceInformation extends SubscriberAndDeviceInformation {
Sonal Kasliwala0bbe6c2020-01-06 10:46:30 +0000267
Esin Karaman996177c2020-03-05 13:21:09 +0000268 MockSubscriberAndDeviceInformation(String id, Ip4Address ipAddress) {
269 this.setId(id);
270 this.setIPAddress(ipAddress);
271 this.setUplinkPort((int) PORT_A.toLong());
272 }
273 }
Sonal Kasliwala0bbe6c2020-01-06 10:46:30 +0000274
Esin Karaman996177c2020-03-05 13:21:09 +0000275 class MockDeviceService extends DeviceServiceAdapter {
Sonal Kasliwala0bbe6c2020-01-06 10:46:30 +0000276
Esin Karaman996177c2020-03-05 13:21:09 +0000277 @Override
278 public Device getDevice(DeviceId deviceId) {
279 if (DEVICE_ID_OF_A.equals(deviceId)) {
280 DefaultAnnotations.Builder annotationsBuilder = DefaultAnnotations.builder()
281 .set(AnnotationKeys.MANAGEMENT_ADDRESS, MANAGEMENT_IP_OF_A.toString());
282 SparseAnnotations annotations = annotationsBuilder.build();
283 Annotations[] da = {annotations};
284
285 Device deviceA = new DefaultDevice(null, DEVICE_ID_OF_A, Device.Type.OTHER, "", "",
286 "", SERIAL_NUMBER_OF_DEVICE_A, null, da);
287 return deviceA;
288 } else {
289 knownOltFlag = true;
290 }
291 return null;
292 }
293 }
Sonal Kasliwala0bbe6c2020-01-06 10:46:30 +0000294
295 public OutputInstruction outputPort(TrafficTreatment trafficTreatment) {
296 List<Instruction> listOfInstructions = trafficTreatment.allInstructions();
297 OutputInstruction output = null;
298 for (Instruction intruction : listOfInstructions) {
299 output = (OutputInstruction) intruction;
300 }
301 return output;
302 }
303
304 public IPCriterion ipAddress(TrafficSelector trafficSelector) {
305 Set<Criterion> criterionSet = trafficSelector.criteria();
306 Iterator<Criterion> it = criterionSet.iterator();
307 IPCriterion ipCriterion = null;
308 while (it.hasNext()) {
309 Criterion criteria = it.next();
310 if (Criterion.Type.IPV4_DST == criteria.type()) {
311 ipCriterion = (IPCriterion) criteria;
312 }
313 }
314 return (IPCriterion) ipCriterion;
315 }
316
Esin Karamane4890012020-04-19 11:58:54 +0000317 public VlanIdCriterion vlanId(TrafficSelector trafficSelector, Criterion.Type type) {
318 Set<Criterion> criterionSet = trafficSelector.criteria();
319 Iterator<Criterion> it = criterionSet.iterator();
320 VlanIdCriterion criterion = null;
321 while (it.hasNext()) {
322 Criterion criteria = it.next();
323 if (type == criteria.type()) {
324 criterion = (VlanIdCriterion) criteria;
325 }
326 }
327 return criterion;
328 }
Sonal Kasliwala0bbe6c2020-01-06 10:46:30 +0000329}