blob: 986c175a414cc5f4b12874f101aa254aacb8012d [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 }
Esin Karamanbb35a3b2020-03-18 13:53:24 +0000171
172 @Override
173 public VlanId egressInnerVlan() {
174 return VlanId.NONE;
175 }
Arjun E Kabf9e6e2020-03-02 10:15:21 +0000176 }
177
178 /**
179 * Mocks the network config registry.
180 */
181 @SuppressWarnings("unchecked")
182 static final class TestNetworkConfigRegistry
183 extends NetworkConfigRegistryAdapter {
184 @Override
185 public <S, C extends Config<S>> C getConfig(S subject, Class<C> configClass) {
186 McastConfig mcastConfig = new MockMcastConfig();
187 return (C) mcastConfig;
188 }
189 }
190
191 public static class TestEventDispatcher extends DefaultEventSinkRegistry
192 implements EventDeliveryService {
193
194 @Override
195 @SuppressWarnings("unchecked")
196 public synchronized void post(Event event) {
197 EventSink sink = getSink(event.getClass());
198 checkState(sink != null, "No sink for event %s", event);
199 sink.process(event);
200 }
201
202 @Override
203 public void setDispatchTimeLimit(long millis) {
204
205 }
206
207 @Override
208 public long getDispatchTimeLimit() {
209 return 0;
210 }
211 }
212
213 public static class MockCordMcastStatisticsEventListener implements CordMcastStatisticsEventListener {
214 protected List<CordMcastStatisticsEvent> mcastEventList = new ArrayList<CordMcastStatisticsEvent>();
215
216 @Override
217 public void event(CordMcastStatisticsEvent event) {
218 mcastEventList.add(event);
219 }
220 }
221
Esin Karaman996177c2020-03-05 13:21:09 +0000222 private class MockSubService implements BaseInformationService<SubscriberAndDeviceInformation> {
223 MockSubscriberAndDeviceInformation deviceA =
224 new MockSubscriberAndDeviceInformation(SERIAL_NUMBER_OF_DEVICE_A, MANAGEMENT_IP_OF_A);
Sonal Kasliwala0bbe6c2020-01-06 10:46:30 +0000225
Esin Karaman996177c2020-03-05 13:21:09 +0000226 @Override
227 public SubscriberAndDeviceInformation get(String id) {
228 return SERIAL_NUMBER_OF_DEVICE_A.equals(id) ? deviceA : null;
229 }
Sonal Kasliwala0bbe6c2020-01-06 10:46:30 +0000230
Esin Karaman996177c2020-03-05 13:21:09 +0000231 @Override
232 public void invalidateAll() {
233 }
Sonal Kasliwala0bbe6c2020-01-06 10:46:30 +0000234
Esin Karaman996177c2020-03-05 13:21:09 +0000235 @Override
236 public void invalidateId(String id) {
237 }
Sonal Kasliwala0bbe6c2020-01-06 10:46:30 +0000238
Esin Karaman996177c2020-03-05 13:21:09 +0000239 @Override
240 public SubscriberAndDeviceInformation getfromCache(String id) {
241 return null;
242 }
243 }
Sonal Kasliwala0bbe6c2020-01-06 10:46:30 +0000244
Esin Karaman996177c2020-03-05 13:21:09 +0000245 private class MockSubscriberAndDeviceInformation extends SubscriberAndDeviceInformation {
Sonal Kasliwala0bbe6c2020-01-06 10:46:30 +0000246
Esin Karaman996177c2020-03-05 13:21:09 +0000247 MockSubscriberAndDeviceInformation(String id, Ip4Address ipAddress) {
248 this.setId(id);
249 this.setIPAddress(ipAddress);
250 this.setUplinkPort((int) PORT_A.toLong());
251 }
252 }
Sonal Kasliwala0bbe6c2020-01-06 10:46:30 +0000253
Esin Karaman996177c2020-03-05 13:21:09 +0000254 class MockDeviceService extends DeviceServiceAdapter {
Sonal Kasliwala0bbe6c2020-01-06 10:46:30 +0000255
Esin Karaman996177c2020-03-05 13:21:09 +0000256 @Override
257 public Device getDevice(DeviceId deviceId) {
258 if (DEVICE_ID_OF_A.equals(deviceId)) {
259 DefaultAnnotations.Builder annotationsBuilder = DefaultAnnotations.builder()
260 .set(AnnotationKeys.MANAGEMENT_ADDRESS, MANAGEMENT_IP_OF_A.toString());
261 SparseAnnotations annotations = annotationsBuilder.build();
262 Annotations[] da = {annotations};
263
264 Device deviceA = new DefaultDevice(null, DEVICE_ID_OF_A, Device.Type.OTHER, "", "",
265 "", SERIAL_NUMBER_OF_DEVICE_A, null, da);
266 return deviceA;
267 } else {
268 knownOltFlag = true;
269 }
270 return null;
271 }
272 }
Sonal Kasliwala0bbe6c2020-01-06 10:46:30 +0000273
274 public OutputInstruction outputPort(TrafficTreatment trafficTreatment) {
275 List<Instruction> listOfInstructions = trafficTreatment.allInstructions();
276 OutputInstruction output = null;
277 for (Instruction intruction : listOfInstructions) {
278 output = (OutputInstruction) intruction;
279 }
280 return output;
281 }
282
283 public IPCriterion ipAddress(TrafficSelector trafficSelector) {
284 Set<Criterion> criterionSet = trafficSelector.criteria();
285 Iterator<Criterion> it = criterionSet.iterator();
286 IPCriterion ipCriterion = null;
287 while (it.hasNext()) {
288 Criterion criteria = it.next();
289 if (Criterion.Type.IPV4_DST == criteria.type()) {
290 ipCriterion = (IPCriterion) criteria;
291 }
292 }
293 return (IPCriterion) ipCriterion;
294 }
295
296}