Initial commit to implement external loadbalancer for kubernetes pods.
- Adds elb-related parameters in KubevirtNode class
- Defines KubernetesExternalLb and related service, manager and store.
Change-Id: I68476887af493c7daeaf1dca0320218f1764653f
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
new file mode 100644
index 0000000..96ecf12
--- /dev/null
+++ b/apps/kubevirt-networking/api/src/main/java/org/onosproject/kubevirtnetworking/api/DefaultKubernetesExternalLb.java
@@ -0,0 +1,187 @@
+/*
+ * 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 com.google.common.collect.ImmutableSet;
+
+import java.util.Objects;
+import java.util.Set;
+
+import static com.google.common.base.Preconditions.checkArgument;
+
+/**
+ * Default implementation of kubernetes external load balancer.
+ */
+public final class DefaultKubernetesExternalLb implements KubernetesExternalLb {
+
+ private static final String NOT_NULL_MSG = "Loadbalancer % cannot be null";
+
+ private final String serviceName;
+ private final String loadbalancerIp;
+ private final Set<Integer> nodePortSet;
+ private final Set<Integer> portSet;
+ private final Set<String> endpointSet;
+ private final String electedGateway;
+
+ public DefaultKubernetesExternalLb(String serviceName, String loadbalancerIp,
+ Set<Integer> nodePortSet, Set<Integer> portSet,
+ Set<String> endpointSet, String electedGateway) {
+ this.serviceName = serviceName;
+ this.loadbalancerIp = loadbalancerIp;
+ this.nodePortSet = nodePortSet;
+ this.portSet = portSet;
+ this.endpointSet = endpointSet;
+ this.electedGateway = electedGateway;
+ }
+
+ @Override
+ public String serviceName() {
+ return serviceName;
+ }
+
+ @Override
+ public String loadBalancerIp() {
+ return loadbalancerIp;
+ }
+
+ @Override
+ public Set<Integer> nodePortSet() {
+ return ImmutableSet.copyOf(nodePortSet);
+ }
+
+ @Override
+ public Set<Integer> portSet() {
+ return ImmutableSet.copyOf(portSet);
+ }
+
+ @Override
+ public Set<String> endpointSet() {
+ return ImmutableSet.copyOf(endpointSet);
+ }
+
+ @Override
+ public String electedGateway() {
+ return electedGateway;
+ }
+
+ @Override
+ public boolean equals(Object o) {
+ if (this == o) {
+ return true;
+ }
+ if (o == null || getClass() != o.getClass()) {
+ return false;
+ }
+
+ 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(endpointSet, that.endpointSet) &&
+ Objects.equals(electedGateway, that.electedGateway);
+ }
+
+ @Override
+ public KubernetesExternalLb updateElectedGateway(String electedGateway) {
+ return DefaultKubernetesExternalLb.builder()
+ .serviceName(serviceName)
+ .loadBalancerIp(loadbalancerIp)
+ .nodePortSet(nodePortSet)
+ .portSet(portSet)
+ .endpointSet(endpointSet)
+ .electedGateway(electedGateway)
+ .build();
+ }
+ @Override
+ public int hashCode() {
+ return Objects.hash(serviceName, loadbalancerIp);
+ }
+
+ @Override
+ public String toString() {
+ return MoreObjects.toStringHelper(this)
+ .add("serviceName", serviceName)
+ .add("loadbalancerIp", loadbalancerIp)
+ .add("nodePort", nodePortSet)
+ .add("port", portSet)
+ .add("endpointSet", endpointSet)
+ .add("electedGateway", electedGateway)
+ .toString();
+ }
+
+ public static Builder builder() {
+ return new Builder();
+ }
+
+ public static final class Builder implements KubernetesExternalLb.Builder {
+ private String serviceName;
+ private String loadbalancerIp;
+ private Set<Integer> nodePortSet;
+ private Set<Integer> portSet;
+ private Set<String> endpointSet;
+ private String electedGateway;
+
+ private Builder() {
+ }
+
+ @Override
+ 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");
+
+ return new DefaultKubernetesExternalLb(serviceName, loadbalancerIp,
+ nodePortSet, portSet, endpointSet, electedGateway);
+ }
+
+ @Override
+ public Builder serviceName(String serviceName) {
+ this.serviceName = serviceName;
+ return this;
+ }
+
+ @Override
+ public Builder loadBalancerIp(String loadBalancerIp) {
+ this.loadbalancerIp = loadBalancerIp;
+ return this;
+ }
+
+ @Override
+ public Builder nodePortSet(Set<Integer> nodePortSet) {
+ this.nodePortSet = nodePortSet;
+ return this;
+ }
+
+ @Override
+ public Builder portSet(Set<Integer> portSet) {
+ this.portSet = portSet;
+ return this;
+ }
+
+ @Override
+ public Builder endpointSet(Set<String> endpointSet) {
+ this.endpointSet = endpointSet;
+ return this;
+ }
+
+ public Builder electedGateway(String electedGateway) {
+ this.electedGateway = electedGateway;
+ 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
new file mode 100644
index 0000000..bc754db
--- /dev/null
+++ b/apps/kubevirt-networking/api/src/main/java/org/onosproject/kubevirtnetworking/api/KubernetesExternalLb.java
@@ -0,0 +1,130 @@
+/*
+ * 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 java.util.Set;
+
+/**
+ * Representation of kubernetes external load balancer.
+ */
+public interface KubernetesExternalLb {
+ /**
+ * Returns the service name.
+ *
+ * @return service name
+ */
+ String serviceName();
+
+ /**
+ * Returns the load balancer IP.
+ *
+ * @return load balancer IP
+ */
+ String loadBalancerIp();
+
+ /**
+ * Returns the set of node port.
+ *
+ * @return node port
+ */
+ Set<Integer> nodePortSet();
+
+ /**
+ * Returns the set of port.
+ *
+ * @return port number
+ */
+ Set<Integer> portSet();
+
+ /**
+ * Returns the set of endpoint.
+ *
+ * @return endpoint set
+ */
+ Set<String> endpointSet();
+
+ /**
+ * Returns the elected gateway node for this service.
+ *
+ * @return gateway node hostname
+ */
+ String electedGateway();
+
+ /**
+ * Updates the elected gateway node host name.
+ *
+ * @param electedGateway updated elected gateway node hostname
+ * @return kubernetes external lb with the updated gateway node hostname
+ */
+ KubernetesExternalLb updateElectedGateway(String electedGateway);
+
+ interface Builder {
+ /**
+ * Builds an immutable kubernetes external load balancer instance.
+ *
+ * @return kubernetes external load balancer
+ */
+ KubernetesExternalLb build();
+
+ /**
+ * Returns kubernetes external load balancer builder with supplied service name.
+ *
+ * @param serviceName external load balancer service name
+ * @return external load balancer builder
+ */
+ Builder serviceName(String serviceName);
+
+ /**
+ * Returns kubernetes external load balancer builder with supplied load balancer Ip.
+ *
+ * @param loadBalancerIp external load balancer Ip
+ * @return external load balancer builder
+ */
+ Builder loadBalancerIp(String 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.
+ *
+ * @param portSet port set
+ * @return external load balancer builder
+ */
+ Builder portSet(Set<Integer> portSet);
+
+ /**
+ * Returns kubernetes external load balancer builder with supplied endpoint set.
+ *
+ * @param endpointSet endpoint set
+ * @return external load balancer builder
+ */
+ Builder endpointSet(Set<String> endpointSet);
+
+ /**
+ * Returns kubernetes external load balancer builder with supplied elected gateway.
+ *
+ * @param gateway gateway node hostname
+ * @return gateway node hostname
+ */
+ Builder electedGateway(String gateway);
+ }
+}
diff --git a/apps/kubevirt-networking/api/src/main/java/org/onosproject/kubevirtnetworking/api/KubernetesExternalLbAdminService.java b/apps/kubevirt-networking/api/src/main/java/org/onosproject/kubevirtnetworking/api/KubernetesExternalLbAdminService.java
new file mode 100644
index 0000000..c53d409
--- /dev/null
+++ b/apps/kubevirt-networking/api/src/main/java/org/onosproject/kubevirtnetworking/api/KubernetesExternalLbAdminService.java
@@ -0,0 +1,47 @@
+/*
+ * 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;
+
+/**
+ * Service for administering the inventory of kubernetes external lb service.
+ */
+public interface KubernetesExternalLbAdminService extends KubernetesExternalLbService {
+ /**
+ * Create a kubernetes external load balancer with the given information.
+ *
+ * @param externalLb a new load balancer
+ */
+ void createExternalLb(KubernetesExternalLb externalLb);
+
+ /**
+ * Update a kubernetes external load balancer with the given information.
+ *
+ * @param externalLb the updated load balancer
+ */
+ void updateExternalLb(KubernetesExternalLb externalLb);
+
+ /**
+ * Removes the load balancer.
+ *
+ * @param serviceName load balancer name
+ */
+ void removeExternalLb(String serviceName);
+
+ /**
+ * Removes all load balancers.
+ */
+ void clear();
+}
diff --git a/apps/kubevirt-networking/api/src/main/java/org/onosproject/kubevirtnetworking/api/KubernetesExternalLbEvent.java b/apps/kubevirt-networking/api/src/main/java/org/onosproject/kubevirtnetworking/api/KubernetesExternalLbEvent.java
new file mode 100644
index 0000000..15f3905
--- /dev/null
+++ b/apps/kubevirt-networking/api/src/main/java/org/onosproject/kubevirtnetworking/api/KubernetesExternalLbEvent.java
@@ -0,0 +1,49 @@
+/*
+ * 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 org.onosproject.event.AbstractEvent;
+
+/**
+ * Manages inventory of kubernetes external load balancer states; not intended for direct use.
+ */
+public class KubernetesExternalLbEvent
+ extends AbstractEvent<KubernetesExternalLbEvent.Type, KubernetesExternalLb> {
+
+ public KubernetesExternalLbEvent(Type type, KubernetesExternalLb subject) {
+ super(type, subject);
+ }
+
+ /**
+ * Kubernetes external lb events.
+ */
+ public enum Type {
+ /**
+ * Signifies that a new kubevirt load balancer is created.
+ */
+ KUBERNETES_EXTERNAL_LOAD_BALANCER_CREATED,
+
+ /**
+ * Signifies that a kubevirt load balancer is removed.
+ */
+ KUBERNETES_EXTERNAL_LOAD_BALANCER_REMOVED,
+
+ /**
+ * Signifies that a kubevirt load balancer is updated.
+ */
+ KUBERNETES_EXTERNAL_LOAD_BALANCER_UPDATED,
+ }
+}
diff --git a/apps/kubevirt-networking/api/src/main/java/org/onosproject/kubevirtnetworking/api/KubernetesExternalLbListener.java b/apps/kubevirt-networking/api/src/main/java/org/onosproject/kubevirtnetworking/api/KubernetesExternalLbListener.java
new file mode 100644
index 0000000..1a48764
--- /dev/null
+++ b/apps/kubevirt-networking/api/src/main/java/org/onosproject/kubevirtnetworking/api/KubernetesExternalLbListener.java
@@ -0,0 +1,24 @@
+/*
+ * 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 org.onosproject.event.EventListener;
+
+/**
+ * Listener for kubernetes external lb event.
+ */
+public interface KubernetesExternalLbListener extends EventListener<KubernetesExternalLbEvent> {
+}
diff --git a/apps/kubevirt-networking/api/src/main/java/org/onosproject/kubevirtnetworking/api/KubernetesExternalLbService.java b/apps/kubevirt-networking/api/src/main/java/org/onosproject/kubevirtnetworking/api/KubernetesExternalLbService.java
new file mode 100644
index 0000000..eeac4f2
--- /dev/null
+++ b/apps/kubevirt-networking/api/src/main/java/org/onosproject/kubevirtnetworking/api/KubernetesExternalLbService.java
@@ -0,0 +1,39 @@
+/*
+ * 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 org.onosproject.event.ListenerService;
+
+import java.util.Set;
+
+public interface KubernetesExternalLbService
+ extends ListenerService<KubernetesExternalLbEvent, KubernetesExternalLbListener> {
+
+ /**
+ * Returns the kubernetes external lb with the supplied service name.
+ *
+ * @param serviceName service name
+ * @return kubernetes external load balancer
+ */
+ KubernetesExternalLb loadBalancer(String serviceName);
+
+ /**
+ * Returns all kubernetes external lb's registered in the service.
+ *
+ * @return set of kubernetes external lb's
+ */
+ Set<KubernetesExternalLb> loadBalancers();
+}
diff --git a/apps/kubevirt-networking/api/src/main/java/org/onosproject/kubevirtnetworking/api/KubernetesExternalLbStore.java b/apps/kubevirt-networking/api/src/main/java/org/onosproject/kubevirtnetworking/api/KubernetesExternalLbStore.java
new file mode 100644
index 0000000..996cbf0
--- /dev/null
+++ b/apps/kubevirt-networking/api/src/main/java/org/onosproject/kubevirtnetworking/api/KubernetesExternalLbStore.java
@@ -0,0 +1,69 @@
+/*
+ * 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 org.onosproject.store.Store;
+
+import java.util.Set;
+
+/**
+ * Manages inventory of kubernetes external load balancer states; not intended for direct use.
+ */
+public interface KubernetesExternalLbStore
+ extends Store<KubernetesExternalLbEvent, KubernetesExternalLbStoreDelegate> {
+
+ /**
+ * Creates a new kubernetes external lb.
+ *
+ * @param lb kubernetes external lb
+ */
+ void createLoadBalancer(KubernetesExternalLb lb);
+
+ /**
+ * Updates a new kubernetes external lb.
+ *
+ * @param lb kubernetes external lb
+ */
+ void updateLoadBalancer(KubernetesExternalLb lb);
+
+ /**
+ * Removes the kubernetes external lb with the given lb name.
+ *
+ * @param serviceName service name
+ * @return kubernetes external lb
+ */
+ KubernetesExternalLb removeLoadBalancer(String serviceName);
+
+ /**
+ * Returns the kubernetes external lb with the given lb name.
+ *
+ * @param serviceName service name
+ * @return kubernetes external lb
+ */
+ KubernetesExternalLb loadBalancer(String serviceName);
+
+ /**
+ * Returns all kubernetes external lbs.
+ *
+ * @return set of kubernetes external lbs
+ */
+ Set<KubernetesExternalLb> loadBalancers();
+
+ /**
+ * Removes all kubernetes external lbs.
+ */
+ void clear();
+}
diff --git a/apps/kubevirt-networking/api/src/main/java/org/onosproject/kubevirtnetworking/api/KubernetesExternalLbStoreDelegate.java b/apps/kubevirt-networking/api/src/main/java/org/onosproject/kubevirtnetworking/api/KubernetesExternalLbStoreDelegate.java
new file mode 100644
index 0000000..a7fba41
--- /dev/null
+++ b/apps/kubevirt-networking/api/src/main/java/org/onosproject/kubevirtnetworking/api/KubernetesExternalLbStoreDelegate.java
@@ -0,0 +1,24 @@
+/*
+ * 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 org.onosproject.store.StoreDelegate;
+
+/**
+ * Kubernetes external lb store delegate abstraction.
+ */
+public interface KubernetesExternalLbStoreDelegate extends StoreDelegate<KubernetesExternalLbEvent> {
+}
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
new file mode 100644
index 0000000..a4a661f
--- /dev/null
+++ b/apps/kubevirt-networking/api/src/test/java/org/onosproject/kubevirtnetworking/api/DefaultKubernetesExternalLbTest.java
@@ -0,0 +1,113 @@
+/*
+ * 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 com.google.common.collect.Sets;
+import com.google.common.testing.EqualsTester;
+import org.junit.Before;
+import org.junit.Test;
+
+import java.util.Set;
+
+import static junit.framework.TestCase.assertEquals;
+import static org.onlab.junit.ImmutableClassChecker.assertThatClassIsImmutable;
+
+/**
+ * Unit tests for the default Kubernetes external lb class.
+ */
+public class DefaultKubernetesExternalLbTest {
+ private static final String SERVICE_NAME_1 = "service_name_1";
+ private static final String SERVICE_NAME_2 = "service_name_2";
+ private static final String LOADBALANCER_IP_1 = "1.1.1.1";
+ private static final String LOADBALANCER_IP_2 = "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 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";
+ private static final String ELECTED_GATEWAY_2 = "gateway2";
+
+ private KubernetesExternalLb lb1;
+ private KubernetesExternalLb sameAsLb1;
+ private KubernetesExternalLb lb2;
+
+ /**
+ * Tests class immutability.
+ */
+ @Test
+ public void testImmutability() {
+ assertThatClassIsImmutable(DefaultKubernetesExternalLb.class);
+ }
+
+ /**
+ * Initial setup for this unit test.
+ */
+ @Before
+ public void setUp() {
+ lb1 = DefaultKubernetesExternalLb.builder()
+ .serviceName(SERVICE_NAME_1)
+ .loadBalancerIp(LOADBALANCER_IP_1)
+ .nodePortSet(NODE_PORT_SET_1)
+ .portSet(PORT_SET_1)
+ .endpointSet(ENDPOINT_SET_1)
+ .electedGateway(ELECTED_GATEWAY_1)
+ .build();
+
+ sameAsLb1 = DefaultKubernetesExternalLb.builder()
+ .serviceName(SERVICE_NAME_1)
+ .loadBalancerIp(LOADBALANCER_IP_1)
+ .nodePortSet(NODE_PORT_SET_1)
+ .portSet(PORT_SET_1)
+ .endpointSet(ENDPOINT_SET_1)
+ .electedGateway(ELECTED_GATEWAY_1)
+ .build();
+
+ lb2 = DefaultKubernetesExternalLb.builder()
+ .serviceName(SERVICE_NAME_2)
+ .loadBalancerIp(LOADBALANCER_IP_2)
+ .nodePortSet(NODE_PORT_SET_2)
+ .portSet(PORT_SET_2)
+ .endpointSet(ENDPOINT_SET_2)
+ .electedGateway(ELECTED_GATEWAY_2)
+ .build();
+ }
+
+ /**
+ * Tests object equality.
+ */
+ @Test
+ public void testEquality() {
+ new EqualsTester().addEqualityGroup(lb1, sameAsLb1)
+ .addEqualityGroup(lb2)
+ .testEquals();
+ }
+
+ /**
+ * Test object construction.
+ */
+ @Test
+ public void testConstruction() {
+ KubernetesExternalLb lb = lb1;
+
+ assertEquals(SERVICE_NAME_1, lb1.serviceName());
+ assertEquals(LOADBALANCER_IP_1, lb1.loadBalancerIp());
+ assertEquals(NODE_PORT_SET_1, lb1.nodePortSet());
+ assertEquals(PORT_SET_1, lb1.portSet());
+ assertEquals(ENDPOINT_SET_1, lb1.endpointSet());
+ }
+}
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
new file mode 100644
index 0000000..fce69d7
--- /dev/null
+++ b/apps/kubevirt-networking/app/src/main/java/org/onosproject/kubevirtnetworking/impl/DistributedKubernetesExternalLbStore.java
@@ -0,0 +1,179 @@
+/*
+ * 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 com.google.common.collect.ImmutableSet;
+import org.onlab.util.KryoNamespace;
+import org.onosproject.core.ApplicationId;
+import org.onosproject.core.CoreService;
+import org.onosproject.kubevirtnetworking.api.DefaultKubernetesExternalLb;
+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.store.AbstractStore;
+import org.onosproject.store.serializers.KryoNamespaces;
+import org.onosproject.store.service.ConsistentMap;
+import org.onosproject.store.service.MapEvent;
+import org.onosproject.store.service.MapEventListener;
+import org.onosproject.store.service.Serializer;
+import org.onosproject.store.service.StorageService;
+import org.onosproject.store.service.Versioned;
+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.util.Collection;
+import java.util.Set;
+import java.util.concurrent.ExecutorService;
+
+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.kubevirtnetworking.api.KubernetesExternalLbEvent.Type.KUBERNETES_EXTERNAL_LOAD_BALANCER_CREATED;
+import static org.onosproject.kubevirtnetworking.api.KubernetesExternalLbEvent.Type.KUBERNETES_EXTERNAL_LOAD_BALANCER_REMOVED;
+import static org.onosproject.kubevirtnetworking.api.KubernetesExternalLbEvent.Type.KUBERNETES_EXTERNAL_LOAD_BALANCER_UPDATED;
+import static org.slf4j.LoggerFactory.getLogger;
+
+/**
+ * Implementation of kubernetes external lb store using consistent map.
+ */
+@Component(immediate = true, service = KubernetesExternalLbStore.class)
+public class DistributedKubernetesExternalLbStore
+ extends AbstractStore<KubernetesExternalLbEvent, KubernetesExternalLbStoreDelegate>
+ implements KubernetesExternalLbStore {
+
+ private final Logger log = getLogger(getClass());
+
+ private static final String ERR_NOT_FOUND = " does not exist";
+ private static final String ERR_DUPLICATE = " already exists";
+ private static final String APP_ID = "org.onosproject.kubevirtnetwork";
+
+ private static final KryoNamespace SERIALIZER_KUBERNETES_EXTERNAL_LB = KryoNamespace.newBuilder()
+ .register(KryoNamespaces.API)
+ .register(KubernetesExternalLb.class)
+ .register(DefaultKubernetesExternalLb.class)
+ .register(Collection.class)
+ .build();
+
+ @Reference(cardinality = ReferenceCardinality.MANDATORY)
+ protected CoreService coreService;
+
+ @Reference(cardinality = ReferenceCardinality.MANDATORY)
+ protected StorageService storageService;
+
+ private final ExecutorService eventExecutor = newSingleThreadExecutor(
+ groupedThreads(this.getClass().getSimpleName(), "event-handler", log));
+
+ private final MapEventListener<String, KubernetesExternalLb> lbMapEventListener =
+ new KubernetesExternalLbMapListener();
+
+ private ConsistentMap<String, KubernetesExternalLb> lbStore;
+
+ @Activate
+ protected void activate() {
+ ApplicationId appId = coreService.registerApplication(APP_ID);
+ lbStore = storageService.<String, KubernetesExternalLb>consistentMapBuilder()
+ .withSerializer(Serializer.using(SERIALIZER_KUBERNETES_EXTERNAL_LB))
+ .withName("kubernetes-lbstore")
+ .withApplicationId(appId)
+ .build();
+
+ lbStore.addListener(lbMapEventListener);
+ log.info("Started");
+ }
+
+ @Deactivate
+ protected void deactivate() {
+ lbStore.removeListener(lbMapEventListener);
+ eventExecutor.shutdown();
+ log.info("Stopped");
+ }
+
+ @Override
+ public void createLoadBalancer(KubernetesExternalLb lb) {
+ lbStore.compute(lb.serviceName(), (seviceName, existing) -> {
+ final String error = lb.serviceName() + ERR_DUPLICATE;
+ checkArgument(existing == null, error);
+ return lb;
+ });
+ }
+
+ @Override
+ public void updateLoadBalancer(KubernetesExternalLb lb) {
+ lbStore.compute(lb.serviceName(), (seviceName, existing) -> {
+ final String error = lb.serviceName() + ERR_NOT_FOUND;
+ checkArgument(existing != null, error);
+ return lb;
+ });
+ }
+
+ @Override
+ public KubernetesExternalLb removeLoadBalancer(String serviceName) {
+
+ Versioned<KubernetesExternalLb> lb = lbStore.remove(serviceName);
+
+ if (lb == null) {
+ final String error = serviceName + ERR_NOT_FOUND;
+ throw new IllegalArgumentException(error);
+ }
+ return lb.value();
+ }
+
+ @Override
+ public KubernetesExternalLb loadBalancer(String serviceName) {
+ return lbStore.asJavaMap().get(serviceName);
+ }
+
+ @Override
+ public Set<KubernetesExternalLb> loadBalancers() {
+ return ImmutableSet.copyOf(lbStore.asJavaMap().values());
+ }
+
+ @Override
+ public void clear() {
+ lbStore.clear();
+ }
+
+ private class KubernetesExternalLbMapListener implements MapEventListener<String, KubernetesExternalLb> {
+ @Override
+ public void event(MapEvent<String, KubernetesExternalLb> event) {
+ switch (event.type()) {
+ case INSERT:
+ eventExecutor.execute(() ->
+ notifyDelegate(new KubernetesExternalLbEvent(
+ KUBERNETES_EXTERNAL_LOAD_BALANCER_CREATED, event.newValue().value())));
+ break;
+ case UPDATE:
+ eventExecutor.execute(() ->
+ notifyDelegate(new KubernetesExternalLbEvent(
+ KUBERNETES_EXTERNAL_LOAD_BALANCER_UPDATED, event.newValue().value())));
+ break;
+ case REMOVE:
+ eventExecutor.execute(() ->
+ notifyDelegate(new KubernetesExternalLbEvent(
+ KUBERNETES_EXTERNAL_LOAD_BALANCER_REMOVED, event.newValue().value())));
+ break;
+ default:
+ //do nothing
+ break;
+ }
+ }
+ }
+}
diff --git a/apps/kubevirt-networking/app/src/main/java/org/onosproject/kubevirtnetworking/impl/KubernetesExternalLbManager.java b/apps/kubevirt-networking/app/src/main/java/org/onosproject/kubevirtnetworking/impl/KubernetesExternalLbManager.java
new file mode 100644
index 0000000..e68d289
--- /dev/null
+++ b/apps/kubevirt-networking/app/src/main/java/org/onosproject/kubevirtnetworking/impl/KubernetesExternalLbManager.java
@@ -0,0 +1,150 @@
+/*
+ * 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 com.google.common.base.Strings;
+import com.google.common.collect.ImmutableSet;
+import org.onosproject.core.ApplicationId;
+import org.onosproject.core.CoreService;
+import org.onosproject.event.ListenerRegistry;
+import org.onosproject.kubevirtnetworking.api.KubernetesExternalLb;
+import org.onosproject.kubevirtnetworking.api.KubernetesExternalLbAdminService;
+import org.onosproject.kubevirtnetworking.api.KubernetesExternalLbEvent;
+import org.onosproject.kubevirtnetworking.api.KubernetesExternalLbListener;
+import org.onosproject.kubevirtnetworking.api.KubernetesExternalLbService;
+import org.onosproject.kubevirtnetworking.api.KubernetesExternalLbStore;
+import org.onosproject.kubevirtnetworking.api.KubernetesExternalLbStoreDelegate;
+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.util.Set;
+
+import static com.google.common.base.Preconditions.checkArgument;
+import static com.google.common.base.Preconditions.checkNotNull;
+import static org.onosproject.kubevirtnetworking.api.Constants.KUBEVIRT_NETWORKING_APP_ID;
+import static org.slf4j.LoggerFactory.getLogger;
+
+/**
+ * Provides implementation of administrating and interfacing kubernetes external lb.
+ */
+@Component(
+ immediate = true,
+ service = {KubernetesExternalLbAdminService.class, KubernetesExternalLbService.class}
+)
+public class KubernetesExternalLbManager
+ extends ListenerRegistry<KubernetesExternalLbEvent, KubernetesExternalLbListener>
+ implements KubernetesExternalLbAdminService, KubernetesExternalLbService {
+
+ protected final Logger log = getLogger(getClass());
+
+ private static final String MSG_LOAD_BALANCER = "Kubernetes external lb %s %s";
+ private static final String MSG_CREATED = "created";
+ private static final String MSG_UPDATED = "updated";
+ private static final String MSG_REMOVED = "removed";
+
+ private static final String ERR_NULL_LOAD_BALANCER = "Kubernetes external lb cannot be null";
+ private static final String ERR_NULL_LOAD_BALANCER_NAME = "Kubernetes external lb name cannot be null";
+ private static final String ERR_IN_USE = " still in use";
+
+ @Reference(cardinality = ReferenceCardinality.MANDATORY)
+ protected CoreService coreService;
+
+ @Reference(cardinality = ReferenceCardinality.MANDATORY)
+ protected KubernetesExternalLbStore lbStore;
+
+ private final InternalKubernetesExternalLbStorageDelegate delegate =
+ new InternalKubernetesExternalLbStorageDelegate();
+
+ private ApplicationId appId;
+
+ @Activate
+ protected void activate() {
+ appId = coreService.registerApplication(KUBEVIRT_NETWORKING_APP_ID);
+
+ lbStore.setDelegate(delegate);
+ log.info("Started");
+ }
+
+ @Deactivate
+ protected void deactivate() {
+ lbStore.unsetDelegate(delegate);
+ log.info("Stopped");
+ }
+
+ @Override
+ public void createExternalLb(KubernetesExternalLb externalLb) {
+ checkNotNull(externalLb, ERR_NULL_LOAD_BALANCER);
+ checkArgument(!Strings.isNullOrEmpty(externalLb.serviceName()), ERR_NULL_LOAD_BALANCER_NAME);
+
+
+ lbStore.createLoadBalancer(externalLb);
+ log.info(String.format(MSG_LOAD_BALANCER, externalLb.serviceName(), MSG_CREATED));
+
+ }
+
+ @Override
+ public void updateExternalLb(KubernetesExternalLb externalLb) {
+ checkNotNull(externalLb, ERR_NULL_LOAD_BALANCER);
+ checkArgument(!Strings.isNullOrEmpty(externalLb.serviceName()), ERR_NULL_LOAD_BALANCER_NAME);
+
+ lbStore.updateLoadBalancer(externalLb);
+ log.info(String.format(MSG_LOAD_BALANCER, externalLb.serviceName(), MSG_UPDATED));
+ }
+
+ @Override
+ public void removeExternalLb(String serviceName) {
+ checkArgument(serviceName != null, ERR_NULL_LOAD_BALANCER_NAME);
+
+ synchronized (this) {
+ KubernetesExternalLb externalLb = lbStore.removeLoadBalancer(serviceName);
+
+ if (externalLb != null) {
+ log.info(String.format(MSG_LOAD_BALANCER, externalLb.serviceName(), MSG_REMOVED));
+ }
+ }
+ }
+
+ @Override
+ public void clear() {
+ lbStore.clear();
+ }
+
+ @Override
+ public KubernetesExternalLb loadBalancer(String serviceName) {
+ checkArgument(!Strings.isNullOrEmpty(serviceName), ERR_NULL_LOAD_BALANCER_NAME);
+
+ return lbStore.loadBalancer(serviceName);
+ }
+
+ @Override
+ public Set<KubernetesExternalLb> loadBalancers() {
+ return ImmutableSet.copyOf(lbStore.loadBalancers());
+ }
+
+ private class InternalKubernetesExternalLbStorageDelegate
+ implements KubernetesExternalLbStoreDelegate {
+
+ @Override
+ public void notify(KubernetesExternalLbEvent event) {
+ log.trace("send kubernetes external lb event {}", event);
+ process(event);
+ }
+ }
+}
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 de71f29..3496008 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
@@ -41,6 +41,7 @@
import org.onosproject.cfg.ConfigProperty;
import org.onosproject.kubevirtnetworking.api.DefaultKubevirtNetwork;
import org.onosproject.kubevirtnetworking.api.DefaultKubevirtPort;
+import org.onosproject.kubevirtnetworking.api.KubernetesExternalLb;
import org.onosproject.kubevirtnetworking.api.KubevirtHostRoute;
import org.onosproject.kubevirtnetworking.api.KubevirtIpPool;
import org.onosproject.kubevirtnetworking.api.KubevirtLoadBalancer;
@@ -542,6 +543,28 @@
}
/**
+ * Returns the gateway node for the specified kubernetes external lb.
+ * Among gateways, only one gateway would act as a gateway per external lb.
+ * Currently gateway node is selected based on modulo operation with router hashcode.
+ *
+ * @param nodeService kubevirt node service
+ * @param externalLb kubernetes external lb
+ * @return elected gateway node
+ */
+ public static KubevirtNode gatewayNodeForSpecifiedService(KubevirtNodeService nodeService,
+ KubernetesExternalLb externalLb) {
+ //TODO: enhance election logic for a better load balancing
+
+ int numOfGateways = nodeService.completeNodes(GATEWAY).size();
+ if (numOfGateways == 0) {
+ return null;
+ }
+
+ return (KubevirtNode) nodeService.completeNodes(GATEWAY)
+ .toArray()[externalLb.hashCode() % numOfGateways];
+ }
+
+ /**
* Returns the mac address of the router.
*
* @param router kubevirt router