blob: 17ad11b7c76e516b3c93426cba9bdf1ce3eb1801 [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;
57import org.onosproject.net.flow.instructions.Instruction;
58import org.onosproject.net.flow.instructions.Instructions.OutputInstruction;
59import org.onosproject.net.flowobjective.FlowObjectiveServiceAdapter;
60import org.onosproject.net.flowobjective.ForwardingObjective;
61import org.onosproject.net.flowobjective.NextObjective;
Sonal Kasliwala0bbe6c2020-01-06 10:46:30 +000062
Sonal Kasliwala0bbe6c2020-01-06 10:46:30 +000063import com.google.common.collect.ImmutableMap;
Daniele Moro8ea9e102020-03-24 18:56:52 +010064import org.opencord.cordmcast.CordMcastStatisticsEvent;
65import org.opencord.cordmcast.CordMcastStatisticsEventListener;
Esin Karaman996177c2020-03-05 13:21:09 +000066import org.opencord.sadis.BandwidthProfileInformation;
67import org.opencord.sadis.BaseInformationService;
68import org.opencord.sadis.SadisService;
69import org.opencord.sadis.SubscriberAndDeviceInformation;
Sonal Kasliwala0bbe6c2020-01-06 10:46:30 +000070
Arjun E Kabf9e6e2020-03-02 10:15:21 +000071import static com.google.common.base.Preconditions.checkState;
72
Sonal Kasliwala0bbe6c2020-01-06 10:46:30 +000073public class McastTestBase {
74
75 // Map to store the forwardingObjective in flowObjectiveService.forward()
76 Map<DeviceId, ForwardingObjective> forwardMap = new HashMap<>();
77 // Map to store the nextObjective in flowObjectiveService.next()
78 Map<DeviceId, NextObjective> nextMap = new HashMap<>();
79 // Device configuration
80 protected static final DeviceId DEVICE_ID_OF_A = DeviceId.deviceId("of:00000a0a0a0a0a00");
81 // Port number
82 protected static final PortNumber PORT_A = PortNumber.portNumber(1048576);
83 protected static final PortNumber PORT_B = PortNumber.portNumber(16);
84 protected static final PortNumber PORT_C = PortNumber.portNumber(24);
85
86 // Connect Point for creating source and sink
87 protected static final ConnectPoint CONNECT_POINT_A = new ConnectPoint(DEVICE_ID_OF_A, PORT_A);
88 protected static final ConnectPoint CONNECT_POINT_B = new ConnectPoint(DEVICE_ID_OF_A, PORT_B);
89 protected static final ConnectPoint CONNECT_POINT_C = new ConnectPoint(DEVICE_ID_OF_A, PORT_C);
90
Esin Karaman996177c2020-03-05 13:21:09 +000091 // serial number of the device A
92 protected static final String SERIAL_NUMBER_OF_DEVICE_A = "serialNumberOfDevA";
93 // Management ip address of the device A
94 protected static final Ip4Address MANAGEMENT_IP_OF_A = Ip4Address.valueOf("10.177.125.4");
Sonal Kasliwala0bbe6c2020-01-06 10:46:30 +000095 //Host id configuration
96 protected static final HostId HOST_ID_NONE = HostId.NONE;
97 // Source connect point
98 protected static final Set<ConnectPoint> SOURCES_CP = new HashSet<ConnectPoint>(Arrays.asList(CONNECT_POINT_A));
99 Map<HostId, Set<ConnectPoint>> sources = ImmutableMap.of(HOST_ID_NONE, SOURCES_CP);
100
101 protected static final IpAddress MULTICAST_IP = IpAddress.valueOf("224.0.0.22");
Arjun E Kabf9e6e2020-03-02 10:15:21 +0000102 protected static final IpAddress SOURCE_IP = IpAddress.valueOf("192.168.1.1");
Sonal Kasliwala0bbe6c2020-01-06 10:46:30 +0000103 // Creating dummy route with IGMP type.
Arjun E Kabf9e6e2020-03-02 10:15:21 +0000104 McastRoute route1 = new McastRoute(SOURCE_IP, MULTICAST_IP, McastRoute.Type.IGMP);
Sonal Kasliwala0bbe6c2020-01-06 10:46:30 +0000105
106 // Creating empty sink used in prevRoute
107 Set<ConnectPoint> sinksCp = new HashSet<ConnectPoint>(Arrays.asList());
108 Map<HostId, Set<ConnectPoint>> sinks = ImmutableMap.of(HOST_ID_NONE, sinksCp);
109
110 // Creating empty source
111 Set<ConnectPoint> sourceCp = new HashSet<ConnectPoint>(Arrays.asList());
112 Map<HostId, Set<ConnectPoint>> emptySource = ImmutableMap.of(HOST_ID_NONE, sourceCp);
113
114 // Flag to check unknown olt device
115 boolean knownOltFlag = false;
116
Arjun E Kabf9e6e2020-03-02 10:15:21 +0000117 // For the tests reduce events period to 1s
118 protected static final int EVENT_GENERATION_PERIOD = 1;
119
Sonal Kasliwala0bbe6c2020-01-06 10:46:30 +0000120 class MockCoreService extends CoreServiceAdapter {
121 @Override
122 public ApplicationId registerApplication(String name) {
123 ApplicationId testApplicationId = TestApplicationId.create("org.opencord.cordmcast");
124 return testApplicationId;
125 }
126 }
127
128 class MockFlowObjectiveService extends FlowObjectiveServiceAdapter {
129 @Override
130 public void forward(DeviceId deviceId, ForwardingObjective forwardingObjective) {
131 synchronized (forwardMap) {
132 forwardMap.put(deviceId, forwardingObjective);
133 forwardMap.notify();
134 }
135 }
136
137 @Override
138 public void next(DeviceId deviceId, NextObjective nextObjective) {
139 nextMap.put(deviceId, nextObjective);
140 }
141 }
142
143 class TestMastershipService extends MastershipServiceAdapter {
144 @Override
145 public boolean isLocalMaster(DeviceId deviceId) {
146 return true;
147 }
148 }
149
Esin Karaman996177c2020-03-05 13:21:09 +0000150 protected class MockSadisService implements SadisService {
Sonal Kasliwala0bbe6c2020-01-06 10:46:30 +0000151
Esin Karaman996177c2020-03-05 13:21:09 +0000152 @Override
153 public BaseInformationService<SubscriberAndDeviceInformation> getSubscriberInfoService() {
154 return new MockSubService();
155 }
Sonal Kasliwala0bbe6c2020-01-06 10:46:30 +0000156
Esin Karaman996177c2020-03-05 13:21:09 +0000157 @Override
158 public BaseInformationService<BandwidthProfileInformation> getBandwidthProfileService() {
159 return null;
160 }
161 }
Sonal Kasliwala0bbe6c2020-01-06 10:46:30 +0000162
Arjun E Kabf9e6e2020-03-02 10:15:21 +0000163 /**
164 * Mocks the McastConfig class to return vlan id value.
165 */
166 static class MockMcastConfig extends McastConfig {
167 @Override
168 public VlanId egressVlan() {
169 return VlanId.vlanId("4000");
170 }
171 }
172
173 /**
174 * Mocks the network config registry.
175 */
176 @SuppressWarnings("unchecked")
177 static final class TestNetworkConfigRegistry
178 extends NetworkConfigRegistryAdapter {
179 @Override
180 public <S, C extends Config<S>> C getConfig(S subject, Class<C> configClass) {
181 McastConfig mcastConfig = new MockMcastConfig();
182 return (C) mcastConfig;
183 }
184 }
185
186 public static class TestEventDispatcher extends DefaultEventSinkRegistry
187 implements EventDeliveryService {
188
189 @Override
190 @SuppressWarnings("unchecked")
191 public synchronized void post(Event event) {
192 EventSink sink = getSink(event.getClass());
193 checkState(sink != null, "No sink for event %s", event);
194 sink.process(event);
195 }
196
197 @Override
198 public void setDispatchTimeLimit(long millis) {
199
200 }
201
202 @Override
203 public long getDispatchTimeLimit() {
204 return 0;
205 }
206 }
207
208 public static class MockCordMcastStatisticsEventListener implements CordMcastStatisticsEventListener {
209 protected List<CordMcastStatisticsEvent> mcastEventList = new ArrayList<CordMcastStatisticsEvent>();
210
211 @Override
212 public void event(CordMcastStatisticsEvent event) {
213 mcastEventList.add(event);
214 }
215 }
216
Esin Karaman996177c2020-03-05 13:21:09 +0000217 private class MockSubService implements BaseInformationService<SubscriberAndDeviceInformation> {
218 MockSubscriberAndDeviceInformation deviceA =
219 new MockSubscriberAndDeviceInformation(SERIAL_NUMBER_OF_DEVICE_A, MANAGEMENT_IP_OF_A);
Sonal Kasliwala0bbe6c2020-01-06 10:46:30 +0000220
Esin Karaman996177c2020-03-05 13:21:09 +0000221 @Override
222 public SubscriberAndDeviceInformation get(String id) {
223 return SERIAL_NUMBER_OF_DEVICE_A.equals(id) ? deviceA : null;
224 }
Sonal Kasliwala0bbe6c2020-01-06 10:46:30 +0000225
Esin Karaman996177c2020-03-05 13:21:09 +0000226 @Override
227 public void invalidateAll() {
228 }
Sonal Kasliwala0bbe6c2020-01-06 10:46:30 +0000229
Esin Karaman996177c2020-03-05 13:21:09 +0000230 @Override
231 public void invalidateId(String id) {
232 }
Sonal Kasliwala0bbe6c2020-01-06 10:46:30 +0000233
Esin Karaman996177c2020-03-05 13:21:09 +0000234 @Override
235 public SubscriberAndDeviceInformation getfromCache(String id) {
236 return null;
237 }
238 }
Sonal Kasliwala0bbe6c2020-01-06 10:46:30 +0000239
Esin Karaman996177c2020-03-05 13:21:09 +0000240 private class MockSubscriberAndDeviceInformation extends SubscriberAndDeviceInformation {
Sonal Kasliwala0bbe6c2020-01-06 10:46:30 +0000241
Esin Karaman996177c2020-03-05 13:21:09 +0000242 MockSubscriberAndDeviceInformation(String id, Ip4Address ipAddress) {
243 this.setId(id);
244 this.setIPAddress(ipAddress);
245 this.setUplinkPort((int) PORT_A.toLong());
246 }
247 }
Sonal Kasliwala0bbe6c2020-01-06 10:46:30 +0000248
Esin Karaman996177c2020-03-05 13:21:09 +0000249 class MockDeviceService extends DeviceServiceAdapter {
Sonal Kasliwala0bbe6c2020-01-06 10:46:30 +0000250
Esin Karaman996177c2020-03-05 13:21:09 +0000251 @Override
252 public Device getDevice(DeviceId deviceId) {
253 if (DEVICE_ID_OF_A.equals(deviceId)) {
254 DefaultAnnotations.Builder annotationsBuilder = DefaultAnnotations.builder()
255 .set(AnnotationKeys.MANAGEMENT_ADDRESS, MANAGEMENT_IP_OF_A.toString());
256 SparseAnnotations annotations = annotationsBuilder.build();
257 Annotations[] da = {annotations};
258
259 Device deviceA = new DefaultDevice(null, DEVICE_ID_OF_A, Device.Type.OTHER, "", "",
260 "", SERIAL_NUMBER_OF_DEVICE_A, null, da);
261 return deviceA;
262 } else {
263 knownOltFlag = true;
264 }
265 return null;
266 }
267 }
Sonal Kasliwala0bbe6c2020-01-06 10:46:30 +0000268
269 public OutputInstruction outputPort(TrafficTreatment trafficTreatment) {
270 List<Instruction> listOfInstructions = trafficTreatment.allInstructions();
271 OutputInstruction output = null;
272 for (Instruction intruction : listOfInstructions) {
273 output = (OutputInstruction) intruction;
274 }
275 return output;
276 }
277
278 public IPCriterion ipAddress(TrafficSelector trafficSelector) {
279 Set<Criterion> criterionSet = trafficSelector.criteria();
280 Iterator<Criterion> it = criterionSet.iterator();
281 IPCriterion ipCriterion = null;
282 while (it.hasNext()) {
283 Criterion criteria = it.next();
284 if (Criterion.Type.IPV4_DST == criteria.type()) {
285 ipCriterion = (IPCriterion) criteria;
286 }
287 }
288 return (IPCriterion) ipCriterion;
289 }
290
291}