Search in sources :

Example 71 with DeviceService

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;
}
Also used : DefaultLinkDescription(org.onosproject.net.link.DefaultLinkDescription) LinkDescription(org.onosproject.net.link.LinkDescription) DeviceId(org.onosproject.net.DeviceId) DefaultDevice(org.onosproject.net.DefaultDevice) Device(org.onosproject.net.Device) Port(org.onosproject.net.Port) DefaultPort(org.onosproject.net.DefaultPort) DeviceService(org.onosproject.net.device.DeviceService) JsonNode(com.fasterxml.jackson.databind.JsonNode) IOException(java.io.IOException) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 72 with DeviceService

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);
        }
    }
}
Also used : Tools(org.onlab.util.Tools) Supplier(com.google.common.base.Supplier) PortNumber(org.onosproject.net.PortNumber) DeviceService(org.onosproject.net.device.DeviceService) PORT_NAME(org.onosproject.net.AnnotationKeys.PORT_NAME) DefaultDevice(org.onosproject.net.DefaultDevice) AnnotationKeys(org.onosproject.net.AnnotationKeys) DefaultLinkDescription(org.onosproject.net.link.DefaultLinkDescription) Link(org.onosproject.net.Link) ConnectPoint(org.onosproject.net.ConnectPoint) AbstractHandlerBehaviour(org.onosproject.net.driver.AbstractHandlerBehaviour) MediaType(javax.ws.rs.core.MediaType) Lists(com.google.common.collect.Lists) ByteArrayInputStream(java.io.ByteArrayInputStream) Port(org.onosproject.net.Port) JsonNode(com.fasterxml.jackson.databind.JsonNode) StreamSupport(java.util.stream.StreamSupport) LinkDescription(org.onosproject.net.link.LinkDescription) Logger(org.slf4j.Logger) Iterator(java.util.Iterator) DefaultPort(org.onosproject.net.DefaultPort) Device(org.onosproject.net.Device) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Preconditions.checkNotNull(com.google.common.base.Preconditions.checkNotNull) Set(java.util.Set) ProviderId(org.onosproject.net.provider.ProviderId) IOException(java.io.IOException) RestSBController(org.onosproject.protocol.rest.RestSBController) Sets(com.google.common.collect.Sets) LinkDiscovery(org.onosproject.net.behaviour.LinkDiscovery) StandardCharsets(java.nio.charset.StandardCharsets) Objects(java.util.Objects) DefaultAnnotations(org.onosproject.net.DefaultAnnotations) List(java.util.List) DriverHandler(org.onosproject.net.driver.DriverHandler) Stream(java.util.stream.Stream) LoggerFactory.getLogger(org.slf4j.LoggerFactory.getLogger) Optional(java.util.Optional) MacAddress(org.onlab.packet.MacAddress) DeviceId(org.onosproject.net.DeviceId) ChassisId(org.onlab.packet.ChassisId) InputStream(java.io.InputStream) ChassisId(org.onlab.packet.ChassisId) DefaultDevice(org.onosproject.net.DefaultDevice) Device(org.onosproject.net.Device) DefaultDevice(org.onosproject.net.DefaultDevice) ByteArrayInputStream(java.io.ByteArrayInputStream) Stream(java.util.stream.Stream) InputStream(java.io.InputStream) MacAddress(org.onlab.packet.MacAddress)

Example 73 with DeviceService

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;
}
Also used : NetconfSession(org.onosproject.netconf.NetconfSession) XPath(javax.xml.xpath.XPath) DefaultLinkDescription(org.onosproject.net.link.DefaultLinkDescription) LinkDescription(org.onosproject.net.link.LinkDescription) HashMap(java.util.HashMap) DeviceId(org.onosproject.net.DeviceId) Device(org.onosproject.net.Device) XPathExpressionException(javax.xml.xpath.XPathExpressionException) Node(org.w3c.dom.Node) NodeList(org.w3c.dom.NodeList) DeviceService(org.onosproject.net.device.DeviceService) ConnectPoint(org.onosproject.net.ConnectPoint) NetconfController(org.onosproject.netconf.NetconfController) ConnectPoint(org.onosproject.net.ConnectPoint) NetconfException(org.onosproject.netconf.NetconfException) HashSet(java.util.HashSet) DefaultLinkDescription(org.onosproject.net.link.DefaultLinkDescription)

Example 74 with DeviceService

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;
}
Also used : NetconfSession(org.onosproject.netconf.NetconfSession) XPath(javax.xml.xpath.XPath) DefaultLinkDescription(org.onosproject.net.link.DefaultLinkDescription) LinkDescription(org.onosproject.net.link.LinkDescription) HashMap(java.util.HashMap) DeviceId(org.onosproject.net.DeviceId) Device(org.onosproject.net.Device) XPathExpressionException(javax.xml.xpath.XPathExpressionException) Node(org.w3c.dom.Node) NodeList(org.w3c.dom.NodeList) DeviceService(org.onosproject.net.device.DeviceService) ConnectPoint(org.onosproject.net.ConnectPoint) NetconfController(org.onosproject.netconf.NetconfController) ConnectPoint(org.onosproject.net.ConnectPoint) NetconfException(org.onosproject.netconf.NetconfException) HashSet(java.util.HashSet) DefaultLinkDescription(org.onosproject.net.link.DefaultLinkDescription)

Example 75 with DeviceService

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());
    }
}
Also used : ChassisId(org.onlab.packet.ChassisId) DeviceId(org.onosproject.net.DeviceId) Device(org.onosproject.net.Device) DefaultDeviceDescription(org.onosproject.net.device.DefaultDeviceDescription) DeviceService(org.onosproject.net.device.DeviceService)

Aggregations

DeviceService (org.onosproject.net.device.DeviceService)187 Device (org.onosproject.net.Device)75 DeviceId (org.onosproject.net.DeviceId)73 Port (org.onosproject.net.Port)59 ConnectPoint (org.onosproject.net.ConnectPoint)42 PortNumber (org.onosproject.net.PortNumber)40 List (java.util.List)30 Collectors (java.util.stream.Collectors)24 Set (java.util.Set)23 AbstractHandlerBehaviour (org.onosproject.net.driver.AbstractHandlerBehaviour)19 Logger (org.slf4j.Logger)19 ArrayList (java.util.ArrayList)18 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)17 Optional (java.util.Optional)17 Test (org.junit.Test)16 JsonNode (com.fasterxml.jackson.databind.JsonNode)15 Collections (java.util.Collections)15 VirtualNetwork (org.onosproject.incubator.net.virtual.VirtualNetwork)15 DriverHandler (org.onosproject.net.driver.DriverHandler)15 Collection (java.util.Collection)14