use of org.onosproject.net.device.PortDescription in project onos by opennetworkinglab.
the class DeviceDescriptionDiscoveryCiscoImpl method discoverPortDetails.
@Override
public List<PortDescription> discoverPortDetails() {
List<PortDescription> ports = Lists.newArrayList();
try {
String response;
try {
response = NxApiRequest.postCli(handler(), SHOW_INTERFACES_CMD);
} catch (NullPointerException e) {
log.error("Failed to perform {} command on the device {}", SHOW_INTERFACES_CMD, handler().data().deviceId());
return ports;
}
ObjectMapper om = new ObjectMapper();
JsonNode json = om.readTree(response);
JsonNode res = json.get(JSON_RESULT);
JsonNode interfaces = res.findValue(JSON_ROW_INTERFACE);
Iterator<JsonNode> iter = interfaces.elements();
Integer ifCount = 1;
while (iter.hasNext()) {
JsonNode ifs = iter.next();
String ifName = ifs.get(JSON_INTERFACE).asText();
if (isPortValid(ifName)) {
Port.Type portType = Port.Type.VIRTUAL;
// Mbps
long portSpeed = ifs.get(INTERFACE_BW).asLong() / 1000;
String portMac = ifs.get(INTERFACE_MAC).asText();
MacAddress mac = MacAddress.valueOf(portMac.replace(".", "").replaceAll("(.{2})", "$1:").trim().substring(0, 17));
boolean state = STATE_UP.equals(ifs.get(INTERFACE_STATE).asText());
String adminState = STATE_UP.equals(ifs.get(INTERFACE_ADMIN_STATE).asText()) ? INTERFACE_ENABLED : INTERFACE_DISABLED;
DefaultAnnotations.Builder annotations = DefaultAnnotations.builder().set(AnnotationKeys.PORT_NAME, ifName).set(AnnotationKeys.PORT_MAC, mac.toString()).set(AnnotationKeys.ADMIN_STATE, adminState);
if (isValidPhysicalPort(ifName)) {
String interfaceNumber = ifName.replace(INTERFACE_ETHERNET, "");
String[] interfaceLocation = interfaceNumber.split("/");
portType = Port.Type.FIBER;
if (interfaceLocation.length == 3) {
String breakout = ifName.substring(0, ifName.lastIndexOf("/"));
annotations.set(BREAKOUT, breakout);
}
}
PortDescription desc = DefaultPortDescription.builder().withPortNumber(PortNumber.portNumber(ifCount)).isEnabled(state).type(portType).portSpeed(portSpeed).annotations(annotations.build()).build();
ports.add(desc);
ifCount++;
}
}
} catch (Exception e) {
log.error("Exception occurred because of ", e);
}
return ports;
}
use of org.onosproject.net.device.PortDescription in project onos by opennetworkinglab.
the class GossipDeviceStoreTest method testRemoveDevice.
@Test
public final void testRemoveDevice() {
putDevice(DID1, SW1, A1);
List<PortDescription> pds = Arrays.asList(DefaultPortDescription.builder().withPortNumber(P1).isEnabled(true).annotations(A2).build());
deviceStore.updatePorts(PID, DID1, pds);
putDevice(DID2, SW1);
assertEquals(2, deviceStore.getDeviceCount());
assertEquals(1, deviceStore.getPorts(DID1).size());
assertAnnotationsEquals(deviceStore.getDevice(DID1).annotations(), A1);
assertAnnotationsEquals(deviceStore.getPort(DID1, P1).annotations(), A2);
Capture<InternalDeviceEvent> message = Capture.newInstance();
Capture<MessageSubject> subject = Capture.newInstance();
Capture<Function<InternalDeviceEvent, byte[]>> encoder = Capture.newInstance();
resetCommunicatorExpectingSingleBroadcast(message, subject, encoder);
DeviceEvent event = deviceStore.removeDevice(DID1);
assertEquals(DEVICE_REMOVED, event.type());
assertDevice(DID1, SW1, event.subject());
assertEquals(1, deviceStore.getDeviceCount());
assertEquals(0, deviceStore.getPorts(DID1).size());
verify(clusterCommunicator);
// TODO: verify broadcast message
assertTrue(message.hasCaptured());
// putBack Device, Port w/o annotation
putDevice(DID1, SW1);
List<PortDescription> pds2 = Arrays.asList(DefaultPortDescription.builder().withPortNumber(P1).isEnabled(true).build());
deviceStore.updatePorts(PID, DID1, pds2);
// annotations should not survive
assertEquals(2, deviceStore.getDeviceCount());
assertEquals(1, deviceStore.getPorts(DID1).size());
assertAnnotationsEquals(deviceStore.getDevice(DID1).annotations());
assertAnnotationsEquals(deviceStore.getPort(DID1, P1).annotations());
}
use of org.onosproject.net.device.PortDescription in project onos by opennetworkinglab.
the class GossipDeviceStoreTest method testUpdatePortStatus.
@Test
public final void testUpdatePortStatus() {
putDevice(DID1, SW1);
List<PortDescription> pds = Arrays.asList(DefaultPortDescription.builder().withPortNumber(P1).isEnabled(true).build());
deviceStore.updatePorts(PID, DID1, pds);
Capture<InternalPortStatusEvent> message = Capture.newInstance();
Capture<MessageSubject> subject = Capture.newInstance();
Capture<Function<InternalPortStatusEvent, byte[]>> encoder = Capture.newInstance();
resetCommunicatorExpectingSingleBroadcast(message, subject, encoder);
final DefaultPortDescription desc = DefaultPortDescription.builder().withPortNumber(P1).isEnabled(false).build();
DeviceEvent event = deviceStore.updatePortStatus(PID, DID1, desc);
assertEquals(PORT_UPDATED, event.type());
assertDevice(DID1, SW1, event.subject());
assertEquals(P1, event.port().number());
assertFalse("Port is disabled", event.port().isEnabled());
verify(clusterCommunicator);
assertInternalPortStatusEvent(NID1, DID1, PID, desc, NO_ANNOTATION, message, subject, encoder);
assertTrue(message.hasCaptured());
}
use of org.onosproject.net.device.PortDescription in project onos by opennetworkinglab.
the class GossipDeviceStoreTest method testUpdatePortStatusAncillary.
@Test
public final void testUpdatePortStatusAncillary() throws IOException {
putDeviceAncillary(DID1, SW1);
putDevice(DID1, SW1);
List<PortDescription> pds = Arrays.asList(DefaultPortDescription.builder().withPortNumber(P1).isEnabled(true).annotations(A1).build());
deviceStore.updatePorts(PID, DID1, pds);
Capture<InternalPortStatusEvent> message = Capture.newInstance();
Capture<MessageSubject> subject = Capture.newInstance();
Capture<Function<InternalPortStatusEvent, byte[]>> encoder = Capture.newInstance();
// update port from primary
resetCommunicatorExpectingSingleBroadcast(message, subject, encoder);
final DefaultPortDescription desc1 = DefaultPortDescription.builder().withPortNumber(P1).isEnabled(false).annotations(A1_2).build();
DeviceEvent event = deviceStore.updatePortStatus(PID, DID1, desc1);
assertEquals(PORT_UPDATED, event.type());
assertDevice(DID1, SW1, event.subject());
assertEquals(P1, event.port().number());
assertAnnotationsEquals(event.port().annotations(), A1, A1_2);
assertFalse("Port is disabled", event.port().isEnabled());
verify(clusterCommunicator);
assertInternalPortStatusEvent(NID1, DID1, PID, desc1, asList(A1, A1_2), message, subject, encoder);
assertTrue(message.hasCaptured());
// update port from ancillary with no attributes
resetCommunicatorExpectingNoBroadcast(message, subject, encoder);
final DefaultPortDescription desc2 = DefaultPortDescription.builder().withPortNumber(P1).isEnabled(true).build();
DeviceEvent event2 = deviceStore.updatePortStatus(PIDA, DID1, desc2);
assertNull("Ancillary is ignored if primary exists", event2);
verify(clusterCommunicator);
assertFalse(message.hasCaptured());
// but, Ancillary annotation update will be notified
resetCommunicatorExpectingSingleBroadcast(message, subject, encoder);
final DefaultPortDescription desc3 = DefaultPortDescription.builder().withPortNumber(P1).isEnabled(true).annotations(A2).build();
DeviceEvent event3 = deviceStore.updatePortStatus(PIDA, DID1, desc3);
assertEquals(PORT_UPDATED, event3.type());
assertDevice(DID1, SW1, event3.subject());
assertEquals(P1, event3.port().number());
assertAnnotationsEquals(event3.port().annotations(), A1, A1_2, A2);
assertFalse("Port is disabled", event3.port().isEnabled());
verify(clusterCommunicator);
assertInternalPortStatusEvent(NID1, DID1, PIDA, desc3, asList(A2), message, subject, encoder);
assertTrue(message.hasCaptured());
// port only reported from Ancillary will be notified as down
resetCommunicatorExpectingSingleBroadcast(message, subject, encoder);
final DefaultPortDescription desc4 = DefaultPortDescription.builder().withPortNumber(P2).isEnabled(true).build();
DeviceEvent event4 = deviceStore.updatePortStatus(PIDA, DID1, desc4);
assertEquals(PORT_ADDED, event4.type());
assertDevice(DID1, SW1, event4.subject());
assertEquals(P2, event4.port().number());
assertAnnotationsEquals(event4.port().annotations());
assertFalse("Port is disabled if not given from primary provider", event4.port().isEnabled());
verify(clusterCommunicator);
// TODO: verify broadcast message content
assertInternalPortStatusEvent(NID1, DID1, PIDA, desc4, NO_ANNOTATION, message, subject, encoder);
assertTrue(message.hasCaptured());
}
use of org.onosproject.net.device.PortDescription in project onos by opennetworkinglab.
the class GossipDeviceStoreTest method testGetPort.
@Test
public final void testGetPort() {
putDevice(DID1, SW1);
putDevice(DID2, SW1);
List<PortDescription> pds = Arrays.asList(DefaultPortDescription.builder().withPortNumber(P1).isEnabled(true).build(), DefaultPortDescription.builder().withPortNumber(P2).isEnabled(false).build());
deviceStore.updatePorts(PID, DID1, pds);
Port port1 = deviceStore.getPort(DID1, P1);
assertEquals(P1, port1.number());
assertTrue("Port is enabled", port1.isEnabled());
Port port2 = deviceStore.getPort(DID1, P2);
assertEquals(P2, port2.number());
assertFalse("Port is disabled", port2.isEnabled());
Port port3 = deviceStore.getPort(DID1, P3);
assertNull("P3 not expected", port3);
}
Aggregations