Retrieve MAC address when kubernetes external lb config or interface has only IP address.

Change-Id: I79538f262a1a5d9bceb20d461743212ec6717d5a
diff --git a/apps/kubevirt-networking/api/src/main/java/org/onosproject/kubevirtnetworking/api/Constants.java b/apps/kubevirt-networking/api/src/main/java/org/onosproject/kubevirtnetworking/api/Constants.java
index 6866537..88ba57c 100644
--- a/apps/kubevirt-networking/api/src/main/java/org/onosproject/kubevirtnetworking/api/Constants.java
+++ b/apps/kubevirt-networking/api/src/main/java/org/onosproject/kubevirtnetworking/api/Constants.java
@@ -89,6 +89,7 @@
     public static final int CLI_ID_LENGTH = 30;
     public static final int CLI_NAME_LENGTH = 30;
     public static final int CLI_LONG_NAME_LENGTH = 50;
+    public static final int CLI_LONG_SERVICE_PORT_LENGTH = 100;
     public static final int CLI_IP_ADDRESSES_LENGTH = 50;
     public static final int CLI_IP_ADDRESS_LENGTH = 25;
     public static final int CLI_IP_ADDRESS_AVAILABILITY = 15;
diff --git a/apps/kubevirt-networking/api/src/main/java/org/onosproject/kubevirtnetworking/api/DefaultKubernetesExternalLb.java b/apps/kubevirt-networking/api/src/main/java/org/onosproject/kubevirtnetworking/api/DefaultKubernetesExternalLb.java
index 10e7cba..c652d60 100644
--- a/apps/kubevirt-networking/api/src/main/java/org/onosproject/kubevirtnetworking/api/DefaultKubernetesExternalLb.java
+++ b/apps/kubevirt-networking/api/src/main/java/org/onosproject/kubevirtnetworking/api/DefaultKubernetesExternalLb.java
@@ -34,8 +34,7 @@
 
     private final String serviceName;
     private final IpAddress loadbalancerIp;
-    private final Set<Integer> nodePortSet;
-    private final Set<Integer> portSet;
+    private final Set<KubernetesServicePort> servicePorts;
     private final Set<String> endpointSet;
     private final String electedGateway;
     private final IpAddress loadbalancerGwIp;
@@ -43,14 +42,13 @@
     private final String electedWorker;
 
     public DefaultKubernetesExternalLb(String serviceName, IpAddress loadbalancerIp,
-                                       Set<Integer> nodePortSet, Set<Integer> portSet,
+                                      Set<KubernetesServicePort> servicePorts,
                                        Set<String> endpointSet, String electedGateway,
                                        String electedWorker,
                                        IpAddress loadbalancerGwIp, MacAddress loadbalancerGwMac) {
         this.serviceName = serviceName;
         this.loadbalancerIp = loadbalancerIp;
-        this.nodePortSet = nodePortSet;
-        this.portSet = portSet;
+        this.servicePorts = servicePorts;
         this.endpointSet = endpointSet;
         this.electedGateway = electedGateway;
         this.electedWorker = electedWorker;
@@ -69,13 +67,8 @@
     }
 
     @Override
-    public Set<Integer> nodePortSet() {
-        return ImmutableSet.copyOf(nodePortSet);
-    }
-
-    @Override
-    public Set<Integer> portSet() {
-        return ImmutableSet.copyOf(portSet);
+    public Set<KubernetesServicePort> servicePorts() {
+        return ImmutableSet.copyOf(servicePorts);
     }
 
     @Override
@@ -114,8 +107,7 @@
 
         DefaultKubernetesExternalLb that = (DefaultKubernetesExternalLb) o;
         return serviceName.equals(that.serviceName) && loadbalancerIp.equals(that.loadbalancerIp) &&
-                Objects.equals(nodePortSet, that.nodePortSet) &&
-                Objects.equals(portSet, that.portSet) &&
+                Objects.equals(servicePorts, that.servicePorts) &&
                 Objects.equals(endpointSet, that.endpointSet) &&
                 Objects.equals(electedGateway, that.electedGateway) &&
                 Objects.equals(electedWorker, that.electedWorker) &&
@@ -128,8 +120,7 @@
         return DefaultKubernetesExternalLb.builder()
                 .serviceName(serviceName)
                 .loadBalancerIp(loadbalancerIp)
-                .nodePortSet(nodePortSet)
-                .portSet(portSet)
+                .servicePorts(servicePorts)
                 .endpointSet(endpointSet)
                 .electedGateway(electedGateway)
                 .electedWorker(electedWorker)
@@ -143,8 +134,7 @@
         return DefaultKubernetesExternalLb.builder()
                 .serviceName(serviceName)
                 .loadBalancerIp(loadbalancerIp)
-                .nodePortSet(nodePortSet)
-                .portSet(portSet)
+                .servicePorts(servicePorts)
                 .endpointSet(endpointSet)
                 .electedGateway(electedGateway)
                 .electedWorker(electedWorker)
@@ -163,8 +153,7 @@
         return MoreObjects.toStringHelper(this)
                 .add("serviceName", serviceName)
                 .add("loadbalancerIp", loadbalancerIp)
-                .add("nodePort", nodePortSet)
-                .add("port", portSet)
+                .add("servucePorts", servicePorts)
                 .add("endpointSet", endpointSet)
                 .add("electedGateway", electedGateway)
                 .add("electedWorker", electedWorker)
@@ -180,8 +169,7 @@
     public static final class Builder implements KubernetesExternalLb.Builder {
         private String serviceName;
         private IpAddress loadbalancerIp;
-        private Set<Integer> nodePortSet;
-        private Set<Integer> portSet;
+        private Set<KubernetesServicePort> servicePorts;
         private Set<String> endpointSet;
         private String electedGateway;
         private String electedWorker;
@@ -195,11 +183,10 @@
         public KubernetesExternalLb build() {
             checkArgument(serviceName != null, NOT_NULL_MSG, "serviceName");
             checkArgument(loadbalancerIp != null, NOT_NULL_MSG, "loadbalancerIp");
-            checkArgument(!nodePortSet.isEmpty(), NOT_NULL_MSG, "nodePortSet");
-            checkArgument(!portSet.isEmpty(), NOT_NULL_MSG, "portSet");
+            checkArgument(!servicePorts.isEmpty(), NOT_NULL_MSG, "servicePorts");
 
             return new DefaultKubernetesExternalLb(serviceName, loadbalancerIp,
-                    nodePortSet, portSet, endpointSet, electedGateway, electedWorker,
+                    servicePorts, endpointSet, electedGateway, electedWorker,
                     loadbalancerGwip, loadbalancerGwMac);
         }
 
@@ -216,14 +203,8 @@
         }
 
         @Override
-        public Builder nodePortSet(Set<Integer> nodePortSet) {
-            this.nodePortSet = nodePortSet;
-            return this;
-        }
-
-        @Override
-        public Builder portSet(Set<Integer> portSet) {
-            this.portSet = portSet;
+        public Builder servicePorts(Set<KubernetesServicePort> servicePorts) {
+            this.servicePorts = servicePorts;
             return this;
         }
 
diff --git a/apps/kubevirt-networking/api/src/main/java/org/onosproject/kubevirtnetworking/api/DefaultKubernetesServicePort.java b/apps/kubevirt-networking/api/src/main/java/org/onosproject/kubevirtnetworking/api/DefaultKubernetesServicePort.java
new file mode 100644
index 0000000..c5f2306
--- /dev/null
+++ b/apps/kubevirt-networking/api/src/main/java/org/onosproject/kubevirtnetworking/api/DefaultKubernetesServicePort.java
@@ -0,0 +1,105 @@
+/*
+ * Copyright 2022-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.kubevirtnetworking.api;
+
+import com.google.common.base.MoreObjects;
+
+import java.util.Objects;
+
+import static com.google.common.base.Preconditions.checkArgument;
+
+public class DefaultKubernetesServicePort implements KubernetesServicePort {
+    private static final String NOT_EMPTY_MSG = "KubernetesServicePort % cannot be empty";
+
+    private Integer port;
+    private Integer nodePort;
+
+    public DefaultKubernetesServicePort(Integer port, Integer nodePort) {
+        this.port = port;
+        this.nodePort = nodePort;
+    }
+
+    @Override
+    public Integer port() {
+        return port;
+    }
+
+    @Override
+    public Integer nodePort() {
+        return nodePort;
+    }
+
+    @Override
+    public boolean equals(Object o) {
+        if (this == o) {
+            return true;
+        }
+        if (o == null || getClass() != o.getClass()) {
+            return false;
+        }
+
+        DefaultKubernetesServicePort that = (DefaultKubernetesServicePort) o;
+
+        return Objects.equals(port, that.port) &&
+                Objects.equals(nodePort, that.nodePort);
+    }
+
+    @Override
+    public String toString() {
+        return MoreObjects.toStringHelper(this)
+                .add("port", port.toString())
+                .add("nodePort", nodePort.toString())
+                .toString();
+    }
+
+    @Override
+    public int hashCode() {
+        return Objects.hash(port, nodePort);
+    }
+
+    public static Builder builder() {
+        return new Builder();
+    }
+
+    public static final class Builder implements KubernetesServicePort.Builder {
+
+        private Integer port;
+        private Integer nodePort;
+
+        private Builder() {
+        }
+
+        @Override
+        public KubernetesServicePort build() {
+            checkArgument(port != null, NOT_EMPTY_MSG, "port");
+            checkArgument(nodePort != null, NOT_EMPTY_MSG, "nodePort");
+
+            return new DefaultKubernetesServicePort(port, nodePort);
+        }
+
+        @Override
+        public KubernetesServicePort.Builder port(Integer port) {
+            this.port = port;
+            return this;
+        }
+
+        @Override
+        public KubernetesServicePort.Builder nodePort(Integer nodePort) {
+            this.nodePort = nodePort;
+            return this;
+        }
+    }
+}
diff --git a/apps/kubevirt-networking/api/src/main/java/org/onosproject/kubevirtnetworking/api/KubernetesExternalLb.java b/apps/kubevirt-networking/api/src/main/java/org/onosproject/kubevirtnetworking/api/KubernetesExternalLb.java
index 4e902b0..6b2eaaa 100644
--- a/apps/kubevirt-networking/api/src/main/java/org/onosproject/kubevirtnetworking/api/KubernetesExternalLb.java
+++ b/apps/kubevirt-networking/api/src/main/java/org/onosproject/kubevirtnetworking/api/KubernetesExternalLb.java
@@ -39,18 +39,11 @@
     IpAddress loadBalancerIp();
 
     /**
-     * Returns the set of node port.
+     * Returns the set of service ports.
      *
      * @return node port
      */
-    Set<Integer> nodePortSet();
-
-    /**
-     * Returns the set of port.
-     *
-     * @return port number
-     */
-    Set<Integer> portSet();
+    Set<KubernetesServicePort> servicePorts();
 
     /**
      * Returns the set of endpoint.
@@ -130,21 +123,14 @@
          */
         Builder loadBalancerIp(IpAddress loadBalancerIp);
 
-        /**
-         * Returns kubernetes external load balancer builder with supplied node port set.
-         *
-         * @param nodePortSet node port set
-         * @return external load balancer builder
-         */
-        Builder nodePortSet(Set<Integer> nodePortSet);
 
         /**
-         * Returns kubernetes external load balancer builder with supplied port set.
+         * Returns kubernetes external load balancer builder with supplied service port set.
          *
-         * @param portSet port set
+         * @param servicePorts service port set
          * @return external load balancer builder
          */
-        Builder portSet(Set<Integer> portSet);
+        Builder servicePorts(Set<KubernetesServicePort> servicePorts);
 
         /**
          * Returns kubernetes external load balancer builder with supplied endpoint set.
diff --git a/apps/kubevirt-networking/api/src/main/java/org/onosproject/kubevirtnetworking/api/KubernetesServicePort.java b/apps/kubevirt-networking/api/src/main/java/org/onosproject/kubevirtnetworking/api/KubernetesServicePort.java
new file mode 100644
index 0000000..7afae49
--- /dev/null
+++ b/apps/kubevirt-networking/api/src/main/java/org/onosproject/kubevirtnetworking/api/KubernetesServicePort.java
@@ -0,0 +1,62 @@
+/*
+ * Copyright 2022-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.kubevirtnetworking.api;
+
+/**
+ * Representation of a Kubernetes service port for kubernetes external load balancer.
+ */
+public interface KubernetesServicePort {
+
+    /**
+     * Returns the port.
+     *
+     * @return port
+     */
+    Integer port();
+
+    /**
+     * Returns the node port.
+     *
+     * @return node port
+     */
+    Integer nodePort();
+
+    interface Builder {
+
+        /**
+         * Builds immutable kubernetes service port instance.
+         *
+         * @return kubernetes service port instance
+         */
+        KubernetesServicePort build();
+
+        /**
+         * Returns kubernetes service port builder with supplied port.
+         *
+         * @param port port
+         * @return kubernetes service port builder
+         */
+        Builder port(Integer port);
+
+        /**
+         * Returns kubernetes service port builder with supplied node port.
+         *
+         * @param nodePort node port
+         * @return kubernetes service port builder
+         */
+        Builder nodePort(Integer nodePort);
+    }
+}
diff --git a/apps/kubevirt-networking/api/src/test/java/org/onosproject/kubevirtnetworking/api/DefaultKubernetesExternalLbTest.java b/apps/kubevirt-networking/api/src/test/java/org/onosproject/kubevirtnetworking/api/DefaultKubernetesExternalLbTest.java
index 228bb9f..ea50fef 100644
--- a/apps/kubevirt-networking/api/src/test/java/org/onosproject/kubevirtnetworking/api/DefaultKubernetesExternalLbTest.java
+++ b/apps/kubevirt-networking/api/src/test/java/org/onosproject/kubevirtnetworking/api/DefaultKubernetesExternalLbTest.java
@@ -35,10 +35,16 @@
     private static final String SERVICE_NAME_2 = "service_name_2";
     private static final IpAddress LOADBALANCER_IP_1 = IpAddress.valueOf("1.1.1.2");
     private static final IpAddress LOADBALANCER_IP_2 = IpAddress.valueOf("2.2.2.2");
-    private static final Set<Integer> NODE_PORT_SET_1 = Sets.newHashSet(Integer.valueOf(32080));
-    private static final Set<Integer> NODE_PORT_SET_2 = Sets.newHashSet(Integer.valueOf(33080));
-    private static final Set<Integer> PORT_SET_1 = Sets.newHashSet(Integer.valueOf(8080));
-    private static final Set<Integer> PORT_SET_2 = Sets.newHashSet(Integer.valueOf(9090));
+    private static final KubernetesServicePort SERVICE_PORT_1 = DefaultKubernetesServicePort.builder()
+            .port(Integer.valueOf(8080))
+            .nodePort(Integer.valueOf(31080))
+            .build();
+    private static final KubernetesServicePort SERVICE_PORT_2 = DefaultKubernetesServicePort.builder()
+            .port(Integer.valueOf(8081))
+            .nodePort(Integer.valueOf(31081))
+            .build();
+    private static final Set<KubernetesServicePort> SERVICE_PORT_SET_1 = Sets.newHashSet(SERVICE_PORT_1);
+    private static final Set<KubernetesServicePort> SERVICE_PORT_SET_2 = Sets.newHashSet(SERVICE_PORT_2);
     private static final Set<String> ENDPOINT_SET_1 = Sets.newHashSet(String.valueOf("1.1.2.1"));
     private static final Set<String> ENDPOINT_SET_2 = Sets.newHashSet(String.valueOf("1.1.2.2"));
     private static final String ELECTED_GATEWAY_1 = "gateway1";
@@ -71,8 +77,7 @@
         lb1 = DefaultKubernetesExternalLb.builder()
                 .serviceName(SERVICE_NAME_1)
                 .loadBalancerIp(LOADBALANCER_IP_1)
-                .nodePortSet(NODE_PORT_SET_1)
-                .portSet(PORT_SET_1)
+                .servicePorts(SERVICE_PORT_SET_1)
                 .endpointSet(ENDPOINT_SET_1)
                 .electedGateway(ELECTED_GATEWAY_1)
                 .electedWorker(ELECTED_WORKER_1)
@@ -83,8 +88,7 @@
         sameAsLb1 = DefaultKubernetesExternalLb.builder()
                 .serviceName(SERVICE_NAME_1)
                 .loadBalancerIp(LOADBALANCER_IP_1)
-                .nodePortSet(NODE_PORT_SET_1)
-                .portSet(PORT_SET_1)
+                .servicePorts(SERVICE_PORT_SET_1)
                 .endpointSet(ENDPOINT_SET_1)
                 .electedGateway(ELECTED_GATEWAY_1)
                 .electedWorker(ELECTED_WORKER_1)
@@ -95,8 +99,7 @@
         lb2 = DefaultKubernetesExternalLb.builder()
                 .serviceName(SERVICE_NAME_2)
                 .loadBalancerIp(LOADBALANCER_IP_2)
-                .nodePortSet(NODE_PORT_SET_2)
-                .portSet(PORT_SET_2)
+                .servicePorts(SERVICE_PORT_SET_2)
                 .endpointSet(ENDPOINT_SET_2)
                 .electedGateway(ELECTED_GATEWAY_2)
                 .electedWorker(ELECTED_WORKER_2)
@@ -124,8 +127,7 @@
 
         assertEquals(SERVICE_NAME_1, lb.serviceName());
         assertEquals(LOADBALANCER_IP_1, lb.loadBalancerIp());
-        assertEquals(NODE_PORT_SET_1, lb.nodePortSet());
-        assertEquals(PORT_SET_1, lb.portSet());
+        assertEquals(SERVICE_PORT_SET_1, lb.servicePorts());
         assertEquals(ENDPOINT_SET_1, lb.endpointSet());
     }
 }
diff --git a/apps/kubevirt-networking/app/src/main/java/org/onosproject/kubevirtnetworking/cli/KubernetesListServiceCommand.java b/apps/kubevirt-networking/app/src/main/java/org/onosproject/kubevirtnetworking/cli/KubernetesListServiceCommand.java
index 47d4a71..fcb7188 100644
--- a/apps/kubevirt-networking/app/src/main/java/org/onosproject/kubevirtnetworking/cli/KubernetesListServiceCommand.java
+++ b/apps/kubevirt-networking/app/src/main/java/org/onosproject/kubevirtnetworking/cli/KubernetesListServiceCommand.java
@@ -27,6 +27,7 @@
 import java.util.List;
 
 import static org.onosproject.kubevirtnetworking.api.Constants.CLI_IP_ADDRESS_LENGTH;
+import static org.onosproject.kubevirtnetworking.api.Constants.CLI_LONG_SERVICE_PORT_LENGTH;
 import static org.onosproject.kubevirtnetworking.api.Constants.CLI_MAC_ADDRESS_LENGTH;
 import static org.onosproject.kubevirtnetworking.api.Constants.CLI_MARGIN_LENGTH;
 import static org.onosproject.kubevirtnetworking.api.Constants.CLI_NAME_LENGTH;
@@ -47,11 +48,12 @@
         List<KubernetesExternalLb> elbList = Lists.newArrayList(service.loadBalancers());
 
         String format = genFormatString(ImmutableList.of(CLI_NAME_LENGTH, CLI_IP_ADDRESS_LENGTH,
-                CLI_NAME_LENGTH, CLI_NAME_LENGTH, CLI_IP_ADDRESS_LENGTH, CLI_MAC_ADDRESS_LENGTH));
+                CLI_NAME_LENGTH, CLI_NAME_LENGTH, CLI_IP_ADDRESS_LENGTH, CLI_MAC_ADDRESS_LENGTH,
+                CLI_LONG_SERVICE_PORT_LENGTH));
 
 
         print(format, "Service Name", "Loadbalancer IP", "Elected Gateway", "Elected Worker",
-                "Loadbalancer GW IP", "Loadbalancer GW MAC");
+                "Loadbalancer GW IP", "Loadbalancer GW MAC", "Service Port");
 
         for (KubernetesExternalLb elb : elbList) {
             String lbIp = elb.loadBalancerIp() == null ? "N/A" : elb.loadBalancerIp().toString();
@@ -59,6 +61,7 @@
             String electedWorker = elb.electedWorker() == null ? "N/A" : elb.electedWorker();
             String lbGwIp = elb.loadBalancerGwIp() == null ? "N/A" : elb.loadBalancerGwIp().toString();
             String lbGwMac = elb.loadBalancerGwMac() == null ? "N/A" : elb.loadBalancerGwMac().toString();
+            String lbServicePort = elb.servicePorts().isEmpty() ? "N/A" : elb.servicePorts().toString();
 
             print(format, StringUtils.substring(elb.serviceName(), 0,
                     CLI_NAME_LENGTH - CLI_MARGIN_LENGTH),
@@ -71,7 +74,9 @@
                     StringUtils.substring(lbGwIp, 0,
                             CLI_IP_ADDRESS_LENGTH - CLI_MARGIN_LENGTH),
                     StringUtils.substring(lbGwMac, 0,
-                            CLI_MAC_ADDRESS_LENGTH - CLI_MARGIN_LENGTH)
+                            CLI_MAC_ADDRESS_LENGTH - CLI_MARGIN_LENGTH),
+                    StringUtils.substring(lbServicePort, 0,
+                            CLI_LONG_SERVICE_PORT_LENGTH - CLI_MARGIN_LENGTH)
             );
         }
     }
diff --git a/apps/kubevirt-networking/app/src/main/java/org/onosproject/kubevirtnetworking/impl/DistributedKubernetesExternalLbStore.java b/apps/kubevirt-networking/app/src/main/java/org/onosproject/kubevirtnetworking/impl/DistributedKubernetesExternalLbStore.java
index 1bd286e..ce78884 100644
--- a/apps/kubevirt-networking/app/src/main/java/org/onosproject/kubevirtnetworking/impl/DistributedKubernetesExternalLbStore.java
+++ b/apps/kubevirt-networking/app/src/main/java/org/onosproject/kubevirtnetworking/impl/DistributedKubernetesExternalLbStore.java
@@ -21,10 +21,12 @@
 import org.onosproject.core.ApplicationId;
 import org.onosproject.core.CoreService;
 import org.onosproject.kubevirtnetworking.api.DefaultKubernetesExternalLb;
+import org.onosproject.kubevirtnetworking.api.DefaultKubernetesServicePort;
 import org.onosproject.kubevirtnetworking.api.KubernetesExternalLb;
 import org.onosproject.kubevirtnetworking.api.KubernetesExternalLbEvent;
 import org.onosproject.kubevirtnetworking.api.KubernetesExternalLbStore;
 import org.onosproject.kubevirtnetworking.api.KubernetesExternalLbStoreDelegate;
+import org.onosproject.kubevirtnetworking.api.KubernetesServicePort;
 import org.onosproject.store.AbstractStore;
 import org.onosproject.store.serializers.KryoNamespaces;
 import org.onosproject.store.service.ConsistentMap;
@@ -72,6 +74,8 @@
             .register(KryoNamespaces.API)
             .register(KubernetesExternalLb.class)
             .register(DefaultKubernetesExternalLb.class)
+            .register(KubernetesServicePort.class)
+            .register(DefaultKubernetesServicePort.class)
             .register(IpAddress.class)
             .register(Collection.class)
             .build();
diff --git a/apps/kubevirt-networking/app/src/main/java/org/onosproject/kubevirtnetworking/impl/KubernetesExternalLbArpHandler.java b/apps/kubevirt-networking/app/src/main/java/org/onosproject/kubevirtnetworking/impl/KubernetesExternalLbArpHandler.java
new file mode 100644
index 0000000..5af9834
--- /dev/null
+++ b/apps/kubevirt-networking/app/src/main/java/org/onosproject/kubevirtnetworking/impl/KubernetesExternalLbArpHandler.java
@@ -0,0 +1,501 @@
+/*
+ * Copyright 2022-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.kubevirtnetworking.impl;
+
+import org.onlab.packet.ARP;
+import org.onlab.packet.EthType;
+import org.onlab.packet.Ethernet;
+import org.onlab.packet.Ip4Address;
+import org.onlab.packet.IpAddress;
+import org.onlab.packet.MacAddress;
+import org.onlab.packet.VlanId;
+import org.onosproject.cluster.ClusterService;
+import org.onosproject.cluster.LeadershipService;
+import org.onosproject.cluster.NodeId;
+import org.onosproject.core.ApplicationId;
+import org.onosproject.core.CoreService;
+import org.onosproject.kubevirtnetworking.api.KubevirtFlowRuleService;
+import org.onosproject.kubevirtnetworking.util.KubevirtNetworkingUtil;
+import org.onosproject.kubevirtnode.api.KubernetesExternalLbConfig;
+import org.onosproject.kubevirtnode.api.KubernetesExternalLbConfigAdminService;
+import org.onosproject.kubevirtnode.api.KubernetesExternalLbConfigEvent;
+import org.onosproject.kubevirtnode.api.KubernetesExternalLbConfigListener;
+import org.onosproject.kubevirtnode.api.KubernetesExternalLbInterface;
+import org.onosproject.kubevirtnode.api.KubevirtNode;
+import org.onosproject.kubevirtnode.api.KubevirtNodeAdminService;
+import org.onosproject.kubevirtnode.api.KubevirtNodeEvent;
+import org.onosproject.kubevirtnode.api.KubevirtNodeListener;
+import org.onosproject.net.PortNumber;
+import org.onosproject.net.device.DeviceService;
+import org.onosproject.net.flow.DefaultTrafficSelector;
+import org.onosproject.net.flow.DefaultTrafficTreatment;
+import org.onosproject.net.flow.TrafficSelector;
+import org.onosproject.net.flow.TrafficTreatment;
+import org.onosproject.net.packet.DefaultOutboundPacket;
+import org.onosproject.net.packet.InboundPacket;
+import org.onosproject.net.packet.PacketContext;
+import org.onosproject.net.packet.PacketProcessor;
+import org.onosproject.net.packet.PacketService;
+import org.osgi.service.component.annotations.Activate;
+import org.osgi.service.component.annotations.Component;
+import org.osgi.service.component.annotations.Deactivate;
+import org.osgi.service.component.annotations.Reference;
+import org.osgi.service.component.annotations.ReferenceCardinality;
+import org.slf4j.Logger;
+
+import java.nio.ByteBuffer;
+import java.util.Objects;
+import java.util.Timer;
+import java.util.TimerTask;
+import java.util.concurrent.ExecutorService;
+
+import static java.util.concurrent.Executors.newSingleThreadExecutor;
+import static org.onlab.util.Tools.groupedThreads;
+import static org.onosproject.kubevirtnetworking.api.Constants.GW_ENTRY_TABLE;
+import static org.onosproject.kubevirtnetworking.api.Constants.KUBERNETES_EXTERNAL_LB_FAKE_MAC;
+import static org.onosproject.kubevirtnetworking.api.Constants.KUBEVIRT_NETWORKING_APP_ID;
+import static org.onosproject.kubevirtnetworking.api.Constants.PRIORITY_ARP_GATEWAY_RULE;
+import static org.onosproject.kubevirtnetworking.util.KubevirtNetworkingUtil.kubernetesElbMac;
+import static org.onosproject.kubevirtnode.api.KubevirtNode.Type.GATEWAY;
+import static org.slf4j.LoggerFactory.getLogger;
+
+/**
+ * Handles arp packets related to the kubernetes external loadbalancer handler.
+ */
+@Component(immediate = true)
+public class KubernetesExternalLbArpHandler {
+    protected final Logger log = getLogger(getClass());
+
+    @Reference(cardinality = ReferenceCardinality.MANDATORY)
+    protected CoreService coreService;
+
+    @Reference(cardinality = ReferenceCardinality.MANDATORY)
+    protected ClusterService clusterService;
+
+    @Reference(cardinality = ReferenceCardinality.MANDATORY)
+    protected LeadershipService leadershipService;
+
+    @Reference(cardinality = ReferenceCardinality.MANDATORY)
+    protected PacketService packetService;
+
+    @Reference(cardinality = ReferenceCardinality.MANDATORY)
+    protected DeviceService deviceService;
+
+    @Reference(cardinality = ReferenceCardinality.MANDATORY)
+    protected KubernetesExternalLbConfigAdminService externalLbConfigAdminService;
+
+    @Reference(cardinality = ReferenceCardinality.MANDATORY)
+    protected KubevirtNodeAdminService nodeAdminService;
+
+    @Reference(cardinality = ReferenceCardinality.MANDATORY)
+    protected KubevirtFlowRuleService kubevirtFlowRuleService;
+
+    private static final IpAddress NON_ROUTABLE_META_ADDRESS = IpAddress.valueOf("0.0.0.0");
+    private final Timer externalLbGwTimer = new Timer("kubernetes-external-lb-gateway");
+    private final Timer externalLbIntfGwTimer = new Timer("kubernetes-external-lb-intf-gateway");
+    private static final long SECONDS = 1000L;
+    private static final long INITIAL_DELAY = 5 * SECONDS;
+    private static final long TASK_PERIOD = 60 * SECONDS;
+
+    private final ExecutorService eventExecutor = newSingleThreadExecutor(
+            groupedThreads(this.getClass().getSimpleName(), "event-handler"));
+
+    private final PacketProcessor packetProcessor = new InternalPacketProcessor();
+    private final InternalKubernetesExternalLbConfigListener
+            lbConfigListener = new InternalKubernetesExternalLbConfigListener();
+
+    private final InternalNodeEventListener
+            nodeEventListener = new InternalNodeEventListener();
+
+
+    private ApplicationId appId;
+    private NodeId localNodeId;
+
+    @Activate
+    protected void activate() {
+        appId = coreService.registerApplication(KUBEVIRT_NETWORKING_APP_ID);
+        localNodeId = clusterService.getLocalNode().id();
+        leadershipService.runForLeadership(appId.name());
+
+        packetService.addProcessor(packetProcessor, PacketProcessor.director(1));
+        externalLbConfigAdminService.addListener(lbConfigListener);
+        nodeAdminService.addListener(nodeEventListener);
+
+        log.info("Started");
+    }
+
+    @Deactivate
+    protected void deactivate() {
+        leadershipService.withdraw(appId.name());
+        packetService.removeProcessor(packetProcessor);
+        externalLbConfigAdminService.removeListener(lbConfigListener);
+        nodeAdminService.removeListener(nodeEventListener);
+
+        eventExecutor.shutdown();
+
+        log.info("Stopped");
+    }
+
+    private class InternalKubernetesExternalLbConfigListener
+            implements KubernetesExternalLbConfigListener {
+
+        private boolean isRelevantHelper() {
+            return Objects.equals(localNodeId, leadershipService.getLeader(appId.name()));
+        }
+
+        @Override
+        public void event(KubernetesExternalLbConfigEvent event) {
+            switch (event.type()) {
+                case KUBERNETES_EXTERNAL_LB_CONFIG_CREATED:
+                case KUBERNETES_EXTERNAL_LB_CONFIG_UPDATED:
+                    eventExecutor.execute(() -> processConfigCreatedOrUpdated(event.subject()));
+                case KUBERNETES_EXTERNAL_LB_CONFIG_REMOVED:
+                default:
+                    //do nothing
+                    break;
+            }
+        }
+        private void processConfigCreatedOrUpdated(KubernetesExternalLbConfig config) {
+            if (!isRelevantHelper()) {
+                return;
+            }
+
+            if (config.loadBalancerGwMac() != null) {
+                return;
+            }
+
+            processKubernetesExternalLbConfigMacLearning(config);
+        }
+    }
+
+    private class InternalNodeEventListener implements KubevirtNodeListener {
+
+        private boolean isRelevantHelper() {
+            return Objects.equals(localNodeId, leadershipService.getLeader(appId.name()));
+        }
+
+
+        @Override
+        public void event(KubevirtNodeEvent event) {
+            switch (event.type()) {
+                case KUBEVIRT_NODE_COMPLETE:
+                    eventExecutor.execute(() -> processNodeCompletion(event.subject()));
+                    break;
+                case KUBEVIRT_NODE_INCOMPLETE:
+                case KUBEVIRT_NODE_UPDATED:
+                case KUBEVIRT_NODE_REMOVED:
+                    break;
+
+                default:
+                    // do nothing
+                    break;
+            }
+        }
+
+        private void processNodeCompletion(KubevirtNode node) {
+            if (!isRelevantHelper()) {
+                return;
+            }
+            if (node.type().equals(GATEWAY)) {
+                KubernetesExternalLbInterface externalLbInterface =
+                        node.kubernetesExternalLbInterface();
+
+                if (externalLbInterface != null && externalLbInterface.externalLbGwMac() == null) {
+                    processKubernetesExternalLbIntfGwMacLearningForGatewayNode(node);
+                }
+
+                KubernetesExternalLbConfig config =
+                        externalLbConfigAdminService.lbConfigs().stream().findAny().orElse(null);
+
+                if (config != null && config.loadBalancerGwMac() == null) {
+                    processKubernetesExternalLbConfigMacLearning(config);
+                }
+            }
+        }
+    }
+
+    private void processKubernetesExternalLbConfigMacLearning(KubernetesExternalLbConfig config) {
+        nodeAdminService.completeExternalLbGatewayNodes().forEach(gateway -> {
+            setRuleArpRequestToController(config.loadBalancerGwIp(),
+                    KUBERNETES_EXTERNAL_LB_FAKE_MAC, gateway, true);
+        });
+
+        KubevirtNode gateway = nodeAdminService.completeExternalLbGatewayNodes()
+                .stream().findAny().orElse(null);
+        if (gateway == null) {
+            return;
+        }
+        PortNumber externalPatchPortNum = KubevirtNetworkingUtil.externalPatchPortNum(deviceService, gateway);
+
+        if (externalPatchPortNum == null) {
+            log.warn("processKubernetesExternalLbConfigMacLearning" +
+                            " called but there's no external patchPort for {}. Stop this task.",
+                    gateway);
+            return;
+        }
+
+        retrievePeerMac(NON_ROUTABLE_META_ADDRESS, KUBERNETES_EXTERNAL_LB_FAKE_MAC,
+                config.loadBalancerGwIp(), gateway, externalPatchPortNum);
+        checkKubernetesExternalLbConfigMacRetrieved(config, gateway);
+    }
+
+
+    private void processKubernetesExternalLbIntfGwMacLearningForGatewayNode(KubevirtNode gatewayNode) {
+
+        MacAddress elbIntfMac = kubernetesElbMac(deviceService, gatewayNode);
+        if (elbIntfMac == null) {
+            log.warn("processKubernetesExternalLbGwMacLearningForNode called but elbIntfMac is null. Stop this task.");
+            return;
+        }
+
+        KubernetesExternalLbInterface externalLbInterface = gatewayNode.kubernetesExternalLbInterface();
+
+        setRuleArpRequestToController(externalLbInterface.externalLbGwIp(), elbIntfMac,
+                gatewayNode, true);
+
+        PortNumber elbPatchPortNum = KubevirtNetworkingUtil.elbPatchPortNum(deviceService, gatewayNode);
+
+        if (elbPatchPortNum == null) {
+            log.warn("processKubernetesExternalLbIntfGwMacLearningForGatewayNode" +
+                            " called but there's no elb patchPort for {}. Stop this task.",
+                    gatewayNode);
+            return;
+        }
+
+        retrievePeerMac(externalLbInterface.externalLbIp(), elbIntfMac,
+                externalLbInterface.externalLbGwIp(), gatewayNode, elbPatchPortNum);
+
+        checkKubernetesExternalLbIntfGwMacRetrieved(gatewayNode);
+    }
+
+    private void checkKubernetesExternalLbIntfGwMacRetrieved(KubevirtNode gateway) {
+        KubernetesExternalLbInterface externalLbInterface = gateway.kubernetesExternalLbInterface();
+
+        MacAddress elbIntfMac = kubernetesElbMac(deviceService, gateway);
+        if (elbIntfMac == null) {
+            log.warn("setDownstreamRules called but elbIntfMac is null. Stop this task.");
+            return;
+        }
+
+        KubernetexExternalLbIntfTimerTask task = new KubernetexExternalLbIntfTimerTask(
+                externalLbInterface.externalLbIp(), elbIntfMac,
+                externalLbInterface.externalLbGwIp(), gateway);
+
+        externalLbIntfGwTimer.schedule(task, INITIAL_DELAY, TASK_PERIOD);
+    }
+
+    private void checkKubernetesExternalLbConfigMacRetrieved(KubernetesExternalLbConfig config,
+                                                             KubevirtNode gateway) {
+        KubernetesExternalLbConfigTimerTask task = new KubernetesExternalLbConfigTimerTask(
+                IpAddress.valueOf("0.0.0.0"), KUBERNETES_EXTERNAL_LB_FAKE_MAC,
+                config.loadBalancerGwIp(), gateway);
+
+        externalLbGwTimer.schedule(task, INITIAL_DELAY, TASK_PERIOD);
+    }
+
+
+    private void setRuleArpRequestToController(IpAddress targetIpAddress,
+                                               MacAddress dstMac,
+                                               KubevirtNode gatewayNode,
+                                               boolean install) {
+        if (targetIpAddress == null || dstMac == null || gatewayNode == null) {
+            return;
+        }
+
+        TrafficSelector selector = DefaultTrafficSelector.builder()
+                .matchEthType(EthType.EtherType.ARP.ethType().toShort())
+                .matchArpOp(ARP.OP_REPLY)
+                .matchArpSpa(targetIpAddress.getIp4Address())
+                .matchArpTha(dstMac)
+                .build();
+
+        TrafficTreatment treatment = DefaultTrafficTreatment.builder()
+                .punt()
+                .build();
+
+        kubevirtFlowRuleService.setRule(
+                appId,
+                gatewayNode.intgBridge(),
+                selector,
+                treatment,
+                PRIORITY_ARP_GATEWAY_RULE,
+                GW_ENTRY_TABLE,
+                install
+        );
+    }
+
+
+    private void retrievePeerMac(IpAddress srcIp, MacAddress srcMac,
+                                 IpAddress peerIp, KubevirtNode gatewayNode,
+                                 PortNumber portNumber) {
+        log.trace("Sending ARP request to the peer {} to retrieve the MAC address.",
+                peerIp.getIp4Address().toString());
+
+        Ethernet ethRequest = ARP.buildArpRequest(srcMac.toBytes(),
+                srcIp.toOctets(),
+                peerIp.toOctets(), VlanId.NO_VID);
+
+        if (gatewayNode == null) {
+            log.warn("retrievePeerMac called but there's no gateway node for {}. Stop this task.",
+                    gatewayNode);
+            return;
+        }
+
+        TrafficTreatment treatment = DefaultTrafficTreatment.builder()
+                .setOutput(portNumber)
+                .build();
+
+        packetService.emit(new DefaultOutboundPacket(
+                gatewayNode.intgBridge(),
+                treatment,
+                ByteBuffer.wrap(ethRequest.serialize())));
+
+    }
+
+    private class KubernetesExternalLbConfigTimerTask extends TimerTask {
+        private final IpAddress srcIp;
+        private final MacAddress srcMac;
+        private final IpAddress peerIp;
+        private final KubevirtNode gatewayNode;
+
+        public KubernetesExternalLbConfigTimerTask(IpAddress srcIp, MacAddress srcMac,
+                                 IpAddress peerIp, KubevirtNode gatewayNode) {
+            this.srcIp = srcIp;
+            this.srcMac = srcMac;
+            this.peerIp = peerIp;
+            this.gatewayNode = gatewayNode;
+        }
+
+        @Override
+        public void run() {
+            KubernetesExternalLbConfig config =
+                    externalLbConfigAdminService.lbConfigs().stream().findAny().orElse(null);
+
+            if (config == null) {
+                return;
+            }
+
+            if (config.loadBalancerGwMac() != null) {
+                log.info("Peer Mac {} for KubernetesExternalLbGateway is retrieved. Stop this task.",
+                        config.loadBalancerGwMac());
+                this.cancel();
+                return;
+            }
+
+            PortNumber externalPatchPortNum = KubevirtNetworkingUtil.externalPatchPortNum(deviceService, gatewayNode);
+
+            if (externalPatchPortNum == null) {
+                log.warn("processKubernetesExternalLbConfigMacLearning" +
+                                " called but there's no external patchPort for {}. Stop this task.",
+                        gatewayNode);
+                return;
+            }
+
+            retrievePeerMac(srcIp, srcMac, peerIp, gatewayNode, externalPatchPortNum);
+        }
+    }
+
+
+    private class KubernetexExternalLbIntfTimerTask extends TimerTask {
+        private final IpAddress srcIp;
+        private final MacAddress srcMac;
+        private final IpAddress peerIp;
+        private final KubevirtNode gatewayNode;
+
+        public KubernetexExternalLbIntfTimerTask(IpAddress srcIp, MacAddress srcMac,
+                                                 IpAddress peerIp, KubevirtNode gatewayNode) {
+            this.srcIp = srcIp;
+            this.srcMac = srcMac;
+            this.peerIp = peerIp;
+            this.gatewayNode = gatewayNode;
+        }
+
+        @Override
+        public void run() {
+
+            KubernetesExternalLbInterface externalLbInterface = gatewayNode.kubernetesExternalLbInterface();
+
+            if (externalLbInterface.externalLbGwMac() != null) {
+                log.info("Peer Mac {} for KubernetesExternalLbIntfGw for node {} is retrieved. Stop this task.",
+                        externalLbInterface.externalLbGwMac(), gatewayNode.hostname());
+                this.cancel();
+                return;
+            }
+
+            PortNumber elbPatchPortNum = KubevirtNetworkingUtil.elbPatchPortNum(deviceService, gatewayNode);
+
+            if (elbPatchPortNum == null) {
+                log.warn("processKubernetesExternalLbIntfGwMacLearningForGatewayNode" +
+                                " called but there's no elb patchPort for {}. Stop this task.",
+                        gatewayNode);
+                return;
+            }
+
+            retrievePeerMac(srcIp, srcMac, peerIp, gatewayNode, elbPatchPortNum);
+        }
+    }
+
+    private class InternalPacketProcessor implements PacketProcessor {
+        @Override
+        public void process(PacketContext context) {
+            if (context.isHandled()) {
+                return;
+            }
+
+            InboundPacket pkt = context.inPacket();
+            Ethernet ethernet = pkt.parsed();
+
+            if (ethernet != null && ethernet.getEtherType() == Ethernet.TYPE_ARP) {
+                processArpPacket(ethernet);
+            }
+        }
+
+        private void processArpPacket(Ethernet ethernet) {
+            ARP arp = (ARP) ethernet.getPayload();
+
+            if (arp.getOpCode() == ARP.OP_REQUEST) {
+                return;
+            }
+            log.trace("ARP request {}", arp);
+
+            KubernetesExternalLbConfig config =
+                    externalLbConfigAdminService.lbConfigs().stream().findAny().orElse(null);
+
+            IpAddress spa = Ip4Address.valueOf(arp.getSenderProtocolAddress());
+            MacAddress sha = MacAddress.valueOf(arp.getSenderHardwareAddress());
+
+            if (config != null && config.loadBalancerGwIp().equals(spa)) {
+                externalLbConfigAdminService.updateKubernetesExternalLbConfig(config.updateLbGatewayMac(sha));
+            }
+
+            nodeAdminService.completeExternalLbGatewayNodes().forEach(gateway -> {
+                KubernetesExternalLbInterface externalLbInterface =
+                        gateway.kubernetesExternalLbInterface();
+                if (externalLbInterface == null) {
+                    return;
+                }
+
+                if (externalLbInterface.externalLbGwIp().equals(spa)) {
+                    if (externalLbInterface.externalLbGwMac() == null ||
+                            !externalLbInterface.externalLbGwMac().equals(sha)) {
+                        nodeAdminService.updateNode(gateway.updateKubernetesElbIntfGwMac(sha));
+                    }
+                }
+            });
+        }
+    }
+}
+
diff --git a/apps/kubevirt-networking/app/src/main/java/org/onosproject/kubevirtnetworking/impl/KubernetesExternalLbHandler.java b/apps/kubevirt-networking/app/src/main/java/org/onosproject/kubevirtnetworking/impl/KubernetesExternalLbHandler.java
index 7b1c733..5953c8a 100644
--- a/apps/kubevirt-networking/app/src/main/java/org/onosproject/kubevirtnetworking/impl/KubernetesExternalLbHandler.java
+++ b/apps/kubevirt-networking/app/src/main/java/org/onosproject/kubevirtnetworking/impl/KubernetesExternalLbHandler.java
@@ -80,7 +80,7 @@
 public class KubernetesExternalLbHandler {
     protected final Logger log = getLogger(getClass());
 
-    private static final int TP_PORT_MINIMUM_NUM = 1025;
+    private static final int TP_PORT_MINIMUM_NUM = 10000;
     private static final int TP_PORT_MAXIMUM_NUM = 65535;
 
     @Reference(cardinality = ReferenceCardinality.MANDATORY)
@@ -182,9 +182,9 @@
                 return;
             }
 
-            if (lb.electedGateway() == null || lb.electedWorker() == null) {
+            if (lb.electedGateway() == null || lb.electedWorker() == null || lb.loadBalancerGwMac() == null) {
                 log.warn("processKubernetesExternalLbCreatedOrUpdated called but electedGateway " +
-                        "or electedWorker is null. Stop this task.");
+                        "or electedWorker or loadBalancerGwMacis null. Stop this task.");
                 return;
             }
 
@@ -198,7 +198,9 @@
                 return;
             }
 
-            if (lb.electedWorker() == null || oldGatway == null) {
+            if (lb.electedWorker() == null || oldGatway == null || lb.loadBalancerGwMac() == null) {
+                log.warn("processKubernetesExternalLbGatewayChanged called but old electedWorker " +
+                        "or electedWorker or loadBalancerGwMacis null. Stop this task.");
                 return;
             }
 
@@ -326,13 +328,13 @@
             return;
         }
 
-        lb.nodePortSet().forEach(nodeport -> {
+        lb.servicePorts().forEach(servicePort -> {
             TrafficSelector selector = DefaultTrafficSelector.builder()
                     .matchEthType(Ethernet.TYPE_IPV4)
                     .matchEthDst(KUBERNETES_EXTERNAL_LB_FAKE_MAC)
                     .matchIPDst(loadBalancerIp.toIpPrefix())
                     .matchIPProtocol(IPv4.PROTOCOL_TCP)
-                    .matchTcpDst(TpPort.tpPort(nodeport.intValue()))
+                    .matchTcpDst(TpPort.tpPort(servicePort.port().intValue()))
                     .build();
 
             ExtensionTreatment natTreatment = RulePopulatorUtil
@@ -351,6 +353,7 @@
                     .setEthSrc(elbIntfMac)
                     .setEthDst(externalLbInterface.externalLbGwMac())
                     .setIpDst(electedWorker.dataIp())
+                    .setTcpDst(TpPort.tpPort(servicePort.nodePort().intValue()))
                     .setOutput(elbBridgePortNum)
                     .build();
 
@@ -404,13 +407,13 @@
             return;
         }
 
-        lb.nodePortSet().forEach(nodePort -> {
+        lb.servicePorts().forEach(servicePort -> {
             TrafficSelector.Builder sBuilder = DefaultTrafficSelector.builder()
                     .matchEthType(Ethernet.TYPE_IPV4)
                     .matchIPSrc(electedWorker.dataIp().toIpPrefix())
                     .matchIPDst(externalLbInterface.externalLbIp().toIpPrefix())
                     .matchIPProtocol(IPv4.PROTOCOL_TCP)
-                    .matchTcpSrc(TpPort.tpPort(nodePort.intValue()));
+                    .matchTcpSrc(TpPort.tpPort(servicePort.nodePort().intValue()));
 
             ExtensionTreatment natTreatment = RulePopulatorUtil
                     .niciraConnTrackTreatmentBuilder(driverService, gateway.intgBridge())
@@ -423,6 +426,7 @@
                     .setEthSrc(KUBERNETES_EXTERNAL_LB_FAKE_MAC)
                     .setIpSrc(lb.loadBalancerIp())
                     .setEthDst(lb.loadBalancerGwMac())
+                    .setTcpSrc(TpPort.tpPort(servicePort.port().intValue()))
                     .extension(natTreatment, gateway.intgBridge())
                     .transition(GW_DROP_TABLE);
 
@@ -438,13 +442,12 @@
             sBuilder = DefaultTrafficSelector.builder()
                     .matchEthType(Ethernet.TYPE_IPV4)
                     .matchIPProtocol(IPv4.PROTOCOL_TCP)
-                    .matchTcpSrc(TpPort.tpPort(nodePort.intValue()));
-
+                    .matchEthSrc(KUBERNETES_EXTERNAL_LB_FAKE_MAC)
+                    .matchIPSrc(lb.loadBalancerIp().toIpPrefix())
+                    .matchEthDst(lb.loadBalancerGwMac())
+                    .matchTcpSrc(TpPort.tpPort(servicePort.port().intValue()));
 
             tBuilder = DefaultTrafficTreatment.builder()
-                    .setEthSrc(KUBERNETES_EXTERNAL_LB_FAKE_MAC)
-                    .setIpSrc(lb.loadBalancerIp())
-                    .setEthDst(lb.loadBalancerGwMac())
                     .setOutput(externalPatchPortNum);
 
             flowService.setRule(
diff --git a/apps/kubevirt-networking/app/src/main/java/org/onosproject/kubevirtnetworking/impl/KubernetesServiceWatcher.java b/apps/kubevirt-networking/app/src/main/java/org/onosproject/kubevirtnetworking/impl/KubernetesServiceWatcher.java
index 2aec742..d80ee25 100644
--- a/apps/kubevirt-networking/app/src/main/java/org/onosproject/kubevirtnetworking/impl/KubernetesServiceWatcher.java
+++ b/apps/kubevirt-networking/app/src/main/java/org/onosproject/kubevirtnetworking/impl/KubernetesServiceWatcher.java
@@ -34,8 +34,10 @@
 import org.onosproject.core.ApplicationId;
 import org.onosproject.core.CoreService;
 import org.onosproject.kubevirtnetworking.api.DefaultKubernetesExternalLb;
+import org.onosproject.kubevirtnetworking.api.DefaultKubernetesServicePort;
 import org.onosproject.kubevirtnetworking.api.KubernetesExternalLb;
 import org.onosproject.kubevirtnetworking.api.KubernetesExternalLbAdminService;
+import org.onosproject.kubevirtnetworking.api.KubernetesServicePort;
 import org.onosproject.kubevirtnode.api.KubernetesExternalLbConfig;
 import org.onosproject.kubevirtnode.api.KubernetesExternalLbConfigEvent;
 import org.onosproject.kubevirtnode.api.KubernetesExternalLbConfigListener;
@@ -63,6 +65,7 @@
 import static java.util.concurrent.Executors.newSingleThreadExecutor;
 import static org.onlab.util.Tools.groupedThreads;
 import static org.onosproject.kubevirtnetworking.api.Constants.KUBEVIRT_NETWORKING_APP_ID;
+import static org.onosproject.kubevirtnetworking.util.KubevirtNetworkingUtil.configMapUpdated;
 import static org.onosproject.kubevirtnetworking.util.KubevirtNetworkingUtil.gatewayNodeForSpecifiedService;
 import static org.onosproject.kubevirtnetworking.util.KubevirtNetworkingUtil.k8sClient;
 import static org.onosproject.kubevirtnetworking.util.KubevirtNetworkingUtil.workerNodeForSpecifiedService;
@@ -175,7 +178,7 @@
             switch (event.type()) {
                 case KUBERNETES_EXTERNAL_LB_CONFIG_CREATED:
                 case KUBERNETES_EXTERNAL_LB_CONFIG_UPDATED:
-                    eventExecutor.execute(this::processConfigUpdate);
+                    eventExecutor.execute(() -> processConfigUpdate(event.subject()));
                     break;
                 case KUBERNETES_EXTERNAL_LB_CONFIG_REMOVED:
                 default:
@@ -184,11 +187,13 @@
             }
         }
 
-        private void processConfigUpdate() {
+        private void processConfigUpdate(KubernetesExternalLbConfig externalLbConfig) {
             if (!isRelevantHelper()) {
                 return;
             }
-            addOrUpdateExternalLoadBalancers();
+            if (configMapUpdated(externalLbConfig)) {
+                addOrUpdateExternalLoadBalancers();
+            }
         }
     }
 
@@ -261,7 +266,9 @@
                 return;
             }
 
-            if (!configMapUpdated()) {
+            KubernetesExternalLbConfig config = lbConfigService.lbConfigs().stream().findAny().orElse(null);
+
+            if (!configMapUpdated(config)) {
                 log.warn("Config map is not set yet. Stop this task");
                 return;
             }
@@ -390,17 +397,6 @@
                 .findAny().isPresent();
     }
 
-    private boolean configMapUpdated() {
-        KubernetesExternalLbConfig config = lbConfigService.lbConfigs().stream().findAny().orElse(null);
-
-        if (config == null) {
-            return false;
-        }
-
-        return config.configName() != null && config.globalIpRange() != null &&
-                config.loadBalancerGwIp() != null && config.loadBalancerGwMac() != null;
-    }
-
     //Only process if the event when the service type is LoadBalancer
     private boolean isLoadBalancerType(Service service) {
         return service.getSpec().getType().equals(TYPE_LOADBALANCER);
@@ -421,13 +417,15 @@
             return null;
         }
 
-        Set<Integer> nodePortSet = Sets.newHashSet();
-        Set<Integer> portSet = Sets.newHashSet();
+        Set<KubernetesServicePort> servicePorts = Sets.newHashSet();
         Set<String> endpointSet = Sets.newHashSet();
 
         service.getSpec().getPorts().forEach(servicePort -> {
-            nodePortSet.add(servicePort.getNodePort());
-            portSet.add(servicePort.getPort());
+            if (servicePort.getPort() != null && servicePort.getNodePort() != null) {
+                servicePorts.add(DefaultKubernetesServicePort.builder()
+                        .nodePort(servicePort.getNodePort())
+                        .port(servicePort.getPort()).build());
+            }
         });
 
         nodeService.completeNodes(WORKER).forEach(workerNode -> {
@@ -452,8 +450,7 @@
 
         return DefaultKubernetesExternalLb.builder().serviceName(serviceName)
                 .loadBalancerIp(IpAddress.valueOf(lbIp))
-                .nodePortSet(nodePortSet)
-                .portSet(portSet)
+                .servicePorts(servicePorts)
                 .endpointSet(endpointSet)
                 .loadBalancerGwIp(IpAddress.valueOf(loadbalancerGatewayIp))
                 .loadBalancerGwMac(loadBalancerGatewayMac)
diff --git a/apps/kubevirt-networking/app/src/main/java/org/onosproject/kubevirtnetworking/util/KubevirtNetworkingUtil.java b/apps/kubevirt-networking/app/src/main/java/org/onosproject/kubevirtnetworking/util/KubevirtNetworkingUtil.java
index 539323f..345e670 100644
--- a/apps/kubevirt-networking/app/src/main/java/org/onosproject/kubevirtnetworking/util/KubevirtNetworkingUtil.java
+++ b/apps/kubevirt-networking/app/src/main/java/org/onosproject/kubevirtnetworking/util/KubevirtNetworkingUtil.java
@@ -53,6 +53,7 @@
 import org.onosproject.kubevirtnetworking.api.KubevirtRouterService;
 import org.onosproject.kubevirtnode.api.DefaultKubevirtNode;
 import org.onosproject.kubevirtnode.api.DefaultKubevirtPhyInterface;
+import org.onosproject.kubevirtnode.api.KubernetesExternalLbConfig;
 import org.onosproject.kubevirtnode.api.KubernetesExternalLbInterface;
 import org.onosproject.kubevirtnode.api.KubevirtApiConfig;
 import org.onosproject.kubevirtnode.api.KubevirtApiConfigService;
@@ -557,16 +558,32 @@
                                                               KubernetesExternalLb externalLb) {
         //TODO: enhance election logic for a better load balancing
 
-        int numOfGateways = nodeService.completeNodes(GATEWAY).size();
+        int numOfGateways = nodeService.completeExternalLbGatewayNodes().size();
         if (numOfGateways == 0) {
             return null;
         }
 
-        return (KubevirtNode) nodeService.completeNodes(GATEWAY)
+        return (KubevirtNode) nodeService.completeExternalLbGatewayNodes()
                 .toArray()[externalLb.hashCode() % numOfGateways];
     }
 
     /**
+     * Returns whether a mac address in kubernetes external lb config is updated.
+     *
+     * @param externalLbConfig kubernetes external lb config
+     * @return true if a mac address is added
+     */
+    public static boolean configMapUpdated(KubernetesExternalLbConfig externalLbConfig) {
+        if (externalLbConfig == null) {
+            return false;
+        }
+
+        return externalLbConfig.configName() != null && externalLbConfig.globalIpRange() != null &&
+                externalLbConfig.loadBalancerGwIp() != null && externalLbConfig.loadBalancerGwMac() != null;
+    }
+
+
+    /**
      * Returns the worker node for the specified kubernetes external lb.
      * Among worker nodes, only one worker would serve the traffic from and to the gateway.
      * Currently worker node is selected based on modulo operation with external lb hashcode.
diff --git a/apps/kubevirt-node/api/src/main/java/org/onosproject/kubevirtnode/api/DefaultKubernetesExternalLbConfig.java b/apps/kubevirt-node/api/src/main/java/org/onosproject/kubevirtnode/api/DefaultKubernetesExternalLbConfig.java
index 1147ac7..082c1be 100644
--- a/apps/kubevirt-node/api/src/main/java/org/onosproject/kubevirtnode/api/DefaultKubernetesExternalLbConfig.java
+++ b/apps/kubevirt-node/api/src/main/java/org/onosproject/kubevirtnode/api/DefaultKubernetesExternalLbConfig.java
@@ -103,7 +103,7 @@
         return DefaultKubernetesExternalLbConfig.builder()
                 .configName(configName)
                 .loadBalancerGwIp(loadBalancerGwIp)
-                .loadBalancerGwMac(loadBalancerGwMac)
+                .loadBalancerGwMac(gatewayMac)
                 .globalIpRange(globalIpRange)
                 .build();
     }
diff --git a/apps/kubevirt-node/api/src/main/java/org/onosproject/kubevirtnode/api/DefaultKubernetesExternalLbInterface.java b/apps/kubevirt-node/api/src/main/java/org/onosproject/kubevirtnode/api/DefaultKubernetesExternalLbInterface.java
index fe53b53..676191e 100644
--- a/apps/kubevirt-node/api/src/main/java/org/onosproject/kubevirtnode/api/DefaultKubernetesExternalLbInterface.java
+++ b/apps/kubevirt-node/api/src/main/java/org/onosproject/kubevirtnode/api/DefaultKubernetesExternalLbInterface.java
@@ -109,7 +109,6 @@
             checkArgument(elbBridgeName != null, NOT_NULL_MSG, "externalLbBridgeName");
             checkArgument(elbIp != null, NOT_NULL_MSG, "externalLbIp");
             checkArgument(elbGwIp != null, NOT_NULL_MSG, "externalLbGwIp");
-            checkArgument(elbGwMac != null, NOT_NULL_MSG, "externalLbGwMac");
 
             return new DefaultKubernetesExternalLbInterface(elbBridgeName, elbIp, elbGwIp, elbGwMac);
         }
diff --git a/apps/kubevirt-node/api/src/main/java/org/onosproject/kubevirtnode/api/DefaultKubevirtNode.java b/apps/kubevirt-node/api/src/main/java/org/onosproject/kubevirtnode/api/DefaultKubevirtNode.java
index 2c3d0be..cc5e43a 100644
--- a/apps/kubevirt-node/api/src/main/java/org/onosproject/kubevirtnode/api/DefaultKubevirtNode.java
+++ b/apps/kubevirt-node/api/src/main/java/org/onosproject/kubevirtnode/api/DefaultKubevirtNode.java
@@ -193,6 +193,31 @@
     }
 
     @Override
+    public KubevirtNode updateKubernetesElbIntfGwMac(MacAddress macAddress) {
+
+        KubernetesExternalLbInterface externalLbInterface = DefaultKubernetesExternalLbInterface.builder()
+                .externalLbIp(this.kubernetesExternalLbIntf.externalLbIp())
+                .externalLbBridgeName(this.kubernetesExternalLbIntf.externalLbBridgeName())
+                .externallbGwIp(this.kubernetesExternalLbIntf.externalLbGwIp())
+                .externalLbGwMac(macAddress)
+                .build();
+
+        return new Builder()
+                .hostname(hostname)
+                .clusterName(clusterName)
+                .type(type)
+                .intgBridge(intgBridge)
+                .tunBridge(tunBridge)
+                .managementIp(managementIp)
+                .dataIp(dataIp)
+                .state(state)
+                .phyIntfs(phyIntfs)
+                .gatewayBridgeName(gatewayBridgeName)
+                .kubernetesExternalLbInterface(externalLbInterface)
+                .build();
+    }
+
+    @Override
     public Collection<KubevirtPhyInterface> phyIntfs() {
         if (phyIntfs == null) {
             return new ArrayList<>();
@@ -366,10 +391,6 @@
         private KubevirtNodeState state;
         private Collection<KubevirtPhyInterface> phyIntfs;
         private String gatewayBridgeName;
-        private String elbBridgeName;
-        private IpAddress elbIp;
-        private IpAddress elbGwIp;
-        private MacAddress elbGwMac;
         private KubernetesExternalLbInterface kubernetesExternalLbInterface;
 
         // private constructor not intended to use from external
diff --git a/apps/kubevirt-node/api/src/main/java/org/onosproject/kubevirtnode/api/KubevirtNode.java b/apps/kubevirt-node/api/src/main/java/org/onosproject/kubevirtnode/api/KubevirtNode.java
index 502784d..4e02997 100644
--- a/apps/kubevirt-node/api/src/main/java/org/onosproject/kubevirtnode/api/KubevirtNode.java
+++ b/apps/kubevirt-node/api/src/main/java/org/onosproject/kubevirtnode/api/KubevirtNode.java
@@ -16,6 +16,7 @@
 package org.onosproject.kubevirtnode.api;
 
 import org.onlab.packet.IpAddress;
+import org.onlab.packet.MacAddress;
 import org.onosproject.net.DeviceId;
 import org.onosproject.net.PortNumber;
 
@@ -140,6 +141,13 @@
     KubevirtNode updateTunBridge(DeviceId deviceId);
 
     /**
+     * Returns new kubevirt node instance with given kubernetes external lb intf gw mac.
+     * @param macAddress kubernetes external lb intf gw mac
+     * @return updated kubevirt node
+     */
+    KubevirtNode updateKubernetesElbIntfGwMac(MacAddress macAddress);
+
+    /**
      * Returns a collection of physical interfaces.
      *
      * @return physical interfaces
diff --git a/apps/kubevirt-node/api/src/main/java/org/onosproject/kubevirtnode/api/KubevirtNodeService.java b/apps/kubevirt-node/api/src/main/java/org/onosproject/kubevirtnode/api/KubevirtNodeService.java
index e9ed65b..118c97b 100644
--- a/apps/kubevirt-node/api/src/main/java/org/onosproject/kubevirtnode/api/KubevirtNodeService.java
+++ b/apps/kubevirt-node/api/src/main/java/org/onosproject/kubevirtnode/api/KubevirtNodeService.java
@@ -59,6 +59,12 @@
     Set<KubevirtNode> completeNodes(KubevirtNode.Type type);
 
     /**
+     * Returns all gateway nodes with complete state and external lb interface added.
+     * @return set of kubevirt nodes
+     */
+    Set<KubevirtNode> completeExternalLbGatewayNodes();
+
+    /**
      * Returns the node with the specified hostname.
      *
      * @param hostname hostname
diff --git a/apps/kubevirt-node/app/src/main/java/org/onosproject/kubevirtnode/impl/DistributedKubernetesExternalLbConfigStore.java b/apps/kubevirt-node/app/src/main/java/org/onosproject/kubevirtnode/impl/DistributedKubernetesExternalLbConfigStore.java
index 16a816e..bee1d7b 100644
--- a/apps/kubevirt-node/app/src/main/java/org/onosproject/kubevirtnode/impl/DistributedKubernetesExternalLbConfigStore.java
+++ b/apps/kubevirt-node/app/src/main/java/org/onosproject/kubevirtnode/impl/DistributedKubernetesExternalLbConfigStore.java
@@ -48,12 +48,9 @@
 import static com.google.common.base.Preconditions.checkArgument;
 import static java.util.concurrent.Executors.newSingleThreadExecutor;
 import static org.onlab.util.Tools.groupedThreads;
-import static org.onosproject.kubevirtnode.api.KubernetesExternalLbConfigEvent.Type
-        .KUBERNETES_EXTERNAL_LB_CONFIG_CREATED;
-import static org.onosproject.kubevirtnode.api.KubernetesExternalLbConfigEvent.Type
-        .KUBERNETES_EXTERNAL_LB_CONFIG_REMOVED;
-import static org.onosproject.kubevirtnode.api.KubernetesExternalLbConfigEvent.Type
-        .KUBERNETES_EXTERNAL_LB_CONFIG_UPDATED;
+import static org.onosproject.kubevirtnode.api.KubernetesExternalLbConfigEvent.Type.KUBERNETES_EXTERNAL_LB_CONFIG_CREATED;
+import static org.onosproject.kubevirtnode.api.KubernetesExternalLbConfigEvent.Type.KUBERNETES_EXTERNAL_LB_CONFIG_REMOVED;
+import static org.onosproject.kubevirtnode.api.KubernetesExternalLbConfigEvent.Type.KUBERNETES_EXTERNAL_LB_CONFIG_UPDATED;
 import static org.slf4j.LoggerFactory.getLogger;
 
 /**
@@ -128,7 +125,14 @@
         lbConfigStore.compute(lbConfig.configName(), (configName, existing) -> {
             final String error = lbConfig.configName() + ERR_NOT_FOUND;
             checkArgument(existing != null, error);
-            return lbConfig;
+
+            if (lbConfig.equals(existing) && lbConfig.loadBalancerGwMac() == null &&
+                    existing.loadBalancerGwMac() != null) {
+                return existing;
+            } else {
+                return lbConfig;
+            }
+
         });
     }
 
diff --git a/apps/kubevirt-node/app/src/main/java/org/onosproject/kubevirtnode/impl/KubevirtNodeManager.java b/apps/kubevirt-node/app/src/main/java/org/onosproject/kubevirtnode/impl/KubevirtNodeManager.java
index c2b9aad..4a148c8 100644
--- a/apps/kubevirt-node/app/src/main/java/org/onosproject/kubevirtnode/impl/KubevirtNodeManager.java
+++ b/apps/kubevirt-node/app/src/main/java/org/onosproject/kubevirtnode/impl/KubevirtNodeManager.java
@@ -24,6 +24,7 @@
 import org.onosproject.core.ApplicationId;
 import org.onosproject.core.CoreService;
 import org.onosproject.event.ListenerRegistry;
+import org.onosproject.kubevirtnode.api.KubernetesExternalLbInterface;
 import org.onosproject.kubevirtnode.api.KubevirtNode;
 import org.onosproject.kubevirtnode.api.KubevirtNodeAdminService;
 import org.onosproject.kubevirtnode.api.KubevirtNodeEvent;
@@ -59,6 +60,7 @@
 import static org.onlab.util.Tools.groupedThreads;
 import static org.onosproject.kubevirtnode.api.Constants.INTEGRATION_BRIDGE;
 import static org.onosproject.kubevirtnode.api.Constants.TUNNEL_BRIDGE;
+import static org.onosproject.kubevirtnode.api.KubevirtNode.Type.GATEWAY;
 import static org.onosproject.kubevirtnode.impl.OsgiPropertyConstants.OVSDB_PORT;
 import static org.onosproject.kubevirtnode.impl.OsgiPropertyConstants.OVSDB_PORT_NUM_DEFAULT;
 import static org.onosproject.kubevirtnode.util.KubevirtNodeUtil.genDpidFromName;
@@ -275,6 +277,23 @@
     }
 
     @Override
+    public Set<KubevirtNode> completeExternalLbGatewayNodes() {
+        Set<KubevirtNode> nodes = nodeStore.nodes().stream()
+                .filter(node -> node.type() == GATEWAY &&
+                        node.state() == KubevirtNodeState.COMPLETE)
+                .filter(node -> {
+                    KubernetesExternalLbInterface externalLbInterface = node.kubernetesExternalLbInterface();
+
+                    if (externalLbInterface != null) {
+                        return true;
+                    }
+                    return false;
+                })
+                .collect(Collectors.toSet());
+        return ImmutableSet.copyOf(nodes);
+    }
+
+    @Override
     public KubevirtNode node(String hostname) {
         return nodeStore.node(hostname);
     }