Add bridge Config at Arista Driver

Change-Id: Iaea2a0641c8d05bfa1d2caca66dc81698650d995
diff --git a/core/api/src/main/java/org/onosproject/net/behaviour/BridgeConfig.java b/core/api/src/main/java/org/onosproject/net/behaviour/BridgeConfig.java
index fe223d0..e9ee007 100644
--- a/core/api/src/main/java/org/onosproject/net/behaviour/BridgeConfig.java
+++ b/core/api/src/main/java/org/onosproject/net/behaviour/BridgeConfig.java
@@ -67,6 +67,18 @@
     void deletePort(BridgeName bridgeName, String portName);
 
     /**
+     * Deletes ports to a given bridge.
+     * @param bridgeName bridge name
+     * @param portNames list port names
+     */
+    default void deletePorts(BridgeName bridgeName, List<String> portNames) {
+        for (String portName : portNames) {
+            deletePort(bridgeName, portName);
+        }
+    }
+
+
+    /**
      * Delete a logical/virtual port.
      *
      * @return collection of port
diff --git a/drivers/arista/src/main/java/org/onosproject/drivers/arista/AristaUtils.java b/drivers/arista/src/main/java/org/onosproject/drivers/arista/AristaUtils.java
index 2a7bef7..0e4b3f9 100644
--- a/drivers/arista/src/main/java/org/onosproject/drivers/arista/AristaUtils.java
+++ b/drivers/arista/src/main/java/org/onosproject/drivers/arista/AristaUtils.java
@@ -29,6 +29,7 @@
 import javax.ws.rs.core.MediaType;
 import java.io.ByteArrayInputStream;
 import java.io.IOException;
+import java.util.ArrayList;
 import java.util.List;
 import java.util.Optional;
 
@@ -129,4 +130,39 @@
         return node.toString();
     }
 
+
+    public static boolean getWithChecking(DriverHandler handler, String command) {
+        List<String> cmds = new ArrayList<>();
+
+        cmds.add(command);
+
+        return getWithChecking(handler, cmds);
+    }
+
+    public static boolean getWithChecking(DriverHandler handler, List<String> commands) {
+        RestSBController controller = checkNotNull(handler.get(RestSBController.class));
+        DeviceId deviceId = checkNotNull(handler.data()).deviceId();
+        String response = generate(commands);
+
+        log.debug("request :{}", response);
+
+        try {
+            ObjectMapper om = new ObjectMapper();
+            JsonNode json = om.readTree(response);
+            JsonNode errNode = json.findPath(ERROR);
+
+            if (errNode.isMissingNode()) {
+                return true;
+            }
+
+            log.error("Error get with checking {}", errNode.asText(""));
+            for (String str : commands) {
+                log.error("Command Failed due to Cmd : {}", str);
+            }
+            return false;
+        } catch (IOException e) {
+            log.error("IO exception occured because of ", e);
+            return false;
+        }
+    }
 }
\ No newline at end of file
diff --git a/drivers/arista/src/main/java/org/onosproject/drivers/arista/BridgeConfigAristaImpl.java b/drivers/arista/src/main/java/org/onosproject/drivers/arista/BridgeConfigAristaImpl.java
new file mode 100644
index 0000000..ce64459
--- /dev/null
+++ b/drivers/arista/src/main/java/org/onosproject/drivers/arista/BridgeConfigAristaImpl.java
@@ -0,0 +1,116 @@
+/*
+ * Copyright 2019-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.arista;
+
+
+import static org.slf4j.LoggerFactory.getLogger;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.List;
+import java.util.Set;
+
+import org.onosproject.net.PortNumber;
+import org.onosproject.net.behaviour.BridgeConfig;
+import org.onosproject.net.behaviour.BridgeDescription;
+import org.onosproject.net.behaviour.BridgeName;
+import org.onosproject.net.device.PortDescription;
+import org.onosproject.net.driver.AbstractHandlerBehaviour;
+import org.slf4j.Logger;
+
+public class BridgeConfigAristaImpl extends AbstractHandlerBehaviour implements BridgeConfig {
+    private final Logger log = getLogger(getClass());
+
+    private static final String CONFIGURE_TERMINAL = "configure";
+    private static final String OPENFLOW_CMD = "openflow";
+    private static final String BIND_CMD = "bind interface %s";
+    private static final String NO_BIND_CMD = "no bind interface %s";
+    private static final String INTERFACE_CMD = "interface %s";
+    private static final String NO_SPEED = "no speed";
+    private static final String SPEED_40G_FULL_CMD = "speed forced 40gfull";
+
+
+    @Override
+    public boolean addBridge(BridgeDescription bridgeDescription) {
+        log.warn("addBridge is not supported");
+        return false;
+    }
+
+    @Override
+    public void deleteBridge(BridgeName bridgeName) {
+        log.warn("deleteBridge is not supported");
+    }
+
+    @Override
+    public Collection<BridgeDescription> getBridges() {
+        log.warn("deleteBridge is not supported");
+        return null;
+    }
+
+    @Override
+    public void addPort(BridgeName bridgeName, String portName) {
+        List<String> cmds = new ArrayList<>();
+        cmds.add(CONFIGURE_TERMINAL);
+        cmds.add(OPENFLOW_CMD);
+        cmds.add(String.format(BIND_CMD, portName));
+
+        AristaUtils.getWithChecking(handler(), cmds);
+    }
+
+    @Override
+    public void deletePort(BridgeName bridgeName, String portName) {
+        List<String> cmds = new ArrayList<>();
+        cmds.add(CONFIGURE_TERMINAL);
+        cmds.add(OPENFLOW_CMD);
+        cmds.add(String.format(NO_BIND_CMD, portName));
+
+        AristaUtils.getWithChecking(handler(), cmds);
+    }
+
+    @Override
+    public void deletePorts(BridgeName bridgeName, List<String> portNames) {
+        List<String> cmds = new ArrayList<>();
+        cmds.add(CONFIGURE_TERMINAL);
+        cmds.add(OPENFLOW_CMD);
+        for (String portName : portNames) {
+            cmds.add(String.format(NO_BIND_CMD, portName));
+        }
+
+        AristaUtils.getWithChecking(handler(), cmds);
+    }
+
+    @Override
+    public Collection<PortDescription> getPorts() {
+        // TODO need to implement
+        log.warn("not implemented yet");
+        return null;
+    }
+
+    @Override
+    public Set<PortNumber> getPortNumbers() {
+        // TODO need to implement
+        log.warn("not implemented yet");
+        return null;
+    }
+
+    @Override
+    public List<PortNumber> getLocalPorts(Iterable<String> ifaceIds) {
+        // TODO need to implement
+        log.warn("not implemented yet");
+        return null;
+    }
+}
diff --git a/drivers/arista/src/main/resources/arista-drivers.xml b/drivers/arista/src/main/resources/arista-drivers.xml
index 9760dbc..2f29988 100644
--- a/drivers/arista/src/main/resources/arista-drivers.xml
+++ b/drivers/arista/src/main/resources/arista-drivers.xml
@@ -20,6 +20,8 @@
                    impl="org.onosproject.drivers.arista.DeviceDescriptionDiscoveryAristaImpl"/>
         <behaviour api="org.onosproject.net.behaviour.ControllerConfig"
                    impl="org.onosproject.drivers.arista.ControllerConfigAristaImpl"/>
+        <behaviour api="org.onosproject.net.behaviour.BridgeConfig"
+                   impl="org.onosproject.drivers.arista.BridgeConfigAristaImpl"/>
         <behaviour api="org.onosproject.net.behaviour.LinkDiscovery"
                    impl="org.onosproject.drivers.arista.LinkDiscoveryAristaImpl"/>
     </driver>