blob: 214a28306d4156049f911e5e50434d15ac225252 [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;
Ilayda Ozdemir0414dde2020-06-27 23:18:07 +000031import org.onosproject.cluster.LeadershipServiceAdapter;
32import org.onosproject.cluster.NodeId;
Sonal Kasliwala0bbe6c2020-01-06 10:46:30 +000033import org.onosproject.core.ApplicationId;
34import org.onosproject.core.CoreServiceAdapter;
Arjun E Kabf9e6e2020-03-02 10:15:21 +000035import org.onosproject.event.DefaultEventSinkRegistry;
36import org.onosproject.event.Event;
37import org.onosproject.event.EventDeliveryService;
38import org.onosproject.event.EventSink;
Sonal Kasliwala0bbe6c2020-01-06 10:46:30 +000039import org.onosproject.mastership.MastershipServiceAdapter;
40import org.onosproject.mcast.api.McastRoute;
Esin Karaman996177c2020-03-05 13:21:09 +000041import org.onosproject.net.AnnotationKeys;
42import org.onosproject.net.Annotations;
Sonal Kasliwala0bbe6c2020-01-06 10:46:30 +000043import org.onosproject.net.ConnectPoint;
Esin Karaman996177c2020-03-05 13:21:09 +000044import org.onosproject.net.DefaultAnnotations;
45import org.onosproject.net.DefaultDevice;
46import org.onosproject.net.Device;
Sonal Kasliwala0bbe6c2020-01-06 10:46:30 +000047import org.onosproject.net.DeviceId;
48import org.onosproject.net.HostId;
49import org.onosproject.net.PortNumber;
Esin Karaman996177c2020-03-05 13:21:09 +000050import org.onosproject.net.SparseAnnotations;
Arjun E Kabf9e6e2020-03-02 10:15:21 +000051import org.onosproject.net.config.Config;
52import org.onosproject.net.config.NetworkConfigRegistryAdapter;
53import org.onosproject.net.config.basics.McastConfig;
Esin Karaman996177c2020-03-05 13:21:09 +000054import org.onosproject.net.device.DeviceServiceAdapter;
Sonal Kasliwala0bbe6c2020-01-06 10:46:30 +000055import org.onosproject.net.flow.TrafficSelector;
56import org.onosproject.net.flow.TrafficTreatment;
57import org.onosproject.net.flow.criteria.Criterion;
58import org.onosproject.net.flow.criteria.IPCriterion;
Esin Karamane4890012020-04-19 11:58:54 +000059import org.onosproject.net.flow.criteria.VlanIdCriterion;
Sonal Kasliwala0bbe6c2020-01-06 10:46:30 +000060import org.onosproject.net.flow.instructions.Instruction;
61import org.onosproject.net.flow.instructions.Instructions.OutputInstruction;
62import org.onosproject.net.flowobjective.FlowObjectiveServiceAdapter;
63import org.onosproject.net.flowobjective.ForwardingObjective;
64import org.onosproject.net.flowobjective.NextObjective;
Sonal Kasliwala0bbe6c2020-01-06 10:46:30 +000065
Sonal Kasliwala0bbe6c2020-01-06 10:46:30 +000066import com.google.common.collect.ImmutableMap;
Daniele Moro8ea9e102020-03-24 18:56:52 +010067import org.opencord.cordmcast.CordMcastStatisticsEvent;
68import org.opencord.cordmcast.CordMcastStatisticsEventListener;
Esin Karaman996177c2020-03-05 13:21:09 +000069import org.opencord.sadis.BandwidthProfileInformation;
70import org.opencord.sadis.BaseInformationService;
71import org.opencord.sadis.SadisService;
72import org.opencord.sadis.SubscriberAndDeviceInformation;
Sonal Kasliwala0bbe6c2020-01-06 10:46:30 +000073
Arjun E Kabf9e6e2020-03-02 10:15:21 +000074import static com.google.common.base.Preconditions.checkState;
75
Sonal Kasliwala0bbe6c2020-01-06 10:46:30 +000076public class McastTestBase {
77
78 // Map to store the forwardingObjective in flowObjectiveService.forward()
79 Map<DeviceId, ForwardingObjective> forwardMap = new HashMap<>();
80 // Map to store the nextObjective in flowObjectiveService.next()
81 Map<DeviceId, NextObjective> nextMap = new HashMap<>();
82 // Device configuration
83 protected static final DeviceId DEVICE_ID_OF_A = DeviceId.deviceId("of:00000a0a0a0a0a00");
84 // Port number
85 protected static final PortNumber PORT_A = PortNumber.portNumber(1048576);
86 protected static final PortNumber PORT_B = PortNumber.portNumber(16);
87 protected static final PortNumber PORT_C = PortNumber.portNumber(24);
88
89 // Connect Point for creating source and sink
90 protected static final ConnectPoint CONNECT_POINT_A = new ConnectPoint(DEVICE_ID_OF_A, PORT_A);
91 protected static final ConnectPoint CONNECT_POINT_B = new ConnectPoint(DEVICE_ID_OF_A, PORT_B);
92 protected static final ConnectPoint CONNECT_POINT_C = new ConnectPoint(DEVICE_ID_OF_A, PORT_C);
93
Esin Karaman996177c2020-03-05 13:21:09 +000094 // serial number of the device A
95 protected static final String SERIAL_NUMBER_OF_DEVICE_A = "serialNumberOfDevA";
96 // Management ip address of the device A
97 protected static final Ip4Address MANAGEMENT_IP_OF_A = Ip4Address.valueOf("10.177.125.4");
Sonal Kasliwala0bbe6c2020-01-06 10:46:30 +000098 //Host id configuration
99 protected static final HostId HOST_ID_NONE = HostId.NONE;
100 // Source connect point
101 protected static final Set<ConnectPoint> SOURCES_CP = new HashSet<ConnectPoint>(Arrays.asList(CONNECT_POINT_A));
102 Map<HostId, Set<ConnectPoint>> sources = ImmutableMap.of(HOST_ID_NONE, SOURCES_CP);
103
104 protected static final IpAddress MULTICAST_IP = IpAddress.valueOf("224.0.0.22");
Arjun E Kabf9e6e2020-03-02 10:15:21 +0000105 protected static final IpAddress SOURCE_IP = IpAddress.valueOf("192.168.1.1");
Sonal Kasliwala0bbe6c2020-01-06 10:46:30 +0000106 // Creating dummy route with IGMP type.
Arjun E Kabf9e6e2020-03-02 10:15:21 +0000107 McastRoute route1 = new McastRoute(SOURCE_IP, MULTICAST_IP, McastRoute.Type.IGMP);
Sonal Kasliwala0bbe6c2020-01-06 10:46:30 +0000108
109 // Creating empty sink used in prevRoute
110 Set<ConnectPoint> sinksCp = new HashSet<ConnectPoint>(Arrays.asList());
111 Map<HostId, Set<ConnectPoint>> sinks = ImmutableMap.of(HOST_ID_NONE, sinksCp);
112
113 // Creating empty source
114 Set<ConnectPoint> sourceCp = new HashSet<ConnectPoint>(Arrays.asList());
115 Map<HostId, Set<ConnectPoint>> emptySource = ImmutableMap.of(HOST_ID_NONE, sourceCp);
116
117 // Flag to check unknown olt device
118 boolean knownOltFlag = false;
119
Arjun E Kabf9e6e2020-03-02 10:15:21 +0000120 // For the tests reduce events period to 1s
121 protected static final int EVENT_GENERATION_PERIOD = 1;
122
Sonal Kasliwala0bbe6c2020-01-06 10:46:30 +0000123 class MockCoreService extends CoreServiceAdapter {
124 @Override
125 public ApplicationId registerApplication(String name) {
126 ApplicationId testApplicationId = TestApplicationId.create("org.opencord.cordmcast");
127 return testApplicationId;
128 }
129 }
130
131 class MockFlowObjectiveService extends FlowObjectiveServiceAdapter {
132 @Override
133 public void forward(DeviceId deviceId, ForwardingObjective forwardingObjective) {
134 synchronized (forwardMap) {
135 forwardMap.put(deviceId, forwardingObjective);
Andrea Campanella03652352020-05-06 16:12:00 +0200136 forwardingObjective.context().ifPresent(context -> {
137 context.onSuccess(forwardingObjective);
138 });
Sonal Kasliwala0bbe6c2020-01-06 10:46:30 +0000139 forwardMap.notify();
140 }
141 }
142
143 @Override
144 public void next(DeviceId deviceId, NextObjective nextObjective) {
145 nextMap.put(deviceId, nextObjective);
146 }
147 }
148
149 class TestMastershipService extends MastershipServiceAdapter {
150 @Override
151 public boolean isLocalMaster(DeviceId deviceId) {
152 return true;
153 }
154 }
155
Ilayda Ozdemir0414dde2020-06-27 23:18:07 +0000156 class LeadershipServiceMcastAdapter extends LeadershipServiceAdapter {
157 @Override
158 public NodeId getLeader(String path) {
159 return NodeId.nodeId("local");
160 }
161 }
162
Esin Karaman996177c2020-03-05 13:21:09 +0000163 protected class MockSadisService implements SadisService {
Sonal Kasliwala0bbe6c2020-01-06 10:46:30 +0000164
Esin Karaman996177c2020-03-05 13:21:09 +0000165 @Override
166 public BaseInformationService<SubscriberAndDeviceInformation> getSubscriberInfoService() {
167 return new MockSubService();
168 }
Sonal Kasliwala0bbe6c2020-01-06 10:46:30 +0000169
Esin Karaman996177c2020-03-05 13:21:09 +0000170 @Override
171 public BaseInformationService<BandwidthProfileInformation> getBandwidthProfileService() {
172 return null;
173 }
174 }
Sonal Kasliwala0bbe6c2020-01-06 10:46:30 +0000175
Arjun E Kabf9e6e2020-03-02 10:15:21 +0000176 /**
177 * Mocks the McastConfig class to return vlan id value.
178 */
179 static class MockMcastConfig extends McastConfig {
Esin Karamane4890012020-04-19 11:58:54 +0000180 private VlanId egressVlan;
181 private VlanId egressInnerVlan;
182
183 public MockMcastConfig(VlanId egressVlan, VlanId egressInnerVlan) {
184 this.egressVlan = egressVlan;
185 this.egressInnerVlan = egressInnerVlan;
186 }
187
Arjun E Kabf9e6e2020-03-02 10:15:21 +0000188 @Override
189 public VlanId egressVlan() {
Esin Karamane4890012020-04-19 11:58:54 +0000190 return egressVlan;
Arjun E Kabf9e6e2020-03-02 10:15:21 +0000191 }
Esin Karamanbb35a3b2020-03-18 13:53:24 +0000192
193 @Override
194 public VlanId egressInnerVlan() {
Esin Karamane4890012020-04-19 11:58:54 +0000195 return egressInnerVlan;
Esin Karamanbb35a3b2020-03-18 13:53:24 +0000196 }
Arjun E Kabf9e6e2020-03-02 10:15:21 +0000197 }
198
199 /**
200 * Mocks the network config registry.
201 */
202 @SuppressWarnings("unchecked")
203 static final class TestNetworkConfigRegistry
204 extends NetworkConfigRegistryAdapter {
Esin Karamane4890012020-04-19 11:58:54 +0000205
206 private VlanId egressVlan = VlanId.vlanId("4000");
207 private VlanId egressInnerVlan = VlanId.NONE;
208
209 public void setEgressVlan(VlanId egressVlan) {
210 this.egressVlan = egressVlan;
211 }
212
213 public void setEgressInnerVlan(VlanId egressInnerVlan) {
214 this.egressInnerVlan = egressInnerVlan;
215 }
216
Arjun E Kabf9e6e2020-03-02 10:15:21 +0000217 @Override
218 public <S, C extends Config<S>> C getConfig(S subject, Class<C> configClass) {
Esin Karamane4890012020-04-19 11:58:54 +0000219 McastConfig mcastConfig = new MockMcastConfig(egressVlan, egressInnerVlan);
Arjun E Kabf9e6e2020-03-02 10:15:21 +0000220 return (C) mcastConfig;
221 }
222 }
223
224 public static class TestEventDispatcher extends DefaultEventSinkRegistry
225 implements EventDeliveryService {
226
227 @Override
228 @SuppressWarnings("unchecked")
229 public synchronized void post(Event event) {
230 EventSink sink = getSink(event.getClass());
231 checkState(sink != null, "No sink for event %s", event);
232 sink.process(event);
233 }
234
235 @Override
236 public void setDispatchTimeLimit(long millis) {
237
238 }
239
240 @Override
241 public long getDispatchTimeLimit() {
242 return 0;
243 }
244 }
245
246 public static class MockCordMcastStatisticsEventListener implements CordMcastStatisticsEventListener {
247 protected List<CordMcastStatisticsEvent> mcastEventList = new ArrayList<CordMcastStatisticsEvent>();
248
249 @Override
250 public void event(CordMcastStatisticsEvent event) {
251 mcastEventList.add(event);
252 }
253 }
254
Esin Karaman996177c2020-03-05 13:21:09 +0000255 private class MockSubService implements BaseInformationService<SubscriberAndDeviceInformation> {
256 MockSubscriberAndDeviceInformation deviceA =
257 new MockSubscriberAndDeviceInformation(SERIAL_NUMBER_OF_DEVICE_A, MANAGEMENT_IP_OF_A);
Sonal Kasliwala0bbe6c2020-01-06 10:46:30 +0000258
Esin Karaman996177c2020-03-05 13:21:09 +0000259 @Override
260 public SubscriberAndDeviceInformation get(String id) {
261 return SERIAL_NUMBER_OF_DEVICE_A.equals(id) ? deviceA : null;
262 }
Sonal Kasliwala0bbe6c2020-01-06 10:46:30 +0000263
Esin Karaman996177c2020-03-05 13:21:09 +0000264 @Override
Ilayda Ozdemir2fccbce2021-02-23 15:36:47 +0000265 public void clearLocalData() {
266 }
267
268 @Override
Esin Karaman996177c2020-03-05 13:21:09 +0000269 public void invalidateAll() {
270 }
Sonal Kasliwala0bbe6c2020-01-06 10:46:30 +0000271
Esin Karaman996177c2020-03-05 13:21:09 +0000272 @Override
273 public void invalidateId(String id) {
274 }
Sonal Kasliwala0bbe6c2020-01-06 10:46:30 +0000275
Esin Karaman996177c2020-03-05 13:21:09 +0000276 @Override
277 public SubscriberAndDeviceInformation getfromCache(String id) {
278 return null;
279 }
280 }
Sonal Kasliwala0bbe6c2020-01-06 10:46:30 +0000281
Esin Karaman996177c2020-03-05 13:21:09 +0000282 private class MockSubscriberAndDeviceInformation extends SubscriberAndDeviceInformation {
Sonal Kasliwala0bbe6c2020-01-06 10:46:30 +0000283
Esin Karaman996177c2020-03-05 13:21:09 +0000284 MockSubscriberAndDeviceInformation(String id, Ip4Address ipAddress) {
285 this.setId(id);
286 this.setIPAddress(ipAddress);
287 this.setUplinkPort((int) PORT_A.toLong());
288 }
289 }
Sonal Kasliwala0bbe6c2020-01-06 10:46:30 +0000290
Esin Karaman996177c2020-03-05 13:21:09 +0000291 class MockDeviceService extends DeviceServiceAdapter {
Sonal Kasliwala0bbe6c2020-01-06 10:46:30 +0000292
Esin Karaman996177c2020-03-05 13:21:09 +0000293 @Override
294 public Device getDevice(DeviceId deviceId) {
295 if (DEVICE_ID_OF_A.equals(deviceId)) {
296 DefaultAnnotations.Builder annotationsBuilder = DefaultAnnotations.builder()
297 .set(AnnotationKeys.MANAGEMENT_ADDRESS, MANAGEMENT_IP_OF_A.toString());
298 SparseAnnotations annotations = annotationsBuilder.build();
299 Annotations[] da = {annotations};
300
301 Device deviceA = new DefaultDevice(null, DEVICE_ID_OF_A, Device.Type.OTHER, "", "",
302 "", SERIAL_NUMBER_OF_DEVICE_A, null, da);
303 return deviceA;
304 } else {
305 knownOltFlag = true;
306 }
307 return null;
308 }
Andrea Campanellab9f72b52021-05-06 12:58:32 +0200309
310 @Override
311 public boolean isAvailable(DeviceId deviceId) {
312 return true;
313 }
Esin Karaman996177c2020-03-05 13:21:09 +0000314 }
Sonal Kasliwala0bbe6c2020-01-06 10:46:30 +0000315
316 public OutputInstruction outputPort(TrafficTreatment trafficTreatment) {
317 List<Instruction> listOfInstructions = trafficTreatment.allInstructions();
318 OutputInstruction output = null;
319 for (Instruction intruction : listOfInstructions) {
320 output = (OutputInstruction) intruction;
321 }
322 return output;
323 }
324
325 public IPCriterion ipAddress(TrafficSelector trafficSelector) {
326 Set<Criterion> criterionSet = trafficSelector.criteria();
327 Iterator<Criterion> it = criterionSet.iterator();
328 IPCriterion ipCriterion = null;
329 while (it.hasNext()) {
330 Criterion criteria = it.next();
331 if (Criterion.Type.IPV4_DST == criteria.type()) {
332 ipCriterion = (IPCriterion) criteria;
333 }
334 }
335 return (IPCriterion) ipCriterion;
336 }
337
Esin Karamane4890012020-04-19 11:58:54 +0000338 public VlanIdCriterion vlanId(TrafficSelector trafficSelector, Criterion.Type type) {
339 Set<Criterion> criterionSet = trafficSelector.criteria();
340 Iterator<Criterion> it = criterionSet.iterator();
341 VlanIdCriterion criterion = null;
342 while (it.hasNext()) {
343 Criterion criteria = it.next();
344 if (type == criteria.type()) {
345 criterion = (VlanIdCriterion) criteria;
346 }
347 }
348 return criterion;
349 }
Sonal Kasliwala0bbe6c2020-01-06 10:46:30 +0000350}