use of org.onosproject.net.device.DeviceService 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.device.DeviceService 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.device.DeviceService in project onos by opennetworkinglab.
the class Ciena5170DeviceDescription method getLinks.
@Override
public Set<LinkDescription> getLinks() {
log.debug("LINKS CHECKING ...");
Set<LinkDescription> links = new HashSet<LinkDescription>();
DeviceId deviceId = handler().data().deviceId();
NetconfController controller = checkNotNull(handler().get(NetconfController.class));
if (controller == null || controller.getDevicesMap() == null || controller.getDevicesMap().get(deviceId) == null) {
log.warn("NETCONF session to device {} not yet established, cannot load links, will be retried", deviceId);
return links;
}
NetconfSession session = controller.getDevicesMap().get(deviceId).getSession();
try {
DeviceService deviceService = this.handler().get(DeviceService.class);
Iterable<Device> devices = deviceService.getAvailableDevices();
Map<String, Device> lookup = new HashMap<String, Device>();
for (Device d : devices) {
lookup.put(d.chassisId().toString().toUpperCase(), d);
}
Node logicalPorts = TEMPLATE_MANAGER.doRequest(session, "link-info");
XPath xp = XPathFactory.newInstance().newXPath();
NodeList ifaces = (NodeList) xp.evaluate("interfaces/interface", logicalPorts, XPathConstants.NODESET);
int count = ifaces.getLength();
Node iface;
Node destChassis;
for (int i = 0; i < count; i += 1) {
iface = ifaces.item(i);
if (xp.evaluate("config/type/text()", iface).equals("ettp")) {
destChassis = (Node) xp.evaluate("state/lldp-remote-port-operational/chassis-id", iface, XPathConstants.NODE);
if (destChassis != null) {
Device dest = lookup.get(destChassis.getTextContent().toUpperCase());
if (dest != null) {
links.add(new DefaultLinkDescription(new ConnectPoint(dest.id(), PortNumber.portNumber(xp.evaluate("state/lldp-remote-port-operational/port-id/text()", iface))), new ConnectPoint(deviceId, PortNumber.portNumber(xp.evaluate("name/text()", iface))), Link.Type.DIRECT, true));
} else {
log.warn("DEST chassisID not found: chassis {} port {}", destChassis.getTextContent().toUpperCase(), xp.evaluate("name/text()", iface));
}
} else {
log.debug("NO LINK for {}", xp.evaluate("name/text()", iface));
}
}
}
} catch (NetconfException | XPathExpressionException e) {
log.error("Unable to retrieve links for device {}, {}", deviceId, e);
}
return links;
}
use of org.onosproject.net.device.DeviceService in project onos by opennetworkinglab.
the class Ciena5162DeviceDescription method getLinks.
@Override
public Set<LinkDescription> getLinks() {
log.debug("LINKS CHECKING ...");
Set<LinkDescription> links = new HashSet<LinkDescription>();
DeviceId deviceId = handler().data().deviceId();
NetconfController controller = checkNotNull(handler().get(NetconfController.class));
if (controller == null || controller.getDevicesMap() == null || controller.getDevicesMap().get(deviceId) == null) {
log.warn("NETCONF session to device {} not yet established, cannot load links, will be retried", deviceId);
return links;
}
NetconfSession session = controller.getDevicesMap().get(deviceId).getSession();
try {
DeviceService deviceService = this.handler().get(DeviceService.class);
Iterable<Device> devices = deviceService.getAvailableDevices();
Map<String, Device> lookup = new HashMap<String, Device>();
for (Device d : devices) {
lookup.put(d.chassisId().toString().toUpperCase(), d);
}
Node logicalPorts = TEMPLATE_MANAGER.doRequest(session, "link-info");
XPath xp = XPathFactory.newInstance().newXPath();
NodeList ifaces = (NodeList) xp.evaluate("interfaces/interface", logicalPorts, XPathConstants.NODESET);
int count = ifaces.getLength();
Node iface;
Node destChassis;
for (int i = 0; i < count; i += 1) {
iface = ifaces.item(i);
if (xp.evaluate("config/type/text()", iface).equals("ettp")) {
destChassis = (Node) xp.evaluate("state/lldp-remote-port-operational/chassis-id", iface, XPathConstants.NODE);
if (destChassis != null) {
Device dest = lookup.get(destChassis.getTextContent().toUpperCase());
if (dest != null) {
links.add(new DefaultLinkDescription(new ConnectPoint(dest.id(), PortNumber.portNumber(xp.evaluate("state/lldp-remote-port-operational/port-id/text()", iface))), new ConnectPoint(deviceId, PortNumber.portNumber(xp.evaluate("name/text()", iface))), Link.Type.DIRECT, true));
} else {
log.warn("DEST chassisID not found: chassis {} port {}", destChassis.getTextContent().toUpperCase(), xp.evaluate("name/text()", iface));
}
} else {
log.debug("NO LINK for {}", xp.evaluate("name/text()", iface));
}
}
}
} catch (NetconfException | XPathExpressionException e) {
log.error("Unable to retrieve links for device {}, {}", deviceId, e);
}
return links;
}
use of org.onosproject.net.device.DeviceService in project onos by opennetworkinglab.
the class CienaWaveserverDeviceDescription method discoverDeviceDetails.
@Override
public DeviceDescription discoverDeviceDetails() {
log.debug("getting device description");
DeviceService deviceService = checkNotNull(handler().get(DeviceService.class));
DeviceId deviceId = handler().data().deviceId();
Device device = deviceService.getDevice(deviceId);
if (device == null) {
return new DefaultDeviceDescription(deviceId.uri(), Device.Type.OTN, "Ciena", "WaveServer", "Unknown", "Unknown", new ChassisId());
} else {
return new DefaultDeviceDescription(device.id().uri(), Device.Type.OTN, device.manufacturer(), device.hwVersion(), device.swVersion(), device.serialNumber(), device.chassisId());
}
}
Aggregations