amit.ghosh | 6104cf5 | 2021-01-07 16:53:28 +0100 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2018-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.olttopology.impl; |
| 17 | |
| 18 | import com.google.common.collect.Lists; |
| 19 | import com.google.common.collect.Sets; |
| 20 | import org.apache.commons.lang.ArrayUtils; |
| 21 | import org.easymock.EasyMock; |
| 22 | import org.junit.After; |
| 23 | import org.junit.Before; |
| 24 | import org.junit.Test; |
| 25 | import org.onlab.osgi.ComponentContextAdapter; |
| 26 | import org.onlab.packet.ChassisId; |
| 27 | import org.onlab.packet.Ethernet; |
| 28 | import org.onlab.packet.Ip4Address; |
| 29 | import org.onlab.packet.LLDP; |
| 30 | import org.onlab.packet.LLDPTLV; |
| 31 | import org.onlab.packet.MacAddress; |
| 32 | import org.onlab.packet.VlanId; |
| 33 | import org.onosproject.cfg.ComponentConfigService; |
| 34 | import org.onosproject.codec.CodecService; |
| 35 | import org.onosproject.core.ApplicationId; |
| 36 | import org.onosproject.core.CoreServiceAdapter; |
| 37 | import org.onosproject.mastership.MastershipServiceAdapter; |
| 38 | import org.onosproject.net.AnnotationKeys; |
| 39 | import org.onosproject.net.Annotations; |
| 40 | import org.onosproject.net.ConnectPoint; |
| 41 | import org.onosproject.net.DefaultAnnotations; |
| 42 | import org.onosproject.net.DefaultDevice; |
| 43 | import org.onosproject.net.Device; |
| 44 | import org.onosproject.net.DeviceId; |
| 45 | import org.onosproject.net.Element; |
| 46 | import org.onosproject.net.Port; |
| 47 | import org.onosproject.net.PortNumber; |
| 48 | import org.onosproject.net.config.NetworkConfigListener; |
| 49 | import org.onosproject.net.device.DeviceEvent; |
| 50 | import org.onosproject.net.device.DeviceListener; |
| 51 | import org.onosproject.net.device.DeviceServiceAdapter; |
| 52 | import org.onosproject.net.provider.ProviderId; |
| 53 | import org.onosproject.store.service.TestStorageService; |
| 54 | import org.opencord.sadis.BaseInformationService; |
| 55 | import org.opencord.sadis.SadisService; |
| 56 | import org.opencord.sadis.SubscriberAndDeviceInformation; |
| 57 | import org.opencord.sadis.UniTagInformation; |
| 58 | import org.slf4j.Logger; |
| 59 | |
| 60 | import java.nio.ByteBuffer; |
| 61 | import java.util.ArrayList; |
| 62 | import java.util.HashMap; |
| 63 | import java.util.List; |
| 64 | import java.util.Map; |
| 65 | import java.util.Set; |
| 66 | |
| 67 | import static org.hamcrest.MatcherAssert.assertThat; |
| 68 | import static org.hamcrest.Matchers.is; |
| 69 | import static org.hamcrest.core.IsNull.notNullValue; |
| 70 | import static org.onosproject.net.intent.TestTools.assertAfter; |
| 71 | import static org.opencord.olttopology.impl.OltTopology.SYSTEMNAME_TLV_TYPE; |
| 72 | import static org.slf4j.LoggerFactory.getLogger; |
| 73 | |
| 74 | /** |
| 75 | * Tests OLT topology app. |
| 76 | */ |
| 77 | public class OltTopologyTest extends OltTopologyTestBase { |
| 78 | private static final VlanId CLIENT_C_TAG = VlanId.vlanId((short) 999); |
| 79 | private static final VlanId CLIENT_S_TAG = VlanId.vlanId((short) 111); |
| 80 | private static final String CLIENT_NAS_PORT_ID = "PON 1/1"; |
| 81 | private static final String CLIENT_CIRCUIT_ID = "CIR-PON 1/1"; |
| 82 | private static final String OLT_DEV_ID = "of:0000c6b1cd40dc93"; |
| 83 | private static final MacAddress OLT_MAC_ADDRESS = MacAddress.valueOf("01:02:03:04:05:06"); |
| 84 | private static final DeviceId DEVICE_ID_1 = DeviceId.deviceId(OLT_DEV_ID); |
| 85 | private static final String SCHEME_NAME = "olttopology"; |
| 86 | private static final DefaultAnnotations DEVICE_ANNOTATIONS = DefaultAnnotations.builder() |
| 87 | .set(AnnotationKeys.PROTOCOL, SCHEME_NAME.toUpperCase()).build(); |
| 88 | private final Logger log = getLogger(getClass()); |
| 89 | ComponentConfigService mockConfigService = |
| 90 | EasyMock.createMock(ComponentConfigService.class); |
| 91 | CodecService mockCodecService = |
| 92 | EasyMock.createMock(CodecService.class); |
| 93 | private OltTopology oltTopology; |
| 94 | private NetworkConfigListener configListener; |
| 95 | private DeviceListener deviceListener; |
| 96 | |
| 97 | @Before |
| 98 | public void setUp() { |
| 99 | oltTopology = new OltTopology(); |
| 100 | oltTopology.mastershipService = new MockMastershipService(); |
| 101 | oltTopology.deviceService = new MockDeviceService(); |
| 102 | oltTopology.coreService = new MockCoreService(); |
| 103 | oltTopology.componentConfigService = mockConfigService; |
| 104 | oltTopology.packetService = new MockPacketService(); |
| 105 | oltTopology.flowObjectiveService = new MockFlowObjectiveService(); |
| 106 | oltTopology.storageService = new TestStorageService(); |
| 107 | oltTopology.codecService = mockCodecService; |
| 108 | oltTopology.subsService = new MockSubService(); |
| 109 | oltTopology.sadisService = new MockSadisService(); |
| 110 | oltTopology.subsService.get(OLT_DEV_ID).setUplinkPort(1); |
| 111 | oltTopology.activate(new ComponentContextAdapter()); |
| 112 | } |
| 113 | |
| 114 | @After |
| 115 | public void tearDown() { |
| 116 | oltTopology.deactivate(); |
| 117 | |
| 118 | } |
| 119 | |
| 120 | private DeviceEvent deviceEvent(DeviceEvent.Type type, DeviceId did, Port port) { |
| 121 | return new DeviceEvent(type, oltTopology.deviceService.getDevice(did), port); |
| 122 | |
| 123 | } |
| 124 | |
| 125 | /** |
| 126 | * Fetches the sent packet at the given index. The requested packet |
| 127 | * must be the last packet on the list. |
| 128 | * |
| 129 | * @param index index into sent packets array |
| 130 | * @return packet |
| 131 | */ |
| 132 | private Ethernet fetchPacket(int index) { |
| 133 | for (int iteration = 0; iteration < 20; iteration++) { |
| 134 | if (savedPackets.size() > index) { |
| 135 | return (Ethernet) savedPackets.get(index); |
| 136 | } else { |
| 137 | try { |
| 138 | Thread.sleep(250); |
| 139 | } catch (Exception ex) { |
| 140 | return null; |
| 141 | } |
| 142 | } |
| 143 | } |
| 144 | return null; |
| 145 | } |
| 146 | |
| 147 | /** |
| 148 | * Testing Port Added Scenario. |
| 149 | * Note: Need to See second packet as first will be sent when MockPort is called |
| 150 | * |
| 151 | */ |
| 152 | @Test |
| 153 | public void testPortAdded() { |
| 154 | Device d = oltTopology.deviceService.getDevice(DEVICE_ID_1); |
| 155 | Port port = new MockPort(); |
| 156 | DeviceEvent portAdd = deviceEvent(DeviceEvent.Type.PORT_ADDED, DEVICE_ID_1, port); |
| 157 | deviceListener.event(portAdd); |
| 158 | Ethernet responsePacket = fetchPacket(1); |
| 159 | assertThat(responsePacket, notNullValue()); |
| 160 | checkLldpPacket(responsePacket); |
| 161 | } |
| 162 | |
| 163 | /** |
| 164 | * Testing Port Delete Scenario. |
| 165 | * |
| 166 | */ |
| 167 | @Test |
| 168 | public void testPortDeleted() { |
| 169 | Device d = oltTopology.deviceService.getDevice(DEVICE_ID_1); |
| 170 | Port port = new MockPort(); |
| 171 | DeviceEvent portAdd = deviceEvent(DeviceEvent.Type.PORT_ADDED, DEVICE_ID_1, port); |
| 172 | deviceListener.event(portAdd); |
| 173 | Ethernet responsePacket = fetchPacket(1); |
| 174 | assertThat(responsePacket, notNullValue()); |
| 175 | checkLldpPacket(responsePacket); |
| 176 | DeviceEvent portRem = deviceEvent(DeviceEvent.Type.PORT_REMOVED, DEVICE_ID_1, port); |
| 177 | deviceListener.event(portRem); |
| 178 | } |
| 179 | |
| 180 | /** |
| 181 | * Tests LLDP packet in scenario. |
| 182 | * |
| 183 | */ |
| 184 | @Test |
| 185 | public void testHandlePacket() { |
| 186 | Device d = oltTopology.deviceService.getDevice(DEVICE_ID_1); |
| 187 | Port port = new MockPort(); |
| 188 | DeviceEvent portAdd = deviceEvent(DeviceEvent.Type.PORT_ADDED, DEVICE_ID_1, port); |
| 189 | deviceListener.event(portAdd); |
| 190 | MacAddress destMac = MacAddress.valueOf(OsgiPropertyConstants.DEFAULT_DEST_MAC_ADDRESS_DEFAULT); |
| 191 | MacAddress srcMac = MacAddress.valueOf("c6:b1:cd:40:dc:93"); |
| 192 | String serialNumber = "switch-1"; |
| 193 | final short ttlInSec = 120; |
| 194 | final short chasisId = 0; |
| 195 | String portName = "p0"; |
| 196 | Ip4Address devIpAddr = Ip4Address.valueOf("192.168.1.1"); |
| 197 | Ethernet packet = createLldpPacket(destMac, srcMac, chasisId, portName, |
| 198 | ttlInSec, serialNumber, devIpAddr.toString()); |
| 199 | |
| 200 | ConnectPoint cp = new ConnectPoint(d.id(), port.number()); |
| 201 | sendInboundPacket(packet, cp); |
| 202 | |
| 203 | assertAfter(ASSERTION_DELAY, ASSERTION_LENGTH, () -> { |
| 204 | validateNeighborList(oltTopology.getNeighbours()); |
| 205 | }); |
| 206 | } |
| 207 | |
| 208 | /** |
| 209 | * Tests Packet out timer functionality. |
| 210 | * |
| 211 | * @throws Exception |
| 212 | */ |
| 213 | @Test |
| 214 | public void testOltTopologyTimerTask() throws Exception { |
| 215 | Device d = oltTopology.deviceService.getDevice(DEVICE_ID_1); |
| 216 | Port port = new MockPort(); |
| 217 | DeviceEvent portAdd = deviceEvent(DeviceEvent.Type.PORT_ADDED, DEVICE_ID_1, port); |
| 218 | deviceListener.event(portAdd); |
| 219 | oltTopology.lldpPeriodicity(2); |
| 220 | Thread.sleep(2000); |
| 221 | Ethernet responsePacket = fetchPacket(1); |
| 222 | assertThat(responsePacket, notNullValue()); |
| 223 | checkLldpPacket(responsePacket); |
| 224 | } |
| 225 | |
| 226 | /** |
| 227 | * Testing Device Delete Scenario with Packet Out. |
| 228 | * |
| 229 | */ |
| 230 | @Test |
| 231 | public void testDeviceDeleted() { |
| 232 | log.info("oltTopology {}", oltTopology); |
| 233 | Device d = oltTopology.deviceService.getDevice(DEVICE_ID_1); |
| 234 | Port port = new MockPort(); |
| 235 | DeviceEvent portAdd = deviceEvent(DeviceEvent.Type.PORT_ADDED, DEVICE_ID_1, port); |
| 236 | deviceListener.event(portAdd); |
| 237 | Ethernet responsePacket = fetchPacket(1); |
| 238 | assertThat(responsePacket, notNullValue()); |
| 239 | checkLldpPacket(responsePacket); |
| 240 | DeviceEvent portRem = deviceEvent(DeviceEvent.Type.DEVICE_REMOVED, DEVICE_ID_1, port); |
| 241 | deviceListener.event(portRem); |
| 242 | } |
| 243 | |
| 244 | /** |
| 245 | * Testing Port Delete Scenario After Packet Handle for Packet in. |
| 246 | * |
| 247 | */ |
| 248 | @Test |
| 249 | public void testPortDelAfterHandlePacket() { |
| 250 | Device d = oltTopology.deviceService.getDevice(DEVICE_ID_1); |
| 251 | Port port = new MockPort(); |
| 252 | DeviceEvent portAdd = deviceEvent(DeviceEvent.Type.PORT_ADDED, DEVICE_ID_1, port); |
| 253 | deviceListener.event(portAdd); |
| 254 | MacAddress destMac = MacAddress.valueOf(OsgiPropertyConstants.DEFAULT_DEST_MAC_ADDRESS_DEFAULT); |
| 255 | MacAddress srcMac = MacAddress.valueOf("c6:b1:cd:40:dc:93"); |
| 256 | String serialNumber = "switch-1"; |
| 257 | final short ttlInSec = 120; |
| 258 | final short chasisId = 0; |
| 259 | String portName = "p0"; |
| 260 | Ip4Address devIpAddr = Ip4Address.valueOf("192.168.1.1"); |
| 261 | Ethernet packet = createLldpPacket(destMac, srcMac, chasisId, portName, |
| 262 | ttlInSec, serialNumber, devIpAddr.toString()); |
| 263 | |
| 264 | ConnectPoint cp = new ConnectPoint(d.id(), port.number()); |
| 265 | sendInboundPacket(packet, cp); |
| 266 | // Need to have a delay before the validation as the packet processing now happens in a different thread |
| 267 | assertAfter(ASSERTION_DELAY, ASSERTION_LENGTH, () -> { |
| 268 | validateNeighborList(oltTopology.getNeighbours()); |
| 269 | }); |
| 270 | DeviceEvent portRem = deviceEvent(DeviceEvent.Type.PORT_REMOVED, DEVICE_ID_1, port); |
| 271 | deviceListener.event(portRem); |
| 272 | assertAfter(ASSERTION_DELAY, ASSERTION_LENGTH, () -> { |
| 273 | assertThat(oltTopology.getNeighbours().size(), is(0)); |
| 274 | }); |
| 275 | } |
| 276 | |
| 277 | /** |
| 278 | * Testing Device Delete Scenario After Packet Handle for Packet in. |
| 279 | * |
| 280 | */ |
| 281 | @Test |
| 282 | public void testDeviceDelAfterHandlePacket() { |
| 283 | Device d = oltTopology.deviceService.getDevice(DEVICE_ID_1); |
| 284 | Port port = new MockPort(); |
| 285 | DeviceEvent portAdd = deviceEvent(DeviceEvent.Type.PORT_ADDED, DEVICE_ID_1, port); |
| 286 | deviceListener.event(portAdd); |
| 287 | MacAddress destMac = MacAddress.valueOf(OsgiPropertyConstants.DEFAULT_DEST_MAC_ADDRESS_DEFAULT); |
| 288 | MacAddress srcMac = MacAddress.valueOf("c6:b1:cd:40:dc:93"); |
| 289 | String serialNumber = "switch-1"; |
| 290 | final short ttlInSec = 120; |
| 291 | final short chasisId = 0; |
| 292 | String portName = "p0"; |
| 293 | Ip4Address devIpAddr = Ip4Address.valueOf("192.168.1.1"); |
| 294 | Ethernet packet = createLldpPacket(destMac, srcMac, chasisId, portName, |
| 295 | ttlInSec, serialNumber, devIpAddr.toString()); |
| 296 | |
| 297 | ConnectPoint cp = new ConnectPoint(d.id(), port.number()); |
| 298 | sendInboundPacket(packet, cp); |
| 299 | assertAfter(ASSERTION_DELAY, ASSERTION_LENGTH, () -> { |
| 300 | validateNeighborList(oltTopology.getNeighbours()); |
| 301 | assertThat(oltTopology.getNeighbours().size(), is(1)); |
| 302 | }); |
| 303 | DeviceEvent portRem = deviceEvent(DeviceEvent.Type.DEVICE_REMOVED, DEVICE_ID_1, port); |
| 304 | deviceListener.event(portRem); |
| 305 | assertAfter(ASSERTION_DELAY, ASSERTION_LENGTH, () -> { |
| 306 | assertThat(oltTopology.getNeighbours().size(), is(0)); |
| 307 | }); |
| 308 | } |
| 309 | |
| 310 | /** |
| 311 | * Creates dummy packet for LLDP packet to test Packet in scenario. |
| 312 | * |
| 313 | * @param destMac Destination Mac address of LLDP packet |
| 314 | * @param srcMac Source mac address of LLDP packet |
| 315 | * @param chassisId Chassis ID tlv value |
| 316 | * @param port port Id tlv value |
| 317 | * @param ttl TTL tlv value |
| 318 | * @param systemName System name tlv value |
| 319 | * @param mgmtAddr Management Address tlv value |
| 320 | * @return LLDP ethernet packet |
| 321 | */ |
| 322 | private Ethernet createLldpPacket(MacAddress destMac, MacAddress srcMac, |
| 323 | int chassisId, String port, short ttl, |
| 324 | String systemName, String mgmtAddr) { |
| 325 | Ethernet ethPkt = new Ethernet(); |
| 326 | ethPkt.setEtherType(Ethernet.TYPE_LLDP); |
| 327 | ethPkt.setDestinationMACAddress(destMac); |
| 328 | ethPkt.setSourceMACAddress(srcMac); |
| 329 | |
| 330 | LLDP lldpPkt = new LLDP(); |
| 331 | |
| 332 | setChassisId(lldpPkt, chassisId); |
| 333 | setPortId(lldpPkt, port); |
| 334 | setTtl(lldpPkt, ttl); |
| 335 | |
| 336 | List<LLDPTLV> optionalTlv = new ArrayList<>(); |
| 337 | optionalTlv.add(createSystemNameTlv(systemName)); |
| 338 | optionalTlv.add(createMgmtAddressTlv(mgmtAddr)); |
| 339 | |
| 340 | lldpPkt.setOptionalTLVList(optionalTlv); |
| 341 | |
| 342 | ethPkt.setPayload(lldpPkt); |
| 343 | return ethPkt; |
| 344 | } |
| 345 | |
| 346 | /** |
| 347 | * Sets Chassis ID TLV for LLDP packet. |
| 348 | * |
| 349 | * @param lldpPkt LLDP packet reference |
| 350 | * @param chassisId Chassid ID tlv value |
| 351 | */ |
| 352 | private void setChassisId(LLDP lldpPkt, final int chassisId) { |
| 353 | final byte chassisTlvSubtype = 1; |
| 354 | |
| 355 | byte[] chassis = ArrayUtils.addAll(new byte[]{chassisTlvSubtype}, |
| 356 | ByteBuffer.allocate(String.valueOf(chassisId).length()) |
| 357 | .put(String.valueOf(chassisId).getBytes()).array()); |
| 358 | |
| 359 | LLDPTLV chassisTlv = new LLDPTLV(); |
| 360 | lldpPkt.setChassisId(chassisTlv.setLength((byte) chassis.length) |
| 361 | .setType(LLDP.CHASSIS_TLV_TYPE) |
| 362 | .setValue(chassis)); |
| 363 | } |
| 364 | |
| 365 | /** |
| 366 | * Sets Port ID tlv for LLDP packet. |
| 367 | * |
| 368 | * @param lldpPkt LLDP packet reference |
| 369 | * @param ifaceName Port Name TLV value |
| 370 | */ |
| 371 | public void setPortId(LLDP lldpPkt, final String ifaceName) { |
| 372 | final byte portTlvSubtype = 5; |
| 373 | |
| 374 | byte[] port = ArrayUtils.addAll(new byte[]{portTlvSubtype}, |
| 375 | ifaceName.getBytes()); |
| 376 | |
| 377 | LLDPTLV portTlv = new LLDPTLV(); |
| 378 | lldpPkt.setPortId(portTlv.setLength((byte) port.length) |
| 379 | .setType(LLDP.PORT_TLV_TYPE) |
| 380 | .setValue(port)); |
| 381 | } |
| 382 | |
| 383 | /** |
| 384 | * Sets TTL tlv for LLDP packet. |
| 385 | * |
| 386 | * @param lldpPkt LLDP Packet reference |
| 387 | * @param timeInSecs TTL tlv value in sec |
| 388 | */ |
| 389 | public void setTtl(LLDP lldpPkt, final short timeInSecs) { |
| 390 | byte[] time = ByteBuffer.allocate(2).putShort(timeInSecs).array(); |
| 391 | LLDPTLV ttlTlv = new LLDPTLV(); |
| 392 | lldpPkt.setTtl(ttlTlv.setType(LLDP.TTL_TLV_TYPE) |
| 393 | .setLength((short) time.length) |
| 394 | .setValue(time)); |
| 395 | } |
| 396 | |
| 397 | /** |
| 398 | * Sets System name tlv for LLDP packet. |
| 399 | * |
| 400 | * @param systemName System name tlv value |
| 401 | * @return systemName tlv |
| 402 | */ |
| 403 | public LLDPTLV createSystemNameTlv(String systemName) { |
| 404 | byte[] bytes = systemName.getBytes(); |
| 405 | LLDPTLV sysNameTlv = new LLDPTLV(); |
| 406 | return sysNameTlv.setType(SYSTEMNAME_TLV_TYPE) |
| 407 | .setLength((byte) bytes.length) |
| 408 | .setValue(bytes); |
| 409 | } |
| 410 | |
| 411 | /** |
| 412 | * Sets Management address tlv for LLDP packet. |
| 413 | * |
| 414 | * @param mgmtAddress Management address tlv value |
| 415 | * @return Management address tlv |
| 416 | */ |
| 417 | public LLDPTLV createMgmtAddressTlv(String mgmtAddress) { |
| 418 | final byte mgmtAddressTlvType = 0x8; |
| 419 | final byte ipAddressSubType = 0x1; // IPv4 |
| 420 | // 5 below is address subtype + IP4 address len |
| 421 | final byte ipAddrStrLen = 0x5; |
| 422 | final byte interfaceSubtype = 0x1; |
| 423 | final int interfaceNum = 0; |
| 424 | final byte oidString = 0x0; |
| 425 | Ip4Address ipAddr = Ip4Address.valueOf(mgmtAddress); |
| 426 | |
| 427 | byte[] addrStr = ArrayUtils.addAll(new byte[]{ipAddressSubType}, |
| 428 | ipAddr.toOctets()); |
| 429 | |
| 430 | byte[] ipAddrBytes = ArrayUtils.addAll(new byte[]{ipAddrStrLen}, |
| 431 | addrStr); |
| 432 | byte[] bytesInterfacetype = ArrayUtils.addAll(ipAddrBytes, |
| 433 | ByteBuffer.allocate(1).put(interfaceSubtype).array()); |
| 434 | byte[] bytesInterfaceNumber = ArrayUtils.addAll(bytesInterfacetype, |
| 435 | ByteBuffer.allocate(4).putInt(interfaceNum).array()); |
| 436 | byte[] finalMgmtAddrBytes = ArrayUtils.addAll(bytesInterfaceNumber, |
| 437 | ByteBuffer.allocate(1).put(oidString).array()); |
| 438 | |
| 439 | LLDPTLV mgmtAddrTlv = new LLDPTLV(); |
| 440 | return mgmtAddrTlv.setType(mgmtAddressTlvType) |
| 441 | .setLength((byte) finalMgmtAddrBytes.length) |
| 442 | .setValue(finalMgmtAddrBytes); |
| 443 | } |
| 444 | |
| 445 | /* |
| 446 | Mocks application id. |
| 447 | */ |
| 448 | private static final class MockApplicationId implements ApplicationId { |
| 449 | |
| 450 | private final short id; |
| 451 | private final String name; |
| 452 | |
| 453 | public MockApplicationId(short id, String name) { |
| 454 | this.id = id; |
| 455 | this.name = name; |
| 456 | } |
| 457 | |
| 458 | @Override |
| 459 | public short id() { |
| 460 | return id; |
| 461 | } |
| 462 | |
| 463 | @Override |
| 464 | public String name() { |
| 465 | return name; |
| 466 | } |
| 467 | } |
| 468 | |
| 469 | /* |
| 470 | Mocks Core service adapter. |
| 471 | */ |
| 472 | private static final class MockCoreService extends CoreServiceAdapter { |
| 473 | |
| 474 | private List<ApplicationId> idList = new ArrayList<>(); |
| 475 | private Map<String, ApplicationId> idMap = new HashMap<>(); |
| 476 | |
| 477 | /* |
| 478 | * (non-Javadoc) |
| 479 | * |
| 480 | * @see |
| 481 | * org.onosproject.core.CoreServiceAdapter#getAppId(java.lang.Short) |
| 482 | */ |
| 483 | @Override |
| 484 | public ApplicationId getAppId(Short id) { |
| 485 | if (id >= idList.size()) { |
| 486 | return null; |
| 487 | } |
| 488 | return idList.get(id); |
| 489 | } |
| 490 | |
| 491 | /* |
| 492 | * (non-Javadoc) |
| 493 | * |
| 494 | * @see |
| 495 | * org.onosproject.core.CoreServiceAdapter#getAppId(java.lang.String) |
| 496 | */ |
| 497 | @Override |
| 498 | public ApplicationId getAppId(String name) { |
| 499 | return idMap.get(name); |
| 500 | } |
| 501 | |
| 502 | /* |
| 503 | * (non-Javadoc) |
| 504 | * |
| 505 | * @see |
| 506 | * org.onosproject.core.CoreServiceAdapter#registerApplication(java.lang |
| 507 | * .String) |
| 508 | */ |
| 509 | @Override |
| 510 | public ApplicationId registerApplication(String name) { |
| 511 | ApplicationId appId = idMap.get(name); |
| 512 | if (appId == null) { |
| 513 | appId = new MockApplicationId((short) idList.size(), name); |
| 514 | idList.add(appId); |
| 515 | idMap.put(name, appId); |
| 516 | } |
| 517 | return appId; |
| 518 | } |
| 519 | |
| 520 | } |
| 521 | |
| 522 | private static class MockMastershipService extends MastershipServiceAdapter { |
| 523 | @Override |
| 524 | public boolean isLocalMaster(DeviceId d) { |
| 525 | return true; |
| 526 | } |
| 527 | } |
| 528 | |
| 529 | private static class MockDevice extends DefaultDevice { |
| 530 | |
| 531 | /* |
| 532 | Mocks OLT device. |
| 533 | */ |
| 534 | public MockDevice(ProviderId providerId, DeviceId id, Type type, |
| 535 | String manufacturer, String hwVersion, String swVersion, |
| 536 | String serialNumber, ChassisId chassisId, Annotations... annotations) { |
| 537 | super(providerId, id, type, manufacturer, hwVersion, swVersion, serialNumber, |
| 538 | chassisId, annotations); |
| 539 | } |
| 540 | } |
| 541 | |
| 542 | private static class MockSadisService implements SadisService { |
| 543 | |
| 544 | @Override |
| 545 | public MockSubService getSubscriberInfoService() { |
| 546 | return new MockSubService(); |
| 547 | } |
| 548 | |
| 549 | @Override |
| 550 | public MockSubService getBandwidthProfileService() { |
| 551 | return new MockSubService(); |
| 552 | } |
| 553 | |
| 554 | } |
| 555 | |
| 556 | /* |
| 557 | Mocks SubscriberAndDeviceInformationService(SADIS) information. |
| 558 | */ |
| 559 | private static class MockSubService implements BaseInformationService { |
| 560 | MockSubscriberAndDeviceInformation device = |
| 561 | new MockSubscriberAndDeviceInformation(OLT_DEV_ID, VlanId.NONE, VlanId.NONE, null, null, |
| 562 | OLT_MAC_ADDRESS, Ip4Address.valueOf("10.10.10.10")); |
| 563 | MockSubscriberAndDeviceInformation sub = |
| 564 | new MockSubscriberAndDeviceInformation(CLIENT_NAS_PORT_ID, CLIENT_C_TAG, |
| 565 | CLIENT_S_TAG, CLIENT_NAS_PORT_ID, CLIENT_CIRCUIT_ID, null, null); |
| 566 | |
| 567 | @Override |
| 568 | public SubscriberAndDeviceInformation get(String id) { |
| 569 | if (id.equals(OLT_DEV_ID)) { |
| 570 | return device; |
| 571 | } else { |
| 572 | return sub; |
| 573 | } |
| 574 | } |
| 575 | |
| 576 | @Override |
| 577 | public void invalidateAll() { |
| 578 | } |
| 579 | |
| 580 | public void invalidateId(String id) { |
| 581 | } |
| 582 | |
| 583 | public SubscriberAndDeviceInformation getfromCache(String id) { |
| 584 | return null; |
| 585 | } |
| 586 | } |
| 587 | |
| 588 | /* |
| 589 | Mocks SubscriberAndDeviceInformation. |
| 590 | */ |
| 591 | private static class MockSubscriberAndDeviceInformation extends SubscriberAndDeviceInformation { |
| 592 | |
| 593 | MockSubscriberAndDeviceInformation(String id, VlanId ctag, |
| 594 | VlanId stag, String nasPortId, |
| 595 | String circuitId, MacAddress hardId, |
| 596 | Ip4Address ipAddress) { |
| 597 | UniTagInformation uniTagInformation = new UniTagInformation.Builder() |
| 598 | .setPonCTag(ctag) |
| 599 | .setPonSTag(stag) |
| 600 | .build(); |
| 601 | List<UniTagInformation> uniTagInformationList = Lists.newArrayList(uniTagInformation); |
| 602 | this.setUniTagList(uniTagInformationList); |
| 603 | // this.setCTag(ctag); |
| 604 | this.setHardwareIdentifier(hardId); |
| 605 | this.setId(id); |
| 606 | this.setIPAddress(ipAddress); |
| 607 | // this.setSTag(stag); |
| 608 | this.setNasPortId(nasPortId); |
| 609 | this.setCircuitId(circuitId); |
| 610 | } |
| 611 | } |
| 612 | |
| 613 | private static class MockPort implements Port { |
| 614 | |
| 615 | @Override |
| 616 | public boolean isEnabled() { |
| 617 | return true; |
| 618 | } |
| 619 | |
| 620 | public long portSpeed() { |
| 621 | return 1000; |
| 622 | } |
| 623 | |
| 624 | public Element element() { |
| 625 | return null; |
| 626 | } |
| 627 | |
| 628 | public PortNumber number() { |
| 629 | return PortNumber.portNumber(1); |
| 630 | } |
| 631 | |
| 632 | public Annotations annotations() { |
| 633 | return new MockAnnotations(); |
| 634 | } |
| 635 | |
| 636 | public Type type() { |
| 637 | return Port.Type.FIBER; |
| 638 | } |
| 639 | |
| 640 | private static class MockAnnotations implements Annotations { |
| 641 | |
| 642 | @Override |
| 643 | public String value(String val) { |
| 644 | return "nni-"; |
| 645 | } |
| 646 | |
| 647 | public Set<String> keys() { |
| 648 | return Sets.newHashSet("portName"); |
| 649 | } |
| 650 | } |
| 651 | } |
| 652 | |
| 653 | /* |
| 654 | Mocks Device Service Adapter. |
| 655 | */ |
| 656 | private class MockDeviceService extends DeviceServiceAdapter { |
| 657 | |
| 658 | private ProviderId providerId = new ProviderId("of", "foo"); |
| 659 | private final Device device1 = new MockDevice(providerId, DEVICE_ID_1, Device.Type.SWITCH, |
| 660 | "foo.inc", "0", "0", OLT_DEV_ID, new ChassisId(), |
| 661 | DEVICE_ANNOTATIONS); |
| 662 | |
| 663 | @Override |
| 664 | public Device getDevice(DeviceId devId) { |
| 665 | return device1; |
| 666 | |
| 667 | } |
| 668 | |
| 669 | @Override |
| 670 | public Iterable<Device> getDevices() { |
| 671 | List<Device> devices = new ArrayList<>(); |
| 672 | devices.add(device1); |
| 673 | return devices; |
| 674 | } |
| 675 | |
| 676 | @Override |
| 677 | public Port getPort(ConnectPoint cp) { |
| 678 | return new MockPort(); |
| 679 | } |
| 680 | |
| 681 | @Override |
| 682 | public Port getPort(DeviceId deviceId, PortNumber portNumber) { |
| 683 | return new MockPort(); |
| 684 | } |
| 685 | |
| 686 | @Override |
| 687 | public List<Port> getPorts(DeviceId deviceId) { |
| 688 | return Lists.newArrayList(new MockPort()); |
| 689 | } |
| 690 | |
| 691 | @Override |
| 692 | public boolean isAvailable(DeviceId d) { |
| 693 | return true; |
| 694 | } |
| 695 | |
| 696 | @Override |
| 697 | public void addListener(DeviceListener listener) { |
| 698 | deviceListener = listener; |
| 699 | } |
| 700 | |
| 701 | @Override |
| 702 | public void removeListener(DeviceListener listener) { |
| 703 | |
| 704 | } |
| 705 | } |
| 706 | } |