blob: 927197666932e7784be85e97a669183c4a255c82 [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);
Andrea Campanella03652352020-05-06 16:12:00 +0200134 forwardingObjective.context().ifPresent(context -> {
135 context.onSuccess(forwardingObjective);
136 });
Sonal Kasliwala0bbe6c2020-01-06 10:46:30 +0000137 forwardMap.notify();
138 }
139 }
140
141 @Override
142 public void next(DeviceId deviceId, NextObjective nextObjective) {
143 nextMap.put(deviceId, nextObjective);
144 }
145 }
146
147 class TestMastershipService extends MastershipServiceAdapter {
148 @Override
149 public boolean isLocalMaster(DeviceId deviceId) {
150 return true;
151 }
152 }
153
Esin Karaman996177c2020-03-05 13:21:09 +0000154 protected class MockSadisService implements SadisService {
Sonal Kasliwala0bbe6c2020-01-06 10:46:30 +0000155
Esin Karaman996177c2020-03-05 13:21:09 +0000156 @Override
157 public BaseInformationService<SubscriberAndDeviceInformation> getSubscriberInfoService() {
158 return new MockSubService();
159 }
Sonal Kasliwala0bbe6c2020-01-06 10:46:30 +0000160
Esin Karaman996177c2020-03-05 13:21:09 +0000161 @Override
162 public BaseInformationService<BandwidthProfileInformation> getBandwidthProfileService() {
163 return null;
164 }
165 }
Sonal Kasliwala0bbe6c2020-01-06 10:46:30 +0000166
Arjun E Kabf9e6e2020-03-02 10:15:21 +0000167 /**
168 * Mocks the McastConfig class to return vlan id value.
169 */
170 static class MockMcastConfig extends McastConfig {
Esin Karamane4890012020-04-19 11:58:54 +0000171 private VlanId egressVlan;
172 private VlanId egressInnerVlan;
173
174 public MockMcastConfig(VlanId egressVlan, VlanId egressInnerVlan) {
175 this.egressVlan = egressVlan;
176 this.egressInnerVlan = egressInnerVlan;
177 }
178
Arjun E Kabf9e6e2020-03-02 10:15:21 +0000179 @Override
180 public VlanId egressVlan() {
Esin Karamane4890012020-04-19 11:58:54 +0000181 return egressVlan;
Arjun E Kabf9e6e2020-03-02 10:15:21 +0000182 }
Esin Karamanbb35a3b2020-03-18 13:53:24 +0000183
184 @Override
185 public VlanId egressInnerVlan() {
Esin Karamane4890012020-04-19 11:58:54 +0000186 return egressInnerVlan;
Esin Karamanbb35a3b2020-03-18 13:53:24 +0000187 }
Arjun E Kabf9e6e2020-03-02 10:15:21 +0000188 }
189
190 /**
191 * Mocks the network config registry.
192 */
193 @SuppressWarnings("unchecked")
194 static final class TestNetworkConfigRegistry
195 extends NetworkConfigRegistryAdapter {
Esin Karamane4890012020-04-19 11:58:54 +0000196
197 private VlanId egressVlan = VlanId.vlanId("4000");
198 private VlanId egressInnerVlan = VlanId.NONE;
199
200 public void setEgressVlan(VlanId egressVlan) {
201 this.egressVlan = egressVlan;
202 }
203
204 public void setEgressInnerVlan(VlanId egressInnerVlan) {
205 this.egressInnerVlan = egressInnerVlan;
206 }
207
Arjun E Kabf9e6e2020-03-02 10:15:21 +0000208 @Override
209 public <S, C extends Config<S>> C getConfig(S subject, Class<C> configClass) {
Esin Karamane4890012020-04-19 11:58:54 +0000210 McastConfig mcastConfig = new MockMcastConfig(egressVlan, egressInnerVlan);
Arjun E Kabf9e6e2020-03-02 10:15:21 +0000211 return (C) mcastConfig;
212 }
213 }
214
215 public static class TestEventDispatcher extends DefaultEventSinkRegistry
216 implements EventDeliveryService {
217
218 @Override
219 @SuppressWarnings("unchecked")
220 public synchronized void post(Event event) {
221 EventSink sink = getSink(event.getClass());
222 checkState(sink != null, "No sink for event %s", event);
223 sink.process(event);
224 }
225
226 @Override
227 public void setDispatchTimeLimit(long millis) {
228
229 }
230
231 @Override
232 public long getDispatchTimeLimit() {
233 return 0;
234 }
235 }
236
237 public static class MockCordMcastStatisticsEventListener implements CordMcastStatisticsEventListener {
238 protected List<CordMcastStatisticsEvent> mcastEventList = new ArrayList<CordMcastStatisticsEvent>();
239
240 @Override
241 public void event(CordMcastStatisticsEvent event) {
242 mcastEventList.add(event);
243 }
244 }
245
Esin Karaman996177c2020-03-05 13:21:09 +0000246 private class MockSubService implements BaseInformationService<SubscriberAndDeviceInformation> {
247 MockSubscriberAndDeviceInformation deviceA =
248 new MockSubscriberAndDeviceInformation(SERIAL_NUMBER_OF_DEVICE_A, MANAGEMENT_IP_OF_A);
Sonal Kasliwala0bbe6c2020-01-06 10:46:30 +0000249
Esin Karaman996177c2020-03-05 13:21:09 +0000250 @Override
251 public SubscriberAndDeviceInformation get(String id) {
252 return SERIAL_NUMBER_OF_DEVICE_A.equals(id) ? deviceA : null;
253 }
Sonal Kasliwala0bbe6c2020-01-06 10:46:30 +0000254
Esin Karaman996177c2020-03-05 13:21:09 +0000255 @Override
256 public void invalidateAll() {
257 }
Sonal Kasliwala0bbe6c2020-01-06 10:46:30 +0000258
Esin Karaman996177c2020-03-05 13:21:09 +0000259 @Override
260 public void invalidateId(String id) {
261 }
Sonal Kasliwala0bbe6c2020-01-06 10:46:30 +0000262
Esin Karaman996177c2020-03-05 13:21:09 +0000263 @Override
264 public SubscriberAndDeviceInformation getfromCache(String id) {
265 return null;
266 }
267 }
Sonal Kasliwala0bbe6c2020-01-06 10:46:30 +0000268
Esin Karaman996177c2020-03-05 13:21:09 +0000269 private class MockSubscriberAndDeviceInformation extends SubscriberAndDeviceInformation {
Sonal Kasliwala0bbe6c2020-01-06 10:46:30 +0000270
Esin Karaman996177c2020-03-05 13:21:09 +0000271 MockSubscriberAndDeviceInformation(String id, Ip4Address ipAddress) {
272 this.setId(id);
273 this.setIPAddress(ipAddress);
274 this.setUplinkPort((int) PORT_A.toLong());
275 }
276 }
Sonal Kasliwala0bbe6c2020-01-06 10:46:30 +0000277
Esin Karaman996177c2020-03-05 13:21:09 +0000278 class MockDeviceService extends DeviceServiceAdapter {
Sonal Kasliwala0bbe6c2020-01-06 10:46:30 +0000279
Esin Karaman996177c2020-03-05 13:21:09 +0000280 @Override
281 public Device getDevice(DeviceId deviceId) {
282 if (DEVICE_ID_OF_A.equals(deviceId)) {
283 DefaultAnnotations.Builder annotationsBuilder = DefaultAnnotations.builder()
284 .set(AnnotationKeys.MANAGEMENT_ADDRESS, MANAGEMENT_IP_OF_A.toString());
285 SparseAnnotations annotations = annotationsBuilder.build();
286 Annotations[] da = {annotations};
287
288 Device deviceA = new DefaultDevice(null, DEVICE_ID_OF_A, Device.Type.OTHER, "", "",
289 "", SERIAL_NUMBER_OF_DEVICE_A, null, da);
290 return deviceA;
291 } else {
292 knownOltFlag = true;
293 }
294 return null;
295 }
296 }
Sonal Kasliwala0bbe6c2020-01-06 10:46:30 +0000297
298 public OutputInstruction outputPort(TrafficTreatment trafficTreatment) {
299 List<Instruction> listOfInstructions = trafficTreatment.allInstructions();
300 OutputInstruction output = null;
301 for (Instruction intruction : listOfInstructions) {
302 output = (OutputInstruction) intruction;
303 }
304 return output;
305 }
306
307 public IPCriterion ipAddress(TrafficSelector trafficSelector) {
308 Set<Criterion> criterionSet = trafficSelector.criteria();
309 Iterator<Criterion> it = criterionSet.iterator();
310 IPCriterion ipCriterion = null;
311 while (it.hasNext()) {
312 Criterion criteria = it.next();
313 if (Criterion.Type.IPV4_DST == criteria.type()) {
314 ipCriterion = (IPCriterion) criteria;
315 }
316 }
317 return (IPCriterion) ipCriterion;
318 }
319
Esin Karamane4890012020-04-19 11:58:54 +0000320 public VlanIdCriterion vlanId(TrafficSelector trafficSelector, Criterion.Type type) {
321 Set<Criterion> criterionSet = trafficSelector.criteria();
322 Iterator<Criterion> it = criterionSet.iterator();
323 VlanIdCriterion criterion = null;
324 while (it.hasNext()) {
325 Criterion criteria = it.next();
326 if (type == criteria.type()) {
327 criterion = (VlanIdCriterion) criteria;
328 }
329 }
330 return criterion;
331 }
Sonal Kasliwala0bbe6c2020-01-06 10:46:30 +0000332}