Search in sources :

Example 71 with NetconfSession

use of org.onosproject.netconf.NetconfSession in project onos by opennetworkinglab.

the class TerminalDeviceDiscovery method discoverPortDetails.

/**
 * Returns a list of PortDescriptions for the device.
 *
 * @return a list of descriptions.
 *
 * The RPC reply follows the following pattern:
 * //CHECKSTYLE:OFF
 * <pre>{@code
 * <?xml version="1.0" encoding="UTF-8"?>
 * <rpc-reply xmlns="urn:ietf:params:xml:ns:netconf:base:1.0" message-id="7">
 * <data>
 *   <components xmlns="http://openconfig.net/yang/platform">
 *     <component>....
 *     </component>
 *     <component>....
 *     </component>
 *   </components>
 * </data>
 * </rpc-reply>
 * }</pre>
 * //CHECKSTYLE:ON
 */
@Override
public List<PortDescription> discoverPortDetails() {
    try {
        XPathExpressionEngine xpe = new XPathExpressionEngine();
        NetconfSession session = getNetconfSession(did());
        if (session == null) {
            log.error("discoverPortDetails called with null session for {}", did());
            return ImmutableList.of();
        }
        CompletableFuture<String> fut = session.rpc(getDeviceComponentsBuilder());
        String rpcReply = fut.get();
        XMLConfiguration xconf = (XMLConfiguration) XmlConfigParser.loadXmlString(rpcReply);
        xconf.setExpressionEngine(xpe);
        HierarchicalConfiguration components = xconf.configurationAt("data/components");
        return parsePorts(components);
    } catch (Exception e) {
        log.error("Exception discoverPortDetails() {}", did(), e);
        return ImmutableList.of();
    }
}
Also used : NetconfSession(org.onosproject.netconf.NetconfSession) XMLConfiguration(org.apache.commons.configuration.XMLConfiguration) XPathExpressionEngine(org.apache.commons.configuration.tree.xpath.XPathExpressionEngine) HierarchicalConfiguration(org.apache.commons.configuration.HierarchicalConfiguration) NetconfException(org.onosproject.netconf.NetconfException)

Example 72 with NetconfSession

use of org.onosproject.netconf.NetconfSession in project onos by opennetworkinglab.

the class NetconfAlarmProvider method activate.

@Activate
public void activate() {
    providerService = providerRegistry.register(this);
    controller.getNetconfDevices().forEach(id -> {
        NetconfDevice device = controller.getNetconfDevice(id);
        if (device.isMasterSession()) {
            NetconfSession session = device.getSession();
            InternalNotificationListener listener = new InternalNotificationListener(device.getDeviceInfo());
            try {
                session.addDeviceOutputListener(listener);
            } catch (NetconfException e) {
                log.error("addDeviceOutputListener Error {} ", e.getMessage());
            }
            idNotificationListenerMap.put(id, listener);
        }
    });
    controller.addDeviceListener(deviceListener);
    log.info("NetconfAlarmProvider Started");
}
Also used : NetconfSession(org.onosproject.netconf.NetconfSession) NetconfException(org.onosproject.netconf.NetconfException) NetconfDevice(org.onosproject.netconf.NetconfDevice) Activate(org.osgi.service.component.annotations.Activate)

Example 73 with NetconfSession

use of org.onosproject.netconf.NetconfSession in project onos by opennetworkinglab.

the class CassiniTerminalDeviceDiscovery method discoverPortDetails.

/**
 * Returns a list of PortDescriptions for the device.
 *
 * @return a list of descriptions.
 * <p>
 * The RPC reply follows the following pattern:
 * //CHECKSTYLE:OFF
 * <pre>{@code
 * <?xml version="1.0" encoding="UTF-8"?>
 * <rpc-reply xmlns="urn:ietf:params:xml:ns:netconf:base:1.0" message-id="7">
 * <data>
 *   <components xmlns="http://openconfig.net/yang/platform">
 *     <component>....
 *     </component>
 *     <component>....
 *     </component>
 *   </components>
 * </data>
 * </rpc-reply>
 * }</pre>
 * //CHECKSTYLE:ON
 */
@Override
public List<PortDescription> discoverPortDetails() {
    try {
        NetconfSession session = getNetconfSession(did());
        if (session == null) {
            log.error("discoverPortDetails called with null session for {}", did());
            return ImmutableList.of();
        }
        CompletableFuture<CharSequence> fut1 = session.asyncGet();
        String rpcReplyTest = fut1.get().toString();
        XMLConfiguration xconf = (XMLConfiguration) XmlConfigParser.loadXmlString(rpcReplyTest);
        xconf.setExpressionEngine(new XPathExpressionEngine());
        HierarchicalConfiguration logicalChannels = xconf.configurationAt("components");
        return discoverPorts(logicalChannels);
    } catch (Exception e) {
        log.error("Exception discoverPortDetails() {}", did(), e);
        return ImmutableList.of();
    }
}
Also used : NetconfSession(org.onosproject.netconf.NetconfSession) XMLConfiguration(org.apache.commons.configuration.XMLConfiguration) XPathExpressionEngine(org.apache.commons.configuration.tree.xpath.XPathExpressionEngine) HierarchicalConfiguration(org.apache.commons.configuration.HierarchicalConfiguration)

Example 74 with NetconfSession

use of org.onosproject.netconf.NetconfSession in project onos by opennetworkinglab.

the class NokiaTerminalDevicePowerConfig method getNetconfSession.

/**
 * Login to the device by providing the correct user and password in order to configure the device
 * Returns the NetconfSession with the device for which the method was called.
 *
 * @param deviceId device identifier
 * @param userName username to access the device
 * @param passwd password to access the device
 * @return The netconf session or null
 */
@Override
public NetconfSession getNetconfSession(DeviceId deviceId, String userName, String passwd) {
    userName = USER_NAME;
    passwd = PASSWORD;
    NetconfController nc = handler().get(NetconfController.class);
    NetconfDevice ndev = nc.getDevicesMap().get(deviceId);
    if (ndev == null) {
        log.debug("NetConf device " + deviceId + " is not found, returning null session");
        return null;
    }
    NetconfSession ns = ndev.getSession();
    if (ns == null) {
        log.error("discoverPorts called with null session for \n {}", deviceId);
        return null;
    }
    try {
        String reply = ns.requestSync(buildLoginRpc(userName, passwd));
        if (reply.contains("<ok/>")) {
            return ns;
        } else {
            log.debug("Reply contains this: \n {}", reply);
            return null;
        }
    } catch (NetconfException e) {
        log.error("Can NOT login to the device", e);
    }
    return ns;
}
Also used : NetconfSession(org.onosproject.netconf.NetconfSession) NetconfException(org.onosproject.netconf.NetconfException) NetconfDevice(org.onosproject.netconf.NetconfDevice) NetconfController(org.onosproject.netconf.NetconfController)

Example 75 with NetconfSession

use of org.onosproject.netconf.NetconfSession in project onos by opennetworkinglab.

the class ClientLineTerminalDeviceFlowRuleProgrammable method fetchConnectionsFromDevice.

/**
 * Fetches list of connections from device.
 *
 * TODO manage allow external flow rules (allowExternalFlowRules)
 * Currently removes from the device all connections that are not currently present in the DeviceConnectionCache.
 *
 * @return connections that are present on the device and in the DeviceConnectionCache.
 */
private List<FlowRule> fetchConnectionsFromDevice() {
    List<FlowRule> confirmedRules = new ArrayList<>();
    String reply;
    FlowRule cacheAddRule;
    FlowRule cacheDropRule;
    NetconfSession session = getNetconfSession();
    // Get relevant information from the device
    StringBuilder requestFilter = new StringBuilder();
    requestFilter.append("<components xmlns='http://openconfig.net/yang/platform'>");
    requestFilter.append("  <component>");
    requestFilter.append("    <name/>");
    requestFilter.append("    <oc-opt-term:optical-channel " + "xmlns:oc-opt-term='http://openconfig.net/yang/terminal-device'>");
    requestFilter.append("      <oc-opt-term:config/>");
    requestFilter.append("    </oc-opt-term:optical-channel>");
    requestFilter.append("  </component>");
    requestFilter.append("</components>");
    requestFilter.append("<terminal-device xmlns='http://openconfig.net/yang/terminal-device'>");
    requestFilter.append("  <logical-channels>");
    requestFilter.append("    <channel>");
    requestFilter.append("      <index/>");
    requestFilter.append("      <config>");
    requestFilter.append("        <admin-state/>");
    requestFilter.append("        <logical-channel-type/>");
    requestFilter.append("      </config>");
    requestFilter.append("      <logical-channel-assignments>");
    requestFilter.append("        <assignment>");
    requestFilter.append("          <config>");
    requestFilter.append("            <logical-channel/>");
    requestFilter.append("          </config>");
    requestFilter.append("        </assignment>");
    requestFilter.append("      </logical-channel-assignments>");
    requestFilter.append("    </channel>");
    requestFilter.append("  </logical-channels>");
    requestFilter.append("</terminal-device>");
    try {
        reply = session.get(requestFilter.toString(), null);
    // log.debug("TRANSPONDER CONNECTIONS - fetchConnectionsFromDevice {} reply {}", did(), reply);
    } catch (NetconfException e) {
        log.error("Failed to retrieve configuration details for device {}", handler().data().deviceId(), e);
        return ImmutableList.of();
    }
    HierarchicalConfiguration cfg = XmlConfigParser.loadXml(new ByteArrayInputStream(reply.getBytes()));
    List<HierarchicalConfiguration> logicalChannels = cfg.configurationsAt("data.terminal-device.logical-channels.channel");
    List<HierarchicalConfiguration> components = cfg.configurationsAt("data.components.component");
    // Retrieve the ENABLED line ports
    List<String> enabledOpticalChannels = logicalChannels.stream().filter(r -> r.getString("config.logical-channel-type").equals(OC_TYPE_PROT_OTN)).filter(r -> r.getString("config.admin-state").equals(OPERATION_ENABLE)).map(r -> r.getString("index")).collect(Collectors.toList());
    log.debug("fetchConnectionsFromDevice {} enabledOpticalChannelsIndex {}", did(), enabledOpticalChannels);
    if (enabledOpticalChannels.size() != 0) {
        for (String channel : enabledOpticalChannels) {
            log.debug("fetchOpticalConnectionsFromDevice {} channel {}", did(), channel);
            // Retrieve the corresponding central frequency from the associated component
            // TODO correlate the components instead of relying on naming
            Frequency centralFreq = components.stream().filter(c -> c.getString("name").equals(PREFIX_CHANNEL + channel)).map(c -> c.getDouble("optical-channel.config.frequency")).map(c -> Frequency.ofMHz(c)).findFirst().orElse(null);
            confirmedRules.addAll(fetchLineConnectionFromDevice(channel, centralFreq));
        }
    }
    // Retrieve the ENABLED client ports
    List<String> enabledClientChannels = logicalChannels.stream().filter(r -> r.getString("config.logical-channel-type").equals(OC_TYPE_PROT_ETH)).filter(r -> r.getString("config.admin-state").equals(OPERATION_ENABLE)).map(r -> r.getString("index")).collect(Collectors.toList());
    log.debug("fetchClientConnectionsFromDevice {} enabledClientChannelsIndex {}", did(), enabledClientChannels);
    if (enabledClientChannels.size() != 0) {
        for (String clientPort : enabledClientChannels) {
            log.debug("fetchClientConnectionsFromDevice {} channel {}", did(), clientPort);
            String linePort = logicalChannels.stream().filter(r -> r.getString("config.logical-channel-type").equals(OC_TYPE_PROT_ETH)).filter(r -> r.getString("config.admin-state").equals(OPERATION_ENABLE)).filter(r -> r.getString("index").equals(clientPort)).map(r -> r.getString("logical-channel-assignments.assignment.config.logical-channel")).findFirst().orElse(null);
            // Build the corresponding flow rule as expected
            // Selector including port
            // Treatment including port
            PortNumber clientPortNumber = PortNumber.portNumber(clientPort);
            PortNumber linePortNumber = PortNumber.portNumber(linePort);
            confirmedRules.addAll(fetchClientConnectionFromDevice(clientPortNumber, linePortNumber));
        }
    }
    // Returns rules that are both on the device and on the cache
    if (confirmedRules.size() != 0) {
        log.info("fetchConnectionsFromDevice {} number of confirmed rules {}", did(), confirmedRules.size());
        return confirmedRules;
    } else {
        return ImmutableList.of();
    }
}
Also used : NetconfSession(org.onosproject.netconf.NetconfSession) NetconfException(org.onosproject.netconf.NetconfException) GridType(org.onosproject.net.GridType) XPath(javax.xml.xpath.XPath) FlowRuleProgrammable(org.onosproject.net.flow.FlowRuleProgrammable) DefaultFlowRule(org.onosproject.net.flow.DefaultFlowRule) PortNumber(org.onosproject.net.PortNumber) DeviceService(org.onosproject.net.device.DeviceService) LoggerFactory(org.slf4j.LoggerFactory) FlowEntry(org.onosproject.net.flow.FlowEntry) Spectrum(org.onlab.util.Spectrum) DefaultTrafficTreatment(org.onosproject.net.flow.DefaultTrafficTreatment) NetconfSession(org.onosproject.netconf.NetconfSession) AbstractHandlerBehaviour(org.onosproject.net.driver.AbstractHandlerBehaviour) ArrayList(java.util.ArrayList) Frequency(org.onlab.util.Frequency) TrafficSelector(org.onosproject.net.flow.TrafficSelector) ImmutableList(com.google.common.collect.ImmutableList) ByteArrayInputStream(java.io.ByteArrayInputStream) NetconfController(org.onosproject.netconf.NetconfController) Document(org.w3c.dom.Document) NamespaceContext(javax.xml.namespace.NamespaceContext) Criteria(org.onosproject.net.flow.criteria.Criteria) DefaultTrafficSelector(org.onosproject.net.flow.DefaultTrafficSelector) TrafficTreatment(org.onosproject.net.flow.TrafficTreatment) OchSignalType(org.onosproject.net.OchSignalType) DefaultFlowEntry(org.onosproject.net.flow.DefaultFlowEntry) InputSource(org.xml.sax.InputSource) Instructions(org.onosproject.net.flow.instructions.Instructions) Logger(org.slf4j.Logger) Iterator(java.util.Iterator) Collection(java.util.Collection) Preconditions.checkNotNull(com.google.common.base.Preconditions.checkNotNull) HierarchicalConfiguration(org.apache.commons.configuration.HierarchicalConfiguration) XmlConfigParser(org.onosproject.drivers.utilities.XmlConfigParser) OdtnDeviceDescriptionDiscovery(org.onosproject.odtn.behaviour.OdtnDeviceDescriptionDiscovery) Collectors(java.util.stream.Collectors) DeviceConnectionCache(org.onosproject.drivers.odtn.impl.DeviceConnectionCache) OchSignal(org.onosproject.net.OchSignal) DatastoreId(org.onosproject.netconf.DatastoreId) XPathFactory(javax.xml.xpath.XPathFactory) List(java.util.List) StringReader(java.io.StringReader) FlowRuleParser(org.onosproject.drivers.odtn.impl.FlowRuleParser) FlowRule(org.onosproject.net.flow.FlowRule) DocumentBuilder(javax.xml.parsers.DocumentBuilder) ChannelSpacing(org.onosproject.net.ChannelSpacing) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) DeviceId(org.onosproject.net.DeviceId) ArrayList(java.util.ArrayList) HierarchicalConfiguration(org.apache.commons.configuration.HierarchicalConfiguration) NetconfException(org.onosproject.netconf.NetconfException) ByteArrayInputStream(java.io.ByteArrayInputStream) Frequency(org.onlab.util.Frequency) DefaultFlowRule(org.onosproject.net.flow.DefaultFlowRule) FlowRule(org.onosproject.net.flow.FlowRule) PortNumber(org.onosproject.net.PortNumber)

Aggregations

NetconfSession (org.onosproject.netconf.NetconfSession)87 NetconfException (org.onosproject.netconf.NetconfException)72 NetconfController (org.onosproject.netconf.NetconfController)44 DeviceId (org.onosproject.net.DeviceId)32 XPath (javax.xml.xpath.XPath)22 XPathExpressionException (javax.xml.xpath.XPathExpressionException)18 XMLConfiguration (org.apache.commons.configuration.XMLConfiguration)18 Node (org.w3c.dom.Node)18 ArrayList (java.util.ArrayList)16 HierarchicalConfiguration (org.apache.commons.configuration.HierarchicalConfiguration)16 ByteArrayInputStream (java.io.ByteArrayInputStream)15 DeviceService (org.onosproject.net.device.DeviceService)13 Device (org.onosproject.net.Device)12 DefaultDeviceDescription (org.onosproject.net.device.DefaultDeviceDescription)11 ChassisId (org.onlab.packet.ChassisId)10 FlowRule (org.onosproject.net.flow.FlowRule)10 HashMap (java.util.HashMap)9 PortDescription (org.onosproject.net.device.PortDescription)9 NodeList (org.w3c.dom.NodeList)9 ConnectPoint (org.onosproject.net.ConnectPoint)8