[ONOS-4797] Implement the Driver for handling Arista switch

Arista switch with its recent network OS, EOS, supports remote configuration & control. The protocol for this is Arista's proprietary but REST style.
Since ONOS has already REST southbound protocol, the control of Arista switch can be easily done by using this.

- Implemnted driver Loader for Arista switch
- Implemented DeviceDescriptionDiscovery for Arista switch
- [Deleted] Implemented the method sending post to Arista switch and return the result as a String

Change-Id: I9d4b8f0d707361949ff530ff414adb0437ae87d9
diff --git a/BUCK b/BUCK
index d2e4aec..f473d55 100644
--- a/BUCK
+++ b/BUCK
@@ -55,6 +55,7 @@
     '//drivers/netconf:onos-drivers-netconf-oar',
     '//drivers/ovsdb:onos-drivers-ovsdb-oar',
     '//drivers/optical:onos-drivers-optical-oar',
+    '//drivers/arista:onos-drivers-arista-oar',
 
     '//providers/netconf/device:onos-providers-netconf-device',
     '//providers/openflow/device:onos-providers-openflow-device',
diff --git a/drivers/arista/BUCK b/drivers/arista/BUCK
new file mode 100644
index 0000000..2de78bc
--- /dev/null
+++ b/drivers/arista/BUCK
@@ -0,0 +1,28 @@
+COMPILE_DEPS = [
+    '//lib:CORE_DEPS',
+    '//lib:javax.ws.rs-api',
+    '//incubator/api:onos-incubator-api',
+    '//utils/rest:onlab-rest',
+    '//drivers/utilities:onos-drivers-utilities',
+    '//protocols/rest/api:onos-protocols-rest-api',
+]
+
+TEST_DEPS = [
+    '//lib:TEST_ADAPTERS',
+    '//core/api:onos-api-tests',
+]
+
+osgi_jar_with_tests (
+    deps = COMPILE_DEPS,
+    test_deps = TEST_DEPS,
+    resources_root = 'src/main/resources',
+    resources = glob(['src/main/resources/**']),
+)
+
+onos_app (
+    title = 'Arista Device Drivers',
+    category = 'Drivers',
+    url = 'http://onosproject.org',
+    description = 'ONOS Arista Device Drivers application.',
+    required_apps = [ 'org.onosproject.restsb' ],
+)
\ No newline at end of file
diff --git a/drivers/arista/features.xml b/drivers/arista/features.xml
new file mode 100755
index 0000000..74a9e87
--- /dev/null
+++ b/drivers/arista/features.xml
@@ -0,0 +1,24 @@
+<?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>
+    </feature>
+</features>
diff --git a/drivers/arista/pom.xml b/drivers/arista/pom.xml
new file mode 100755
index 0000000..39e039f
--- /dev/null
+++ b/drivers/arista/pom.xml
@@ -0,0 +1,56 @@
+<?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.7.0-SNAPSHOT</version>
+    </parent>
+    <modelVersion>4.0.0</modelVersion>
+
+    <artifactId>onos-drivers-arista</artifactId>
+    <packaging>bundle</packaging>
+
+    <description>Arista device drivers</description>
+
+    <properties>
+        <onos.app.name>org.onosproject.drivers.arista</onos.app.name>
+        <onos.app.title>Arista Device Drivers</onos.app.title>
+        <onos.app.category>Drivers</onos.app.category>
+        <onos.app.url>https://eos.arista.com/arista-eapi-101</onos.app.url>
+        <onos.app.requires>
+            org.onosproject.restsb
+        </onos.app.requires>
+    </properties>
+
+    <dependencies>
+        <dependency>
+            <groupId>org.onosproject</groupId>
+            <artifactId>onos-drivers-utilities</artifactId>
+            <version>${project.version}</version>
+        </dependency>
+        <dependency>
+            <groupId>org.onosproject</groupId>
+            <artifactId>onos-restsb-api</artifactId>
+            <version>${project.version}</version>
+        </dependency>
+    </dependencies>
+
+</project>
\ No newline at end of file
diff --git a/drivers/arista/src/main/java/org/onosproject/drivers/arista/AristaDriversLoader.java b/drivers/arista/src/main/java/org/onosproject/drivers/arista/AristaDriversLoader.java
new file mode 100755
index 0000000..457d159
--- /dev/null
+++ b/drivers/arista/src/main/java/org/onosproject/drivers/arista/AristaDriversLoader.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.arista;
+
+import org.apache.felix.scr.annotations.Component;
+import org.onosproject.net.driver.AbstractDriverLoader;
+
+/**
+ * Loader for Arista device drivers.
+ */
+@Component(immediate = true)
+public class AristaDriversLoader extends AbstractDriverLoader {
+    public AristaDriversLoader() {
+        super("/arista-drivers.xml");
+    }
+}
diff --git a/drivers/arista/src/main/java/org/onosproject/drivers/arista/DeviceDescriptionDiscoveryAristaImpl.java b/drivers/arista/src/main/java/org/onosproject/drivers/arista/DeviceDescriptionDiscoveryAristaImpl.java
new file mode 100644
index 0000000..d82d39b
--- /dev/null
+++ b/drivers/arista/src/main/java/org/onosproject/drivers/arista/DeviceDescriptionDiscoveryAristaImpl.java
@@ -0,0 +1,153 @@
+/*
+ * 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.arista;
+
+import java.io.ByteArrayInputStream;
+import java.io.IOException;
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.fasterxml.jackson.databind.node.ArrayNode;
+import com.fasterxml.jackson.databind.node.ObjectNode;
+import com.google.common.collect.Lists;
+import org.onosproject.net.AnnotationKeys;
+import org.onosproject.net.DefaultAnnotations;
+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.DefaultPortDescription;
+import org.onosproject.net.device.DeviceDescription;
+import org.onosproject.net.device.DeviceDescriptionDiscovery;
+import org.onosproject.net.device.PortDescription;
+import org.onosproject.net.driver.AbstractHandlerBehaviour;
+import org.onosproject.net.driver.DriverHandler;
+import org.onosproject.protocol.rest.RestSBController;
+import javax.ws.rs.core.MediaType;
+import org.slf4j.Logger;
+
+import java.util.List;
+
+import static com.google.common.base.Preconditions.checkNotNull;
+import static org.slf4j.LoggerFactory.getLogger;
+
+/**
+ * Discovers the ports from Arista EOS device.
+ */
+public class DeviceDescriptionDiscoveryAristaImpl extends AbstractHandlerBehaviour
+        implements DeviceDescriptionDiscovery {
+
+    private static final String JSON = "json";
+    private static final String RESULT = "result";
+    private static final String INTERFACE_STATUSES = "interfaceStatuses";
+    private static final String LINK_STATUS = "linkStatus";
+    private static final String LINE_PROTOCOL_STATUS = "lineProtocolStatus";
+    private static final String BANDWIDTH = "bandwidth";
+    private static final String ETHERNET = "Ethernet";
+    private static final String MANAGEMENT = "Management";
+    private static final String INTERFACE_TYPE = "interfaceType";
+    private static final int WEIGHTING_FACTOR_MANAGEMENT_INTERFACE = 100;
+    private static final String JSONRPC = "jsonrpc";
+    private static final String METHOD = "method";
+    private static final String RUN_CMDS = "runCmds";
+    private static final String VERSION = "version";
+    private static final String ID = "id";
+    private static final String GET_PORT = "GetPort";
+    private static final String PARAMS = "params";
+    private static final String FORMAT = "format";
+    private static final String TIMESTAMPS = "timestamps";
+    private static final String CMDS = "cmds";
+    private static final String SHOW_INTERFACES_STATUS = "show interfaces status";
+    private static final String TWO_POINT_ZERO = "2.0";
+    private static final long MBPS = 1000000;
+
+    private final Logger log = getLogger(getClass());
+
+    private static final String API_ENDPOINT = "/command-api/";
+
+    @Override
+    public DeviceDescription discoverDeviceDetails() {
+        log.info("No description to be added for device");
+        //TODO to be implemented if needed.
+        return null;
+    }
+
+    @Override
+    public List<PortDescription> discoverPortDetails() {
+        List<PortDescription> ports = Lists.newArrayList();
+        DriverHandler handler = handler();
+        RestSBController controller = checkNotNull(handler.get(RestSBController.class));
+        DeviceId deviceId = handler.data().deviceId();
+
+        ObjectMapper mapper = new ObjectMapper();
+
+        ObjectNode sendObjNode = mapper.createObjectNode();
+
+        sendObjNode.put(JSONRPC, TWO_POINT_ZERO)
+                .put(METHOD, RUN_CMDS)
+                .put(ID, GET_PORT)
+                .putObject(PARAMS)
+                .put(FORMAT, JSON)
+                .put(TIMESTAMPS, false)
+                .put(VERSION, 1)
+                .putArray(CMDS).add(SHOW_INTERFACES_STATUS);
+
+        String response = controller.post(deviceId, API_ENDPOINT,
+                new ByteArrayInputStream(sendObjNode.toString().getBytes()),
+                MediaType.APPLICATION_JSON, String.class);
+
+        try {
+            ObjectNode node = (ObjectNode) mapper.readTree(response);
+            ArrayNode arrayNode = (ArrayNode) node.get(RESULT);
+
+            JsonNode jsonNode = arrayNode.iterator().next().get(INTERFACE_STATUSES);
+
+            jsonNode.fieldNames().forEachRemaining(name -> {
+                JsonNode interfaceNode = jsonNode.get(name);
+
+                Long bandwidth = interfaceNode.path(BANDWIDTH).asLong() / MBPS;
+
+                SparseAnnotations annotations = DefaultAnnotations.builder()
+                        .set(AnnotationKeys.BANDWIDTH, bandwidth.toString())
+                        .set(AnnotationKeys.NAME, name)
+                        .set(LINK_STATUS, interfaceNode.path(LINK_STATUS).asText())
+                        .set(LINE_PROTOCOL_STATUS, interfaceNode.path(LINE_PROTOCOL_STATUS).asText())
+                        .set(INTERFACE_TYPE, interfaceNode.path(INTERFACE_TYPE).asText())
+                        .build();
+
+                PortDescription portDescription = new DefaultPortDescription(PortNumber
+                        .portNumber(getPortNumber(name)),
+                        true, Port.Type.FIBER, bandwidth, annotations);
+                ports.add(portDescription);
+
+            });
+
+        } catch (IOException e) {
+            log.warn("IO exception occured because of ", e);
+        }
+        return ports;
+    }
+
+    private int getPortNumber(String interfaceName) {
+        if (interfaceName.startsWith(ETHERNET)) {
+            return Integer.valueOf(interfaceName.substring(ETHERNET.length()));
+        } else {
+            return Integer.valueOf(interfaceName.substring(MANAGEMENT.length())).intValue()
+                    + WEIGHTING_FACTOR_MANAGEMENT_INTERFACE;
+        }
+    }
+}
+
diff --git a/drivers/arista/src/main/java/org/onosproject/drivers/arista/package-info.java b/drivers/arista/src/main/java/org/onosproject/drivers/arista/package-info.java
new file mode 100644
index 0000000..16eaa62
--- /dev/null
+++ b/drivers/arista/src/main/java/org/onosproject/drivers/arista/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 Arista EOS device drivers.
+ */
+package org.onosproject.drivers.arista;
\ No newline at end of file
diff --git a/drivers/arista/src/main/resources/arista-drivers.xml b/drivers/arista/src/main/resources/arista-drivers.xml
new file mode 100755
index 0000000..c007040
--- /dev/null
+++ b/drivers/arista/src/main/resources/arista-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="restArista" manufacturer="Arista Networks" hwVersion="1.0.0" swVersion="1.0.0">
+        <behaviour api="org.onosproject.net.device.DeviceDescriptionDiscovery"
+                   impl="org.onosproject.drivers.arista.DeviceDescriptionDiscoveryAristaImpl"/>
+    </driver>
+</drivers>
+
diff --git a/drivers/pom.xml b/drivers/pom.xml
index a70b1ff..3faccf9 100644
--- a/drivers/pom.xml
+++ b/drivers/pom.xml
@@ -43,6 +43,7 @@
         <module>bmv2</module>
         <module>corsa</module>
         <module>optical</module>
+        <module>arista</module>
     </modules>
 
     <!--<properties>