Search in sources :

Example 26 with DefaultLinkDescription

use of org.onosproject.net.link.DefaultLinkDescription in project onos by opennetworkinglab.

the class PolatisLinkDiscovery method getLinks.

/**
 * Returns the set of LinkDescriptions originating from a Polatis switch.
 * <p>
 * This is the callback required by the LinkDiscovery behaviour.
 * @return Set of outbound unidirectional links as LinkDescriptions
 */
@Override
public Set<LinkDescription> getLinks() {
    Set<LinkDescription> links = new HashSet<>();
    DeviceId deviceID = handler().data().deviceId();
    log.debug("*** Checking peer-port fields on device {}", deviceID.toString());
    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, will try again...", deviceID);
        return links;
    }
    DeviceService deviceService = checkNotNull(handler().get(DeviceService.class));
    Device device = deviceService.getDevice(deviceID);
    int numInputPorts = Integer.parseInt(device.annotations().value(KEY_INPUTPORTS));
    int numOutputPorts = Integer.parseInt(device.annotations().value(KEY_OUTPUTPORTS));
    log.trace("Talking to device " + handler().data().deviceId().toString());
    String reply = netconfGet(handler(), getPortsFilter());
    // Get port details from switch as PortDescription objects
    List<PortDescription> ports = parsePorts(reply, numInputPorts, numOutputPorts);
    int numPeerPortEntries = 0;
    int numPortsScanned = 0;
    ObjectMapper mapper = new ObjectMapper();
    for (PortDescription port : ports) {
        numPortsScanned++;
        if (deviceService.getPort(new ConnectPoint(deviceID, port.portNumber())).isEnabled()) {
            String peerPortData = port.annotations().value(KEY_PORTPEER);
            if (!peerPortData.equals("")) {
                numPeerPortEntries++;
                if (peerPortData.charAt(0) == '{') {
                    ConnectPoint nearEndCP = new ConnectPoint(deviceID, port.portNumber());
                    ConnectPoint farEndCP = null;
                    try {
                        farEndCP = parsePeerportDataForCP(mapper.readTree(peerPortData));
                    } catch (JsonProcessingException jpe) {
                        log.debug("Error processing peer-port JSON: {}", jpe.toString());
                    }
                    if (farEndCP != null) {
                        log.trace("Found ref on port {} to peer ConnectPoint: {}", port.portNumber(), farEndCP.toString());
                        if (checkPeer(nearEndCP, farEndCP, this.handler(), true)) {
                            log.trace("Peer {} checks out", farEndCP.toString());
                            // now add link to Set<LinkDescription>
                            DefaultAnnotations annotations = DefaultAnnotations.builder().set(KEY_LINKBIDIR, VALUE_FALSE).set(KEY_LINKALLOWED, VALUE_TRUE).build();
                            ConnectPoint aEndCP = nearEndCP;
                            ConnectPoint bEndCP = farEndCP;
                            // reverse direction of unidirectional link if near-end port is INPUT
                            if (port.annotations().value(KEY_PORTDIR).equals(VALUE_INPUT)) {
                                aEndCP = farEndCP;
                                bEndCP = nearEndCP;
                            }
                            LinkDescription newLinkDesc = new DefaultLinkDescription(aEndCP, bEndCP, Link.Type.OPTICAL, true, annotations);
                            links.add(newLinkDesc);
                            log.debug("Adding link {}", newLinkDesc);
                        }
                    }
                }
            }
        }
    }
    log.debug("Scanned {} ports, {} had peer-port entries, {} {} valid", numPortsScanned, numPeerPortEntries, links.size(), links.size() == 1 ? "is" : "are");
    log.trace("Links found on this iteration: {}", links);
    return links;
}
Also used : DefaultAnnotations(org.onosproject.net.DefaultAnnotations) DefaultLinkDescription(org.onosproject.net.link.DefaultLinkDescription) LinkDescription(org.onosproject.net.link.LinkDescription) DeviceId(org.onosproject.net.DeviceId) Device(org.onosproject.net.Device) DeviceService(org.onosproject.net.device.DeviceService) PortDescription(org.onosproject.net.device.PortDescription) ConnectPoint(org.onosproject.net.ConnectPoint) NetconfController(org.onosproject.netconf.NetconfController) ConnectPoint(org.onosproject.net.ConnectPoint) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) HashSet(java.util.HashSet) DefaultLinkDescription(org.onosproject.net.link.DefaultLinkDescription)

Example 27 with DefaultLinkDescription

use of org.onosproject.net.link.DefaultLinkDescription in project onos by opennetworkinglab.

the class CienaWaveserverAiLinkDiscovery method getLinks.

@Override
public Set<LinkDescription> getLinks() {
    log.debug("LINKS CHECKING ...");
    Set<LinkDescription> links = new HashSet<>();
    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<>();
        for (Device d : devices) {
            lookup.put(d.chassisId().toString(), d);
        }
        log.debug("MAP: {}", lookup);
        XPath xp = XPathFactory.newInstance().newXPath();
        Node node = TEMPLATE_MANAGER.doRequest(session, "discoverPortDetails");
        NodeList nodeList = (NodeList) xp.evaluate("waveserver-ports/ports", node, XPathConstants.NODESET);
        int count = nodeList.getLength();
        Node nodeListItem;
        for (int i = 0; i < count; i += 1) {
            Long portAsLong;
            Long destPortAsLong;
            String destChassis = null;
            String destPort = null;
            nodeListItem = nodeList.item(i);
            String port = xp.evaluate("port-id/text()", nodeListItem);
            portAsLong = portIdConvert(port);
            log.debug("CHECKING: {}", port);
            if (xp.evaluate("id/type/text()", nodeListItem).equals("otn")) {
                String label = xp.evaluate("id/label/text()", nodeListItem);
                final String r1 = "\\$\\{remote_mac:(.*?)\\}";
                final Pattern p1 = Pattern.compile(r1);
                final Matcher m1 = p1.matcher(label);
                if (m1.find()) {
                    destChassis = m1.group(1).replaceFirst("^0+(?!$)", "");
                }
                final String r2 = "\\$\\{remote_port:(.*?)\\}";
                final Pattern p2 = Pattern.compile(r2);
                final Matcher m2 = p2.matcher(label);
                if (m2.find()) {
                    destPort = m2.group(1);
                }
                destPortAsLong = portIdConvert(destPort);
                if (destChassis != null && destPort != null) {
                    log.debug("LOOKING FOR OTN neighbor chassis: {}", destChassis);
                    Device dest = lookup.get(destChassis);
                    if (dest != null) {
                        links.add(new DefaultLinkDescription(new ConnectPoint(dest.id(), PortNumber.portNumber(destPortAsLong, destPort)), new ConnectPoint(deviceId, PortNumber.portNumber(portAsLong, port)), Link.Type.TUNNEL, true));
                    } else {
                        log.error("DEST OTN CHASSIS is NULL for {}", xp.evaluate("port-id/text()", nodeListItem));
                    }
                } else {
                    log.error("NO LINK for {}", xp.evaluate("port-id/text()", nodeListItem));
                }
            } else if (xp.evaluate("id/type/text()", nodeListItem).equals("ethernet")) {
                Map<String, Object> templateContext = new HashMap<>();
                templateContext.put("port-number", port);
                node = TEMPLATE_MANAGER.doRequest(session, "getLinks", templateContext);
                String chassisIdSubtype = xp.evaluate("waveserver-lldp/port/remote/chassis/chassis-id/chassis-id-subtype/text()", node);
                if (chassisIdSubtype.equals("mac-address")) {
                    destChassis = xp.evaluate("waveserver-lldp/port/remote/chassis/chassis-id/chassis-id/text()", node).trim().toLowerCase();
                    if (destChassis.startsWith("0x")) {
                        destChassis = destChassis.substring(2);
                    }
                } else {
                    log.error("Unknown Chassis-id-subtype {}", xp.evaluate("waveserver-lldp/port/remote/chassis/chassis-id/chassis-id-subtype/text()", node));
                }
                destPort = xp.evaluate("waveserver-lldp/port/remote/port/id/id/text()", node);
                destPortAsLong = Long.valueOf(destPort);
                if (destChassis != null && !destPort.equals("")) {
                    log.debug("LOOKING FOR ethernet neighbor chassisId: {}", destChassis);
                    Device dest = lookup.get(destChassis);
                    if (dest != null) {
                        links.add(new DefaultLinkDescription(new ConnectPoint(deviceId, PortNumber.portNumber(portAsLong, port)), new ConnectPoint(dest.id(), PortNumber.portNumber(destPortAsLong, destPort)), Link.Type.TUNNEL, true));
                    } else {
                        log.debug("DEST CHASSIS is NULL for port {}", xp.evaluate("port-id/text()", nodeListItem));
                    }
                } else {
                    log.debug("NO LINK for {}", xp.evaluate("port-id/text()", nodeListItem));
                }
            }
        }
    } catch (NetconfException | XPathExpressionException e) {
        log.error("Unable to retrieve links for device {}, {}", deviceId, e);
    }
    return links;
}
Also used : NetconfSession(org.onosproject.netconf.NetconfSession) DefaultLinkDescription(org.onosproject.net.link.DefaultLinkDescription) LinkDescription(org.onosproject.net.link.LinkDescription) HashMap(java.util.HashMap) Matcher(java.util.regex.Matcher) DeviceId(org.onosproject.net.DeviceId) XPathExpressionException(javax.xml.xpath.XPathExpressionException) Node(org.w3c.dom.Node) NetconfController(org.onosproject.netconf.NetconfController) NetconfException(org.onosproject.netconf.NetconfException) HashSet(java.util.HashSet) DefaultLinkDescription(org.onosproject.net.link.DefaultLinkDescription) XPath(javax.xml.xpath.XPath) Pattern(java.util.regex.Pattern) Device(org.onosproject.net.Device) NodeList(org.w3c.dom.NodeList) DeviceService(org.onosproject.net.device.DeviceService) ConnectPoint(org.onosproject.net.ConnectPoint) ConnectPoint(org.onosproject.net.ConnectPoint) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

DefaultLinkDescription (org.onosproject.net.link.DefaultLinkDescription)27 ConnectPoint (org.onosproject.net.ConnectPoint)20 LinkDescription (org.onosproject.net.link.LinkDescription)11 Test (org.junit.Test)8 DefaultAnnotations (org.onosproject.net.DefaultAnnotations)6 DeviceId (org.onosproject.net.DeviceId)6 Device (org.onosproject.net.Device)5 DeviceService (org.onosproject.net.device.DeviceService)5 HashSet (java.util.HashSet)4 Link (org.onosproject.net.Link)4 LinkEvent (org.onosproject.net.link.LinkEvent)4 NetconfController (org.onosproject.netconf.NetconfController)4 HashMap (java.util.HashMap)3 XPath (javax.xml.xpath.XPath)3 XPathExpressionException (javax.xml.xpath.XPathExpressionException)3 Type (org.onosproject.net.Link.Type)3 NetconfException (org.onosproject.netconf.NetconfException)3 NetconfSession (org.onosproject.netconf.NetconfSession)3 Node (org.w3c.dom.Node)3 NodeList (org.w3c.dom.NodeList)3