Support LB in kubevirt networking application.

Change-Id: I59578fc0b778173a20c958b641cf9bf1abe69bc6
(cherry picked from commit c8a8759e3adc292a429a4ca7173d15c220b7ea3c)
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 04ac0c8..c6a9179 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
@@ -68,6 +68,7 @@
     public static final int PRIORITY_ICMP_RULE = 43000;
     public static final int PRIORITY_FORWARDING_RULE = 30000;
     public static final int PRIORITY_FLOATING_GATEWAY_TUN_BRIDGE_RULE = 32000;
+    public static final int PRIORITY_LB_GATEWAY_TUN_BRIDGE_RULE = 33000;
     public static final int PRIORITY_DHCP_RULE = 42000;
     public static final int PRIORITY_ARP_GATEWAY_RULE = 41000;
     public static final int PRIORITY_ARP_DEFAULT_RULE = 40000;
@@ -104,4 +105,6 @@
     public static final int PRIORITY_STATEFUL_SNAT_RULE = 40500;
     public static final int PRIORITY_FLOATING_IP_RULE = 40800;
     public static final int PRIORITY_INTERNAL_ROUTING_RULE = 41000;
+    public static final int PRIORITY_LB_RULE = 41500;
+    public static final int PRIORITY_LB_FIP_RULE = 41500;
 }
diff --git a/apps/kubevirt-networking/api/src/main/java/org/onosproject/kubevirtnetworking/api/DefaultKubevirtLoadBalancerRule.java b/apps/kubevirt-networking/api/src/main/java/org/onosproject/kubevirtnetworking/api/DefaultKubevirtLoadBalancerRule.java
index 453610e..bd2f64a 100644
--- a/apps/kubevirt-networking/api/src/main/java/org/onosproject/kubevirtnetworking/api/DefaultKubevirtLoadBalancerRule.java
+++ b/apps/kubevirt-networking/api/src/main/java/org/onosproject/kubevirtnetworking/api/DefaultKubevirtLoadBalancerRule.java
@@ -26,6 +26,7 @@
  */
 public final class DefaultKubevirtLoadBalancerRule implements KubevirtLoadBalancerRule {
     private static final String NOT_NULL_MSG = "Load Balancer Rule % cannot be null";
+    private static final String ICMP = "ICMP";
 
     private final String protocol;
     private final Integer portRangeMax;
@@ -70,9 +71,10 @@
             return false;
         }
         DefaultKubevirtLoadBalancerRule that = (DefaultKubevirtLoadBalancerRule) o;
+
         return protocol.equals(that.protocol) &&
-                portRangeMax.equals(that.portRangeMax) &&
-                portRangeMin.equals(that.portRangeMin);
+                Objects.equals(portRangeMax, that.portRangeMax) &&
+                Objects.equals(portRangeMin, that.portRangeMin);
     }
 
     @Override
@@ -107,9 +109,11 @@
         @Override
         public KubevirtLoadBalancerRule build() {
             checkArgument(protocol != null, NOT_NULL_MSG, "protocol");
-            checkArgument(portRangeMax != null, NOT_NULL_MSG, "portRangeMax");
-            checkArgument(portRangeMin != null, NOT_NULL_MSG, "portRangeMin");
 
+            if (!protocol.equalsIgnoreCase(ICMP)) {
+                checkArgument(portRangeMax != null, NOT_NULL_MSG, "portRangeMax");
+                checkArgument(portRangeMin != null, NOT_NULL_MSG, "portRangeMin");
+            }
             return new DefaultKubevirtLoadBalancerRule(protocol, portRangeMax, portRangeMin);
         }
 
diff --git a/apps/kubevirt-networking/api/src/main/java/org/onosproject/kubevirtnetworking/api/KubevirtGroupRuleService.java b/apps/kubevirt-networking/api/src/main/java/org/onosproject/kubevirtnetworking/api/KubevirtGroupRuleService.java
new file mode 100644
index 0000000..9d54fd3
--- /dev/null
+++ b/apps/kubevirt-networking/api/src/main/java/org/onosproject/kubevirtnetworking/api/KubevirtGroupRuleService.java
@@ -0,0 +1,77 @@
+/*
+ * Copyright 2021-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 org.onosproject.core.ApplicationId;
+import org.onosproject.net.DeviceId;
+import org.onosproject.net.group.GroupBucket;
+import org.onosproject.net.group.GroupDescription;
+
+import java.util.List;
+
+/**
+ * Service for setting group rules.
+ */
+public interface KubevirtGroupRuleService {
+
+    /**
+     * Configures the group table rule.
+     *
+     * @param appId         application ID
+     * @param deviceId      device ID
+     * @param groupId       group ID
+     * @param type          group type
+     * @param buckets       a list of group buckets
+     * @param install       true for rule addition, false for rule removal
+     */
+    void setRule(ApplicationId appId, DeviceId deviceId, int groupId,
+                 GroupDescription.Type type, List<GroupBucket> buckets, boolean install);
+
+    /**
+     * Checks whether has the group in store with given device ID and group ID.
+     *
+     * @param deviceId      device ID
+     * @param groupId       group ID
+     * @return true if the group exists, false otherwise
+     */
+    boolean hasGroup(DeviceId deviceId, int groupId);
+
+    /**
+     * Configures buckets to the existing group.
+     * With install flag true, this method will add buckets to existing buckets,
+     * while with install flag false, this method will remove buckets from
+     * existing buckets.
+     *
+     * @param appId         application ID
+     * @param deviceId      device ID
+     * @param groupId       group ID
+     * @param buckets       a list of group buckets
+     * @param install       true for buckets addition, false for buckets removal
+     */
+    void setBuckets(ApplicationId appId, DeviceId deviceId, int groupId,
+                    List<GroupBucket> buckets, boolean install);
+
+    /**
+     * Configures buckets.
+     *
+     * @param appId         application ID
+     * @param deviceId      device ID
+     * @param groupId       group ID
+     * @param buckets       a lit of group buckets
+     */
+    void setBuckets(ApplicationId appId, DeviceId deviceId, int groupId,
+                    List<GroupBucket> buckets);
+}
diff --git a/apps/kubevirt-networking/api/src/main/java/org/onosproject/kubevirtnetworking/api/KubevirtLoadBalancerEvent.java b/apps/kubevirt-networking/api/src/main/java/org/onosproject/kubevirtnetworking/api/KubevirtLoadBalancerEvent.java
index ad09091..e80d1a7 100644
--- a/apps/kubevirt-networking/api/src/main/java/org/onosproject/kubevirtnetworking/api/KubevirtLoadBalancerEvent.java
+++ b/apps/kubevirt-networking/api/src/main/java/org/onosproject/kubevirtnetworking/api/KubevirtLoadBalancerEvent.java
@@ -15,11 +15,17 @@
  */
 package org.onosproject.kubevirtnetworking.api;
 
+import org.onlab.packet.IpAddress;
 import org.onosproject.event.AbstractEvent;
 
+import java.util.Set;
+
 public class KubevirtLoadBalancerEvent
         extends AbstractEvent<KubevirtLoadBalancerEvent.Type, KubevirtLoadBalancer> {
 
+    private final KubevirtLoadBalancer old;
+    private final Set<IpAddress> members;
+
     /**
      * LoadBalancerEvent constructor.
      *
@@ -28,6 +34,52 @@
      */
     public KubevirtLoadBalancerEvent(Type type, KubevirtLoadBalancer lb) {
         super(type, lb);
+        this.old = null;
+        this.members = null;
+    }
+
+    /**
+     * Creates and event of a given type for the specified kubevirt loadbalancer.
+     *
+     * @param type kubevirt loadbalancer event type
+     * @param lb kubevirt loadbalancer
+     * @param old old kubevirt loadbalancer
+     */
+    public KubevirtLoadBalancerEvent(Type type, KubevirtLoadBalancer lb, KubevirtLoadBalancer old) {
+        super(type, lb);
+        this.old = old;
+        this.members = null;
+    }
+
+    /**
+     * Creates and event of a given type for the specified kubevirt loadbalancer.
+     *
+     * @param type kubevirt loadbalancer event type
+     * @param lb kubevirt loadbalancer
+     * @param members kubevirt loadbalancer members
+     */
+    public KubevirtLoadBalancerEvent(Type type, KubevirtLoadBalancer lb, Set<IpAddress> members) {
+        super(type, lb);
+        this.old = null;
+        this.members = members;
+    }
+
+    /**
+     * Returns the old kubevirt loadbalancer of the event.
+     *
+     * @return old kubevirt loadbalancer
+     */
+    public KubevirtLoadBalancer oldLb() {
+        return old;
+    }
+
+    /**
+     * Returns members of kubevirt loadbalancer of the event.
+     *
+     * @return kubevirt loadbalancer members
+     */
+    public Set<IpAddress> members() {
+        return members;
     }
 
     public enum Type {
@@ -37,13 +89,23 @@
         KUBEVIRT_LOAD_BALANCER_CREATED,
 
         /**
-         * Signifies that a new kubevirt load balancer is removed.
+         * Signifies that a kubevirt load balancer is removed.
          */
         KUBEVIRT_LOAD_BALANCER_REMOVED,
 
         /**
-         * Signifies that a new kubevirt load balancer is updated.
+         * Signifies that a kubevirt load balancer is updated.
          */
         KUBEVIRT_LOAD_BALANCER_UPDATED,
+
+        /**
+         * Signifies that a kubevirt load balancer member is added.
+         */
+        KUBEVIRT_LOAD_BALANCER_MEMBER_ADDED,
+
+        /**
+         * Signifies that a kubevirt load balancer member is added.
+         */
+        KUBEVIRT_LOAD_BALANCER_MEMBER_REMOVED,
     }
 }
diff --git a/apps/kubevirt-networking/api/src/main/java/org/onosproject/kubevirtnetworking/api/KubevirtRouterEvent.java b/apps/kubevirt-networking/api/src/main/java/org/onosproject/kubevirtnetworking/api/KubevirtRouterEvent.java
index ccd927d..4cd31eb 100644
--- a/apps/kubevirt-networking/api/src/main/java/org/onosproject/kubevirtnetworking/api/KubevirtRouterEvent.java
+++ b/apps/kubevirt-networking/api/src/main/java/org/onosproject/kubevirtnetworking/api/KubevirtRouterEvent.java
@@ -225,7 +225,19 @@
         /**
          * Signified that the snat status disabled for this router.
          */
-        KUBEVIRT_SNAT_STATUS_DISABLED
+        KUBEVIRT_SNAT_STATUS_DISABLED,
+        /**
+         * Signifies that the floating IP is associated to a lb VIP.
+         */
+        KUBEVIRT_FLOATING_IP_LB_ASSOCIATED,
+        /**
+         * Signifies that the floating IP is disassociated to a lb VIP.
+         */
+        KUBEVIRT_FLOATING_IP_LB_DISASSOCIATED,
+        /**
+         * Signifies the that peer router mac address is retrieved for this router.
+         */
+        KUBEVIRT_PEER_ROUTER_MAC_RETRIEVED,
     }
 
     /**