use of org.onosproject.net.Port in project onos by opennetworkinglab.
the class DevicePortsListCommand method printPorts.
protected void printPorts(DeviceService service, Device device) {
List<Port> ports = new ArrayList<>(service.getPorts(device.id()));
Collections.sort(ports, Comparators.PORT_COMPARATOR);
for (Port port : ports) {
if (!isIncluded(port)) {
continue;
}
String portName = port.number().toString();
Object portIsEnabled = port.isEnabled() ? "enabled" : "disabled";
String portType = port.type().toString().toLowerCase();
String annotations = annotations(port.annotations());
print(FMT, portName, portIsEnabled, portType, port.portSpeed(), annotations);
}
}
use of org.onosproject.net.Port in project onos by opennetworkinglab.
the class GossipDeviceStoreTest method testEvents.
// If Delegates should be called only on remote events,
// then Simple* should never call them, thus not test required.
// TODO add test for Port events when we have them
@Ignore("Ignore until Delegate spec. is clear.")
@Test
public final void testEvents() throws InterruptedException {
final CountDownLatch addLatch = new CountDownLatch(1);
DeviceStoreDelegate checkAdd = event -> {
assertEquals(DEVICE_ADDED, event.type());
assertDevice(DID1, SW1, event.subject());
addLatch.countDown();
};
final CountDownLatch updateLatch = new CountDownLatch(1);
DeviceStoreDelegate checkUpdate = event -> {
assertEquals(DEVICE_UPDATED, event.type());
assertDevice(DID1, SW2, event.subject());
updateLatch.countDown();
};
final CountDownLatch removeLatch = new CountDownLatch(1);
DeviceStoreDelegate checkRemove = event -> {
assertEquals(DEVICE_REMOVED, event.type());
assertDevice(DID1, SW2, event.subject());
removeLatch.countDown();
};
DeviceDescription description = new DefaultDeviceDescription(DID1.uri(), SWITCH, MFR, HW, SW1, SN, CID);
deviceStore.setDelegate(checkAdd);
deviceStore.createOrUpdateDevice(PID, DID1, description);
assertTrue("Add event fired", addLatch.await(1, TimeUnit.SECONDS));
DeviceDescription description2 = new DefaultDeviceDescription(DID1.uri(), SWITCH, MFR, HW, SW2, SN, CID);
deviceStore.unsetDelegate(checkAdd);
deviceStore.setDelegate(checkUpdate);
deviceStore.createOrUpdateDevice(PID, DID1, description2);
assertTrue("Update event fired", updateLatch.await(1, TimeUnit.SECONDS));
deviceStore.unsetDelegate(checkUpdate);
deviceStore.setDelegate(checkRemove);
deviceStore.removeDevice(DID1);
assertTrue("Remove event fired", removeLatch.await(1, TimeUnit.SECONDS));
}
use of org.onosproject.net.Port in project onos by opennetworkinglab.
the class GossipDeviceStoreTest method testGetPorts.
@Test
public final void testGetPorts() {
putDevice(DID1, SW1);
putDevice(DID2, SW1);
List<PortDescription> pds = Arrays.asList(DefaultPortDescription.builder().withPortNumber(P1).isEnabled(true).build(), DefaultPortDescription.builder().withPortNumber(P2).isEnabled(true).build());
deviceStore.updatePorts(PID, DID1, pds);
Set<PortNumber> expectedPorts = Sets.newHashSet(P1, P2);
List<Port> ports = deviceStore.getPorts(DID1);
for (Port port : ports) {
assertTrue("Port is enabled", port.isEnabled());
assertTrue("PortNumber is one of expected", expectedPorts.remove(port.number()));
}
assertTrue("Event for all expectedport appeared", expectedPorts.isEmpty());
assertTrue("DID2 has no ports", deviceStore.getPorts(DID2).isEmpty());
}
use of org.onosproject.net.Port in project onos by opennetworkinglab.
the class FlowRuleJuniperImpl method findIpDst.
/**
* Helper method to find the next hop IP address.
* The logic is to check if the destination ports have an IP address
* by checking the logical interface (e.g., for port physical ge-2/0/1,
* a logical interface may be ge-2/0/1.0
*
* @param deviceId the device id of the flow rule to be installed
* @param port output port of the flow rule treatment
* @return optional IPv4 address of a next hop
*/
private Optional<Ip4Address> findIpDst(DeviceId deviceId, Port port) {
LinkService linkService = this.handler().get(LinkService.class);
Set<Link> links = linkService.getEgressLinks(new ConnectPoint(deviceId, port.number()));
DeviceService deviceService = this.handler().get(DeviceService.class);
// Using only links with adjacency discovered by the LLDP protocol (see LinkDiscoveryJuniperImpl)
Map<DeviceId, Port> dstPorts = links.stream().filter(l -> JuniperUtils.AK_IP.toUpperCase().equals(l.annotations().value(AnnotationKeys.LAYER))).collect(Collectors.toMap(l -> l.dst().deviceId(), l -> deviceService.getPort(l.dst().deviceId(), l.dst().port())));
for (Map.Entry<DeviceId, Port> entry : dstPorts.entrySet()) {
String portName = entry.getValue().annotations().value(AnnotationKeys.PORT_NAME);
Optional<Port> childPort = deviceService.getPorts(entry.getKey()).stream().filter(p -> Strings.nullToEmpty(p.annotations().value(AnnotationKeys.PORT_NAME)).contains(portName.trim())).filter(this::isIp).findAny();
if (childPort.isPresent()) {
return Optional.ofNullable(Ip4Address.valueOf(childPort.get().annotations().value(JuniperUtils.AK_IP)));
}
}
return Optional.empty();
}
use of org.onosproject.net.Port in project onos by opennetworkinglab.
the class LinkDiscoveryJuniperImpl method getLinks.
@Override
public Set<LinkDescription> getLinks() {
DeviceId localDeviceId = this.handler().data().deviceId();
NetconfSession session = lookupNetconfSession(localDeviceId);
String reply;
try {
reply = session.get(requestBuilder(REQ_LLDP_NBR_INFO));
} catch (NetconfException e) {
log.warn("Failed to retrieve lldp-neighbors-information for device {}", localDeviceId);
return ImmutableSet.of();
}
log.debug("Reply from device {} : {}", localDeviceId, reply);
Set<LinkAbstraction> linkAbstractions = parseJuniperLldp(loadXmlString(reply));
log.debug("Set of LinkAbstraction discovered {}", linkAbstractions);
DeviceService deviceService = this.handler().get(DeviceService.class);
Set<LinkDescription> descriptions = new HashSet<>();
// for each lldp neighbor create two LinkDescription
for (LinkAbstraction linkAbs : linkAbstractions) {
// find source port by local port name
Optional<Port> localPort = deviceService.getPorts(localDeviceId).stream().filter(port -> linkAbs.localPortName.equals(port.annotations().value(PORT_NAME))).findAny();
if (!localPort.isPresent()) {
log.warn("Port name {} does not exist in device {}", linkAbs.localPortName, localDeviceId);
continue;
}
// find destination device by remote chassis id
com.google.common.base.Optional<Device> dev = Iterables.tryFind(deviceService.getAvailableDevices(), input -> input.chassisId().equals(linkAbs.remoteChassisId));
if (!dev.isPresent()) {
log.warn("Device with chassis ID {} does not exist. Referenced by {}/{}", linkAbs.remoteChassisId, localDeviceId, linkAbs);
continue;
}
Device remoteDevice = dev.get();
// find destination port by interface index
Optional<Port> remotePort = deviceService.getPorts(remoteDevice.id()).stream().filter(port -> {
if (port.number().toLong() == linkAbs.remotePortIndex) {
return true;
}
if (port.annotations().value(AnnotationKeys.PORT_MAC) != null && linkAbs.remotePortId != null && port.annotations().value(AnnotationKeys.PORT_MAC).equals(linkAbs.remotePortId)) {
return true;
}
if (port.annotations().value(AnnotationKeys.PORT_NAME) != null && linkAbs.remotePortId != null && port.annotations().value(AnnotationKeys.PORT_NAME).equals(linkAbs.remotePortId)) {
return true;
}
if (port.annotations().value(AnnotationKeys.PORT_NAME) != null && linkAbs.remotePortDescription != null && port.annotations().value(AnnotationKeys.PORT_NAME).equals(linkAbs.remotePortDescription)) {
return true;
}
return false;
}).findAny();
if (!remotePort.isPresent()) {
log.warn("Port does not exist in remote device {}. Referenced by {}/{}", remoteDevice.id(), localDeviceId, linkAbs);
continue;
}
if (!localPort.get().isEnabled() || !remotePort.get().isEnabled()) {
log.debug("Ports are disabled. Cannot create a link between {}/{} and {}/{}", localDeviceId, localPort.get(), remoteDevice.id(), remotePort.get());
continue;
}
JuniperUtils.createOneWayLinkDescription(localDeviceId, localPort.get(), remoteDevice.id(), remotePort.get(), descriptions);
}
return descriptions;
}
Aggregations