[GEANT] Interface configuration for Cisco devices.

Change-Id: Ieb9979c3c4d7ebbf996d787a1528ba0548342572
diff --git a/cli/src/main/java/org/onosproject/cli/net/DeviceInterfaceAddCommand.java b/cli/src/main/java/org/onosproject/cli/net/DeviceInterfaceAddCommand.java
new file mode 100644
index 0000000..9ceeb65
--- /dev/null
+++ b/cli/src/main/java/org/onosproject/cli/net/DeviceInterfaceAddCommand.java
@@ -0,0 +1,91 @@
+/*
+ * Copyright 2016 Open Networking Laboratory
+ *
+ * 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.cli.net;
+
+import org.apache.karaf.shell.commands.Argument;
+import org.apache.karaf.shell.commands.Command;
+import org.apache.karaf.shell.commands.Option;
+import org.onlab.packet.VlanId;
+import org.onosproject.cli.AbstractShellCommand;
+import org.onosproject.net.DeviceId;
+import org.onosproject.net.behaviour.InterfaceConfig;
+import org.onosproject.net.driver.DriverHandler;
+import org.onosproject.net.driver.DriverService;
+
+/**
+ * Configures a device interface.
+ */
+@Command(scope = "onos", name = "device-add-interface",
+         description = "Configures a device interface")
+public class DeviceInterfaceAddCommand extends AbstractShellCommand {
+
+    private static final String CONFIG_VLAN_SUCCESS =
+            "VLAN %s set on device %s interface %s.";
+    private static final String CONFIG_VLAN_FAILURE =
+            "Failed to set VLAN %s on device %s interface %s.";
+
+    private static final String CONFIG_TRUNK_SUCCESS =
+            "Trunk mode set for VLAN %s on device %s interface %s.";
+    private static final String CONFIG_TRUNK_FAILURE =
+            "Failed to set trunk mode for VLAN %s on device %s interface %s.";
+
+    @Argument(index = 0, name = "uri", description = "Device ID",
+            required = true, multiValued = false)
+    private String uri = null;
+
+    @Argument(index = 1, name = "interface",
+              description = "Interface name",
+              required = true, multiValued = false)
+    private String portName = null;
+
+    @Argument(index = 2, name = "vlan",
+            description = "VLAN ID",
+            required = true, multiValued = false)
+    private String vlanString = null;
+
+    @Option(name = "-t", aliases = "--trunk",
+            description = "Configure interface as trunk for VLAN",
+            required = false, multiValued = false)
+    private boolean trunkMode = false;
+
+    @Override
+    protected void execute() {
+        DriverService service = get(DriverService.class);
+        DeviceId deviceId = DeviceId.deviceId(uri);
+        DriverHandler h = service.createHandler(deviceId);
+        InterfaceConfig interfaceConfig = h.behaviour(InterfaceConfig.class);
+
+        VlanId vlanId = VlanId.vlanId(Short.parseShort(vlanString));
+
+        if (trunkMode) {
+            // Trunk mode to be enabled for VLAN.
+            if (interfaceConfig.addTrunkInterface(deviceId, portName, vlanId)) {
+                print(CONFIG_TRUNK_SUCCESS, vlanId, deviceId, portName);
+            } else {
+                print(CONFIG_TRUNK_FAILURE, vlanId, deviceId, portName);
+            }
+            return;
+        }
+
+        // VLAN to be added to interface.
+        if (interfaceConfig.addInterfaceToVlan(deviceId, portName, vlanId)) {
+            print(CONFIG_VLAN_SUCCESS, vlanId, deviceId, portName);
+        } else {
+            print(CONFIG_VLAN_FAILURE, vlanId, deviceId, portName);
+        }
+    }
+
+}
diff --git a/cli/src/main/resources/OSGI-INF/blueprint/shell-config.xml b/cli/src/main/resources/OSGI-INF/blueprint/shell-config.xml
index 1a47a0a..a04af2e 100644
--- a/cli/src/main/resources/OSGI-INF/blueprint/shell-config.xml
+++ b/cli/src/main/resources/OSGI-INF/blueprint/shell-config.xml
@@ -155,6 +155,12 @@
             </completers>
         </command>
         <command>
+            <action class="org.onosproject.cli.net.DeviceInterfaceAddCommand"/>
+            <completers>
+                <ref component-id="deviceIdCompleter"/>
+            </completers>
+        </command>
+        <command>
             <action class="org.onosproject.cli.net.AddMeter"/>
             <completers>
                 <ref component-id="deviceIdCompleter"/>
diff --git a/core/api/src/main/java/org/onosproject/net/behaviour/InterfaceConfig.java b/core/api/src/main/java/org/onosproject/net/behaviour/InterfaceConfig.java
new file mode 100644
index 0000000..4079350
--- /dev/null
+++ b/core/api/src/main/java/org/onosproject/net/behaviour/InterfaceConfig.java
@@ -0,0 +1,49 @@
+/*
+ * Copyright 2016 Open Networking Laboratory
+ *
+ * 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.net.behaviour;
+
+import org.onlab.packet.VlanId;
+import org.onosproject.net.DeviceId;
+import org.onosproject.net.driver.HandlerBehaviour;
+
+/**
+ * Means to configure interfaces on devices.
+ */
+public interface InterfaceConfig extends HandlerBehaviour {
+
+    /**
+     * Adds an interface to a VLAN.
+     * @param deviceId the device ID
+     * @param intf the name of the interface
+     * @param vlanId the VLAN ID
+     * @return the result of operation
+     */
+    boolean addInterfaceToVlan(DeviceId deviceId, String intf, VlanId vlanId);
+
+    /**
+     *  Configures an interface as trunk for VLAN.
+     * @param deviceId the device ID
+     * @param intf the name of the interface
+     * @param vlanId the VLAN ID
+     * @return the result of operation
+     */
+    boolean addTrunkInterface(DeviceId deviceId, String intf, VlanId vlanId);
+
+    /* TODO Addition of more methods to make the behavior symmetrical.
+    Methods removeVlanFromInterface, getInterfacesForVlan, getVlansForInterface
+    should be added to complete the behavior.
+     */
+}
diff --git a/drivers/cisco/features.xml b/drivers/cisco/features.xml
new file mode 100644
index 0000000..6fa9fe2
--- /dev/null
+++ b/drivers/cisco/features.xml
@@ -0,0 +1,26 @@
+<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
+<!--
+  ~ Copyright 2016 Open Networking Laboratory
+  ~
+  ~ 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.
+  -->
+<features xmlns="http://karaf.apache.org/xmlns/features/v1.2.0" name="${project.artifactId}-${project.version}">
+    <feature name="${project.artifactId}" version="${project.version}"
+             description="${project.description}">
+        <feature>onos-api</feature>
+        <bundle>mvn:${project.groupId}/${project.artifactId}/${project.version}</bundle>
+
+        <bundle>mvn:${project.groupId}/onos-drivers-utilities/${project.version}</bundle>
+        <bundle>mvn:${project.groupId}/onos-netconf-api/${project.version}</bundle>
+    </feature>
+</features>
diff --git a/drivers/cisco/pom.xml b/drivers/cisco/pom.xml
new file mode 100644
index 0000000..de8a80d
--- /dev/null
+++ b/drivers/cisco/pom.xml
@@ -0,0 +1,50 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+  ~ Copyright 2016 Open Networking Laboratory
+  ~
+  ~ 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.
+  -->
+
+<project xmlns="http://maven.apache.org/POM/4.0.0"
+         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+    <parent>
+        <artifactId>onos-drivers-general</artifactId>
+        <groupId>org.onosproject</groupId>
+        <version>1.5.0-SNAPSHOT</version>
+    </parent>
+    <modelVersion>4.0.0</modelVersion>
+
+    <artifactId>onos-drivers-cisco</artifactId>
+    <packaging>bundle</packaging>
+
+    <description>Cisco device drivers</description>
+
+    <properties>
+        <onos.app.name>org.onosproject.drivers.cisco</onos.app.name>
+    </properties>
+
+    <dependencies>
+        <dependency>
+            <groupId>org.onosproject</groupId>
+            <artifactId>onos-drivers-utilities</artifactId>
+            <version>${project.version}</version>
+        </dependency>
+        <dependency>
+            <groupId>org.onosproject</groupId>
+            <artifactId>onos-netconf-api</artifactId>
+            <version>${project.version}</version>
+        </dependency>
+    </dependencies>
+
+</project>
\ No newline at end of file
diff --git a/drivers/cisco/src/main/java/org/onosproject/drivers/cisco/CiscoDriversLoader.java b/drivers/cisco/src/main/java/org/onosproject/drivers/cisco/CiscoDriversLoader.java
new file mode 100644
index 0000000..1e1d19d
--- /dev/null
+++ b/drivers/cisco/src/main/java/org/onosproject/drivers/cisco/CiscoDriversLoader.java
@@ -0,0 +1,30 @@
+/*
+ * Copyright 2016 Open Networking Laboratory
+ *
+ * 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.cisco;
+
+import org.apache.felix.scr.annotations.Component;
+import org.onosproject.net.driver.AbstractDriverLoader;
+
+/**
+ * Loader for Cisco device drivers.
+ */
+@Component(immediate = true)
+public class CiscoDriversLoader extends AbstractDriverLoader {
+    public CiscoDriversLoader() {
+        super("/cisco-drivers.xml");
+    }
+}
diff --git a/drivers/cisco/src/main/java/org/onosproject/drivers/cisco/InterfaceConfigCiscoSmXImpl.java b/drivers/cisco/src/main/java/org/onosproject/drivers/cisco/InterfaceConfigCiscoSmXImpl.java
new file mode 100644
index 0000000..6df1076
--- /dev/null
+++ b/drivers/cisco/src/main/java/org/onosproject/drivers/cisco/InterfaceConfigCiscoSmXImpl.java
@@ -0,0 +1,171 @@
+/*
+ *
+ *  * Copyright 2016 Open Networking Laboratory
+ *  *
+ *  * 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.cisco;
+
+import org.onlab.packet.VlanId;
+import org.onosproject.drivers.utilities.XmlConfigParser;
+import org.onosproject.net.DeviceId;
+import org.onosproject.net.behaviour.InterfaceConfig;
+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 java.io.ByteArrayInputStream;
+import java.nio.charset.StandardCharsets;
+
+import static com.google.common.base.Preconditions.checkNotNull;
+import static org.slf4j.LoggerFactory.getLogger;
+
+/**
+ * Configures interfaces on Cisco SM-X devices.
+ */
+public class InterfaceConfigCiscoSmXImpl extends AbstractHandlerBehaviour
+        implements InterfaceConfig {
+
+    private final Logger log = getLogger(getClass());
+
+    /**
+     * Adds an interface to a VLAN.
+     * @param deviceId the device ID
+     * @param intf the name of the interface
+     * @param vlanId the VLAN ID
+     * @return the result of operation
+     */
+    @Override
+    public boolean addInterfaceToVlan(DeviceId deviceId, String intf, VlanId vlanId) {
+        NetconfController controller = checkNotNull(handler()
+                                       .get(NetconfController.class));
+
+        NetconfSession session = controller.getDevicesMap().get(handler()
+                                 .data().deviceId()).getSession();
+        String reply;
+        try {
+            reply = session.requestSync(addInterfaceToVlanBuilder(intf, vlanId)).trim();
+        } catch (NetconfException e) {
+            log.error("Failed to configure VLAN ID {} on device {} port {}.",
+                      vlanId, deviceId, intf, e);
+            return false;
+        }
+
+        return XmlConfigParser.configSuccess(XmlConfigParser.loadXml(
+                new ByteArrayInputStream(reply.getBytes(StandardCharsets.UTF_8))));
+    }
+
+    /**
+     * Builds a request crafted to add an interface to a VLAN.
+     * @param intf the name of the interface
+     * @param vlanId the VLAN ID
+     * @return the request string.
+     */
+    private String addInterfaceToVlanBuilder(String intf, VlanId vlanId) {
+        StringBuilder rpc = new StringBuilder("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
+        rpc.append("<rpc xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\" ");
+        rpc.append("xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">");
+        rpc.append("<edit-config>");
+        rpc.append("<target>");
+        rpc.append("<running/>");
+        rpc.append("</target>");
+        rpc.append("<config>");
+        rpc.append("<xml-config-data>");
+        rpc.append("<Device-Configuration><interface><Param>");
+        rpc.append(intf);
+        rpc.append("</Param>");
+        rpc.append("<ConfigIf-Configuration>");
+        rpc.append("<switchport><access><vlan><VLANIDVLANPortAccessMode>");
+        rpc.append(vlanId);
+        rpc.append("</VLANIDVLANPortAccessMode></vlan></access></switchport>");
+        rpc.append("<switchport><mode><access/></mode></switchport>");
+        rpc.append("</ConfigIf-Configuration>");
+        rpc.append("</interface>");
+        rpc.append("</Device-Configuration>");
+        rpc.append("</xml-config-data>");
+        rpc.append("</config>");
+        rpc.append("</edit-config>");
+        rpc.append("</rpc>");
+
+        return rpc.toString();
+    }
+
+    /**
+     * Configures an interface as trunk for VLAN.
+     * @param deviceId the device ID
+     * @param intf the name of the interface
+     * @param vlanId the VLAN ID
+     * @return the result of operation
+     */
+    @Override
+    public boolean addTrunkInterface(DeviceId deviceId, String intf, VlanId vlanId) {
+        NetconfController controller = checkNotNull(handler()
+                                       .get(NetconfController.class));
+
+        NetconfSession session = controller.getDevicesMap().get(handler()
+                                 .data().deviceId()).getSession();
+        String reply;
+        try {
+            reply = session.requestSync(addTrunkInterface(intf, vlanId)).trim();
+        } catch (NetconfException e) {
+            log.error("Failed to configure VLAN ID {} on device {} port {}.",
+                      vlanId, deviceId, intf, e);
+            return false;
+        }
+
+        return XmlConfigParser.configSuccess(XmlConfigParser.loadXml(
+                new ByteArrayInputStream(reply.getBytes(StandardCharsets.UTF_8))));
+    }
+
+    /**
+     * Builds a request crafted to configure an interface as trunk for VLAN.
+     * @param intf the name of the interface
+     * @param vlanId the VLAN ID
+     * @return the request string.
+     */
+    private String addTrunkInterface(String intf, VlanId vlanId) {
+        StringBuilder rpc = new StringBuilder("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
+        rpc.append("<rpc xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\" ");
+        rpc.append("xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">");
+        rpc.append("<edit-config>");
+        rpc.append("<target>");
+        rpc.append("<running/>");
+        rpc.append("</target>");
+        rpc.append("<config>");
+        rpc.append("<xml-config-data>");
+        rpc.append("<Device-Configuration><interface><Param>");
+        rpc.append(intf);
+        rpc.append("</Param>");
+        rpc.append("<ConfigIf-Configuration>");
+        rpc.append("<switchport><trunk><encapsulation><dot1q/></encapsulation></trunk></switchport>");
+        rpc.append("<switchport><trunk><allowed><vlan><VLANIDsAllowedVLANsPortTrunkingMode>");
+        rpc.append(vlanId);
+        rpc.append("</VLANIDsAllowedVLANsPortTrunkingMode></vlan></allowed></trunk></switchport>");
+        rpc.append("<switchport><mode><trunk/></mode></switchport>");
+        rpc.append("</ConfigIf-Configuration>");
+        rpc.append("</interface>");
+        rpc.append("</Device-Configuration>");
+        rpc.append("</xml-config-data>");
+        rpc.append("</config>");
+        rpc.append("</edit-config>");
+        rpc.append("</rpc>");
+
+        return rpc.toString();
+    }
+
+}
+
diff --git a/drivers/cisco/src/main/java/org/onosproject/drivers/cisco/package-info.java b/drivers/cisco/src/main/java/org/onosproject/drivers/cisco/package-info.java
new file mode 100644
index 0000000..86ca832
--- /dev/null
+++ b/drivers/cisco/src/main/java/org/onosproject/drivers/cisco/package-info.java
@@ -0,0 +1,20 @@
+/*
+ * Copyright 2016 Open Networking Laboratory
+ *
+ * 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 Cisco device drivers.
+ */
+package org.onosproject.drivers.cisco;
\ No newline at end of file
diff --git a/drivers/cisco/src/main/resources/cisco-drivers.xml b/drivers/cisco/src/main/resources/cisco-drivers.xml
new file mode 100644
index 0000000..2c07021
--- /dev/null
+++ b/drivers/cisco/src/main/resources/cisco-drivers.xml
@@ -0,0 +1,23 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+  ~ Copyright 2016 Open Networking Laboratory
+  ~
+  ~ 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.
+  -->
+<drivers>
+    <driver name="cisco-netconf" extends="netconf" manufacturer="Cisco"
+            hwVersion="SM-X" swVersion="IOS 15">
+        <behaviour api="org.onosproject.net.behaviour.InterfaceConfig"
+                   impl="org.onosproject.drivers.cisco.InterfaceConfigCiscoSmXImpl"/>
+    </driver>
+</drivers>
diff --git a/drivers/pom.xml b/drivers/pom.xml
index 03514ff..290d165 100644
--- a/drivers/pom.xml
+++ b/drivers/pom.xml
@@ -35,6 +35,7 @@
         <module>default</module>
         <module>ciena</module>
         <module>fujitsu</module>
+        <module>cisco</module>
         <module>netconf</module>
         <module>ovsdb</module>
         <module>utilities</module>
diff --git a/drivers/utilities/src/main/java/org/onosproject/drivers/utilities/XmlConfigParser.java b/drivers/utilities/src/main/java/org/onosproject/drivers/utilities/XmlConfigParser.java
index c846fd1..0566668 100644
--- a/drivers/utilities/src/main/java/org/onosproject/drivers/utilities/XmlConfigParser.java
+++ b/drivers/utilities/src/main/java/org/onosproject/drivers/utilities/XmlConfigParser.java
@@ -226,4 +226,18 @@
         return speed / 1000;
     }
     //TODO implement mor methods for parsing configuration when you need them
+
+    /**
+     * Parses a config reply and returns the result.
+     * @param reply a tree-like source
+     * @return the configuration result
+     */
+    public static boolean configSuccess(HierarchicalConfiguration reply) {
+        if (reply != null) {
+            if (reply.containsKey("ok")) {
+                return true;
+            }
+        }
+        return false;
+    }
 }