blob: 823df6b48e0f495bf805d213de1063e93a8df2ad [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 */
16package org.opencord.cordmcast;
17
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;
Esin Karaman996177c2020-03-05 13:21:09 +000064import org.opencord.sadis.BandwidthProfileInformation;
65import org.opencord.sadis.BaseInformationService;
66import org.opencord.sadis.SadisService;
67import org.opencord.sadis.SubscriberAndDeviceInformation;
Sonal Kasliwala0bbe6c2020-01-06 10:46:30 +000068
Arjun E Kabf9e6e2020-03-02 10:15:21 +000069import static com.google.common.base.Preconditions.checkState;
70
Sonal Kasliwala0bbe6c2020-01-06 10:46:30 +000071public class McastTestBase {
72
73 // Map to store the forwardingObjective in flowObjectiveService.forward()
74 Map<DeviceId, ForwardingObjective> forwardMap = new HashMap<>();
75 // Map to store the nextObjective in flowObjectiveService.next()
76 Map<DeviceId, NextObjective> nextMap = new HashMap<>();
77 // Device configuration
78 protected static final DeviceId DEVICE_ID_OF_A = DeviceId.deviceId("of:00000a0a0a0a0a00");
79 // Port number
80 protected static final PortNumber PORT_A = PortNumber.portNumber(1048576);
81 protected static final PortNumber PORT_B = PortNumber.portNumber(16);
82 protected static final PortNumber PORT_C = PortNumber.portNumber(24);
83
84 // Connect Point for creating source and sink
85 protected static final ConnectPoint CONNECT_POINT_A = new ConnectPoint(DEVICE_ID_OF_A, PORT_A);
86 protected static final ConnectPoint CONNECT_POINT_B = new ConnectPoint(DEVICE_ID_OF_A, PORT_B);
87 protected static final ConnectPoint CONNECT_POINT_C = new ConnectPoint(DEVICE_ID_OF_A, PORT_C);
88
Esin Karaman996177c2020-03-05 13:21:09 +000089 // serial number of the device A
90 protected static final String SERIAL_NUMBER_OF_DEVICE_A = "serialNumberOfDevA";
91 // Management ip address of the device A
92 protected static final Ip4Address MANAGEMENT_IP_OF_A = Ip4Address.valueOf("10.177.125.4");
Sonal Kasliwala0bbe6c2020-01-06 10:46:30 +000093 //Host id configuration
94 protected static final HostId HOST_ID_NONE = HostId.NONE;
95 // Source connect point
96 protected static final Set<ConnectPoint> SOURCES_CP = new HashSet<ConnectPoint>(Arrays.asList(CONNECT_POINT_A));
97 Map<HostId, Set<ConnectPoint>> sources = ImmutableMap.of(HOST_ID_NONE, SOURCES_CP);
98
99 protected static final IpAddress MULTICAST_IP = IpAddress.valueOf("224.0.0.22");
Arjun E Kabf9e6e2020-03-02 10:15:21 +0000100 protected static final IpAddress SOURCE_IP = IpAddress.valueOf("192.168.1.1");
Sonal Kasliwala0bbe6c2020-01-06 10:46:30 +0000101 // Creating dummy route with IGMP type.
Arjun E Kabf9e6e2020-03-02 10:15:21 +0000102 McastRoute route1 = new McastRoute(SOURCE_IP, MULTICAST_IP, McastRoute.Type.IGMP);
Sonal Kasliwala0bbe6c2020-01-06 10:46:30 +0000103
104 // Creating empty sink used in prevRoute
105 Set<ConnectPoint> sinksCp = new HashSet<ConnectPoint>(Arrays.asList());
106 Map<HostId, Set<ConnectPoint>> sinks = ImmutableMap.of(HOST_ID_NONE, sinksCp);
107
108 // Creating empty source
109 Set<ConnectPoint> sourceCp = new HashSet<ConnectPoint>(Arrays.asList());
110 Map<HostId, Set<ConnectPoint>> emptySource = ImmutableMap.of(HOST_ID_NONE, sourceCp);
111
112 // Flag to check unknown olt device
113 boolean knownOltFlag = false;
114
Arjun E Kabf9e6e2020-03-02 10:15:21 +0000115 // For the tests reduce events period to 1s
116 protected static final int EVENT_GENERATION_PERIOD = 1;
117
Sonal Kasliwala0bbe6c2020-01-06 10:46:30 +0000118 class MockCoreService extends CoreServiceAdapter {
119 @Override
120 public ApplicationId registerApplication(String name) {
121 ApplicationId testApplicationId = TestApplicationId.create("org.opencord.cordmcast");
122 return testApplicationId;
123 }
124 }
125
126 class MockFlowObjectiveService extends FlowObjectiveServiceAdapter {
127 @Override
128 public void forward(DeviceId deviceId, ForwardingObjective forwardingObjective) {
129 synchronized (forwardMap) {
130 forwardMap.put(deviceId, forwardingObjective);
131 forwardMap.notify();
132 }
133 }
134
135 @Override
136 public void next(DeviceId deviceId, NextObjective nextObjective) {
137 nextMap.put(deviceId, nextObjective);
138 }
139 }
140
141 class TestMastershipService extends MastershipServiceAdapter {
142 @Override
143 public boolean isLocalMaster(DeviceId deviceId) {
144 return true;
145 }
146 }
147
Esin Karaman996177c2020-03-05 13:21:09 +0000148 protected class MockSadisService implements SadisService {
Sonal Kasliwala0bbe6c2020-01-06 10:46:30 +0000149
Esin Karaman996177c2020-03-05 13:21:09 +0000150 @Override
151 public BaseInformationService<SubscriberAndDeviceInformation> getSubscriberInfoService() {
152 return new MockSubService();
153 }
Sonal Kasliwala0bbe6c2020-01-06 10:46:30 +0000154
Esin Karaman996177c2020-03-05 13:21:09 +0000155 @Override
156 public BaseInformationService<BandwidthProfileInformation> getBandwidthProfileService() {
157 return null;
158 }
159 }
Sonal Kasliwala0bbe6c2020-01-06 10:46:30 +0000160
Arjun E Kabf9e6e2020-03-02 10:15:21 +0000161 /**
162 * Mocks the McastConfig class to return vlan id value.
163 */
164 static class MockMcastConfig extends McastConfig {
165 @Override
166 public VlanId egressVlan() {
167 return VlanId.vlanId("4000");
168 }
169 }
170
171 /**
172 * Mocks the network config registry.
173 */
174 @SuppressWarnings("unchecked")
175 static final class TestNetworkConfigRegistry
176 extends NetworkConfigRegistryAdapter {
177 @Override
178 public <S, C extends Config<S>> C getConfig(S subject, Class<C> configClass) {
179 McastConfig mcastConfig = new MockMcastConfig();
180 return (C) mcastConfig;
181 }
182 }
183
184 public static class TestEventDispatcher extends DefaultEventSinkRegistry
185 implements EventDeliveryService {
186
187 @Override
188 @SuppressWarnings("unchecked")
189 public synchronized void post(Event event) {
190 EventSink sink = getSink(event.getClass());
191 checkState(sink != null, "No sink for event %s", event);
192 sink.process(event);
193 }
194
195 @Override
196 public void setDispatchTimeLimit(long millis) {
197
198 }
199
200 @Override
201 public long getDispatchTimeLimit() {
202 return 0;
203 }
204 }
205
206 public static class MockCordMcastStatisticsEventListener implements CordMcastStatisticsEventListener {
207 protected List<CordMcastStatisticsEvent> mcastEventList = new ArrayList<CordMcastStatisticsEvent>();
208
209 @Override
210 public void event(CordMcastStatisticsEvent event) {
211 mcastEventList.add(event);
212 }
213 }
214
Esin Karaman996177c2020-03-05 13:21:09 +0000215 private class MockSubService implements BaseInformationService<SubscriberAndDeviceInformation> {
216 MockSubscriberAndDeviceInformation deviceA =
217 new MockSubscriberAndDeviceInformation(SERIAL_NUMBER_OF_DEVICE_A, MANAGEMENT_IP_OF_A);
Sonal Kasliwala0bbe6c2020-01-06 10:46:30 +0000218
Esin Karaman996177c2020-03-05 13:21:09 +0000219 @Override
220 public SubscriberAndDeviceInformation get(String id) {
221 return SERIAL_NUMBER_OF_DEVICE_A.equals(id) ? deviceA : null;
222 }
Sonal Kasliwala0bbe6c2020-01-06 10:46:30 +0000223
Esin Karaman996177c2020-03-05 13:21:09 +0000224 @Override
225 public void invalidateAll() {
226 }
Sonal Kasliwala0bbe6c2020-01-06 10:46:30 +0000227
Esin Karaman996177c2020-03-05 13:21:09 +0000228 @Override
229 public void invalidateId(String id) {
230 }
Sonal Kasliwala0bbe6c2020-01-06 10:46:30 +0000231
Esin Karaman996177c2020-03-05 13:21:09 +0000232 @Override
233 public SubscriberAndDeviceInformation getfromCache(String id) {
234 return null;
235 }
236 }
Sonal Kasliwala0bbe6c2020-01-06 10:46:30 +0000237
Esin Karaman996177c2020-03-05 13:21:09 +0000238 private class MockSubscriberAndDeviceInformation extends SubscriberAndDeviceInformation {
Sonal Kasliwala0bbe6c2020-01-06 10:46:30 +0000239
Esin Karaman996177c2020-03-05 13:21:09 +0000240 MockSubscriberAndDeviceInformation(String id, Ip4Address ipAddress) {
241 this.setId(id);
242 this.setIPAddress(ipAddress);
243 this.setUplinkPort((int) PORT_A.toLong());
244 }
245 }
Sonal Kasliwala0bbe6c2020-01-06 10:46:30 +0000246
Esin Karaman996177c2020-03-05 13:21:09 +0000247 class MockDeviceService extends DeviceServiceAdapter {
Sonal Kasliwala0bbe6c2020-01-06 10:46:30 +0000248
Esin Karaman996177c2020-03-05 13:21:09 +0000249 @Override
250 public Device getDevice(DeviceId deviceId) {
251 if (DEVICE_ID_OF_A.equals(deviceId)) {
252 DefaultAnnotations.Builder annotationsBuilder = DefaultAnnotations.builder()
253 .set(AnnotationKeys.MANAGEMENT_ADDRESS, MANAGEMENT_IP_OF_A.toString());
254 SparseAnnotations annotations = annotationsBuilder.build();
255 Annotations[] da = {annotations};
256
257 Device deviceA = new DefaultDevice(null, DEVICE_ID_OF_A, Device.Type.OTHER, "", "",
258 "", SERIAL_NUMBER_OF_DEVICE_A, null, da);
259 return deviceA;
260 } else {
261 knownOltFlag = true;
262 }
263 return null;
264 }
265 }
Sonal Kasliwala0bbe6c2020-01-06 10:46:30 +0000266
267 public OutputInstruction outputPort(TrafficTreatment trafficTreatment) {
268 List<Instruction> listOfInstructions = trafficTreatment.allInstructions();
269 OutputInstruction output = null;
270 for (Instruction intruction : listOfInstructions) {
271 output = (OutputInstruction) intruction;
272 }
273 return output;
274 }
275
276 public IPCriterion ipAddress(TrafficSelector trafficSelector) {
277 Set<Criterion> criterionSet = trafficSelector.criteria();
278 Iterator<Criterion> it = criterionSet.iterator();
279 IPCriterion ipCriterion = null;
280 while (it.hasNext()) {
281 Criterion criteria = it.next();
282 if (Criterion.Type.IPV4_DST == criteria.type()) {
283 ipCriterion = (IPCriterion) criteria;
284 }
285 }
286 return (IPCriterion) ipCriterion;
287 }
288
289}