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 | |
Sonal Kasliwal | a0bbe6c | 2020-01-06 10:46:30 +0000 | [diff] [blame] | 18 | import java.util.Arrays; |
| 19 | import java.util.HashMap; |
| 20 | import java.util.HashSet; |
| 21 | import java.util.Iterator; |
| 22 | import java.util.List; |
| 23 | import java.util.Map; |
Sonal Kasliwal | a0bbe6c | 2020-01-06 10:46:30 +0000 | [diff] [blame] | 24 | import java.util.Set; |
| 25 | |
Esin Karaman | 996177c | 2020-03-05 13:21:09 +0000 | [diff] [blame] | 26 | import org.onlab.packet.Ip4Address; |
Sonal Kasliwal | a0bbe6c | 2020-01-06 10:46:30 +0000 | [diff] [blame] | 27 | import org.onlab.packet.IpAddress; |
Sonal Kasliwal | a0bbe6c | 2020-01-06 10:46:30 +0000 | [diff] [blame] | 28 | import org.onosproject.TestApplicationId; |
| 29 | import org.onosproject.core.ApplicationId; |
| 30 | import org.onosproject.core.CoreServiceAdapter; |
| 31 | import org.onosproject.mastership.MastershipServiceAdapter; |
| 32 | import org.onosproject.mcast.api.McastRoute; |
Esin Karaman | 996177c | 2020-03-05 13:21:09 +0000 | [diff] [blame] | 33 | import org.onosproject.net.AnnotationKeys; |
| 34 | import org.onosproject.net.Annotations; |
Sonal Kasliwal | a0bbe6c | 2020-01-06 10:46:30 +0000 | [diff] [blame] | 35 | import org.onosproject.net.ConnectPoint; |
Esin Karaman | 996177c | 2020-03-05 13:21:09 +0000 | [diff] [blame] | 36 | import org.onosproject.net.DefaultAnnotations; |
| 37 | import org.onosproject.net.DefaultDevice; |
| 38 | import org.onosproject.net.Device; |
Sonal Kasliwal | a0bbe6c | 2020-01-06 10:46:30 +0000 | [diff] [blame] | 39 | import org.onosproject.net.DeviceId; |
| 40 | import org.onosproject.net.HostId; |
| 41 | import org.onosproject.net.PortNumber; |
Esin Karaman | 996177c | 2020-03-05 13:21:09 +0000 | [diff] [blame] | 42 | import org.onosproject.net.SparseAnnotations; |
| 43 | import org.onosproject.net.device.DeviceServiceAdapter; |
Sonal Kasliwal | a0bbe6c | 2020-01-06 10:46:30 +0000 | [diff] [blame] | 44 | import org.onosproject.net.flow.TrafficSelector; |
| 45 | import org.onosproject.net.flow.TrafficTreatment; |
| 46 | import org.onosproject.net.flow.criteria.Criterion; |
| 47 | import org.onosproject.net.flow.criteria.IPCriterion; |
| 48 | import org.onosproject.net.flow.instructions.Instruction; |
| 49 | import org.onosproject.net.flow.instructions.Instructions.OutputInstruction; |
| 50 | import org.onosproject.net.flowobjective.FlowObjectiveServiceAdapter; |
| 51 | import org.onosproject.net.flowobjective.ForwardingObjective; |
| 52 | import org.onosproject.net.flowobjective.NextObjective; |
Sonal Kasliwal | a0bbe6c | 2020-01-06 10:46:30 +0000 | [diff] [blame] | 53 | |
Sonal Kasliwal | a0bbe6c | 2020-01-06 10:46:30 +0000 | [diff] [blame] | 54 | import com.google.common.collect.ImmutableMap; |
Esin Karaman | 996177c | 2020-03-05 13:21:09 +0000 | [diff] [blame] | 55 | import org.opencord.sadis.BandwidthProfileInformation; |
| 56 | import org.opencord.sadis.BaseInformationService; |
| 57 | import org.opencord.sadis.SadisService; |
| 58 | import org.opencord.sadis.SubscriberAndDeviceInformation; |
Sonal Kasliwal | a0bbe6c | 2020-01-06 10:46:30 +0000 | [diff] [blame] | 59 | |
| 60 | public class McastTestBase { |
| 61 | |
| 62 | // Map to store the forwardingObjective in flowObjectiveService.forward() |
| 63 | Map<DeviceId, ForwardingObjective> forwardMap = new HashMap<>(); |
| 64 | // Map to store the nextObjective in flowObjectiveService.next() |
| 65 | Map<DeviceId, NextObjective> nextMap = new HashMap<>(); |
| 66 | // Device configuration |
| 67 | protected static final DeviceId DEVICE_ID_OF_A = DeviceId.deviceId("of:00000a0a0a0a0a00"); |
| 68 | // Port number |
| 69 | protected static final PortNumber PORT_A = PortNumber.portNumber(1048576); |
| 70 | protected static final PortNumber PORT_B = PortNumber.portNumber(16); |
| 71 | protected static final PortNumber PORT_C = PortNumber.portNumber(24); |
| 72 | |
| 73 | // Connect Point for creating source and sink |
| 74 | protected static final ConnectPoint CONNECT_POINT_A = new ConnectPoint(DEVICE_ID_OF_A, PORT_A); |
| 75 | protected static final ConnectPoint CONNECT_POINT_B = new ConnectPoint(DEVICE_ID_OF_A, PORT_B); |
| 76 | protected static final ConnectPoint CONNECT_POINT_C = new ConnectPoint(DEVICE_ID_OF_A, PORT_C); |
| 77 | |
Esin Karaman | 996177c | 2020-03-05 13:21:09 +0000 | [diff] [blame] | 78 | // serial number of the device A |
| 79 | protected static final String SERIAL_NUMBER_OF_DEVICE_A = "serialNumberOfDevA"; |
| 80 | // Management ip address of the device A |
| 81 | protected static final Ip4Address MANAGEMENT_IP_OF_A = Ip4Address.valueOf("10.177.125.4"); |
Sonal Kasliwal | a0bbe6c | 2020-01-06 10:46:30 +0000 | [diff] [blame] | 82 | //Host id configuration |
| 83 | protected static final HostId HOST_ID_NONE = HostId.NONE; |
| 84 | // Source connect point |
| 85 | protected static final Set<ConnectPoint> SOURCES_CP = new HashSet<ConnectPoint>(Arrays.asList(CONNECT_POINT_A)); |
| 86 | Map<HostId, Set<ConnectPoint>> sources = ImmutableMap.of(HOST_ID_NONE, SOURCES_CP); |
| 87 | |
| 88 | protected static final IpAddress MULTICAST_IP = IpAddress.valueOf("224.0.0.22"); |
| 89 | // Creating dummy route with IGMP type. |
| 90 | McastRoute route1 = new McastRoute(null, MULTICAST_IP, McastRoute.Type.IGMP); |
| 91 | |
| 92 | // Creating empty sink used in prevRoute |
| 93 | Set<ConnectPoint> sinksCp = new HashSet<ConnectPoint>(Arrays.asList()); |
| 94 | Map<HostId, Set<ConnectPoint>> sinks = ImmutableMap.of(HOST_ID_NONE, sinksCp); |
| 95 | |
| 96 | // Creating empty source |
| 97 | Set<ConnectPoint> sourceCp = new HashSet<ConnectPoint>(Arrays.asList()); |
| 98 | Map<HostId, Set<ConnectPoint>> emptySource = ImmutableMap.of(HOST_ID_NONE, sourceCp); |
| 99 | |
| 100 | // Flag to check unknown olt device |
| 101 | boolean knownOltFlag = false; |
| 102 | |
| 103 | class MockCoreService extends CoreServiceAdapter { |
| 104 | @Override |
| 105 | public ApplicationId registerApplication(String name) { |
| 106 | ApplicationId testApplicationId = TestApplicationId.create("org.opencord.cordmcast"); |
| 107 | return testApplicationId; |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | class MockFlowObjectiveService extends FlowObjectiveServiceAdapter { |
| 112 | @Override |
| 113 | public void forward(DeviceId deviceId, ForwardingObjective forwardingObjective) { |
| 114 | synchronized (forwardMap) { |
| 115 | forwardMap.put(deviceId, forwardingObjective); |
| 116 | forwardMap.notify(); |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | @Override |
| 121 | public void next(DeviceId deviceId, NextObjective nextObjective) { |
| 122 | nextMap.put(deviceId, nextObjective); |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | class TestMastershipService extends MastershipServiceAdapter { |
| 127 | @Override |
| 128 | public boolean isLocalMaster(DeviceId deviceId) { |
| 129 | return true; |
| 130 | } |
| 131 | } |
| 132 | |
Esin Karaman | 996177c | 2020-03-05 13:21:09 +0000 | [diff] [blame] | 133 | protected class MockSadisService implements SadisService { |
Sonal Kasliwal | a0bbe6c | 2020-01-06 10:46:30 +0000 | [diff] [blame] | 134 | |
Esin Karaman | 996177c | 2020-03-05 13:21:09 +0000 | [diff] [blame] | 135 | @Override |
| 136 | public BaseInformationService<SubscriberAndDeviceInformation> getSubscriberInfoService() { |
| 137 | return new MockSubService(); |
| 138 | } |
Sonal Kasliwal | a0bbe6c | 2020-01-06 10:46:30 +0000 | [diff] [blame] | 139 | |
Esin Karaman | 996177c | 2020-03-05 13:21:09 +0000 | [diff] [blame] | 140 | @Override |
| 141 | public BaseInformationService<BandwidthProfileInformation> getBandwidthProfileService() { |
| 142 | return null; |
| 143 | } |
| 144 | } |
Sonal Kasliwal | a0bbe6c | 2020-01-06 10:46:30 +0000 | [diff] [blame] | 145 | |
Esin Karaman | 996177c | 2020-03-05 13:21:09 +0000 | [diff] [blame] | 146 | private class MockSubService implements BaseInformationService<SubscriberAndDeviceInformation> { |
| 147 | MockSubscriberAndDeviceInformation deviceA = |
| 148 | new MockSubscriberAndDeviceInformation(SERIAL_NUMBER_OF_DEVICE_A, MANAGEMENT_IP_OF_A); |
Sonal Kasliwal | a0bbe6c | 2020-01-06 10:46:30 +0000 | [diff] [blame] | 149 | |
Esin Karaman | 996177c | 2020-03-05 13:21:09 +0000 | [diff] [blame] | 150 | @Override |
| 151 | public SubscriberAndDeviceInformation get(String id) { |
| 152 | return SERIAL_NUMBER_OF_DEVICE_A.equals(id) ? deviceA : null; |
| 153 | } |
Sonal Kasliwal | a0bbe6c | 2020-01-06 10:46:30 +0000 | [diff] [blame] | 154 | |
Esin Karaman | 996177c | 2020-03-05 13:21:09 +0000 | [diff] [blame] | 155 | @Override |
| 156 | public void invalidateAll() { |
| 157 | } |
Sonal Kasliwal | a0bbe6c | 2020-01-06 10:46:30 +0000 | [diff] [blame] | 158 | |
Esin Karaman | 996177c | 2020-03-05 13:21:09 +0000 | [diff] [blame] | 159 | @Override |
| 160 | public void invalidateId(String id) { |
| 161 | } |
Sonal Kasliwal | a0bbe6c | 2020-01-06 10:46:30 +0000 | [diff] [blame] | 162 | |
Esin Karaman | 996177c | 2020-03-05 13:21:09 +0000 | [diff] [blame] | 163 | @Override |
| 164 | public SubscriberAndDeviceInformation getfromCache(String id) { |
| 165 | return null; |
| 166 | } |
| 167 | } |
Sonal Kasliwal | a0bbe6c | 2020-01-06 10:46:30 +0000 | [diff] [blame] | 168 | |
Esin Karaman | 996177c | 2020-03-05 13:21:09 +0000 | [diff] [blame] | 169 | private class MockSubscriberAndDeviceInformation extends SubscriberAndDeviceInformation { |
Sonal Kasliwal | a0bbe6c | 2020-01-06 10:46:30 +0000 | [diff] [blame] | 170 | |
Esin Karaman | 996177c | 2020-03-05 13:21:09 +0000 | [diff] [blame] | 171 | MockSubscriberAndDeviceInformation(String id, Ip4Address ipAddress) { |
| 172 | this.setId(id); |
| 173 | this.setIPAddress(ipAddress); |
| 174 | this.setUplinkPort((int) PORT_A.toLong()); |
| 175 | } |
| 176 | } |
Sonal Kasliwal | a0bbe6c | 2020-01-06 10:46:30 +0000 | [diff] [blame] | 177 | |
Esin Karaman | 996177c | 2020-03-05 13:21:09 +0000 | [diff] [blame] | 178 | class MockDeviceService extends DeviceServiceAdapter { |
Sonal Kasliwal | a0bbe6c | 2020-01-06 10:46:30 +0000 | [diff] [blame] | 179 | |
Esin Karaman | 996177c | 2020-03-05 13:21:09 +0000 | [diff] [blame] | 180 | @Override |
| 181 | public Device getDevice(DeviceId deviceId) { |
| 182 | if (DEVICE_ID_OF_A.equals(deviceId)) { |
| 183 | DefaultAnnotations.Builder annotationsBuilder = DefaultAnnotations.builder() |
| 184 | .set(AnnotationKeys.MANAGEMENT_ADDRESS, MANAGEMENT_IP_OF_A.toString()); |
| 185 | SparseAnnotations annotations = annotationsBuilder.build(); |
| 186 | Annotations[] da = {annotations}; |
| 187 | |
| 188 | Device deviceA = new DefaultDevice(null, DEVICE_ID_OF_A, Device.Type.OTHER, "", "", |
| 189 | "", SERIAL_NUMBER_OF_DEVICE_A, null, da); |
| 190 | return deviceA; |
| 191 | } else { |
| 192 | knownOltFlag = true; |
| 193 | } |
| 194 | return null; |
| 195 | } |
| 196 | } |
Sonal Kasliwal | a0bbe6c | 2020-01-06 10:46:30 +0000 | [diff] [blame] | 197 | |
| 198 | public OutputInstruction outputPort(TrafficTreatment trafficTreatment) { |
| 199 | List<Instruction> listOfInstructions = trafficTreatment.allInstructions(); |
| 200 | OutputInstruction output = null; |
| 201 | for (Instruction intruction : listOfInstructions) { |
| 202 | output = (OutputInstruction) intruction; |
| 203 | } |
| 204 | return output; |
| 205 | } |
| 206 | |
| 207 | public IPCriterion ipAddress(TrafficSelector trafficSelector) { |
| 208 | Set<Criterion> criterionSet = trafficSelector.criteria(); |
| 209 | Iterator<Criterion> it = criterionSet.iterator(); |
| 210 | IPCriterion ipCriterion = null; |
| 211 | while (it.hasNext()) { |
| 212 | Criterion criteria = it.next(); |
| 213 | if (Criterion.Type.IPV4_DST == criteria.type()) { |
| 214 | ipCriterion = (IPCriterion) criteria; |
| 215 | } |
| 216 | } |
| 217 | return (IPCriterion) ipCriterion; |
| 218 | } |
| 219 | |
| 220 | } |