blob: 273c8626ebf5798bf9948441aea582aa980eb04b [file] [log] [blame]
Sonal Kasliwalee230262020-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
18import java.io.IOException;
19import java.util.Arrays;
20import java.util.HashMap;
21import java.util.HashSet;
22import java.util.Iterator;
23import java.util.List;
24import java.util.Map;
25import java.util.Optional;
26import java.util.Set;
27
28import org.onlab.packet.IpAddress;
29import org.onlab.packet.VlanId;
30import org.onosproject.TestApplicationId;
31import org.onosproject.core.ApplicationId;
32import org.onosproject.core.CoreServiceAdapter;
33import org.onosproject.mastership.MastershipServiceAdapter;
34import org.onosproject.mcast.api.McastRoute;
35import org.onosproject.net.ConnectPoint;
36import org.onosproject.net.DeviceId;
37import org.onosproject.net.HostId;
38import org.onosproject.net.PortNumber;
39import org.onosproject.net.flow.TrafficSelector;
40import org.onosproject.net.flow.TrafficTreatment;
41import org.onosproject.net.flow.criteria.Criterion;
42import org.onosproject.net.flow.criteria.IPCriterion;
43import org.onosproject.net.flow.instructions.Instruction;
44import org.onosproject.net.flow.instructions.Instructions.OutputInstruction;
45import org.onosproject.net.flowobjective.FlowObjectiveServiceAdapter;
46import org.onosproject.net.flowobjective.ForwardingObjective;
47import org.onosproject.net.flowobjective.NextObjective;
48import org.opencord.cordconfig.CordConfigListener;
49import org.opencord.cordconfig.CordConfigService;
50import org.opencord.cordconfig.access.AccessAgentData;
51import org.opencord.cordconfig.access.AccessDeviceData;
52
53import com.fasterxml.jackson.databind.JsonNode;
54import com.fasterxml.jackson.databind.ObjectMapper;
55import com.google.common.collect.ImmutableMap;
56
57public class McastTestBase {
58
59 // Map to store the forwardingObjective in flowObjectiveService.forward()
60 Map<DeviceId, ForwardingObjective> forwardMap = new HashMap<>();
61 // Map to store the nextObjective in flowObjectiveService.next()
62 Map<DeviceId, NextObjective> nextMap = new HashMap<>();
63 // Device configuration
64 protected static final DeviceId DEVICE_ID_OF_A = DeviceId.deviceId("of:00000a0a0a0a0a00");
65 // Port number
66 protected static final PortNumber PORT_A = PortNumber.portNumber(1048576);
67 protected static final PortNumber PORT_B = PortNumber.portNumber(16);
68 protected static final PortNumber PORT_C = PortNumber.portNumber(24);
69
70 // Connect Point for creating source and sink
71 protected static final ConnectPoint CONNECT_POINT_A = new ConnectPoint(DEVICE_ID_OF_A, PORT_A);
72 protected static final ConnectPoint CONNECT_POINT_B = new ConnectPoint(DEVICE_ID_OF_A, PORT_B);
73 protected static final ConnectPoint CONNECT_POINT_C = new ConnectPoint(DEVICE_ID_OF_A, PORT_C);
74
75 //Host id configuration
76 protected static final HostId HOST_ID_NONE = HostId.NONE;
77 // Source connect point
78 protected static final Set<ConnectPoint> SOURCES_CP = new HashSet<ConnectPoint>(Arrays.asList(CONNECT_POINT_A));
79 Map<HostId, Set<ConnectPoint>> sources = ImmutableMap.of(HOST_ID_NONE, SOURCES_CP);
80
81 protected static final IpAddress MULTICAST_IP = IpAddress.valueOf("224.0.0.22");
82 // Creating dummy route with IGMP type.
83 McastRoute route1 = new McastRoute(null, MULTICAST_IP, McastRoute.Type.IGMP);
84
85 // Creating empty sink used in prevRoute
86 Set<ConnectPoint> sinksCp = new HashSet<ConnectPoint>(Arrays.asList());
87 Map<HostId, Set<ConnectPoint>> sinks = ImmutableMap.of(HOST_ID_NONE, sinksCp);
88
89 // Creating empty source
90 Set<ConnectPoint> sourceCp = new HashSet<ConnectPoint>(Arrays.asList());
91 Map<HostId, Set<ConnectPoint>> emptySource = ImmutableMap.of(HOST_ID_NONE, sourceCp);
92
93 // Flag to check unknown olt device
94 boolean knownOltFlag = false;
95
96 class MockCoreService extends CoreServiceAdapter {
97 @Override
98 public ApplicationId registerApplication(String name) {
99 ApplicationId testApplicationId = TestApplicationId.create("org.opencord.cordmcast");
100 return testApplicationId;
101 }
102 }
103
104 class MockFlowObjectiveService extends FlowObjectiveServiceAdapter {
105 @Override
106 public void forward(DeviceId deviceId, ForwardingObjective forwardingObjective) {
107 synchronized (forwardMap) {
108 forwardMap.put(deviceId, forwardingObjective);
109 forwardMap.notify();
110 }
111 }
112
113 @Override
114 public void next(DeviceId deviceId, NextObjective nextObjective) {
115 nextMap.put(deviceId, nextObjective);
116 }
117 }
118
119 class TestMastershipService extends MastershipServiceAdapter {
120 @Override
121 public boolean isLocalMaster(DeviceId deviceId) {
122 return true;
123 }
124 }
125
126 class MockCordConfigService implements CordConfigService {
127
128 @Override
129 public void addListener(CordConfigListener listener) {
130
131 }
132
133 @Override
134 public void removeListener(CordConfigListener listener) {
135
136 }
137
138 @Override
139 public Set<AccessDeviceData> getAccessDevices() {
140
141 return null;
142 }
143
144 @Override
145 public Optional<AccessDeviceData> getAccessDevice(DeviceId deviceId) {
146 if (deviceId == DEVICE_ID_OF_A) {
147 PortNumber uplink = PortNumber.portNumber(3);
148 VlanId vlan = VlanId.vlanId((short) 0);
149 ObjectMapper mapper = new ObjectMapper();
150 JsonNode defaultVlanNode = null;
151 try {
152 defaultVlanNode =
153 (JsonNode) mapper.readTree("{\"driver\":\"pmc-olt\" , \"type \" : \"OLT\"}");
154 } catch (IOException e) {
155 e.printStackTrace();
156 }
157
158 Optional<VlanId> defaultVlan;
159 if (defaultVlanNode.isMissingNode()) {
160 defaultVlan = Optional.empty();
161 } else {
162 defaultVlan = Optional.of(VlanId.vlanId(defaultVlanNode.shortValue()));
163 }
164 Optional<AccessDeviceData> accessDeviceData = null;
165 AccessDeviceData accessDevice = new AccessDeviceData(deviceId, uplink, vlan, defaultVlan);
166 accessDeviceData = Optional.of(accessDevice);
167 return accessDeviceData;
168 } else {
169 knownOltFlag = true;
170 return Optional.empty();
171 }
172 }
173
174 @Override
175 public Set<AccessAgentData> getAccessAgents() {
176 return null;
177 }
178
179 @Override
180 public Optional<AccessAgentData> getAccessAgent(DeviceId deviceId) {
181 return null;
182 }
183
184 }
185
186 public OutputInstruction outputPort(TrafficTreatment trafficTreatment) {
187 List<Instruction> listOfInstructions = trafficTreatment.allInstructions();
188 OutputInstruction output = null;
189 for (Instruction intruction : listOfInstructions) {
190 output = (OutputInstruction) intruction;
191 }
192 return output;
193 }
194
195 public IPCriterion ipAddress(TrafficSelector trafficSelector) {
196 Set<Criterion> criterionSet = trafficSelector.criteria();
197 Iterator<Criterion> it = criterionSet.iterator();
198 IPCriterion ipCriterion = null;
199 while (it.hasNext()) {
200 Criterion criteria = it.next();
201 if (Criterion.Type.IPV4_DST == criteria.type()) {
202 ipCriterion = (IPCriterion) criteria;
203 }
204 }
205 return (IPCriterion) ipCriterion;
206 }
207
208}