Sonal Kasliwal | a0bbe6c | 2020-01-06 10:46:30 +0000 | [diff] [blame^] | 1 | /* |
| 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 | */ |
| 16 | package org.opencord.cordmcast; |
| 17 | |
| 18 | import java.io.IOException; |
| 19 | import java.util.Arrays; |
| 20 | import java.util.HashMap; |
| 21 | import java.util.HashSet; |
| 22 | import java.util.Iterator; |
| 23 | import java.util.List; |
| 24 | import java.util.Map; |
| 25 | import java.util.Optional; |
| 26 | import java.util.Set; |
| 27 | |
| 28 | import org.onlab.packet.IpAddress; |
| 29 | import org.onlab.packet.VlanId; |
| 30 | import org.onosproject.TestApplicationId; |
| 31 | import org.onosproject.core.ApplicationId; |
| 32 | import org.onosproject.core.CoreServiceAdapter; |
| 33 | import org.onosproject.mastership.MastershipServiceAdapter; |
| 34 | import org.onosproject.mcast.api.McastRoute; |
| 35 | import org.onosproject.net.ConnectPoint; |
| 36 | import org.onosproject.net.DeviceId; |
| 37 | import org.onosproject.net.HostId; |
| 38 | import org.onosproject.net.PortNumber; |
| 39 | import org.onosproject.net.flow.TrafficSelector; |
| 40 | import org.onosproject.net.flow.TrafficTreatment; |
| 41 | import org.onosproject.net.flow.criteria.Criterion; |
| 42 | import org.onosproject.net.flow.criteria.IPCriterion; |
| 43 | import org.onosproject.net.flow.instructions.Instruction; |
| 44 | import org.onosproject.net.flow.instructions.Instructions.OutputInstruction; |
| 45 | import org.onosproject.net.flowobjective.FlowObjectiveServiceAdapter; |
| 46 | import org.onosproject.net.flowobjective.ForwardingObjective; |
| 47 | import org.onosproject.net.flowobjective.NextObjective; |
| 48 | import org.opencord.cordconfig.CordConfigListener; |
| 49 | import org.opencord.cordconfig.CordConfigService; |
| 50 | import org.opencord.cordconfig.access.AccessAgentData; |
| 51 | import org.opencord.cordconfig.access.AccessDeviceData; |
| 52 | |
| 53 | import com.fasterxml.jackson.databind.JsonNode; |
| 54 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 55 | import com.google.common.collect.ImmutableMap; |
| 56 | |
| 57 | public 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 | } |