use of org.onosproject.net.Port in project onos by opennetworkinglab.
the class VirtualNetworkDeviceManagerTest method testDeviceEventsForAddRemovalDeviceAndPorts.
/**
* Tests DeviceEvents received during virtual device/port addition and removal.
*/
@Test
public void testDeviceEventsForAddRemovalDeviceAndPorts() throws TestUtils.TestUtilsException {
manager.registerTenantId(TenantId.tenantId(tenantIdValue1));
VirtualNetwork virtualNetwork = manager.createVirtualNetwork(TenantId.tenantId(tenantIdValue1));
// add virtual device before virtual device manager is created
VirtualDevice device1 = manager.createVirtualDevice(virtualNetwork.id(), VDID1);
// no DeviceEvent expected
validateEvents();
testDirectory.add(EventDeliveryService.class, dispatcher);
DeviceService deviceService = manager.get(virtualNetwork.id(), DeviceService.class);
// virtual device manager is created; register DeviceEvent listener
deviceService.addListener(testListener);
// list to keep track of expected event types
List<DeviceEvent.Type> expectedEventTypes = new ArrayList<>();
// add virtual device
VirtualDevice device2 = manager.createVirtualDevice(virtualNetwork.id(), VDID2);
expectedEventTypes.add(DeviceEvent.Type.DEVICE_ADDED);
ConnectPoint cp = new ConnectPoint(PHYDID1, PortNumber.portNumber(1));
// add 2 virtual ports
manager.createVirtualPort(virtualNetwork.id(), device2.id(), PortNumber.portNumber(1), cp);
expectedEventTypes.add(DeviceEvent.Type.PORT_ADDED);
manager.createVirtualPort(virtualNetwork.id(), device2.id(), PortNumber.portNumber(2), cp);
expectedEventTypes.add(DeviceEvent.Type.PORT_ADDED);
// verify virtual ports were added
Set<VirtualPort> virtualPorts = manager.getVirtualPorts(virtualNetwork.id(), device2.id());
assertNotNull("The virtual port set should not be null", virtualPorts);
assertEquals("The virtual port set size did not match.", 2, virtualPorts.size());
virtualPorts.forEach(vp -> assertFalse("Initial virtual port state should be disabled", vp.isEnabled()));
// verify change state of virtual port (disabled -> enabled)
manager.updatePortState(virtualNetwork.id(), device2.id(), PortNumber.portNumber(1), true);
Port changedPort = deviceService.getPort(device2.id(), PortNumber.portNumber(1));
assertNotNull("The changed virtual port should not be null", changedPort);
assertEquals("Virtual port state should be enabled", true, changedPort.isEnabled());
expectedEventTypes.add(DeviceEvent.Type.PORT_UPDATED);
// verify change state of virtual port (disabled -> disabled)
manager.updatePortState(virtualNetwork.id(), device2.id(), PortNumber.portNumber(2), false);
changedPort = deviceService.getPort(device2.id(), PortNumber.portNumber(2));
assertNotNull("The changed virtual port should not be null", changedPort);
assertEquals("Virtual port state should be disabled", false, changedPort.isEnabled());
// remove 2 virtual ports
for (VirtualPort virtualPort : virtualPorts) {
manager.removeVirtualPort(virtualNetwork.id(), (DeviceId) virtualPort.element().id(), virtualPort.number());
expectedEventTypes.add(DeviceEvent.Type.PORT_REMOVED);
// attempt to remove the same virtual port again - no DeviceEvent.Type.PORT_REMOVED expected.
manager.removeVirtualPort(virtualNetwork.id(), (DeviceId) virtualPort.element().id(), virtualPort.number());
}
// verify virtual ports were removed
virtualPorts = manager.getVirtualPorts(virtualNetwork.id(), device2.id());
assertTrue("The virtual port set should be empty.", virtualPorts.isEmpty());
// Add/remove one virtual port again.
VirtualPort virtualPort = manager.createVirtualPort(virtualNetwork.id(), device2.id(), PortNumber.portNumber(1), cp);
expectedEventTypes.add(DeviceEvent.Type.PORT_ADDED);
ConnectPoint newCp = new ConnectPoint(PHYDID3, PortNumber.portNumber(2));
manager.bindVirtualPort(virtualNetwork.id(), device2.id(), PortNumber.portNumber(1), newCp);
expectedEventTypes.add(DeviceEvent.Type.PORT_UPDATED);
manager.removeVirtualPort(virtualNetwork.id(), (DeviceId) virtualPort.element().id(), virtualPort.number());
expectedEventTypes.add(DeviceEvent.Type.PORT_REMOVED);
// verify no virtual ports remain
virtualPorts = manager.getVirtualPorts(virtualNetwork.id(), device2.id());
assertTrue("The virtual port set should be empty.", virtualPorts.isEmpty());
// remove virtual device
manager.removeVirtualDevice(virtualNetwork.id(), device2.id());
expectedEventTypes.add(DeviceEvent.Type.DEVICE_REMOVED);
// Validate that the events were all received in the correct order.
validateEvents((Enum[]) expectedEventTypes.toArray(new DeviceEvent.Type[expectedEventTypes.size()]));
// cleanup
deviceService.removeListener(testListener);
}
use of org.onosproject.net.Port in project onos by opennetworkinglab.
the class LinkDiscoveryCiscoImpl method getLinks.
@Override
public Set<LinkDescription> getLinks() {
String response = retrieveResponse(SHOW_LLDP_NEIGHBOR_DETAIL_CMD);
DeviceId localDeviceId = this.handler().data().deviceId();
DeviceService deviceService = this.handler().get(DeviceService.class);
Set<LinkDescription> linkDescriptions = Sets.newHashSet();
List<Port> ports = deviceService.getPorts(localDeviceId);
if (ports.size() == 0 || Objects.isNull(response)) {
return linkDescriptions;
}
try {
ObjectMapper om = new ObjectMapper();
JsonNode json = om.readTree(response);
if (json == null) {
return linkDescriptions;
}
JsonNode res = json.at("/" + JSON_RESULT);
if (res.isMissingNode()) {
return linkDescriptions;
}
JsonNode lldpNeighborsRow = res.at("/" + TABLE_NBOR_DETAIL);
if (lldpNeighborsRow.isMissingNode()) {
return linkDescriptions;
}
JsonNode lldpNeighbors = lldpNeighborsRow.at("/" + ROW_NBOR_DETAIL);
if (lldpNeighbors.isMissingNode()) {
return linkDescriptions;
}
Iterator<JsonNode> iterator = lldpNeighbors.elements();
while (iterator.hasNext()) {
JsonNode neighbors = iterator.next();
String remoteChassisId = neighbors.get(CHASSIS_ID).asText();
String remotePortName = neighbors.get(PORT_ID).asText();
String remotePortDesc = neighbors.get(PORT_DESC).asText();
String lldpLocalPort = neighbors.get(LOCAL_PORT_ID).asText().replaceAll("(Eth.{0,5})(.\\d{0,5}/\\d{0,5})", "Ethernet$2");
Port localPort = findLocalPortByName(ports, lldpLocalPort);
if (localPort == null) {
log.warn("local port not found. LldpLocalPort value: {}", lldpLocalPort);
continue;
}
Device remoteDevice = findRemoteDeviceByChassisId(deviceService, remoteChassisId);
Port remotePort = findDestinationPortByName(remotePortName, remotePortDesc, deviceService, remoteDevice);
if (!localPort.isEnabled() || !remotePort.isEnabled()) {
log.debug("Ports are disabled. Cannot create a link between {}/{} and {}/{}", localDeviceId, localPort, remoteDevice.id(), remotePort);
continue;
}
linkDescriptions.addAll(buildLinkPair(localDeviceId, localPort, remoteDevice.id(), remotePort));
}
} catch (IOException e) {
log.error("Failed to get links ", e);
}
log.debug("Returning linkDescriptions: {}", linkDescriptions);
return linkDescriptions;
}
use of org.onosproject.net.Port in project onos by opennetworkinglab.
the class LinkDiscoveryCiscoImpl method findRemoteDeviceByChassisId.
private Device findRemoteDeviceByChassisId(DeviceService deviceService, String remoteChassisIdString) {
String forMacTmp = remoteChassisIdString.replace(".", "").replaceAll("(.{2})", "$1:").trim().substring(0, 17);
MacAddress mac = MacAddress.valueOf(forMacTmp);
ChassisId remoteChassisId = new ChassisId(mac.toLong());
Optional<Device> remoteDeviceOptional;
Supplier<Stream<Device>> deviceStream = () -> StreamSupport.stream(deviceService.getAvailableDevices().spliterator(), false);
remoteDeviceOptional = deviceStream.get().filter(device -> device.chassisId() != null && MacAddress.valueOf(device.chassisId().value()).equals(mac)).findAny();
if (remoteDeviceOptional.isPresent()) {
return remoteDeviceOptional.get();
} else {
remoteDeviceOptional = deviceStream.get().filter(device -> Tools.stream(deviceService.getPorts(device.id())).anyMatch(port -> port.annotations().keys().contains(AnnotationKeys.PORT_MAC) && MacAddress.valueOf(port.annotations().value(AnnotationKeys.PORT_MAC)).equals(mac))).findAny();
if (remoteDeviceOptional.isPresent()) {
return remoteDeviceOptional.get();
} else {
log.debug("remote device not found. remoteChassisId value: {}", remoteChassisId);
return new DefaultDevice(ProviderId.NONE, DeviceId.deviceId(LLDP + mac.toString()), Device.Type.SWITCH, UNKNOWN, UNKNOWN, UNKNOWN, UNKNOWN, remoteChassisId, DefaultAnnotations.EMPTY);
}
}
}
use of org.onosproject.net.Port in project onos by opennetworkinglab.
the class HostMonitorTest method testMonitorIpv6HostDoesNotExist.
@Test
public void testMonitorIpv6HostDoesNotExist() throws Exception {
HostManager hostManager = createMock(HostManager.class);
DeviceId devId = DeviceId.deviceId("fake");
Device device = createMock(Device.class);
expect(device.id()).andReturn(devId).anyTimes();
replay(device);
PortNumber portNum = PortNumber.portNumber(2L);
Port port = createMock(Port.class);
expect(port.number()).andReturn(portNum).anyTimes();
expect(port.isEnabled()).andReturn(true).anyTimes();
replay(port);
TestDeviceService deviceService = new TestDeviceService();
deviceService.addDevice(device, Collections.singleton(port));
ConnectPoint cp = new ConnectPoint(devId, portNum);
expect(hostManager.getHostsByIp(TARGET_IPV6_ADDR)).andReturn(Collections.emptySet()).anyTimes();
replay(hostManager);
InterfaceService interfaceService = createMock(InterfaceService.class);
expect(interfaceService.getMatchingInterfaces(TARGET_IPV6_ADDR)).andReturn(Collections.singleton(new Interface(Interface.NO_INTERFACE_NAME, cp, Collections.singletonList(IA2), sourceMac2, VlanId.NONE))).anyTimes();
replay(interfaceService);
TestPacketService packetService = new TestPacketService();
// Run the test
hostMonitor = new HostMonitor(packetService, hostManager, interfaceService, edgePortService, deviceService);
hostMonitor.addMonitoringFor(TARGET_IPV6_ADDR);
hostMonitor.run();
// Check that a packet was sent to our PacketService and that it has
// the properties we expect
assertEquals(2, packetService.packets.size());
OutboundPacket packet = packetService.packets.get(0);
// Check the output port is correct
assertEquals(1, packet.treatment().immediate().size());
Instruction instruction = packet.treatment().immediate().get(0);
assertTrue(instruction instanceof OutputInstruction);
OutputInstruction oi = (OutputInstruction) instruction;
assertEquals(portNum, oi.port());
// Check the output packet is correct (well the important bits anyway)
final byte[] pktData = new byte[packet.data().remaining()];
packet.data().get(pktData);
Ethernet eth = Ethernet.deserializer().deserialize(pktData, 0, pktData.length);
assertEquals(Ethernet.VLAN_UNTAGGED, eth.getVlanID());
IPv6 ipv6 = (IPv6) eth.getPayload();
assertArrayEquals(SOURCE_IPV6_ADDR.toOctets(), ipv6.getSourceAddress());
NeighborSolicitation ns = (NeighborSolicitation) ipv6.getPayload().getPayload();
assertArrayEquals(sourceMac2.toBytes(), ns.getOptions().get(0).data());
assertArrayEquals(TARGET_IPV6_ADDR.toOctets(), ns.getTargetAddress());
}
use of org.onosproject.net.Port in project onos by opennetworkinglab.
the class HostMonitorTest method testMonitorIpv4HostDoesNotExist.
@Test
public void testMonitorIpv4HostDoesNotExist() throws Exception {
HostManager hostManager = createMock(HostManager.class);
DeviceId devId = DeviceId.deviceId("fake");
Device device = createMock(Device.class);
expect(device.id()).andReturn(devId).anyTimes();
replay(device);
PortNumber portNum = PortNumber.portNumber(1L);
Port port = createMock(Port.class);
expect(port.number()).andReturn(portNum).anyTimes();
expect(port.isEnabled()).andReturn(true).anyTimes();
replay(port);
TestDeviceService deviceService = new TestDeviceService();
deviceService.addDevice(device, Collections.singleton(port));
ConnectPoint cp = new ConnectPoint(devId, portNum);
expect(hostManager.getHostsByIp(TARGET_IPV4_ADDR)).andReturn(Collections.emptySet()).anyTimes();
replay(hostManager);
InterfaceService interfaceService = createMock(InterfaceService.class);
expect(interfaceService.getMatchingInterfaces(TARGET_IPV4_ADDR)).andReturn(Collections.singleton(new Interface(Interface.NO_INTERFACE_NAME, cp, Collections.singletonList(IA1), sourceMac, VlanId.NONE))).anyTimes();
replay(interfaceService);
TestPacketService packetService = new TestPacketService();
// Run the test
hostMonitor = new HostMonitor(packetService, hostManager, interfaceService, edgePortService, deviceService);
hostMonitor.addMonitoringFor(TARGET_IPV4_ADDR);
hostMonitor.run();
// Check that a packet was sent to our PacketService and that it has
// the properties we expect
assertEquals(2, packetService.packets.size());
OutboundPacket packet = packetService.packets.get(0);
// Check the output port is correct
assertEquals(1, packet.treatment().immediate().size());
Instruction instruction = packet.treatment().immediate().get(0);
assertTrue(instruction instanceof OutputInstruction);
OutputInstruction oi = (OutputInstruction) instruction;
assertEquals(portNum, oi.port());
// Check the output packet is correct (well the important bits anyway)
final byte[] pktData = new byte[packet.data().remaining()];
packet.data().get(pktData);
Ethernet eth = Ethernet.deserializer().deserialize(pktData, 0, pktData.length);
assertEquals(Ethernet.VLAN_UNTAGGED, eth.getVlanID());
ARP arp = (ARP) eth.getPayload();
assertArrayEquals(SOURCE_IPV4_ADDR.toOctets(), arp.getSenderProtocolAddress());
assertArrayEquals(sourceMac.toBytes(), arp.getSenderHardwareAddress());
assertArrayEquals(TARGET_IPV4_ADDR.toOctets(), arp.getTargetProtocolAddress());
}
Aggregations