CORD-247 Add host management network interface to integration bridge

- Node can have management network connectivity by adding "hostManagementIface"
field to the network config "nodes" block, it's optional field
- Added Builder of CordVtnNode
- Improved cordvtn-node-check result
- Some renamings, intBrId -> integrationBridgeId, dpIp -> dataIp,
dpIntf -> dataIface, and dpPort -> dataPort

Change-Id: I7a54edfb273181f0cb35b69fcfc7a448015734bf
diff --git a/src/main/java/org/opencord/cordvtn/impl/CordVtnNodeManager.java b/src/main/java/org/opencord/cordvtn/impl/CordVtnNodeManager.java
index fbac581..df3d336 100644
--- a/src/main/java/org/opencord/cordvtn/impl/CordVtnNodeManager.java
+++ b/src/main/java/org/opencord/cordvtn/impl/CordVtnNodeManager.java
@@ -15,7 +15,7 @@
  */
 package org.opencord.cordvtn.impl;
 
-import com.google.common.collect.Sets;
+import com.google.common.base.Strings;
 import com.jcraft.jsch.Session;
 import org.apache.felix.scr.annotations.Activate;
 import org.apache.felix.scr.annotations.Component;
@@ -24,11 +24,13 @@
 import org.apache.felix.scr.annotations.ReferenceCardinality;
 import org.apache.felix.scr.annotations.Service;
 import org.onlab.packet.IpAddress;
-import org.onlab.util.ItemNotFoundException;
 import org.onlab.util.KryoNamespace;
 import org.onosproject.cluster.ClusterService;
 import org.onosproject.cluster.LeadershipService;
 import org.onosproject.cluster.NodeId;
+import org.onosproject.net.behaviour.BridgeDescription;
+import org.onosproject.net.behaviour.DefaultBridgeDescription;
+import org.onosproject.net.behaviour.InterfaceConfig;
 import org.opencord.cordvtn.api.ConnectionHandler;
 import org.opencord.cordvtn.api.CordVtnConfig;
 import org.opencord.cordvtn.api.CordVtnNode;
@@ -39,19 +41,15 @@
 import org.onosproject.core.ApplicationId;
 import org.onosproject.core.CoreService;
 import org.onosproject.net.ConnectPoint;
-import org.onosproject.net.DefaultAnnotations;
 import org.onosproject.net.Device;
 import org.onosproject.net.DeviceId;
-import org.onosproject.net.Host;
 import org.onosproject.net.Port;
 import org.onosproject.net.PortNumber;
 import org.onosproject.net.behaviour.BridgeConfig;
 import org.onosproject.net.behaviour.BridgeName;
 import org.onosproject.net.behaviour.ControllerInfo;
 import org.onosproject.net.behaviour.DefaultTunnelDescription;
-import org.onosproject.net.behaviour.TunnelConfig;
 import org.onosproject.net.behaviour.TunnelDescription;
-import org.onosproject.net.behaviour.TunnelName;
 import org.onosproject.net.config.NetworkConfigEvent;
 import org.onosproject.net.config.NetworkConfigListener;
 import org.onosproject.net.config.NetworkConfigRegistry;
@@ -73,10 +71,7 @@
 import org.onosproject.store.service.Versioned;
 import org.slf4j.Logger;
 
-import java.util.ArrayList;
-import java.util.HashMap;
 import java.util.List;
-import java.util.Map;
 import java.util.Objects;
 import java.util.Set;
 import java.util.concurrent.ExecutorService;
@@ -85,6 +80,7 @@
 import static com.google.common.base.Preconditions.checkNotNull;
 import static java.util.concurrent.Executors.newSingleThreadScheduledExecutor;
 import static org.onlab.util.Tools.groupedThreads;
+import static org.onosproject.net.AnnotationKeys.PORT_NAME;
 import static org.onosproject.net.Device.Type.SWITCH;
 import static org.onosproject.net.behaviour.TunnelDescription.Type.VXLAN;
 import static org.opencord.cordvtn.api.Constants.*;
@@ -109,14 +105,7 @@
             .register(SshAccessInfo.class)
             .register(NetworkAddress.class);
 
-    private static final Map<String, String> DEFAULT_TUNNEL_OPTIONS = new HashMap<String, String>() {
-        {
-            put("key", "flow");
-            put("remote_ip", "flow");
-        }
-    };
     private static final int DPID_BEGIN = 3;
-    private static final int OFPORT = 6653;
 
     @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
     protected CoreService coreService;
@@ -134,7 +123,7 @@
     protected DeviceAdminService adminService;
 
     @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
-    protected OvsdbController controller;
+    protected OvsdbController ovsdbController;
 
     @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
     protected ClusterService clusterService;
@@ -173,21 +162,27 @@
         INIT {
             @Override
             public void process(CordVtnNodeManager nodeManager, CordVtnNode node) {
+                // make sure there is OVSDB connection
                 if (!nodeManager.isOvsdbConnected(node)) {
                     nodeManager.connectOvsdb(node);
-                } else {
-                    nodeManager.createIntegrationBridge(node);
+                    return;
                 }
+                nodeManager.createIntegrationBridge(node);
             }
         },
         BRIDGE_CREATED {
             @Override
             public void process(CordVtnNodeManager nodeManager, CordVtnNode node) {
+                // make sure there is OVSDB connection
                 if (!nodeManager.isOvsdbConnected(node)) {
                     nodeManager.connectOvsdb(node);
-                } else {
-                    nodeManager.createTunnelInterface(node);
-                    nodeManager.addDataPlaneInterface(node);
+                    return;
+                }
+
+                nodeManager.createTunnelInterface(node);
+                nodeManager.addSystemInterface(node, node.dataIface());
+                if (node.hostMgmtIface().isPresent()) {
+                    nodeManager.addSystemInterface(node, node.hostMgmtIface().get());
                 }
             }
         },
@@ -261,11 +256,10 @@
      */
     public void deleteNode(CordVtnNode node) {
         checkNotNull(node);
-
-        if (isOvsdbConnected(node)) {
-            disconnectOvsdb(node);
+        OvsdbClientService ovsdbClient = getOvsdbClient(node);
+        if (ovsdbClient != null && ovsdbClient.isConnected()) {
+            ovsdbClient.disconnect();
         }
-
         nodeStore.remove(node.hostname());
     }
 
@@ -277,50 +271,7 @@
      */
     public boolean isNodeInitComplete(CordVtnNode node) {
         checkNotNull(node);
-        return nodeStore.containsKey(node.hostname()) && getNodeState(node).equals(NodeState.COMPLETE);
-    }
-
-    /**
-     * Returns detailed node initialization state.
-     *
-     * @param node cordvtn node
-     * @return string including detailed node init state
-     */
-    public String checkNodeInitState(CordVtnNode node) {
-        checkNotNull(node);
-
-        if (!nodeStore.containsKey(node.hostname())) {
-            log.warn("Node {} does not exist, add node first", node.hostname());
-            return null;
-        }
-
-        Session session = connect(node.sshInfo());
-        if (session == null) {
-            log.debug("Failed to SSH to {}", node.hostname());
-            return null;
-        }
-
-        Set<IpAddress> intBrIps = getCurrentIps(session, DEFAULT_BRIDGE);
-        String result = String.format(
-                "Current state : %s%n" +
-                        "br-int created and connected to ONOS : %s (%s)%n" +
-                        "VXLAN interface added to br-int : %s%n" +
-                        "Data plane interface is added to br-int and enabled : %s (%s)%n" +
-                        "IP flushed from data plane interface : %s (%s)%n" +
-                        "Data plane IP added to br-int : %s (%s)%n" +
-                        "Local management IP added to br-int : %s (%s)",
-                node.state(),
-                isBrIntCreated(node) ? MSG_OK : MSG_NO, node.intBrId(),
-                isTunnelIntfCreated(node) ? MSG_OK : MSG_NO,
-                isDataPlaneIntfAdded(node) ? MSG_OK : MSG_NO, node.dpIntf(),
-                isInterfaceUp(session, node.dpIntf()) &&
-                        getCurrentIps(session, node.dpIntf()).isEmpty() ? MSG_OK : MSG_NO, node.dpIntf(),
-                intBrIps.contains(node.dpIp().ip()) ? MSG_OK : MSG_NO, node.dpIp().cidr(),
-                intBrIps.contains(node.localMgmtIp().ip()) ? MSG_OK : MSG_NO, node.localMgmtIp().cidr());
-
-        disconnect(session);
-
-        return result;
+        return getNodeState(node).equals(NodeState.COMPLETE);
     }
 
     /**
@@ -347,7 +298,8 @@
      * @return set of nodes
      */
     public Set<CordVtnNode> completeNodes() {
-        return getNodes().stream().filter(this::isNodeInitComplete).collect(Collectors.toSet());
+        return getNodes().stream().filter(this::isNodeStateComplete)
+                .collect(Collectors.toSet());
     }
 
     /**
@@ -356,14 +308,14 @@
      * @param deviceId integration bridge device id
      * @return port number; null otherwise
      */
-    public PortNumber dpPort(DeviceId deviceId) {
+    public PortNumber dataPort(DeviceId deviceId) {
         CordVtnNode node = nodeByBridgeId(deviceId);
         if (node == null) {
             log.warn("Failed to get node for {}", deviceId);
             return null;
         }
         Port port = deviceService.getPorts(deviceId).stream()
-                .filter(p -> portName(p).contains(node.dpIntf()) &&
+                .filter(p -> portName(p).contains(node.dataIface()) &&
                         p.isEnabled())
                 .findFirst().orElse(null);
 
@@ -376,13 +328,13 @@
      * @param deviceId integration bridge device id
      * @return ip address; null otherwise
      */
-    public IpAddress dpIp(DeviceId deviceId) {
+    public IpAddress dataIp(DeviceId deviceId) {
         CordVtnNode node = nodeByBridgeId(deviceId);
         if (node == null) {
             log.warn("Failed to get node for {}", deviceId);
             return null;
         }
-        return node.dpIp().ip();
+        return node.dataIp().ip();
     }
 
     /**
@@ -438,18 +390,29 @@
      * @param node cordvtn node
      */
     private void postInit(CordVtnNode node) {
-        disconnectOvsdb(node);
-        pipeline.initPipeline(node, dpPort(node.intBrId()), tunnelPort(node.intBrId()));
+        checkNotNull(node);
 
-        deviceService.getPorts(node.intBrId()).stream()
+        // disconnect OVSDB session once the node bootstrap is done
+        OvsdbClientService ovsdbClient = getOvsdbClient(node);
+        if (ovsdbClient != null && ovsdbClient.isConnected()) {
+            ovsdbClient.disconnect();
+        }
+
+        pipeline.initPipeline(node, dataPort(node.integrationBridgeId()),
+                              tunnelPort(node.integrationBridgeId()));
+
+        // adds existing instances to the host list
+        deviceService.getPorts(node.integrationBridgeId()).stream()
                 .filter(port -> portName(port).startsWith(VPORT_PREFIX) &&
                         port.isEnabled())
                 .forEach(port -> instanceService.addInstance(connectPoint(port)));
 
+        // removes stale instances from the host list
         hostService.getHosts().forEach(host -> {
-            if (deviceService.getPort(host.location().deviceId(),
-                                      host.location().port()) == null) {
-                instanceService.removeInstance(connectPoint(host));
+            if (deviceService.getPort(
+                    host.location().deviceId(),
+                    host.location().port()) == null) {
+                instanceService.removeInstance(host.location());
             }
         });
 
@@ -464,7 +427,6 @@
      */
     private void setNodeState(CordVtnNode node, NodeState newState) {
         checkNotNull(node);
-
         log.debug("Changed {} state: {}", node.hostname(), newState);
         nodeStore.put(node.hostname(), CordVtnNode.getUpdatedNode(node, newState));
     }
@@ -477,17 +439,18 @@
      */
     private NodeState getNodeState(CordVtnNode node) {
         checkNotNull(node);
-
-        if (isBrIntCreated(node) && isTunnelIntfCreated(node) &&
-                isDataPlaneIntfAdded(node) && isIpAddressSet(node)) {
-            return NodeState.COMPLETE;
-        } else if (isDataPlaneIntfAdded(node) && isTunnelIntfCreated(node)) {
-            return NodeState.PORTS_ADDED;
-        } else if (isBrIntCreated(node)) {
-            return NodeState.BRIDGE_CREATED;
-        } else {
+        if (!isIntegrationBridgeCreated(node)) {
             return NodeState.INIT;
         }
+        for (String iface : node.systemIfaces()) {
+            if (!isIfaceCreated(node, iface)) {
+                return NodeState.BRIDGE_CREATED;
+            }
+        }
+        if (!isIpAddressSet(node)) {
+            return NodeState.PORTS_ADDED;
+        }
+        return NodeState.COMPLETE;
     }
 
     /**
@@ -498,10 +461,10 @@
      */
     private boolean isOvsdbConnected(CordVtnNode node) {
         checkNotNull(node);
-
         OvsdbClientService ovsdbClient = getOvsdbClient(node);
         return deviceService.isAvailable(node.ovsdbId()) &&
-                ovsdbClient != null && ovsdbClient.isConnected();
+                ovsdbClient != null &&
+                ovsdbClient.isConnected();
     }
 
     /**
@@ -511,150 +474,82 @@
      */
     private void connectOvsdb(CordVtnNode node) {
         checkNotNull(node);
-
-        if (!nodeStore.containsKey(node.hostname())) {
-            log.warn("Node {} does not exist", node.hostname());
-            return;
-        }
-
-        if (!isOvsdbConnected(node)) {
-            controller.connect(node.hostMgmtIp().ip(), node.ovsdbPort());
-        }
-    }
-
-    /**
-     * Disconnects OVSDB server for a given node.
-     *
-     * @param node cordvtn node
-     */
-    private void disconnectOvsdb(CordVtnNode node) {
-        checkNotNull(node);
-
-        if (!nodeStore.containsKey(node.hostname())) {
-            log.warn("Node {} does not exist", node.hostname());
-            return;
-        }
-
-        if (isOvsdbConnected(node)) {
-            OvsdbClientService ovsdbClient = getOvsdbClient(node);
-            ovsdbClient.disconnect();
-        }
+        ovsdbController.connect(node.hostMgmtIp().ip(), node.ovsdbPort());
     }
 
     /**
      * Returns OVSDB client for a given node.
      *
      * @param node cordvtn node
-     * @return OVSDB client, or null if it fails to get OVSDB client
+     * @return ovsdb client, or null if there's no ovsdb connection
      */
     private OvsdbClientService getOvsdbClient(CordVtnNode node) {
         checkNotNull(node);
-
-        OvsdbClientService ovsdbClient = controller.getOvsdbClient(
-                new OvsdbNodeId(node.hostMgmtIp().ip(), node.ovsdbPort().toInt()));
-        if (ovsdbClient == null) {
-            log.trace("Couldn't find OVSDB client for {}", node.hostname());
-        }
-        return ovsdbClient;
+        OvsdbNodeId ovsdb = new OvsdbNodeId(
+                node.hostMgmtIp().ip(), node.ovsdbPort().toInt());
+        return ovsdbController.getOvsdbClient(ovsdb);
     }
 
-    /**
-     * Creates an integration bridge for a given node.
-     *
-     * @param node cordvtn node
-     */
     private void createIntegrationBridge(CordVtnNode node) {
-        if (isBrIntCreated(node)) {
+        Device device = deviceService.getDevice(node.ovsdbId());
+        if (device == null || !device.is(BridgeConfig.class)) {
+            log.error("Failed to create integration bridge on {}", node.ovsdbId());
             return;
         }
 
-        List<ControllerInfo> controllers = new ArrayList<>();
-        Sets.newHashSet(clusterService.getNodes()).stream()
-                .forEach(controller -> {
-                    ControllerInfo ctrlInfo = new ControllerInfo(controller.ip(), OFPORT, "tcp");
-                    controllers.add(ctrlInfo);
-                });
+        List<ControllerInfo> controllers = clusterService.getNodes().stream()
+                .map(controller -> new ControllerInfo(controller.ip(), OF_PORT, "tcp"))
+                .collect(Collectors.toList());
 
-        String dpid = node.intBrId().toString().substring(DPID_BEGIN);
+        String dpid = node.integrationBridgeId().toString().substring(DPID_BEGIN);
+        BridgeDescription bridgeDesc = DefaultBridgeDescription.builder()
+                .name(INTEGRATION_BRIDGE)
+                .failMode(BridgeDescription.FailMode.SECURE)
+                .datapathId(dpid)
+                .disableInBand()
+                .controllers(controllers)
+                .build();
 
-        try {
-            Device device = deviceService.getDevice(node.ovsdbId());
-            if (device.is(BridgeConfig.class)) {
-                BridgeConfig bridgeConfig =  device.as(BridgeConfig.class);
-                bridgeConfig.addBridge(BridgeName.bridgeName(DEFAULT_BRIDGE), dpid, controllers);
-            } else {
-                log.warn("The bridging behaviour is not supported in device {}", device.id());
-            }
-        } catch (ItemNotFoundException e) {
-            log.warn("Failed to create integration bridge on {}", node.hostname());
-        }
+        BridgeConfig bridgeConfig =  device.as(BridgeConfig.class);
+        bridgeConfig.addBridge(bridgeDesc);
     }
 
-    /**
-     * Creates tunnel interface to the integration bridge for a given node.
-     *
-     * @param node cordvtn node
-     */
     private void createTunnelInterface(CordVtnNode node) {
-        if (isTunnelIntfCreated(node)) {
+        Device device = deviceService.getDevice(node.ovsdbId());
+        if (device == null || !device.is(InterfaceConfig.class)) {
+            log.error("Failed to create tunnel interface on {}", node.ovsdbId());
             return;
         }
 
-        DefaultAnnotations.Builder optionBuilder = DefaultAnnotations.builder();
-        for (String key : DEFAULT_TUNNEL_OPTIONS.keySet()) {
-            optionBuilder.set(key, DEFAULT_TUNNEL_OPTIONS.get(key));
-        }
+        TunnelDescription tunnelDesc = DefaultTunnelDescription.builder()
+                .name(DEFAULT_TUNNEL)
+                .type(VXLAN)
+                .enableFlowDst()
+                .enableFlowKey()
+                .build();
 
-        TunnelDescription description = new DefaultTunnelDescription(
-                null, null, VXLAN, TunnelName.tunnelName(DEFAULT_TUNNEL),
-                optionBuilder.build());
-
-        try {
-            Device device = deviceService.getDevice(node.ovsdbId());
-            if (device.is(TunnelConfig.class)) {
-                TunnelConfig tunnelConfig =  device.as(TunnelConfig.class);
-                tunnelConfig.createTunnelInterface(BridgeName.bridgeName(DEFAULT_BRIDGE), description);
-            } else {
-                log.warn("The tunneling behaviour is not supported in device {}", device.id());
-            }
-        } catch (ItemNotFoundException e) {
-            log.warn("Failed to create tunnel interface on {}", node.hostname());
-        }
+        InterfaceConfig ifaceConfig =  device.as(InterfaceConfig.class);
+        ifaceConfig.addTunnelInterface(BridgeName.of(INTEGRATION_BRIDGE), tunnelDesc);
     }
 
-    /**
-     * Adds data plane interface to a given node.
-     *
-     * @param node cordvtn node
-     */
-    private void addDataPlaneInterface(CordVtnNode node) {
-        if (isDataPlaneIntfAdded(node)) {
-            return;
-        }
-
+    private void addSystemInterface(CordVtnNode node, String ifaceName) {
         Session session = connect(node.sshInfo());
-        if (session == null) {
-            log.debug("Failed to SSH to {}", node.hostname());
+        if (session == null || !isInterfaceUp(session, ifaceName)) {
+            log.warn("Interface {} is not available on {}", ifaceName, node.hostname());
+            disconnect(session);
+            return;
+        } else {
+            disconnect(session);
+        }
+
+        Device device = deviceService.getDevice(node.ovsdbId());
+        if (!device.is(BridgeConfig.class)) {
+            log.error("BridgeConfig is not supported for {}", node.ovsdbId());
             return;
         }
 
-        if (!isInterfaceUp(session, node.dpIntf())) {
-            log.warn("Interface {} is not available", node.dpIntf());
-            return;
-        }
-        disconnect(session);
-
-        try {
-            Device device = deviceService.getDevice(node.ovsdbId());
-            if (device.is(BridgeConfig.class)) {
-                BridgeConfig bridgeConfig =  device.as(BridgeConfig.class);
-                bridgeConfig.addPort(BridgeName.bridgeName(DEFAULT_BRIDGE), node.dpIntf());
-            } else {
-                log.warn("The bridging behaviour is not supported in device {}", device.id());
-            }
-        } catch (ItemNotFoundException e) {
-            log.warn("Failed to add {} on {}", node.dpIntf(), node.hostname());
-        }
+        BridgeConfig bridgeConfig = device.as(BridgeConfig.class);
+        bridgeConfig.addPort(BridgeName.of(INTEGRATION_BRIDGE), ifaceName);
     }
 
     /**
@@ -669,62 +564,37 @@
             log.debug("Failed to SSH to {}", node.hostname());
             return;
         }
-
-        getCurrentIps(session, DEFAULT_BRIDGE).stream()
+        getCurrentIps(session, INTEGRATION_BRIDGE).stream()
                 .filter(ip -> !ip.equals(node.localMgmtIp().ip()))
-                .filter(ip -> !ip.equals(node.dpIp().ip()))
-                .forEach(ip -> deleteIp(session, ip, DEFAULT_BRIDGE));
+                .filter(ip -> !ip.equals(node.dataIp().ip()))
+                .forEach(ip -> deleteIp(session, ip, INTEGRATION_BRIDGE));
 
-        boolean result = flushIp(session, node.dpIntf()) &&
-                setInterfaceUp(session, node.dpIntf()) &&
-                addIp(session, node.dpIp(), DEFAULT_BRIDGE) &&
-                addIp(session, node.localMgmtIp(), DEFAULT_BRIDGE) &&
-                setInterfaceUp(session, DEFAULT_BRIDGE);
+        boolean result = flushIp(session, node.dataIface()) &&
+                setInterfaceUp(session, node.dataIface()) &&
+                addIp(session, node.dataIp(), INTEGRATION_BRIDGE) &&
+                addIp(session, node.localMgmtIp(), INTEGRATION_BRIDGE) &&
+                setInterfaceUp(session, INTEGRATION_BRIDGE);
 
         disconnect(session);
-
         if (result) {
             setNodeState(node, NodeState.COMPLETE);
         }
     }
 
-    /**
-     * Checks if integration bridge exists and available.
-     *
-     * @param node cordvtn node
-     * @return true if the bridge is available, false otherwise
-     */
-    private boolean isBrIntCreated(CordVtnNode node) {
-        return (deviceService.getDevice(node.intBrId()) != null
-                && deviceService.isAvailable(node.intBrId()));
+    private boolean isIntegrationBridgeCreated(CordVtnNode node) {
+        return deviceService.getDevice(node.integrationBridgeId()) != null &&
+                deviceService.isAvailable(node.integrationBridgeId());
     }
 
-    /**
-     * Checks if tunnel interface exists.
-     *
-     * @param node cordvtn node
-     * @return true if the interface exists, false otherwise
-     */
-    private boolean isTunnelIntfCreated(CordVtnNode node) {
-        return deviceService.getPorts(node.intBrId())
-                    .stream()
-                    .filter(p -> portName(p).contains(DEFAULT_TUNNEL) &&
-                            p.isEnabled())
-                    .findAny().isPresent();
-    }
-
-    /**
-     * Checks if data plane interface exists.
-     *
-     * @param node cordvtn node
-     * @return true if the interface exists, false otherwise
-     */
-    private boolean isDataPlaneIntfAdded(CordVtnNode node) {
-        return deviceService.getPorts(node.intBrId())
-                    .stream()
-                    .filter(p -> portName(p).contains(node.dpIntf()) &&
-                            p.isEnabled())
-                    .findAny().isPresent();
+    private boolean isIfaceCreated(CordVtnNode node, String ifaceName) {
+        if (Strings.isNullOrEmpty(ifaceName)) {
+            return false;
+        }
+        return deviceService.getPorts(node.integrationBridgeId()).stream()
+                .filter(p -> portName(p).contains(ifaceName) &&
+                        p.isEnabled())
+                .findAny()
+                .isPresent();
     }
 
     /**
@@ -740,12 +610,12 @@
             return false;
         }
 
-        Set<IpAddress> intBrIps = getCurrentIps(session, DEFAULT_BRIDGE);
-        boolean result = getCurrentIps(session, node.dpIntf()).isEmpty() &&
-                isInterfaceUp(session, node.dpIntf()) &&
-                intBrIps.contains(node.dpIp().ip()) &&
+        Set<IpAddress> intBrIps = getCurrentIps(session, INTEGRATION_BRIDGE);
+        boolean result = getCurrentIps(session, node.dataIface()).isEmpty() &&
+                isInterfaceUp(session, node.dataIface()) &&
+                intBrIps.contains(node.dataIp().ip()) &&
                 intBrIps.contains(node.localMgmtIp().ip()) &&
-                isInterfaceUp(session, DEFAULT_BRIDGE);
+                isInterfaceUp(session, INTEGRATION_BRIDGE);
 
         disconnect(session);
         return result;
@@ -762,16 +632,6 @@
     }
 
     /**
-     * Returns connect point of a given host.
-     *
-     * @param host host
-     * @return connect point
-     */
-    private ConnectPoint connectPoint(Host host) {
-        return new ConnectPoint(host.location().deviceId(), host.location().port());
-    }
-
-    /**
      * Returns cordvtn node associated with a given OVSDB device.
      *
      * @param ovsdbId OVSDB device id
@@ -791,7 +651,7 @@
      */
     private CordVtnNode nodeByBridgeId(DeviceId bridgeId) {
         return getNodes().stream()
-                .filter(node -> node.intBrId().equals(bridgeId))
+                .filter(node -> node.integrationBridgeId().equals(bridgeId))
                 .findFirst().orElse(null);
     }
 
@@ -802,7 +662,7 @@
      * @return port name
      */
     private String portName(Port port) {
-        return port.annotations().value("portName");
+        return port.annotations().value(PORT_NAME);
     }
 
     private class OvsdbHandler implements ConnectionHandler<Device> {
@@ -819,10 +679,8 @@
 
         @Override
         public void disconnected(Device device) {
-            if (!deviceService.isAvailable(device.id())) {
-                log.debug("Device {} is disconnected", device.id());
-                adminService.removeDevice(device.id());
-            }
+            log.debug("Device {} is disconnected", device.id());
+            adminService.removeDevice(device.id());
         }
     }
 
@@ -842,7 +700,7 @@
         public void disconnected(Device device) {
             CordVtnNode node = nodeByBridgeId(device.id());
             if (node != null) {
-                log.debug("Integration Bridge is disconnected from {}", node.hostname());
+                log.warn("Integration Bridge is disconnected from {}", node.hostname());
                 setNodeState(node, NodeState.INCOMPLETE);
             }
         }
@@ -869,9 +727,9 @@
                 if (isNodeStateComplete(node)) {
                     instanceService.addInstance(connectPoint(port));
                 } else {
-                    log.debug("VM is detected on incomplete node, ignore it.", portName);
+                    log.warn("VM is detected on incomplete node, ignore it.", portName);
                 }
-            } else if (portName.contains(DEFAULT_TUNNEL) || portName.equals(node.dpIntf())) {
+            } else if (node.systemIfaces().contains(portName)) {
                 setNodeState(node, getNodeState(node));
             }
         }
@@ -897,9 +755,9 @@
                 if (isNodeStateComplete(node)) {
                     instanceService.removeInstance(connectPoint(port));
                 } else {
-                    log.debug("VM is vanished from incomplete node, ignore it.", portName);
+                    log.warn("VM is vanished from incomplete node, ignore it.", portName);
                 }
-            } else if (portName.contains(DEFAULT_TUNNEL) || portName.equals(node.dpIntf())) {
+            } else if (node.systemIfaces().contains(portName)) {
                 setNodeState(node, NodeState.INCOMPLETE);
             }
         }
diff --git a/src/main/java/org/opencord/cordvtn/impl/CordVtnPipeline.java b/src/main/java/org/opencord/cordvtn/impl/CordVtnPipeline.java
index 8aa0946..2aa9f1f 100644
--- a/src/main/java/org/opencord/cordvtn/impl/CordVtnPipeline.java
+++ b/src/main/java/org/opencord/cordvtn/impl/CordVtnPipeline.java
@@ -115,26 +115,26 @@
      * Installs table miss rule to a give device.
      *
      * @param node cordvtn node
-     * @param dpPort data plane port number
+     * @param dataPort data plane port number
      * @param tunnelPort tunnel port number
      */
-    public void initPipeline(CordVtnNode node, PortNumber dpPort, PortNumber tunnelPort) {
+    public void initPipeline(CordVtnNode node, PortNumber dataPort, PortNumber tunnelPort) {
         checkNotNull(node);
 
-        processTableZero(node.intBrId(), dpPort, node.dpIp().ip());
-        processInPortTable(node.intBrId(), tunnelPort, dpPort);
-        processAccessTypeTable(node.intBrId(), dpPort);
-        processVlanTable(node.intBrId(), dpPort);
+        processTableZero(node.integrationBridgeId(), dataPort, node.dataIp().ip());
+        processInPortTable(node.integrationBridgeId(), tunnelPort, dataPort);
+        processAccessTypeTable(node.integrationBridgeId(), dataPort);
+        processVlanTable(node.integrationBridgeId(), dataPort);
     }
 
-    private void processTableZero(DeviceId deviceId, PortNumber dpPort, IpAddress dpIp) {
+    private void processTableZero(DeviceId deviceId, PortNumber dataPort, IpAddress dataIp) {
         // take vxlan packet out onto the physical port
         TrafficSelector selector = DefaultTrafficSelector.builder()
                 .matchInPort(PortNumber.LOCAL)
                 .build();
 
         TrafficTreatment treatment = DefaultTrafficTreatment.builder()
-                .setOutput(dpPort)
+                .setOutput(dataPort)
                 .build();
 
         FlowRule flowRule = DefaultFlowRule.builder()
@@ -151,7 +151,7 @@
 
         // take a vxlan encap'd packet through the Linux stack
         selector = DefaultTrafficSelector.builder()
-                .matchInPort(dpPort)
+                .matchInPort(dataPort)
                 .matchEthType(Ethernet.TYPE_IPV4)
                 .matchIPProtocol(IPv4.PROTOCOL_UDP)
                 .matchUdpDst(TpPort.tpPort(VXLAN_UDP_PORT))
@@ -175,9 +175,9 @@
 
         // take a packet to the data plane ip through Linux stack
         selector = DefaultTrafficSelector.builder()
-                .matchInPort(dpPort)
+                .matchInPort(dataPort)
                 .matchEthType(Ethernet.TYPE_IPV4)
-                .matchIPDst(dpIp.toIpPrefix())
+                .matchIPDst(dataIp.toIpPrefix())
                 .build();
 
         treatment = DefaultTrafficTreatment.builder()
@@ -198,9 +198,9 @@
 
         // take an arp packet from physical through Linux stack
         selector = DefaultTrafficSelector.builder()
-                .matchInPort(dpPort)
+                .matchInPort(dataPort)
                 .matchEthType(Ethernet.TYPE_ARP)
-                .matchArpTpa(dpIp.getIp4Address())
+                .matchArpTpa(dataIp.getIp4Address())
                 .build();
 
         treatment = DefaultTrafficTreatment.builder()
@@ -261,7 +261,7 @@
         processFlowRule(true, flowRule);
     }
 
-    private void processInPortTable(DeviceId deviceId, PortNumber tunnelPort, PortNumber dpPort) {
+    private void processInPortTable(DeviceId deviceId, PortNumber tunnelPort, PortNumber dataPort) {
         checkNotNull(tunnelPort);
 
         TrafficSelector selector = DefaultTrafficSelector.builder()
@@ -285,7 +285,7 @@
         processFlowRule(true, flowRule);
 
         selector = DefaultTrafficSelector.builder()
-                .matchInPort(dpPort)
+                .matchInPort(dataPort)
                 .build();
 
         treatment = DefaultTrafficTreatment.builder()
@@ -305,12 +305,12 @@
         processFlowRule(true, flowRule);
     }
 
-    private void processAccessTypeTable(DeviceId deviceId, PortNumber dpPort) {
+    private void processAccessTypeTable(DeviceId deviceId, PortNumber dataPort) {
         TrafficSelector selector = DefaultTrafficSelector.builder()
                 .build();
 
         TrafficTreatment treatment = DefaultTrafficTreatment.builder()
-                .setOutput(dpPort)
+                .setOutput(dataPort)
                 .build();
 
         FlowRule flowRule = DefaultFlowRule.builder()
@@ -326,7 +326,7 @@
         processFlowRule(true, flowRule);
     }
 
-    private void processVlanTable(DeviceId deviceId, PortNumber dpPort) {
+    private void processVlanTable(DeviceId deviceId, PortNumber dataPort) {
         // for traffic going out to WAN, strip vid 500 and take through data plane interface
         TrafficSelector selector = DefaultTrafficSelector.builder()
                 .matchVlanId(VLAN_WAN)
@@ -334,7 +334,7 @@
 
         TrafficTreatment treatment = DefaultTrafficTreatment.builder()
                 .popVlan()
-                .setOutput(dpPort)
+                .setOutput(dataPort)
                 .build();
 
         FlowRule flowRule = DefaultFlowRule.builder()
diff --git a/src/main/java/org/opencord/cordvtn/impl/DependencyManager.java b/src/main/java/org/opencord/cordvtn/impl/DependencyManager.java
index 2eb73db..eaebb69 100644
--- a/src/main/java/org/opencord/cordvtn/impl/DependencyManager.java
+++ b/src/main/java/org/opencord/cordvtn/impl/DependencyManager.java
@@ -157,7 +157,7 @@
         GroupKey groupKey = getGroupKey(service.id());
 
         Set<DeviceId> devices = nodeManager.completeNodes().stream()
-                .map(CordVtnNode::intBrId)
+                .map(CordVtnNode::integrationBridgeId)
                 .collect(Collectors.toSet());
 
         for (DeviceId deviceId : devices) {
@@ -217,7 +217,7 @@
         Map<DeviceId, Set<PortNumber>> inPorts = Maps.newHashMap();
 
         nodeManager.completeNodes().stream().forEach(node -> {
-            DeviceId deviceId = node.intBrId();
+            DeviceId deviceId = node.integrationBridgeId();
             GroupId groupId = createServiceGroup(deviceId, pService);
             outGroups.put(deviceId, groupId);
 
@@ -279,7 +279,7 @@
                 .build();
 
         nodeManager.completeNodes().stream().forEach(node -> {
-            DeviceId deviceId = node.intBrId();
+            DeviceId deviceId = node.integrationBridgeId();
             FlowRule flowRuleDirect = DefaultFlowRule.builder()
                     .fromApp(appId)
                     .withSelector(selector)
@@ -365,7 +365,7 @@
                                                 Set<Instance> instances) {
         List<GroupBucket> buckets = Lists.newArrayList();
         instances.stream().forEach(instance -> {
-            Ip4Address tunnelIp = nodeManager.dpIp(instance.deviceId()).getIp4Address();
+            Ip4Address tunnelIp = nodeManager.dataIp(instance.deviceId()).getIp4Address();
             TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder();
 
             if (deviceId.equals(instance.deviceId())) {
diff --git a/src/main/java/org/opencord/cordvtn/impl/InstanceManager.java b/src/main/java/org/opencord/cordvtn/impl/InstanceManager.java
index 3041bbf..6269e98 100644
--- a/src/main/java/org/opencord/cordvtn/impl/InstanceManager.java
+++ b/src/main/java/org/opencord/cordvtn/impl/InstanceManager.java
@@ -69,6 +69,7 @@
 import static java.util.concurrent.Executors.newSingleThreadScheduledExecutor;
 import static org.onlab.util.Tools.groupedThreads;
 import static org.onosproject.dhcp.IpAssignment.AssignmentStatus.Option_RangeNotEnforced;
+import static org.onosproject.net.AnnotationKeys.PORT_NAME;
 import static org.onosproject.xosclient.api.VtnServiceApi.NetworkType.MANAGEMENT;
 import static org.opencord.cordvtn.api.Constants.*;
 import static org.slf4j.LoggerFactory.getLogger;
diff --git a/src/main/java/org/opencord/cordvtn/impl/RemoteIpCommandUtil.java b/src/main/java/org/opencord/cordvtn/impl/RemoteIpCommandUtil.java
index bd2d922..0be4fc4 100644
--- a/src/main/java/org/opencord/cordvtn/impl/RemoteIpCommandUtil.java
+++ b/src/main/java/org/opencord/cordvtn/impl/RemoteIpCommandUtil.java
@@ -204,7 +204,7 @@
      * @param session session
      */
     public static void disconnect(Session session) {
-        if (session.isConnected()) {
+        if (session != null && session.isConnected()) {
             session.disconnect();
         }
     }
diff --git a/src/main/java/org/opencord/cordvtn/impl/handler/DefaultInstanceHandler.java b/src/main/java/org/opencord/cordvtn/impl/handler/DefaultInstanceHandler.java
index 98673cf..a4ca067 100644
--- a/src/main/java/org/opencord/cordvtn/impl/handler/DefaultInstanceHandler.java
+++ b/src/main/java/org/opencord/cordvtn/impl/handler/DefaultInstanceHandler.java
@@ -152,7 +152,7 @@
     }
 
     private void dstIpRule(Instance instance, long vni, boolean install) {
-        Ip4Address tunnelIp = nodeManager.dpIp(instance.deviceId()).getIp4Address();
+        Ip4Address tunnelIp = nodeManager.dataIp(instance.deviceId()).getIp4Address();
 
         TrafficSelector selector = DefaultTrafficSelector.builder()
                 .matchEthType(Ethernet.TYPE_IPV4)
@@ -177,11 +177,12 @@
         pipeline.processFlowRule(install, flowRule);
 
         for (CordVtnNode node : nodeManager.completeNodes()) {
-            if (node.intBrId().equals(instance.deviceId())) {
+            if (node.integrationBridgeId().equals(instance.deviceId())) {
                 continue;
             }
 
-            ExtensionTreatment tunnelDst = pipeline.tunnelDstTreatment(node.intBrId(), tunnelIp);
+            ExtensionTreatment tunnelDst =
+                    pipeline.tunnelDstTreatment(node.integrationBridgeId(), tunnelIp);
             if (tunnelDst == null) {
                 continue;
             }
@@ -189,8 +190,8 @@
             treatment = DefaultTrafficTreatment.builder()
                     .setEthDst(instance.mac())
                     .setTunnelId(vni)
-                    .extension(tunnelDst, node.intBrId())
-                    .setOutput(nodeManager.tunnelPort(node.intBrId()))
+                    .extension(tunnelDst, node.integrationBridgeId())
+                    .setOutput(nodeManager.tunnelPort(node.integrationBridgeId()))
                     .build();
 
             flowRule = DefaultFlowRule.builder()
@@ -198,7 +199,7 @@
                     .withSelector(selector)
                     .withTreatment(treatment)
                     .withPriority(CordVtnPipeline.PRIORITY_DEFAULT)
-                    .forDevice(node.intBrId())
+                    .forDevice(node.integrationBridgeId())
                     .forTable(CordVtnPipeline.TABLE_DST_IP)
                     .makePermanent()
                     .build();
@@ -248,7 +249,7 @@
                     .withSelector(selector)
                     .withTreatment(treatment)
                     .withPriority(CordVtnPipeline.PRIORITY_DEFAULT)
-                    .forDevice(node.intBrId())
+                    .forDevice(node.integrationBridgeId())
                     .forTable(CordVtnPipeline.TABLE_ACCESS_TYPE)
                     .makePermanent()
                     .build();
@@ -273,7 +274,7 @@
                     .withSelector(selector)
                     .withTreatment(treatment)
                     .withPriority(CordVtnPipeline.PRIORITY_LOW)
-                    .forDevice(node.intBrId())
+                    .forDevice(node.integrationBridgeId())
                     .forTable(CordVtnPipeline.TABLE_ACCESS_TYPE)
                     .makePermanent()
                     .build();
diff --git a/src/main/java/org/opencord/cordvtn/impl/handler/VsgInstanceHandler.java b/src/main/java/org/opencord/cordvtn/impl/handler/VsgInstanceHandler.java
index 1f96023..648c341 100644
--- a/src/main/java/org/opencord/cordvtn/impl/handler/VsgInstanceHandler.java
+++ b/src/main/java/org/opencord/cordvtn/impl/handler/VsgInstanceHandler.java
@@ -114,7 +114,7 @@
                 return;
             }
             populateVsgRules(vsgVm, getStag(vtnPort),
-                             nodeManager.dpPort(vsgVm.deviceId()),
+                             nodeManager.dataPort(vsgVm.deviceId()),
                              vtnPort.addressPairs().keySet(),
                              true);
 
@@ -159,7 +159,7 @@
         }
 
         populateVsgRules(vsgVm, getStag(vtnPort),
-                         nodeManager.dpPort(vsgVm.deviceId()),
+                         nodeManager.dataPort(vsgVm.deviceId()),
                          vtnPort.addressPairs().keySet(),
                          false);
     }
@@ -229,11 +229,11 @@
         instanceService.addNestedInstance(hostId, hostDesc);
     }
 
-    private void populateVsgRules(Instance vsgVm, VlanId stag, PortNumber dpPort,
+    private void populateVsgRules(Instance vsgVm, VlanId stag, PortNumber dataPort,
                                   Set<IpAddress> vsgWanIps, boolean install) {
         // for traffics with s-tag, strip the tag and take through the vSG VM
         TrafficSelector selector = DefaultTrafficSelector.builder()
-                .matchInPort(dpPort)
+                .matchInPort(dataPort)
                 .matchVlanId(stag)
                 .build();
 
@@ -261,7 +261,7 @@
                 .build();
 
         treatment = DefaultTrafficTreatment.builder()
-                .setOutput(dpPort)
+                .setOutput(dataPort)
                 .build();
 
         flowRule = DefaultFlowRule.builder()