Waveserver Ai Driver support

Change-Id: I1d65e30b4b46dbad8802fd2edd9dec74b5d2ec18

revert readme

Change-Id: I32f7a9ec21f743d98cdece2ceb097fc65b784589
diff --git a/drivers/ciena/waveserverai/src/main/java/org/onosproject/drivers/ciena/waveserverai/CienaDriversLoader.java b/drivers/ciena/waveserverai/src/main/java/org/onosproject/drivers/ciena/waveserverai/CienaDriversLoader.java
new file mode 100644
index 0000000..0511337
--- /dev/null
+++ b/drivers/ciena/waveserverai/src/main/java/org/onosproject/drivers/ciena/waveserverai/CienaDriversLoader.java
@@ -0,0 +1,36 @@
+/*
+ * Copyright 2016-present Open Networking Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.onosproject.drivers.ciena.waveserverai;
+
+import org.apache.felix.scr.annotations.Component;
+import org.onosproject.net.driver.AbstractDriverLoader;
+import org.onosproject.net.optical.OpticalDevice;
+
+/**
+ * Loader for Ciena Waveserver Ai device drivers.
+ */
+@Component(immediate = true)
+public class CienaDriversLoader extends AbstractDriverLoader {
+
+    // OSGI: help bundle plugin discover runtime package dependency.
+    @SuppressWarnings("unused")
+    private OpticalDevice optical;
+
+    public CienaDriversLoader() {
+        super("/ciena-drivers.xml");
+    }
+}
diff --git a/drivers/ciena/waveserverai/src/main/java/org/onosproject/drivers/ciena/waveserverai/netconf/CienaWaveserverAiDeviceDescription.java b/drivers/ciena/waveserverai/src/main/java/org/onosproject/drivers/ciena/waveserverai/netconf/CienaWaveserverAiDeviceDescription.java
new file mode 100644
index 0000000..9f6f5e3
--- /dev/null
+++ b/drivers/ciena/waveserverai/src/main/java/org/onosproject/drivers/ciena/waveserverai/netconf/CienaWaveserverAiDeviceDescription.java
@@ -0,0 +1,258 @@
+/*
+ * Copyright 2016-present Open Networking Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.onosproject.drivers.ciena.waveserverai.netconf;
+
+import com.google.common.collect.ImmutableList;
+import org.onlab.packet.ChassisId;
+import org.onosproject.drivers.netconf.TemplateManager;
+import org.onosproject.net.AnnotationKeys;
+import org.onosproject.net.DefaultAnnotations;
+import org.onosproject.net.Device;
+import org.onosproject.net.DeviceId;
+import org.onosproject.net.Port;
+import org.onosproject.net.PortNumber;
+import org.onosproject.net.SparseAnnotations;
+import org.onosproject.net.device.DefaultDeviceDescription;
+import org.onosproject.net.device.DefaultPortDescription;
+import org.onosproject.net.device.DeviceDescription;
+import org.onosproject.net.device.DeviceDescriptionDiscovery;
+import org.onosproject.net.device.DeviceService;
+import org.onosproject.net.device.PortDescription;
+import org.onosproject.net.driver.AbstractHandlerBehaviour;
+import org.onosproject.netconf.NetconfController;
+import org.onosproject.netconf.NetconfDevice;
+import org.onosproject.netconf.NetconfException;
+import org.onosproject.netconf.NetconfSession;
+import org.slf4j.Logger;
+import org.w3c.dom.Node;
+import org.w3c.dom.NodeList;
+
+import javax.xml.xpath.XPath;
+import javax.xml.xpath.XPathConstants;
+import javax.xml.xpath.XPathExpressionException;
+import javax.xml.xpath.XPathFactory;
+import java.util.ArrayList;
+import java.util.List;
+
+import static com.google.common.base.Preconditions.checkNotNull;
+import static org.slf4j.LoggerFactory.getLogger;
+
+/**
+ * Discovers the device and ports from a Ciena WaveServer Ai Netconf device.
+ */
+
+public class CienaWaveserverAiDeviceDescription extends AbstractHandlerBehaviour
+        implements DeviceDescriptionDiscovery {
+    private static final TemplateManager TEMPLATE_MANAGER = new TemplateManager();
+
+    private final Logger log = getLogger(getClass());
+
+    public CienaWaveserverAiDeviceDescription() {
+        log.info("Loaded handler behaviour CienaWaveserverAiDeviceDescription.");
+    }
+
+    static {
+        TEMPLATE_MANAGER.load(CienaWaveserverAiDeviceDescription.class,
+                             "/templates/requests/%s.j2",
+                             "discoverDeviceDetails", "discoverPortDetails");
+    }
+
+    @Override
+    public DeviceDescription discoverDeviceDetails() {
+        log.debug("Adding description for Waveserver Ai device");
+
+        NetconfSession session = getNetconfSession();
+        Device device = getDevice(handler().data().deviceId());
+
+        try {
+            XPath xp = XPathFactory.newInstance().newXPath();
+
+            Node node = TEMPLATE_MANAGER.doRequest(session, "discoverDeviceDetails");
+            String chassisId = xp.evaluate("waveserver-chassis/mac-addresses/chassis/base/text()", node);
+            chassisId = chassisId.replace(":", "");
+            SparseAnnotations annotationDevice = DefaultAnnotations.builder()
+                    .set("name", xp.evaluate("waveserver-system/host-name/current-host-name/text()", node))
+                    .build();
+            return new DefaultDeviceDescription(device.id().uri(),
+                                                Device.Type.OTN,
+                                                "Ciena",
+                                                "WaverserverAi",
+                                                xp.evaluate("waveserver-software/status/active-version/text()",
+                                                            node),
+                                                xp.evaluate("waveserver-chassis/identification/serial-number/text()",
+                                                            node),
+                                                new ChassisId(Long.valueOf(chassisId, 16)),
+                                                (SparseAnnotations) annotationDevice);
+        } catch (NetconfException | XPathExpressionException e) {
+            log.error("Unable to retrieve device information for device {}, {}", device.chassisId(), e);
+        }
+
+        return new DefaultDeviceDescription(device.id().uri(), Device.Type.OTN, "Ciena", "WaverserverAi", "unknown",
+                                            "unknown", device.chassisId());
+    }
+
+    @Override
+    public List<PortDescription> discoverPortDetails() {
+        log.info("Adding ports for Waveserver Ai device");
+        List<PortDescription> ports = new ArrayList<>();
+
+        Device device = getDevice(handler().data().deviceId());
+        NetconfSession session = getNetconfSession();
+
+        try {
+            XPath xp = XPathFactory.newInstance().newXPath();
+            Node nodeListItem;
+
+            Node node = TEMPLATE_MANAGER.doRequest(session, "discoverPortDetails");
+            NodeList nodeList = (NodeList) xp.evaluate("waveserver-ports/ports", node, XPathConstants.NODESET);
+            int count = nodeList.getLength();
+            for (int i = 0; i < count; ++i) {
+                nodeListItem = nodeList.item(i);
+                DefaultAnnotations annotationPort = DefaultAnnotations.builder()
+                        .set(AnnotationKeys.PORT_NAME, xp.evaluate("port-id/text()", nodeListItem))
+                        .set(AnnotationKeys.PROTOCOL, xp.evaluate("id/type/text()", nodeListItem))
+                        .build();
+                String port = xp.evaluate("port-id/text()", nodeListItem);
+                ports.add(DefaultPortDescription.builder()
+                          .withPortNumber(PortNumber.portNumber(
+                                  portIdConvert(port), port))
+                          .isEnabled(portStateConvert(
+                                  xp.evaluate("state/operational-state/text()", nodeListItem)))
+                          .portSpeed(portSpeedToLong(xp.evaluate("id/speed/text()", nodeListItem)))
+                          .type(Port.Type.PACKET)
+                          .annotations(annotationPort)
+                          .build());
+            }
+        } catch (NetconfException | XPathExpressionException e) {
+            log.error("Unable to retrieve port information for device {}, {}", device.chassisId(), e);
+        }
+        return ImmutableList.copyOf(ports);
+    }
+
+    /**
+     * Returns the Device of the deviceId.
+     *
+     * @return device
+     */
+    private Device getDevice(DeviceId deviceId) {
+        DeviceService deviceService = checkNotNull(handler().get(DeviceService.class));
+        return deviceService.getDevice(deviceId);
+    }
+
+    /**
+     * Returns the NETCONF session of the device.
+     *
+     * @return session
+     */
+    private NetconfSession getNetconfSession() {
+        NetconfController controller = checkNotNull(handler().get(NetconfController.class));
+        NetconfDevice ncDevice = controller.getDevicesMap().get(handler().data().deviceId());
+        if (ncDevice == null) {
+            log.error(
+                    "Internal ONOS Error. Device has been marked as reachable, "
+                            + "but deviceID {} is not in Devices Map. Continuing with empty description",
+                    handler().data().deviceId());
+        }
+        return controller.getDevicesMap().get(handler().data().deviceId())
+                .getSession();
+    }
+
+    /**
+     * Convert port speed in Gbps tp port speed in Mbps.
+     *
+     * @param speed
+     *            port speed as string
+     * @return port speed
+     */
+    public static Long portSpeedToLong(String speed) {
+        double d = Double.parseDouble(speed);
+        return (new Double(d * 1000)).longValue();
+    }
+
+    /**
+     * Convert port operational state to Boolean.
+     *
+     * @param state
+     *            port state as string
+     * @return port state
+     */
+    public static Boolean portStateConvert(String state) {
+        switch (state) {
+            case "up":
+                return true;
+            case "enabled":
+                return true;
+            case "disabled":
+                return false;
+            case "down":
+                return false;
+            default:
+                return false;
+        }
+    }
+
+    /**
+     * Convert port operational state to Boolean.
+     *
+     * @param state
+     *            port state as Boolean
+     * @return port state
+     */
+    public static String portStateConvert(Boolean state) {
+        if (state) {
+            return "enabled";
+        } else {
+            return "disabled";
+        }
+    }
+
+    /**
+     * Convert port name to port id.
+     *
+     * @param name
+     *            port name as String
+     * @return port id
+     */
+    public static Long portIdConvert(String name) {
+        String result = "1";
+        String replaceString = name.replace("-Ethernet", "");
+        String[] arrOfStr = replaceString.split("-");
+        if (arrOfStr[0].length() == 1) {
+            result += "0" + arrOfStr[0];
+        } else {
+            result += arrOfStr[0];
+        }
+        if (arrOfStr[1].length() == 1) {
+            result += "0" + arrOfStr[1];
+        } else {
+            result += arrOfStr[1];
+        }
+        return Long.valueOf(result);
+    }
+    /**
+     * Convert port id to port name.
+     *
+     * @param id
+     *            port id as Long
+     * @return port name as String
+     */
+    public static String portIdConvert(Long id) {
+        Integer pre = Integer.parseInt(id.toString().substring(1, 3));
+        Integer post = Integer.parseInt(id.toString().substring(3, 5));
+        return pre.toString() + "-" + post.toString();
+    }
+
+}
diff --git a/drivers/ciena/waveserverai/src/main/java/org/onosproject/drivers/ciena/waveserverai/netconf/CienaWaveserverAiLinkDiscovery.java b/drivers/ciena/waveserverai/src/main/java/org/onosproject/drivers/ciena/waveserverai/netconf/CienaWaveserverAiLinkDiscovery.java
new file mode 100644
index 0000000..1308760
--- /dev/null
+++ b/drivers/ciena/waveserverai/src/main/java/org/onosproject/drivers/ciena/waveserverai/netconf/CienaWaveserverAiLinkDiscovery.java
@@ -0,0 +1,184 @@
+/*
+ * Copyright 2016-present Open Networking Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.onosproject.drivers.ciena.waveserverai.netconf;
+
+import org.onosproject.drivers.netconf.TemplateManager;
+import org.onosproject.net.ConnectPoint;
+import org.onosproject.net.Device;
+import org.onosproject.net.DeviceId;
+import org.onosproject.net.Link;
+import org.onosproject.net.PortNumber;
+import org.onosproject.net.behaviour.LinkDiscovery;
+import org.onosproject.net.device.DeviceService;
+import org.onosproject.net.driver.AbstractHandlerBehaviour;
+import org.onosproject.net.link.DefaultLinkDescription;
+import org.onosproject.net.link.LinkDescription;
+import org.onosproject.netconf.NetconfController;
+import org.onosproject.netconf.NetconfException;
+import org.onosproject.netconf.NetconfSession;
+import org.slf4j.Logger;
+import org.w3c.dom.Node;
+import org.w3c.dom.NodeList;
+
+import javax.xml.xpath.XPath;
+import javax.xml.xpath.XPathConstants;
+import javax.xml.xpath.XPathExpressionException;
+import javax.xml.xpath.XPathFactory;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Map;
+import java.util.Set;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+import static com.google.common.base.Preconditions.checkNotNull;
+import static org.onosproject.drivers.ciena.waveserverai.netconf.CienaWaveserverAiDeviceDescription.portIdConvert;
+import static org.slf4j.LoggerFactory.getLogger;
+
+/**
+ * Discovers the device and ports from a Ciena WaveServer Ai Netconf device.
+ */
+
+public class CienaWaveserverAiLinkDiscovery extends AbstractHandlerBehaviour
+        implements LinkDiscovery {
+    private static final TemplateManager TEMPLATE_MANAGER = new TemplateManager();
+
+    private final Logger log = getLogger(getClass());
+
+    public CienaWaveserverAiLinkDiscovery() {
+        log.info("Loaded handler behaviour CienaWaveserverAiLinkDiscovery.");
+    }
+
+    static {
+        TEMPLATE_MANAGER.load(CienaWaveserverAiLinkDiscovery.class,
+                             "/templates/requests/%s.j2",
+                             "getLinks", "discoverPortDetails");
+    }
+
+    @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(deviceId,
+                                                     PortNumber.portNumber(portAsLong, port)),
+                                    new ConnectPoint(dest.id(),
+                                                     PortNumber.portNumber(destPortAsLong, destPort)),
+                                    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;
+    }
+
+}
diff --git a/drivers/ciena/waveserverai/src/main/java/org/onosproject/drivers/ciena/waveserverai/netconf/CienaWaveserverAiPortAdmin.java b/drivers/ciena/waveserverai/src/main/java/org/onosproject/drivers/ciena/waveserverai/netconf/CienaWaveserverAiPortAdmin.java
new file mode 100644
index 0000000..509b382
--- /dev/null
+++ b/drivers/ciena/waveserverai/src/main/java/org/onosproject/drivers/ciena/waveserverai/netconf/CienaWaveserverAiPortAdmin.java
@@ -0,0 +1,116 @@
+/*
+ * Copyright 2018-present Open Networking Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.onosproject.drivers.ciena.waveserverai.netconf;
+
+import org.onosproject.net.PortNumber;
+import org.onosproject.net.behaviour.PortAdmin;
+import org.onosproject.net.driver.AbstractHandlerBehaviour;
+import org.onosproject.netconf.NetconfController;
+import org.onosproject.netconf.NetconfException;
+import org.onosproject.netconf.NetconfSession;
+import org.onosproject.drivers.netconf.TemplateManager;
+import org.slf4j.Logger;
+import org.w3c.dom.Node;
+
+import javax.xml.xpath.XPath;
+import javax.xml.xpath.XPathConstants;
+import javax.xml.xpath.XPathExpressionException;
+import javax.xml.xpath.XPathFactory;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.concurrent.CompletableFuture;
+
+import static com.google.common.base.Preconditions.checkNotNull;
+import static org.onosproject.drivers.ciena.waveserverai.netconf.CienaWaveserverAiDeviceDescription.portIdConvert;
+import static org.onosproject.drivers.ciena.waveserverai.netconf.CienaWaveserverAiDeviceDescription.portStateConvert;
+import static org.slf4j.LoggerFactory.getLogger;
+
+/**
+ * Handles port administration for Ciena Waveserver Ai devices using the NETCONF
+ * protocol.
+ */
+public class CienaWaveserverAiPortAdmin extends AbstractHandlerBehaviour implements PortAdmin {
+    private static final TemplateManager TEMPLATE_MANAGER = new TemplateManager();
+    public static final Logger log = getLogger(CienaWaveserverAiPortAdmin.class);
+
+    static {
+        TEMPLATE_MANAGER.load(CienaWaveserverAiPortAdmin.class,
+                              "/templates/requests/%s.j2", "isEnabled", "setAdminState");
+    }
+
+    /**
+     * Sets the administrative state of the given port to the given value.
+     *
+     * @param number
+     *            port number
+     * @param state
+     *            state, true for enabled, false for disabled
+     * @return true if successfully set
+     */
+    private CompletableFuture<Boolean> setAdminState(PortNumber number, Boolean state) {
+        NetconfController controller = checkNotNull(handler().get(NetconfController.class));
+        NetconfSession session = controller.getDevicesMap().get(handler().data().deviceId()).getSession();
+        String port = portIdConvert(Long.valueOf(String.valueOf(number)));
+        String adminState = portStateConvert(state);
+
+        try {
+            Map<String, Object> templateContext = new HashMap<String, Object>();
+            templateContext.put("port-number", port);
+            templateContext.put("admin-state", adminState);
+            Node req = (Node) TEMPLATE_MANAGER.doRequest(session, "setAdminState", templateContext, "/",
+                                                        XPathConstants.NODE);
+            XPath xp = XPathFactory.newInstance().newXPath();
+
+            // If OK element exists then it worked.
+            Node ok = (Node) xp.evaluate("/rpc-reply/ok", req, XPathConstants.NODE);
+
+            return CompletableFuture.completedFuture(ok != null);
+        } catch (XPathExpressionException | NetconfException e) {
+            log.error("Unable to set port admin state for port {} to {}", port, handler().data().deviceId(), adminState,
+                      e);
+        }
+        return CompletableFuture.completedFuture(false);
+    }
+
+    @Override
+    public CompletableFuture<Boolean> enable(PortNumber number) {
+        return setAdminState(number, true);
+    }
+
+    @Override
+    public CompletableFuture<Boolean> disable(PortNumber number) {
+        return setAdminState(number, false);
+    }
+
+    @Override
+    public CompletableFuture<Boolean> isEnabled(PortNumber number) {
+        NetconfController controller = checkNotNull(handler().get(NetconfController.class));
+        NetconfSession session = controller.getDevicesMap().get(handler().data().deviceId()).getSession();
+
+        try {
+            log.debug("Querying port state for port {} from device {}", number, handler().data().deviceId());
+            Map<String, Object> templateContext = new HashMap<String, Object>();
+            templateContext.put("port-number", number.toString());
+            Node port = TEMPLATE_MANAGER.doRequest(session, "isEnabled", templateContext);
+            XPath xp = XPathFactory.newInstance().newXPath();
+            return CompletableFuture.completedFuture(portStateConvert(
+                    xp.evaluate("state/operational-state/text()", port)));
+        } catch (XPathExpressionException | NetconfException e) {
+            log.error("Unable to query port state for port {} from device {}", number, handler().data().deviceId(), e);
+        }
+        return CompletableFuture.completedFuture(false);
+    }
+}
diff --git a/drivers/ciena/waveserverai/src/main/java/org/onosproject/drivers/ciena/waveserverai/netconf/CienaWaveserverAiPortStatisticsDiscovery.java b/drivers/ciena/waveserverai/src/main/java/org/onosproject/drivers/ciena/waveserverai/netconf/CienaWaveserverAiPortStatisticsDiscovery.java
new file mode 100644
index 0000000..24c7a08
--- /dev/null
+++ b/drivers/ciena/waveserverai/src/main/java/org/onosproject/drivers/ciena/waveserverai/netconf/CienaWaveserverAiPortStatisticsDiscovery.java
@@ -0,0 +1,111 @@
+/*
+ * Copyright 2016-present Open Networking Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.onosproject.drivers.ciena.waveserverai.netconf;
+
+import com.google.common.collect.ImmutableList;
+import org.onosproject.drivers.netconf.TemplateManager;
+import org.onosproject.net.DeviceId;
+import org.onosproject.net.PortNumber;
+import org.onosproject.net.device.DefaultPortStatistics;
+import org.onosproject.net.device.PortStatistics;
+import org.onosproject.net.device.PortStatisticsDiscovery;
+import org.onosproject.net.driver.AbstractHandlerBehaviour;
+import org.onosproject.netconf.NetconfController;
+import org.onosproject.netconf.NetconfException;
+import org.onosproject.netconf.NetconfSession;
+import org.slf4j.Logger;
+import org.w3c.dom.Node;
+import org.w3c.dom.NodeList;
+
+import javax.xml.xpath.XPath;
+import javax.xml.xpath.XPathConstants;
+import javax.xml.xpath.XPathExpressionException;
+import javax.xml.xpath.XPathFactory;
+import java.util.ArrayList;
+import java.util.Collection;
+
+import static com.google.common.base.Preconditions.checkNotNull;
+import static org.onosproject.drivers.ciena.waveserverai.netconf.CienaWaveserverAiDeviceDescription.portIdConvert;
+import static org.slf4j.LoggerFactory.getLogger;
+
+/**
+ * Discovers the device and ports from a Ciena WaveServer Ai Netconf device.
+ */
+
+public class CienaWaveserverAiPortStatisticsDiscovery extends AbstractHandlerBehaviour
+        implements PortStatisticsDiscovery {
+    private static final TemplateManager TEMPLATE_MANAGER = new TemplateManager();
+
+    private final Logger log = getLogger(getClass());
+
+    public CienaWaveserverAiPortStatisticsDiscovery() {
+        log.info("Loaded handler behaviour CienaWaveserverAiPortStatisticsDiscovery.");
+    }
+
+    static {
+        TEMPLATE_MANAGER.load(CienaWaveserverAiPortStatisticsDiscovery.class,
+                             "/templates/requests/%s.j2",
+                             "discoverPortStatistics");
+    }
+
+    @Override
+    public Collection<PortStatistics> discoverPortStatistics() {
+        log.debug("Calculating port stats for Waveserver Ai device");
+        Collection<PortStatistics> portStats = new ArrayList<>();
+
+        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 portStats;
+        }
+        NetconfSession session = controller.getDevicesMap().get(deviceId).getSession();
+
+        try {
+            XPath xp = XPathFactory.newInstance().newXPath();
+            String tx = "current-bin/statistics/interface-counts/tx/";
+            String rx = "current-bin/statistics/interface-counts/rx/";
+
+            Node node = TEMPLATE_MANAGER.doRequest(session, "discoverPortStatistics");
+            NodeList nodeList = (NodeList) xp.evaluate("waveserver-pm/ethernet-performance-instances",
+                                                       node, XPathConstants.NODESET);
+            Node nodeListItem;
+            int count = nodeList.getLength();
+            for (int i = 0; i < count; ++i) {
+                nodeListItem = nodeList.item(i);
+                portStats.add(DefaultPortStatistics.builder()
+                          .setDeviceId(deviceId)
+                          .setPort(PortNumber.portNumber(
+                                  portIdConvert(xp.evaluate("instance-name/text()", nodeListItem))))
+                          .setPacketsReceived(Long.parseLong(xp.evaluate(rx + "packets/value/text()", nodeListItem)))
+                          .setPacketsSent(Long.parseLong(xp.evaluate(tx + "packets/value/text()", nodeListItem)))
+                          .setBytesReceived(Long.parseLong(xp.evaluate(rx + "bytes/value/text()", nodeListItem)))
+                          .setBytesSent(Long.parseLong(xp.evaluate(tx + "bytes/value/text()", nodeListItem)))
+//                          .setPacketsRxDropped(packetsRxDropped)
+//                          .setPacketsRxErrors(packetsRxErrors)
+//                          .setPacketsTxDropped(packetsTxDropped)
+//                          .setPacketsTxErrors(packetsTxErrors)
+                          .build());
+            }
+        } catch (NetconfException | XPathExpressionException e) {
+            log.error("Unable to retrieve port stats information for device {}, {}", deviceId, e);
+        }
+
+        return ImmutableList.copyOf(portStats);
+    }
+
+}
diff --git a/drivers/ciena/waveserverai/src/main/java/org/onosproject/drivers/ciena/waveserverai/netconf/package-info.java b/drivers/ciena/waveserverai/src/main/java/org/onosproject/drivers/ciena/waveserverai/netconf/package-info.java
new file mode 100644
index 0000000..d45797f
--- /dev/null
+++ b/drivers/ciena/waveserverai/src/main/java/org/onosproject/drivers/ciena/waveserverai/netconf/package-info.java
@@ -0,0 +1,20 @@
+/*
+ * Copyright 2016-present Open Networking Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+/**
+ * Package for Ciena device drivers.
+ */
+package org.onosproject.drivers.ciena.waveserverai.netconf;
\ No newline at end of file
diff --git a/drivers/ciena/waveserverai/src/main/java/org/onosproject/drivers/ciena/waveserverai/package-info.java b/drivers/ciena/waveserverai/src/main/java/org/onosproject/drivers/ciena/waveserverai/package-info.java
new file mode 100644
index 0000000..2b6c869
--- /dev/null
+++ b/drivers/ciena/waveserverai/src/main/java/org/onosproject/drivers/ciena/waveserverai/package-info.java
@@ -0,0 +1,20 @@
+/*
+ * Copyright 2016-present Open Networking Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+/**
+ * Package for Ciena device drivers.
+ */
+package org.onosproject.drivers.ciena.waveserverai;
\ No newline at end of file