Refactored tunnel and interface config behavior

- Added tunnel interface configuratoin to interfaceConfig and
  deprecated tunnelConfig
- OVSDB client service provides interface create/remove APIs instead
  of tunnel interface create/remove, and this APIs can be used to
  create/remove various types of interfaces like tunnel, patch, tap and so on
- Use tunnel description when create tunnel interface so that we can extend
  more config options later
- Some cleaup OVSDB client

Change-Id: I4653595504a27b18384a92ebe4b31ce9d99237cd
diff --git a/drivers/cisco/src/main/java/org/onosproject/drivers/cisco/InterfaceConfigCiscoIosImpl.java b/drivers/cisco/src/main/java/org/onosproject/drivers/cisco/InterfaceConfigCiscoIosImpl.java
index f31c8b0..a631e46 100644
--- a/drivers/cisco/src/main/java/org/onosproject/drivers/cisco/InterfaceConfigCiscoIosImpl.java
+++ b/drivers/cisco/src/main/java/org/onosproject/drivers/cisco/InterfaceConfigCiscoIosImpl.java
@@ -22,6 +22,7 @@
 import org.onosproject.drivers.utilities.XmlConfigParser;
 import org.onosproject.net.DeviceId;
 import org.onosproject.net.behaviour.InterfaceConfig;
+import org.onosproject.net.behaviour.TunnelDescription;
 import org.onosproject.net.device.DeviceInterfaceDescription;
 import org.onosproject.net.driver.AbstractHandlerBehaviour;
 import org.onosproject.netconf.NetconfController;
@@ -480,5 +481,14 @@
         return rpc.toString();
     }
 
+    @Override
+    public boolean addTunnelMode(String ifaceName, TunnelDescription tunnelDesc) {
+        throw new UnsupportedOperationException("Add tunnel mode is not supported");
+    }
+
+    @Override
+    public boolean removeTunnelMode(String ifaceName) {
+        throw new UnsupportedOperationException("Remove tunnel mode is not supported");
+    }
 }
 
diff --git a/drivers/ovsdb/src/main/java/org/onosproject/drivers/ovsdb/OvsdbInterfaceConfig.java b/drivers/ovsdb/src/main/java/org/onosproject/drivers/ovsdb/OvsdbInterfaceConfig.java
new file mode 100644
index 0000000..e35599c
--- /dev/null
+++ b/drivers/ovsdb/src/main/java/org/onosproject/drivers/ovsdb/OvsdbInterfaceConfig.java
@@ -0,0 +1,147 @@
+/*
+ * Copyright 2016-present 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.ovsdb;
+
+import org.onlab.packet.IpAddress;
+import org.onlab.packet.VlanId;
+import org.onosproject.net.DeviceId;
+import org.onosproject.net.behaviour.InterfaceConfig;
+import org.onosproject.net.behaviour.TunnelDescription;
+import org.onosproject.net.device.DeviceInterfaceDescription;
+import org.onosproject.net.driver.AbstractHandlerBehaviour;
+import org.onosproject.net.driver.DriverHandler;
+import org.onosproject.ovsdb.controller.OvsdbClientService;
+import org.onosproject.ovsdb.controller.OvsdbController;
+import org.onosproject.ovsdb.controller.OvsdbInterface;
+import org.onosproject.ovsdb.controller.OvsdbNodeId;
+import org.slf4j.Logger;
+
+import java.util.List;
+
+import static org.slf4j.LoggerFactory.getLogger;
+
+/**
+ * OVSDB-based implementation of interface config behaviour.
+ */
+public class OvsdbInterfaceConfig extends AbstractHandlerBehaviour implements InterfaceConfig {
+
+    private final Logger log = getLogger(getClass());
+
+    @Override
+    public boolean addTunnelMode(String ifaceName, TunnelDescription tunnelDesc) {
+        OvsdbInterface ovsdbIface = OvsdbInterface.builder(tunnelDesc).build();
+        OvsdbClientService ovsdbClient = getOvsdbClient(handler());
+
+        if (!tunnelDesc.deviceId().isPresent()) {
+            log.warn("Device ID is required {}", tunnelDesc);
+            return false;
+        }
+        return ovsdbClient.createInterface(tunnelDesc.deviceId().get(), ovsdbIface);
+    }
+
+    @Override
+    public boolean removeTunnelMode(String ifaceName) {
+        OvsdbClientService ovsdbClient = getOvsdbClient(handler());
+        return ovsdbClient.dropInterface(ifaceName);
+    }
+
+    @Override
+    public boolean addAccessMode(String ifaceName, VlanId vlanId) {
+        // TODO implement
+        throw new UnsupportedOperationException("Not implemented yet");
+    }
+
+    @Override
+    public boolean removeAccessMode(String ifaceName) {
+        // TODO implement
+        throw new UnsupportedOperationException("Not implemented yet");
+    }
+
+    @Override
+    public boolean addTrunkMode(String ifaceName, List<VlanId> vlanIds) {
+        // TODO implement
+        throw new UnsupportedOperationException("Not implemented yet");
+    }
+
+    @Override
+    public boolean removeTrunkMode(String ifaceName) {
+        // TODO implement
+        throw new UnsupportedOperationException("Not implemented yet");
+    }
+
+    @Override
+    public boolean addRateLimit(String ifaceName, short limit) {
+        // TODO implement
+        throw new UnsupportedOperationException("Not implemented yet");
+    }
+
+    @Override
+    public boolean removeRateLimit(String ifaceName) {
+        // TODO implement
+        throw new UnsupportedOperationException("Not implemented yet");
+    }
+
+    @Override
+    public List<DeviceInterfaceDescription> getInterfaces() {
+        // TODO implement
+        throw new UnsupportedOperationException("Not implemented yet");
+    }
+
+    // deprecated interfaces
+    @Override
+    public boolean addAccessInterface(DeviceId deviceId, String ifaceName, VlanId vlanId) {
+        throw new UnsupportedOperationException("Not implemented yet");
+    }
+
+    @Override
+    public boolean removeAccessInterface(DeviceId deviceId, String ifaceName) {
+        throw new UnsupportedOperationException("Not implemented yet");
+    }
+
+    @Override
+    public boolean addTrunkInterface(DeviceId deviceId, String ifaceName, List<VlanId> vlanIds) {
+        throw new UnsupportedOperationException("Not implemented yet");
+    }
+
+    @Override
+    public List<DeviceInterfaceDescription> getInterfaces(DeviceId deviceId) {
+        return null;
+    }
+
+    @Override
+    public boolean removeTrunkInterface(DeviceId deviceId, String ifaceName) {
+        throw new UnsupportedOperationException("Not implemented yet");
+    }
+
+    // OvsdbNodeId(IP) is used in the adaptor while DeviceId(ovsdb:IP)
+    // is used in the core. So DeviceId need be changed to OvsdbNodeId.
+    private OvsdbNodeId changeDeviceIdToNodeId(DeviceId deviceId) {
+        String[] splits = deviceId.toString().split(":");
+        if (splits.length < 1) {
+            return null;
+        }
+        IpAddress ipAddress = IpAddress.valueOf(splits[1]);
+        return new OvsdbNodeId(ipAddress, 0);
+    }
+
+    private OvsdbClientService getOvsdbClient(DriverHandler handler) {
+        OvsdbController ovsController = handler.get(OvsdbController.class);
+        OvsdbNodeId nodeId = changeDeviceIdToNodeId(handler.data().deviceId());
+
+        return ovsController.getOvsdbClient(nodeId);
+    }
+}
diff --git a/drivers/ovsdb/src/main/java/org/onosproject/drivers/ovsdb/OvsdbTunnelConfig.java b/drivers/ovsdb/src/main/java/org/onosproject/drivers/ovsdb/OvsdbTunnelConfig.java
index 03ce0b5..783f511 100644
--- a/drivers/ovsdb/src/main/java/org/onosproject/drivers/ovsdb/OvsdbTunnelConfig.java
+++ b/drivers/ovsdb/src/main/java/org/onosproject/drivers/ovsdb/OvsdbTunnelConfig.java
@@ -17,68 +17,43 @@
 package org.onosproject.drivers.ovsdb;
 
 import org.onlab.packet.IpAddress;
-import org.onosproject.net.DefaultAnnotations;
 import org.onosproject.net.DeviceId;
 import org.onosproject.net.behaviour.BridgeName;
-import org.onosproject.net.behaviour.DefaultTunnelDescription;
-import org.onosproject.net.behaviour.IpTunnelEndPoint;
 import org.onosproject.net.behaviour.TunnelConfig;
 import org.onosproject.net.behaviour.TunnelDescription;
-import org.onosproject.net.behaviour.TunnelName;
 import org.onosproject.net.driver.AbstractHandlerBehaviour;
 import org.onosproject.net.driver.DriverHandler;
 import org.onosproject.ovsdb.controller.OvsdbClientService;
 import org.onosproject.ovsdb.controller.OvsdbController;
+import org.onosproject.ovsdb.controller.OvsdbInterface;
 import org.onosproject.ovsdb.controller.OvsdbNodeId;
-import org.onosproject.ovsdb.controller.OvsdbTunnel;
 
 import java.util.Collection;
-import java.util.Map;
-import java.util.Set;
-import java.util.stream.Collectors;
+import java.util.Collections;
 
 /**
  * OVSDB-based implementation of tunnel config behaviour.
+ *
+ * @deprecated version 1.7.0 - Hummingbird; use interface config instead
  */
+@Deprecated
 public class OvsdbTunnelConfig extends AbstractHandlerBehaviour
         implements TunnelConfig {
 
-    private static final String DEFAULT_ADDRESS = "0.0.0.0";
-    private static final String OPTION_LOCAL_IP = "local_ip";
-    private static final String OPTION_REMOTE_IP = "remote_ip";
-
     @Override
     public boolean createTunnelInterface(BridgeName bridgeName, TunnelDescription tunnel) {
-        Map<String, String> options = ((DefaultAnnotations) tunnel.annotations()).asMap();
-        if (tunnel.src() != null) {
-            options.put(OPTION_LOCAL_IP, ((IpTunnelEndPoint) tunnel.src()).ip().toString());
-        }
-        if (tunnel.dst() != null) {
-            options.put(OPTION_REMOTE_IP, ((IpTunnelEndPoint) tunnel.dst()).ip().toString());
-        }
-
         DriverHandler handler = handler();
-        OvsdbClientService ovsdbClient = getOvsdbNode(handler);
-        return ovsdbClient.createTunnel(bridgeName.name(), tunnel.tunnelName().toString(),
-                                        tunnel.type().toString().toLowerCase(), options);
+        OvsdbClientService ovsdbNode = getOvsdbNode(handler);
+
+        OvsdbInterface ovsdbIface = OvsdbInterface.builder(tunnel).build();
+        return ovsdbNode.createInterface(bridgeName.name(), ovsdbIface);
     }
 
     @Override
     public void removeTunnel(TunnelDescription tunnel) {
         DriverHandler handler = handler();
         OvsdbClientService ovsdbNode = getOvsdbNode(handler);
-        IpTunnelEndPoint ipSrc = IpTunnelEndPoint.ipTunnelPoint(IpAddress
-                .valueOf(DEFAULT_ADDRESS));
-        IpTunnelEndPoint ipDst = IpTunnelEndPoint.ipTunnelPoint(IpAddress
-                .valueOf(DEFAULT_ADDRESS));
-        if (tunnel.src() instanceof IpTunnelEndPoint) {
-            ipSrc = (IpTunnelEndPoint) tunnel.src();
-        }
-        if (tunnel.dst() instanceof IpTunnelEndPoint) {
-            ipDst = (IpTunnelEndPoint) tunnel.dst();
-        }
-      //Even if source point ip or destination point ip equals 0:0:0:0, it is still work-in-progress.
-        ovsdbNode.dropTunnel(ipSrc.ip(), ipDst.ip());
+        ovsdbNode.dropInterface(tunnel.ifaceName());
     }
 
     @Override
@@ -89,20 +64,7 @@
 
     @Override
     public Collection<TunnelDescription> getTunnels() {
-        DriverHandler handler = handler();
-        OvsdbClientService ovsdbNode = getOvsdbNode(handler);
-        Set<OvsdbTunnel> tunnels = ovsdbNode.getTunnels();
-
-        return tunnels.stream()
-                .map(x ->
-                        new DefaultTunnelDescription(
-                                IpTunnelEndPoint.ipTunnelPoint(x.localIp()),
-                                IpTunnelEndPoint.ipTunnelPoint(x.remoteIp()),
-                                TunnelDescription.Type.VXLAN,
-                                TunnelName.tunnelName(x.tunnelName().toString())
-                        )
-                )
-                .collect(Collectors.toSet());
+        return Collections.emptyList();
     }
 
     // OvsdbNodeId(IP) is used in the adaptor while DeviceId(ovsdb:IP)
diff --git a/drivers/ovsdb/src/main/resources/ovsdb-drivers.xml b/drivers/ovsdb/src/main/resources/ovsdb-drivers.xml
index e70925b..9e88ba9 100644
--- a/drivers/ovsdb/src/main/resources/ovsdb-drivers.xml
+++ b/drivers/ovsdb/src/main/resources/ovsdb-drivers.xml
@@ -21,6 +21,8 @@
                    impl="org.onosproject.drivers.ovsdb.OvsdbTunnelConfig"/>
         <behaviour api="org.onosproject.net.behaviour.BridgeConfig"
                    impl="org.onosproject.drivers.ovsdb.OvsdbBridgeConfig"/>
+        <behaviour api="org.onosproject.net.behaviour.InterfaceConfig"
+                   impl="org.onosproject.drivers.ovsdb.OvsdbInterfaceConfig"/>
     </driver>
     <driver name="ovs" extends="default"
             manufacturer="Nicira, Inc\." hwVersion="Open vSwitch" swVersion="2\..*">