Use port's MacAddress to associate ONOS port and Kubernetes port

Change-Id: I0a53962c61ddea06f4fb6bc6ab2a6f756cbc0052
diff --git a/apps/k8s-networking/app/src/main/java/org/onosproject/k8snetworking/impl/K8sSwitchingHostProvider.java b/apps/k8s-networking/app/src/main/java/org/onosproject/k8snetworking/impl/K8sSwitchingHostProvider.java
index 167d4ca..c8888ee 100644
--- a/apps/k8s-networking/app/src/main/java/org/onosproject/k8snetworking/impl/K8sSwitchingHostProvider.java
+++ b/apps/k8s-networking/app/src/main/java/org/onosproject/k8snetworking/impl/K8sSwitchingHostProvider.java
@@ -71,9 +71,11 @@
 import static org.onosproject.k8snetworking.api.Constants.GRE;
 import static org.onosproject.k8snetworking.api.Constants.K8S_NETWORKING_APP_ID;
 import static org.onosproject.k8snetworking.api.Constants.VXLAN;
-import static org.onosproject.k8snetworking.util.K8sNetworkingUtil.existingContainerPort;
+import static org.onosproject.k8snetworking.util.K8sNetworkingUtil.existingContainerPortByMac;
+import static org.onosproject.k8snetworking.util.K8sNetworkingUtil.existingContainerPortByName;
 import static org.onosproject.k8snetworking.util.K8sNetworkingUtil.isContainer;
 import static org.onosproject.k8snode.api.K8sNodeState.INIT;
+import static org.onosproject.net.AnnotationKeys.PORT_MAC;
 import static org.onosproject.net.AnnotationKeys.PORT_NAME;
 
 /**
@@ -161,10 +163,13 @@
      * @param port port object used in ONOS
      */
     private void processPortAdded(Port port) {
-        K8sPort k8sPort = portToK8sPort(port);
+        K8sPort k8sPort = portToK8sPortByName(port);
         if (k8sPort == null) {
-            log.warn(ERR_ADD_HOST + "Kubernetes port for {} not found", port);
-            return;
+            k8sPort = portToK8sPortByMac(port);
+            if (k8sPort == null) {
+                log.warn(ERR_ADD_HOST + "Kubernetes port for {} not found", port);
+                return;
+            }
         }
 
         K8sNetwork k8sNet = k8sNetworkService.network(k8sPort.networkId());
@@ -240,11 +245,14 @@
 
         hosts.forEach(h -> hostProviderService.hostVanished(h.id()));
 
-        K8sPort k8sPort = portToK8sPort(port);
+        K8sPort k8sPort = portToK8sPortByName(port);
 
         if (k8sPort == null) {
-            log.warn(ERR_ADD_HOST + "Kubernetes port for {} not found", port);
-            return;
+            k8sPort = portToK8sPortByMac(port);
+            if (k8sPort == null) {
+                log.warn(ERR_ADD_HOST + "Kubernetes port for {} not found", port);
+                return;
+            }
         }
 
         k8sNetworkService.removePort(k8sPort.portId());
@@ -256,11 +264,14 @@
      * @param port ONOS port
      */
     private void processPortInactivated(Port port) {
-        K8sPort k8sPort = portToK8sPort(port);
+        K8sPort k8sPort = portToK8sPortByName(port);
 
         if (k8sPort == null) {
-            log.warn(ERR_ADD_HOST + "Kubernetes port for {} not found", port);
-            return;
+            k8sPort = portToK8sPortByMac(port);
+            if (k8sPort == null) {
+                log.warn(ERR_ADD_HOST + "Kubernetes port for {} not found", port);
+                return;
+            }
         }
 
         k8sNetworkService.updatePort(k8sPort.updateState(K8sPort.State.INACTIVE));
@@ -272,7 +283,7 @@
      * @param port ONOS port
      * @return mapped kubernetes port
      */
-    private K8sPort portToK8sPort(Port port) {
+    private K8sPort portToK8sPortByName(Port port) {
         String portName = port.annotations().value(PORT_NAME);
         if (Strings.isNullOrEmpty(portName)) {
             return null;
@@ -280,7 +291,29 @@
 
         if (isContainer(portName)) {
             return k8sNetworkService.ports().stream()
-                    .filter(p -> existingContainerPort(p.portId(), portName))
+                    .filter(p -> existingContainerPortByName(p.portId(), portName))
+                    .findAny().orElse(null);
+        } else {
+            return null;
+        }
+    }
+
+    /**
+     * Converts ONOS port to kubernetes port.
+     *
+     * @param port ONOS port
+     * @return mapped kubernetes port
+     */
+    private K8sPort portToK8sPortByMac(Port port) {
+        String portName = port.annotations().value(PORT_NAME);
+        String portMac = port.annotations().value(PORT_MAC);
+        if (Strings.isNullOrEmpty(portMac) || Strings.isNullOrEmpty(portName)) {
+            return null;
+        }
+
+        if (isContainer(portName)) {
+            return k8sNetworkService.ports().stream()
+                    .filter(p -> existingContainerPortByMac(p.macAddress().toString(), portMac))
                     .findAny().orElse(null);
         } else {
             return null;
@@ -457,12 +490,13 @@
         }
 
         private void processK8sPortAddition(K8sNetworkEvent event) {
-            String portId = event.port().portId();
+            String mac = event.port().macAddress().toString();
             for (Device device : deviceService.getDevices()) {
                 Port port = deviceService.getPorts(device.id()).stream()
                         .filter(Port::isEnabled)
+                        .filter(p -> p.annotations().value(PORT_MAC) != null)
                         .filter(p -> p.annotations().value(PORT_NAME) != null)
-                        .filter(p -> existingContainerPort(portId, p.annotations().value(PORT_NAME)))
+                        .filter(p -> existingContainerPortByMac(mac, p.annotations().value(PORT_MAC)))
                         .findAny().orElse(null);
 
                 if (port != null) {
diff --git a/apps/k8s-networking/app/src/main/java/org/onosproject/k8snetworking/util/K8sNetworkingUtil.java b/apps/k8s-networking/app/src/main/java/org/onosproject/k8snetworking/util/K8sNetworkingUtil.java
index 0b8c4c9..e045d89 100644
--- a/apps/k8s-networking/app/src/main/java/org/onosproject/k8snetworking/util/K8sNetworkingUtil.java
+++ b/apps/k8s-networking/app/src/main/java/org/onosproject/k8snetworking/util/K8sNetworkingUtil.java
@@ -124,7 +124,7 @@
      * @param comparedName  port name to be compared
      * @return true if the compared port name exists, false otherwise
      */
-    public static boolean existingContainerPort(String sourceName, String comparedName) {
+    public static boolean existingContainerPortByName(String sourceName, String comparedName) {
         if (comparedName == null) {
             return false;
         }
@@ -141,6 +141,24 @@
     }
 
     /**
+     * Checks that whether the compared ports exist in the source MAC address.
+     *
+     * @param sourceMac     source port MAC address
+     * @param comparedMac   MAC address of port to be compared
+     * @return true if the compared port MAC address exists, false otherwise
+     */
+    public static boolean existingContainerPortByMac(String sourceMac, String comparedMac) {
+        if (comparedMac == null || sourceMac == null) {
+            return false;
+        }
+
+        String shortSourceMac = sourceMac.substring(3).toUpperCase();
+        String shortComparedMac = comparedMac.substring(3).toUpperCase();
+
+        return shortSourceMac.equals(shortComparedMac);
+    }
+
+    /**
      * Returns the tunnel port number with specified net ID and kubernetes node.
      *
      * @param netId         network ID
diff --git a/apps/k8s-networking/app/src/test/java/org/onosproject/k8snetworking/util/K8sNetworkUtilTest.java b/apps/k8s-networking/app/src/test/java/org/onosproject/k8snetworking/util/K8sNetworkUtilTest.java
index 9a3b34e..b90e770 100644
--- a/apps/k8s-networking/app/src/test/java/org/onosproject/k8snetworking/util/K8sNetworkUtilTest.java
+++ b/apps/k8s-networking/app/src/test/java/org/onosproject/k8snetworking/util/K8sNetworkUtilTest.java
@@ -21,6 +21,9 @@
 import java.util.Set;
 
 import static junit.framework.TestCase.assertEquals;
+import static junit.framework.TestCase.assertFalse;
+import static junit.framework.TestCase.assertTrue;
+import static org.onosproject.k8snetworking.util.K8sNetworkingUtil.existingContainerPortByMac;
 import static org.onosproject.k8snetworking.util.K8sNetworkingUtil.getSubnetIps;
 
 /**
@@ -45,4 +48,26 @@
         Set<IpAddress> dClassIps = getSubnetIps(dClassCidr);
         assertEquals(0, dClassIps.size());
     }
+
+    /**
+     * Tests the existing container port by MAC.
+     */
+    @Test
+    public void testExistingContainerPortByMac() {
+        String sourceMacStr = "fe:85:5a:d8:68:1d";
+        String comparedMacStr = "8A:85:5A:D8:68:1D";
+
+        boolean result1 = existingContainerPortByMac(sourceMacStr, comparedMacStr);
+        boolean result2 = existingContainerPortByMac(comparedMacStr, sourceMacStr);
+
+        assertTrue(result1);
+        assertTrue(result2);
+
+        String wrongMacStr = "8A:85:5A:D8:68:1F";
+        boolean result3 = existingContainerPortByMac(sourceMacStr, wrongMacStr);
+        boolean result4 = existingContainerPortByMac(wrongMacStr, sourceMacStr);
+
+        assertFalse(result3);
+        assertFalse(result4);
+    }
 }