Initial support for multi kubernetes clusters for k8s nodes

Change-Id: I6ca132898f8e157e0583de38a637fdc135f21d6f
(cherry picked from commit e2a04cedde73618ef24575e70cb221e03854de1d)
diff --git a/apps/k8s-node/app/src/main/java/org/onosproject/k8snode/codec/K8sNodeCodec.java b/apps/k8s-node/app/src/main/java/org/onosproject/k8snode/codec/K8sNodeCodec.java
index 41bad16..8793c46 100644
--- a/apps/k8s-node/app/src/main/java/org/onosproject/k8snode/codec/K8sNodeCodec.java
+++ b/apps/k8s-node/app/src/main/java/org/onosproject/k8snode/codec/K8sNodeCodec.java
@@ -17,6 +17,7 @@
 
 import com.fasterxml.jackson.databind.JsonNode;
 import com.fasterxml.jackson.databind.node.ObjectNode;
+import org.apache.commons.lang.StringUtils;
 import org.onlab.packet.IpAddress;
 import org.onosproject.codec.CodecContext;
 import org.onosproject.codec.JsonCodec;
@@ -28,6 +29,8 @@
 
 import static com.google.common.base.Preconditions.checkNotNull;
 import static org.onlab.util.Tools.nullIsIllegal;
+import static org.onosproject.k8snode.api.Constants.DEFAULT_CLUSTER_NAME;
+import static org.onosproject.k8snode.api.Constants.DEFAULT_SEGMENT_ID;
 import static org.slf4j.LoggerFactory.getLogger;
 
 /**
@@ -37,13 +40,16 @@
 
     private final Logger log = getLogger(getClass());
 
+    private static final String CLUSTER_NAME = "clusterName";
     private static final String HOSTNAME = "hostname";
     private static final String TYPE = "type";
+    private static final String SEGMENT_ID = "segmentId";
     private static final String MANAGEMENT_IP = "managementIp";
     private static final String DATA_IP = "dataIp";
     private static final String INTEGRATION_BRIDGE = "integrationBridge";
     private static final String EXTERNAL_BRIDGE = "externalBridge";
     private static final String LOCAL_BRIDGE = "localBridge";
+    private static final String TUNNEL_BRIDGE = "tunnelBridge";
     private static final String STATE = "state";
     private static final String EXTERNAL_INTF = "externalInterface";
     private static final String EXTERNAL_BRIDGE_IP = "externalBridgeIp";
@@ -56,8 +62,10 @@
         checkNotNull(node, "Kubernetes node cannot be null");
 
         ObjectNode result = context.mapper().createObjectNode()
+                .put(CLUSTER_NAME, node.clusterName())
                 .put(HOSTNAME, node.hostname())
                 .put(TYPE, node.type().name())
+                .put(SEGMENT_ID, node.segmentId())
                 .put(STATE, node.state().name())
                 .put(MANAGEMENT_IP, node.managementIp().toString());
 
@@ -73,6 +81,10 @@
             result.put(LOCAL_BRIDGE, node.localBridge().toString());
         }
 
+        if (node.tunBridge() != null) {
+            result.put(TUNNEL_BRIDGE, node.tunBridge().toString());
+        }
+
         if (node.dataIp() != null) {
             result.put(DATA_IP, node.dataIp().toString());
         }
@@ -98,6 +110,12 @@
             return null;
         }
 
+        String clusterName = json.get(CLUSTER_NAME).asText();
+
+        if (StringUtils.isEmpty(clusterName)) {
+            clusterName = DEFAULT_CLUSTER_NAME;
+        }
+
         String hostname = nullIsIllegal(json.get(HOSTNAME).asText(),
                 HOSTNAME + MISSING_MESSAGE);
         String type = nullIsIllegal(json.get(TYPE).asText(),
@@ -106,6 +124,7 @@
                 MANAGEMENT_IP + MISSING_MESSAGE);
 
         DefaultK8sNode.Builder nodeBuilder = DefaultK8sNode.builder()
+                .clusterName(clusterName)
                 .hostname(hostname)
                 .type(K8sNode.Type.valueOf(type))
                 .state(K8sNodeState.INIT)
@@ -115,6 +134,13 @@
             nodeBuilder.dataIp(IpAddress.valueOf(json.get(DATA_IP).asText()));
         }
 
+        JsonNode segmentIdJson = json.get(SEGMENT_ID);
+        int segmentId = DEFAULT_SEGMENT_ID;
+        if (segmentIdJson != null) {
+            segmentId = segmentIdJson.asInt();
+        }
+        nodeBuilder.segmentId(segmentId);
+
         JsonNode intBridgeJson = json.get(INTEGRATION_BRIDGE);
         if (intBridgeJson != null) {
             nodeBuilder.intgBridge(DeviceId.deviceId(intBridgeJson.asText()));
@@ -130,6 +156,11 @@
             nodeBuilder.localBridge(DeviceId.deviceId(localBridgeJson.asText()));
         }
 
+        JsonNode tunBridgeJson = json.get(TUNNEL_BRIDGE);
+        if (tunBridgeJson != null) {
+            nodeBuilder.tunBridge(DeviceId.deviceId(tunBridgeJson.asText()));
+        }
+
         JsonNode extIntfJson = json.get(EXTERNAL_INTF);
         if (extIntfJson != null) {
             nodeBuilder.extIntf(extIntfJson.asText());