Implement kubevirt floating IP's class, codec, watcher and APIs
Change-Id: I2111902e86083add8a00af62557fac1e98b7e7fc
(cherry picked from commit e48a617ccaf19d0d29ccc3211f6254be5e26826e)
diff --git a/apps/kubevirt-networking/api/src/main/java/org/onosproject/kubevirtnetworking/api/DefaultKubevirtFloatingIp.java b/apps/kubevirt-networking/api/src/main/java/org/onosproject/kubevirtnetworking/api/DefaultKubevirtFloatingIp.java
new file mode 100644
index 0000000..6dc98e2
--- /dev/null
+++ b/apps/kubevirt-networking/api/src/main/java/org/onosproject/kubevirtnetworking/api/DefaultKubevirtFloatingIp.java
@@ -0,0 +1,190 @@
+/*
+ * 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.base.MoreObjects;
+import org.onlab.packet.IpAddress;
+
+import java.util.Objects;
+
+import static com.google.common.base.Preconditions.checkArgument;
+
+/**
+ * Default implementation class of kubevirt floating IP.
+ */
+public final class DefaultKubevirtFloatingIp implements KubevirtFloatingIp {
+
+ private static final String NOT_NULL_MSG = "Floating IP % cannot be null";
+
+ private final String id;
+ private final String routerName;
+ private final String podName;
+ private final IpAddress floatingIp;
+ private final IpAddress fixedIp;
+
+ /**
+ * A default constructor.
+ *
+ * @param id floating IP identifier
+ * @param routerName router name
+ * @param podName POD name
+ * @param floatingIp floating IP address
+ * @param fixedIp fixed IP address
+ */
+ public DefaultKubevirtFloatingIp(String id, String routerName, String podName,
+ IpAddress floatingIp, IpAddress fixedIp) {
+ this.id = id;
+ this.routerName = routerName;
+ this.podName = podName;
+ this.floatingIp = floatingIp;
+ this.fixedIp = fixedIp;
+ }
+
+ @Override
+ public String id() {
+ return id;
+ }
+
+ @Override
+ public String routerName() {
+ return routerName;
+ }
+
+ @Override
+ public IpAddress floatingIp() {
+ return floatingIp;
+ }
+
+ @Override
+ public IpAddress fixedIp() {
+ return fixedIp;
+ }
+
+ @Override
+ public String podName() {
+ return podName;
+ }
+
+ @Override
+ public KubevirtFloatingIp updateFixedIp(IpAddress ip) {
+ return DefaultKubevirtFloatingIp.builder()
+ .id(id)
+ .routerName(routerName)
+ .floatingIp(floatingIp)
+ .fixedIp(ip)
+ .podName(podName)
+ .build();
+ }
+
+ @Override
+ public KubevirtFloatingIp updatePodName(String name) {
+ return DefaultKubevirtFloatingIp.builder()
+ .id(id)
+ .routerName(routerName)
+ .floatingIp(floatingIp)
+ .fixedIp(fixedIp)
+ .podName(name)
+ .build();
+ }
+
+ @Override
+ public boolean equals(Object o) {
+ if (this == o) {
+ return true;
+ }
+ if (o == null || getClass() != o.getClass()) {
+ return false;
+ }
+ DefaultKubevirtFloatingIp that = (DefaultKubevirtFloatingIp) o;
+ return id.equals(that.id) && routerName.equals(that.routerName) &&
+ Objects.equals(podName, that.podName) &&
+ floatingIp.equals(that.floatingIp) &&
+ Objects.equals(fixedIp, that.fixedIp);
+ }
+
+ @Override
+ public int hashCode() {
+ return Objects.hash(id, routerName, podName, floatingIp, fixedIp);
+ }
+
+ @Override
+ public String toString() {
+ return MoreObjects.toStringHelper(this)
+ .add("id", id)
+ .add("routerName", routerName)
+ .add("podName", podName)
+ .add("floatingIp", floatingIp)
+ .add("fixedIp", fixedIp)
+ .toString();
+ }
+
+ /**
+ * Returns new builder instance.
+ *
+ * @return kubevirt floating IP builder
+ */
+ public static Builder builder() {
+ return new Builder();
+ }
+
+ public static final class Builder implements KubevirtFloatingIp.Builder {
+
+ private String id;
+ private String routerName;
+ private String podName;
+ private IpAddress floatingIp;
+ private IpAddress fixedIp;
+
+ @Override
+ public KubevirtFloatingIp build() {
+ checkArgument(id != null, NOT_NULL_MSG, "id");
+ checkArgument(routerName != null, NOT_NULL_MSG, "routerName");
+ checkArgument(floatingIp != null, NOT_NULL_MSG, "floatingIp");
+
+ return new DefaultKubevirtFloatingIp(id, routerName, podName, floatingIp, fixedIp);
+ }
+
+ @Override
+ public Builder id(String id) {
+ this.id = id;
+ return this;
+ }
+
+ @Override
+ public Builder routerName(String name) {
+ this.routerName = name;
+ return this;
+ }
+
+ @Override
+ public Builder floatingIp(IpAddress ip) {
+ this.floatingIp = ip;
+ return this;
+ }
+
+ @Override
+ public Builder fixedIp(IpAddress ip) {
+ this.fixedIp = ip;
+ return this;
+ }
+
+ @Override
+ public Builder podName(String name) {
+ this.podName = name;
+ return this;
+ }
+ }
+}
diff --git a/apps/kubevirt-networking/api/src/main/java/org/onosproject/kubevirtnetworking/api/KubevirtFloatingIp.java b/apps/kubevirt-networking/api/src/main/java/org/onosproject/kubevirtnetworking/api/KubevirtFloatingIp.java
new file mode 100644
index 0000000..f879d2b
--- /dev/null
+++ b/apps/kubevirt-networking/api/src/main/java/org/onosproject/kubevirtnetworking/api/KubevirtFloatingIp.java
@@ -0,0 +1,124 @@
+/*
+ * 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.onlab.packet.IpAddress;
+
+/**
+ * Representation of floating IP address.
+ */
+public interface KubevirtFloatingIp {
+
+ /**
+ * Returns the floating IP identifier.
+ *
+ * @return floating IP identifier
+ */
+ String id();
+
+ /**
+ * Returns the name of router where the floating IP is associated.
+ *
+ * @return name of router
+ */
+ String routerName();
+
+ /**
+ * Returns the floating IP address.
+ *
+ * @return floating IP address
+ */
+ IpAddress floatingIp();
+
+ /**
+ * Returns the fixed IP address.
+ *
+ * @return fixed IP address
+ */
+ IpAddress fixedIp();
+
+ /**
+ * Returns the name of POD where this floating IP is associated with.
+ *
+ * @return POD name
+ */
+ String podName();
+
+ /**
+ * Updates the kubevirt floating IP with the supplied fixed IP address.
+ *
+ * @param ip fixed IP address
+ * @return kubevirt floating IP
+ */
+ KubevirtFloatingIp updateFixedIp(IpAddress ip);
+
+ /**
+ * Updates the kubevirt floating IP with the supplied pod Name.
+ *
+ * @param name POD name
+ * @return kubevirt floating IP
+ */
+ KubevirtFloatingIp updatePodName(String name);
+
+ interface Builder {
+ /**
+ * Builds an immutable floaing IP instance.
+ *
+ * @return kubevirt floating IP
+ */
+ KubevirtFloatingIp build();
+
+ /**
+ * Returns kubevirt floating IP builder with supplied identifier.
+ *
+ * @param id floating IP identifier
+ * @return floating IP builder
+ */
+ Builder id(String id);
+
+ /**
+ * Returns kubevirt floating IP builder with supplied router name.
+ *
+ * @param name router name
+ * @return floating IP builder
+ */
+ Builder routerName(String name);
+
+ /**
+ * Returns kubevirt floating IP builder with supplied floating IP address.
+ *
+ * @param ip floating IP address
+ * @return floating IP builder
+ */
+ Builder floatingIp(IpAddress ip);
+
+ /**
+ * Returns kubevirt floating IP builder with supplied fixed IP address.
+ *
+ * @param ip fixed IP address
+ * @return floating IP builder
+ */
+ Builder fixedIp(IpAddress ip);
+
+ /**
+ * Returns kubevirt floating IP builder with supplied POD name.
+ *
+ * @param name POD name
+ * @return floating IP builder
+ */
+ Builder podName(String name);
+ }
+}
diff --git a/apps/kubevirt-networking/api/src/main/java/org/onosproject/kubevirtnetworking/api/KubevirtRouter.java b/apps/kubevirt-networking/api/src/main/java/org/onosproject/kubevirtnetworking/api/KubevirtRouter.java
index e9f5e88..697198b 100644
--- a/apps/kubevirt-networking/api/src/main/java/org/onosproject/kubevirtnetworking/api/KubevirtRouter.java
+++ b/apps/kubevirt-networking/api/src/main/java/org/onosproject/kubevirtnetworking/api/KubevirtRouter.java
@@ -77,7 +77,7 @@
interface Builder {
/**
- * Builds an immutable network instance.
+ * Builds an immutable router instance.
*
* @return kubevirt router
*/
diff --git a/apps/kubevirt-networking/api/src/main/java/org/onosproject/kubevirtnetworking/api/KubevirtRouterAdminService.java b/apps/kubevirt-networking/api/src/main/java/org/onosproject/kubevirtnetworking/api/KubevirtRouterAdminService.java
index 1ec1cee..30d7890 100644
--- a/apps/kubevirt-networking/api/src/main/java/org/onosproject/kubevirtnetworking/api/KubevirtRouterAdminService.java
+++ b/apps/kubevirt-networking/api/src/main/java/org/onosproject/kubevirtnetworking/api/KubevirtRouterAdminService.java
@@ -52,6 +52,27 @@
void updatePeerRouterMac(String name, MacAddress mac);
/**
+ * Creates a kubevirt floating IP with the given information.
+ *
+ * @param floatingIp a new floating IP
+ */
+ void createFloatingIp(KubevirtFloatingIp floatingIp);
+
+ /**
+ * Updates the kubevirt floating IP with the given information.
+ *
+ * @param floatingIp the updated floating IP
+ */
+ void updateFloatingIp(KubevirtFloatingIp floatingIp);
+
+ /**
+ * Removes the floating IP with the given identifier.
+ *
+ * @param id floating IP identifier
+ */
+ void removeFloatingIp(String id);
+
+ /**
* Removes all routers.
*/
void clear();
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 b195ecb..c9e1f16 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
@@ -17,11 +17,16 @@
import org.onosproject.event.AbstractEvent;
+import static com.google.common.base.MoreObjects.toStringHelper;
+
/**
* Kubevirt router event class.
*/
public class KubevirtRouterEvent extends AbstractEvent<KubevirtRouterEvent.Type, KubevirtRouter> {
+ private final KubevirtFloatingIp floatingIp;
+ private final String podName;
+
/**
* Creates an event of a given type for the specified kubevirt router.
*
@@ -30,6 +35,35 @@
*/
public KubevirtRouterEvent(Type type, KubevirtRouter subject) {
super(type, subject);
+ this.floatingIp = null;
+ this.podName = null;
+ }
+
+ /**
+ * Creates an event of a given type for the specified kubevirt router.
+ *
+ * @param type kubevirt router event type
+ * @param subject kubevirt router
+ * @param floatingIp kubevirt floating IP
+ */
+ public KubevirtRouterEvent(Type type, KubevirtRouter subject, KubevirtFloatingIp floatingIp) {
+ super(type, subject);
+ this.floatingIp = floatingIp;
+ this.podName = null;
+ }
+
+ /**
+ * Creates an event of a given type for the specified kubevirt router.
+ *
+ * @param type kubevirt router event type
+ * @param subject kubevirt router
+ * @param floatingIp kubevirt floating IP
+ * @param podName kubevirt POD name
+ */
+ public KubevirtRouterEvent(Type type, KubevirtRouter subject, KubevirtFloatingIp floatingIp, String podName) {
+ super(type, subject);
+ this.floatingIp = floatingIp;
+ this.podName = podName;
}
public enum Type {
@@ -47,5 +81,61 @@
* Signifies that the kubevirt router is removed.
*/
KUBEVIRT_ROUTER_REMOVED,
+
+ /**
+ * Signifies that a new kubevirt floating IP is created.
+ */
+ KUBEVIRT_FLOATING_IP_CREATED,
+
+ /**
+ * Signifies that the kubevirt floating IP is updated.
+ */
+ KUBEVIRT_FLOATING_IP_UPDATED,
+
+ /**
+ * Signifies that the kubevirt floating IP is removed.
+ */
+ KUBEVIRT_FLOATING_IP_REMOVED,
+
+ /**
+ * Signifies that the floating IP is associated to a fixed IP.
+ */
+ KUBEVIRT_FLOATING_IP_ASSOCIATED,
+
+ /**
+ * Signifies that the floating IP disassociated from the fixed IP.
+ */
+ KUBEVIRT_FLOATING_IP_DISASSOCIATED
+ }
+
+ /**
+ * Returns the floating IP of the router event.
+ *
+ * @return kubevirt floating IP; null if the event is not relevant to the floating IP
+ */
+ public KubevirtFloatingIp floatingIp() {
+ return floatingIp;
+ }
+
+ /**
+ * Returns the pod name of the router event.
+ *
+ * @return kubevirt pod name; null if the event is not relevant to the pod name
+ */
+ public String podName() {
+ return podName;
+ }
+
+ @Override
+ public String toString() {
+ if (floatingIp == null) {
+ return super.toString();
+ }
+ return toStringHelper(this)
+ .add("type", type())
+ .add("router", subject())
+ .add("floatingIp", floatingIp)
+ .add("podName", podName)
+ .toString();
}
}
diff --git a/apps/kubevirt-networking/api/src/main/java/org/onosproject/kubevirtnetworking/api/KubevirtRouterService.java b/apps/kubevirt-networking/api/src/main/java/org/onosproject/kubevirtnetworking/api/KubevirtRouterService.java
index 03b6d35..0b25fe4 100644
--- a/apps/kubevirt-networking/api/src/main/java/org/onosproject/kubevirtnetworking/api/KubevirtRouterService.java
+++ b/apps/kubevirt-networking/api/src/main/java/org/onosproject/kubevirtnetworking/api/KubevirtRouterService.java
@@ -39,4 +39,35 @@
* @return set of kubevirt routers
*/
Set<KubevirtRouter> routers();
+
+ /**
+ * Returns the kubevirt floating IP with the supplied identifier.
+ *
+ * @param id floating IP identifier
+ * @return kubevirt floating IP
+ */
+ KubevirtFloatingIp floatingIp(String id);
+
+ /**
+ * Returns the kubevirt floating IP associated with the supplied POD.
+ *
+ * @param podName name of POD
+ * @return kubevirt floating IP
+ */
+ KubevirtFloatingIp floatingIpByPodName(String podName);
+
+ /**
+ * Returns the kubevirt floating IPs bound to the given router.
+ *
+ * @param routerName name of router
+ * @return set of kubevirt floating IPs
+ */
+ Set<KubevirtFloatingIp> floatingIpsByRouter(String routerName);
+
+ /**
+ * Returns all kubevirt floating IPs.
+ *
+ * @return set of kubevirt floating IPs
+ */
+ Set<KubevirtFloatingIp> floatingIps();
}
diff --git a/apps/kubevirt-networking/api/src/main/java/org/onosproject/kubevirtnetworking/api/KubevirtRouterStore.java b/apps/kubevirt-networking/api/src/main/java/org/onosproject/kubevirtnetworking/api/KubevirtRouterStore.java
index c63daa2..7313aa9 100644
--- a/apps/kubevirt-networking/api/src/main/java/org/onosproject/kubevirtnetworking/api/KubevirtRouterStore.java
+++ b/apps/kubevirt-networking/api/src/main/java/org/onosproject/kubevirtnetworking/api/KubevirtRouterStore.java
@@ -63,7 +63,44 @@
Set<KubevirtRouter> routers();
/**
- * Removes all kubevirt routers.
+ * Creates a new kubevirt floating IP.
+ *
+ * @param floatingIp kubevirt floating IP
+ */
+ void createFloatingIp(KubevirtFloatingIp floatingIp);
+
+ /**
+ * Updates the kubevirt floating IP.
+ *
+ * @param floatingIp kubevirt floating IP
+ */
+ void updateFloatingIp(KubevirtFloatingIp floatingIp);
+
+ /**
+ * Removes the kubevirt floating IP with the given identifier.
+ *
+ * @param id floating IP identifier
+ * @return removed kubevirt floating IP; null if failed
+ */
+ KubevirtFloatingIp removeFloatingIp(String id);
+
+ /**
+ * Returns the kubevirt floating IP with the given identifier.
+ *
+ * @param id floating IP identifier
+ * @return kubevirt floating IP; null if not found
+ */
+ KubevirtFloatingIp floatingIp(String id);
+
+ /**
+ * Returns all kubevirt floating IPs.
+ *
+ * @return set of kubevirt floating IPs
+ */
+ Set<KubevirtFloatingIp> floatingIps();
+
+ /**
+ * Removes all kubevirt routers and floating IPs.
*/
void clear();
}
diff --git a/apps/kubevirt-networking/api/src/test/java/org/onosproject/kubevirtnetworking/api/DefaultKubevirtFloatingIpTest.java b/apps/kubevirt-networking/api/src/test/java/org/onosproject/kubevirtnetworking/api/DefaultKubevirtFloatingIpTest.java
new file mode 100644
index 0000000..f5f400e
--- /dev/null
+++ b/apps/kubevirt-networking/api/src/test/java/org/onosproject/kubevirtnetworking/api/DefaultKubevirtFloatingIpTest.java
@@ -0,0 +1,107 @@
+/*
+ * 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.testing.EqualsTester;
+import org.junit.Before;
+import org.junit.Test;
+import org.onlab.packet.IpAddress;
+
+import static junit.framework.TestCase.assertEquals;
+import static org.onlab.junit.ImmutableClassChecker.assertThatClassIsImmutable;
+
+/**
+ * Unit tests for the default kubevirt floating IP class.
+ */
+public class DefaultKubevirtFloatingIpTest {
+
+ private static final String ID_1 = "fip_id_1";
+ private static final String ID_2 = "fip_id_2";
+ private static final String ROUTER_NAME_1 = "router-1";
+ private static final String ROUTER_NAME_2 = "router-2";
+ private static final IpAddress FLOATING_IP_1 = IpAddress.valueOf("10.10.10.10");
+ private static final IpAddress FLOATING_IP_2 = IpAddress.valueOf("20.20.20.20");
+ private static final String POD_NAME_1 = "pod-1";
+ private static final String POD_NAME_2 = "pod-2";
+ private static final IpAddress FIXED_IP_1 = IpAddress.valueOf("30.30.30.30");
+ private static final IpAddress FIXED_IP_2 = IpAddress.valueOf("40.40.40.40");
+
+ private KubevirtFloatingIp fip1;
+ private KubevirtFloatingIp sameAsFip1;
+ private KubevirtFloatingIp fip2;
+
+ /**
+ * Tests class immutability.
+ */
+ @Test
+ public void testImmutability() {
+ assertThatClassIsImmutable(DefaultKubevirtFloatingIp.class);
+ }
+
+ /**
+ * Initial setup for this unit test.
+ */
+ @Before
+ public void setUp() {
+ fip1 = DefaultKubevirtFloatingIp.builder()
+ .id(ID_1)
+ .routerName(ROUTER_NAME_1)
+ .floatingIp(FLOATING_IP_1)
+ .podName(POD_NAME_1)
+ .fixedIp(FIXED_IP_1)
+ .build();
+
+ sameAsFip1 = DefaultKubevirtFloatingIp.builder()
+ .id(ID_1)
+ .routerName(ROUTER_NAME_1)
+ .floatingIp(FLOATING_IP_1)
+ .podName(POD_NAME_1)
+ .fixedIp(FIXED_IP_1)
+ .build();
+
+ fip2 = DefaultKubevirtFloatingIp.builder()
+ .id(ID_2)
+ .routerName(ROUTER_NAME_2)
+ .floatingIp(FLOATING_IP_2)
+ .podName(POD_NAME_2)
+ .fixedIp(FIXED_IP_2)
+ .build();
+ }
+
+ /**
+ * Tests object equality.
+ */
+ @Test
+ public void testEquality() {
+ new EqualsTester().addEqualityGroup(fip1, sameAsFip1)
+ .addEqualityGroup(fip2)
+ .testEquals();
+ }
+
+ /**
+ * Test object construction.
+ */
+ @Test
+ public void testConstruction() {
+ KubevirtFloatingIp fip = fip1;
+
+ assertEquals(ID_1, fip.id());
+ assertEquals(ROUTER_NAME_1, fip.routerName());
+ assertEquals(FLOATING_IP_1, fip.floatingIp());
+ assertEquals(POD_NAME_1, fip.podName());
+ assertEquals(FIXED_IP_1, fip.fixedIp());
+ }
+}
diff --git a/apps/kubevirt-networking/app/src/main/java/org/onosproject/kubevirtnetworking/cli/KubevirtListFloatingIpCommand.java b/apps/kubevirt-networking/app/src/main/java/org/onosproject/kubevirtnetworking/cli/KubevirtListFloatingIpCommand.java
new file mode 100644
index 0000000..574bba5
--- /dev/null
+++ b/apps/kubevirt-networking/app/src/main/java/org/onosproject/kubevirtnetworking/cli/KubevirtListFloatingIpCommand.java
@@ -0,0 +1,87 @@
+/*
+ * 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.cli;
+
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.fasterxml.jackson.databind.node.ArrayNode;
+import com.google.common.collect.ImmutableList;
+import com.google.common.collect.Lists;
+import org.apache.commons.lang.StringUtils;
+import org.apache.karaf.shell.api.action.Command;
+import org.apache.karaf.shell.api.action.lifecycle.Service;
+import org.onosproject.cli.AbstractShellCommand;
+import org.onosproject.kubevirtnetworking.api.KubevirtFloatingIp;
+import org.onosproject.kubevirtnetworking.api.KubevirtRouterService;
+
+import java.util.Comparator;
+import java.util.List;
+
+import static org.onosproject.kubevirtnetworking.api.Constants.CLI_IP_ADDRESS_LENGTH;
+import static org.onosproject.kubevirtnetworking.api.Constants.CLI_MARGIN_LENGTH;
+import static org.onosproject.kubevirtnetworking.api.Constants.CLI_NAME_LENGTH;
+import static org.onosproject.kubevirtnetworking.util.KubevirtNetworkingUtil.genFormatString;
+import static org.onosproject.kubevirtnetworking.util.KubevirtNetworkingUtil.prettyJson;
+
+/**
+ * Lists kubevirt floating IPs.
+ */
+@Service
+@Command(scope = "onos", name = "kubevirt-fips",
+ description = "Lists all kubevirt floating IPs")
+public class KubevirtListFloatingIpCommand extends AbstractShellCommand {
+
+ @Override
+ protected void doExecute() throws Exception {
+ KubevirtRouterService service = get(KubevirtRouterService.class);
+ List<KubevirtFloatingIp> fips = Lists.newArrayList(service.floatingIps());
+ fips.sort(Comparator.comparing(KubevirtFloatingIp::routerName));
+
+ String format = genFormatString(ImmutableList.of(CLI_NAME_LENGTH,
+ CLI_IP_ADDRESS_LENGTH, CLI_NAME_LENGTH, CLI_IP_ADDRESS_LENGTH));
+
+ if (outputJson()) {
+ print("%s", json(fips));
+ } else {
+ print(format, "Router Name", "Floating IP", "POD Name", "Fixed IP");
+ for (KubevirtFloatingIp fip : fips) {
+
+ String fixedIp = fip.fixedIp() == null ? "N/A" : fip.fixedIp().toString();
+ String podName = fip.podName() == null ? "N/A" : fip.podName();
+
+ print(format, StringUtils.substring(fip.routerName(), 0,
+ CLI_NAME_LENGTH - CLI_MARGIN_LENGTH),
+ StringUtils.substring(fip.floatingIp().toString(), 0,
+ CLI_IP_ADDRESS_LENGTH - CLI_MARGIN_LENGTH),
+ StringUtils.substring(podName, 0,
+ CLI_NAME_LENGTH - CLI_MARGIN_LENGTH),
+ StringUtils.substring(fixedIp, 0,
+ CLI_IP_ADDRESS_LENGTH - CLI_MARGIN_LENGTH)
+ );
+ }
+ }
+ }
+
+ private String json(List<KubevirtFloatingIp> fips) {
+ ObjectMapper mapper = new ObjectMapper();
+ ArrayNode result = mapper.createArrayNode();
+
+ for (KubevirtFloatingIp fip : fips) {
+ result.add(jsonForEntity(fip, KubevirtFloatingIp.class));
+ }
+
+ return prettyJson(mapper, result.toString());
+ }
+}
diff --git a/apps/kubevirt-networking/app/src/main/java/org/onosproject/kubevirtnetworking/cli/KubevirtListRouterCommand.java b/apps/kubevirt-networking/app/src/main/java/org/onosproject/kubevirtnetworking/cli/KubevirtListRouterCommand.java
index 4ebd21b..772b841 100644
--- a/apps/kubevirt-networking/app/src/main/java/org/onosproject/kubevirtnetworking/cli/KubevirtListRouterCommand.java
+++ b/apps/kubevirt-networking/app/src/main/java/org/onosproject/kubevirtnetworking/cli/KubevirtListRouterCommand.java
@@ -23,7 +23,6 @@
import org.apache.karaf.shell.api.action.Command;
import org.apache.karaf.shell.api.action.lifecycle.Service;
import org.onosproject.cli.AbstractShellCommand;
-import org.onosproject.kubevirtnetworking.api.KubevirtNetworkService;
import org.onosproject.kubevirtnetworking.api.KubevirtRouter;
import org.onosproject.kubevirtnetworking.api.KubevirtRouterService;
@@ -39,6 +38,9 @@
import static org.onosproject.kubevirtnetworking.util.KubevirtNetworkingUtil.genFormatString;
import static org.onosproject.kubevirtnetworking.util.KubevirtNetworkingUtil.prettyJson;
+/**
+ * Lists kubevirt routers.
+ */
@Service
@Command(scope = "onos", name = "kubevirt-routers",
description = "Lists all kubevirt routers")
@@ -47,7 +49,6 @@
@Override
protected void doExecute() throws Exception {
KubevirtRouterService service = get(KubevirtRouterService.class);
- KubevirtNetworkService netService = get(KubevirtNetworkService.class);
List<KubevirtRouter> routers = Lists.newArrayList(service.routers());
routers.sort(Comparator.comparing(KubevirtRouter::name));
diff --git a/apps/kubevirt-networking/app/src/main/java/org/onosproject/kubevirtnetworking/codec/KubevirtFloatingIpCodec.java b/apps/kubevirt-networking/app/src/main/java/org/onosproject/kubevirtnetworking/codec/KubevirtFloatingIpCodec.java
new file mode 100644
index 0000000..16fd082
--- /dev/null
+++ b/apps/kubevirt-networking/app/src/main/java/org/onosproject/kubevirtnetworking/codec/KubevirtFloatingIpCodec.java
@@ -0,0 +1,95 @@
+/*
+ * 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.codec;
+
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.node.ObjectNode;
+import org.onlab.packet.IpAddress;
+import org.onosproject.codec.CodecContext;
+import org.onosproject.codec.JsonCodec;
+import org.onosproject.kubevirtnetworking.api.DefaultKubevirtFloatingIp;
+import org.onosproject.kubevirtnetworking.api.KubevirtFloatingIp;
+import org.slf4j.Logger;
+
+import static com.google.common.base.Preconditions.checkNotNull;
+import static org.onlab.util.Tools.nullIsIllegal;
+import static org.slf4j.LoggerFactory.getLogger;
+
+/**
+ * Kubevirt floating IP codec used for serializing and de-serializing JSON string.
+ */
+public final class KubevirtFloatingIpCodec extends JsonCodec<KubevirtFloatingIp> {
+
+ private final Logger log = getLogger(getClass());
+
+ private static final String ID = "id";
+ private static final String ROUTER_NAME = "routerName";
+ private static final String POD_NAME = "podName";
+ private static final String FLOATING_IP = "floatingIp";
+ private static final String FIXED_IP = "fixedIp";
+
+ private static final String MISSING_MESSAGE = " is required in KubevirtFloatingIp";
+
+ @Override
+ public ObjectNode encode(KubevirtFloatingIp fip, CodecContext context) {
+ checkNotNull(fip, "Kubevirt floating IP cannot be null");
+
+ ObjectNode result = context.mapper().createObjectNode()
+ .put(ID, fip.id())
+ .put(ROUTER_NAME, fip.routerName())
+ .put(FLOATING_IP, fip.floatingIp().toString());
+
+ if (fip.podName() != null) {
+ result.put(POD_NAME, fip.podName());
+ }
+
+ if (fip.fixedIp() != null) {
+ result.put(FIXED_IP, fip.fixedIp().toString());
+ }
+
+ return result;
+ }
+
+ @Override
+ public KubevirtFloatingIp decode(ObjectNode json, CodecContext context) {
+ if (json == null || !json.isObject()) {
+ return null;
+ }
+
+ String id = nullIsIllegal(json.get(ID).asText(), ID + MISSING_MESSAGE);
+ String routerName = nullIsIllegal(json.get(ROUTER_NAME).asText(),
+ ROUTER_NAME + MISSING_MESSAGE);
+ String floatingIp = nullIsIllegal(json.get(FLOATING_IP).asText(),
+ FLOATING_IP + MISSING_MESSAGE);
+
+ KubevirtFloatingIp.Builder builder = DefaultKubevirtFloatingIp.builder()
+ .id(id)
+ .routerName(routerName)
+ .floatingIp(IpAddress.valueOf(floatingIp));
+
+ JsonNode podName = json.get(POD_NAME);
+ if (podName != null) {
+ builder.podName(podName.asText());
+ }
+
+ JsonNode fixedIp = json.get(FIXED_IP);
+ if (fixedIp != null) {
+ builder.fixedIp(IpAddress.valueOf(fixedIp.asText()));
+ }
+
+ return builder.build();
+ }
+}
diff --git a/apps/kubevirt-networking/app/src/main/java/org/onosproject/kubevirtnetworking/codec/KubevirtRouterCodec.java b/apps/kubevirt-networking/app/src/main/java/org/onosproject/kubevirtnetworking/codec/KubevirtRouterCodec.java
index 4937535..d8529f6 100644
--- a/apps/kubevirt-networking/app/src/main/java/org/onosproject/kubevirtnetworking/codec/KubevirtRouterCodec.java
+++ b/apps/kubevirt-networking/app/src/main/java/org/onosproject/kubevirtnetworking/codec/KubevirtRouterCodec.java
@@ -57,7 +57,7 @@
@Override
public ObjectNode encode(KubevirtRouter router, CodecContext context) {
- checkNotNull(router, "Kubevirt router pool cannot be null");
+ checkNotNull(router, "Kubevirt router cannot be null");
ObjectNode result = context.mapper().createObjectNode()
.put(NAME, router.name())
diff --git a/apps/kubevirt-networking/app/src/main/java/org/onosproject/kubevirtnetworking/impl/DistributedKubevirtRouterStore.java b/apps/kubevirt-networking/app/src/main/java/org/onosproject/kubevirtnetworking/impl/DistributedKubevirtRouterStore.java
index 537d91c..2d566fc 100644
--- a/apps/kubevirt-networking/app/src/main/java/org/onosproject/kubevirtnetworking/impl/DistributedKubevirtRouterStore.java
+++ b/apps/kubevirt-networking/app/src/main/java/org/onosproject/kubevirtnetworking/impl/DistributedKubevirtRouterStore.java
@@ -15,11 +15,14 @@
*/
package org.onosproject.kubevirtnetworking.impl;
+import com.google.common.base.Strings;
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.DefaultKubevirtFloatingIp;
import org.onosproject.kubevirtnetworking.api.DefaultKubevirtRouter;
+import org.onosproject.kubevirtnetworking.api.KubevirtFloatingIp;
import org.onosproject.kubevirtnetworking.api.KubevirtPeerRouter;
import org.onosproject.kubevirtnetworking.api.KubevirtRouter;
import org.onosproject.kubevirtnetworking.api.KubevirtRouterEvent;
@@ -47,6 +50,11 @@
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.KubevirtRouterEvent.Type.KUBEVIRT_FLOATING_IP_ASSOCIATED;
+import static org.onosproject.kubevirtnetworking.api.KubevirtRouterEvent.Type.KUBEVIRT_FLOATING_IP_CREATED;
+import static org.onosproject.kubevirtnetworking.api.KubevirtRouterEvent.Type.KUBEVIRT_FLOATING_IP_DISASSOCIATED;
+import static org.onosproject.kubevirtnetworking.api.KubevirtRouterEvent.Type.KUBEVIRT_FLOATING_IP_REMOVED;
+import static org.onosproject.kubevirtnetworking.api.KubevirtRouterEvent.Type.KUBEVIRT_FLOATING_IP_UPDATED;
import static org.onosproject.kubevirtnetworking.api.KubevirtRouterEvent.Type.KUBEVIRT_ROUTER_CREATED;
import static org.onosproject.kubevirtnetworking.api.KubevirtRouterEvent.Type.KUBEVIRT_ROUTER_REMOVED;
import static org.onosproject.kubevirtnetworking.api.KubevirtRouterEvent.Type.KUBEVIRT_ROUTER_UPDATED;
@@ -72,6 +80,8 @@
.register(KubevirtRouter.class)
.register(DefaultKubevirtRouter.class)
.register(KubevirtPeerRouter.class)
+ .register(KubevirtFloatingIp.class)
+ .register(DefaultKubevirtFloatingIp.class)
.register(Collection.class)
.build();
@@ -86,8 +96,11 @@
private final MapEventListener<String, KubevirtRouter> routerMapListener =
new KubevirtRouterMapListener();
+ private final MapEventListener<String, KubevirtFloatingIp> fipMapListener =
+ new KubevirtFloatingIpMapListener();
private ConsistentMap<String, KubevirtRouter> routerStore;
+ private ConsistentMap<String, KubevirtFloatingIp> fipStore;
@Activate
protected void activate() {
@@ -97,13 +110,20 @@
.withName("kubevirt-routerstore")
.withApplicationId(appId)
.build();
+ fipStore = storageService.<String, KubevirtFloatingIp>consistentMapBuilder()
+ .withSerializer(Serializer.using(SERIALIZER_KUBEVIRT_ROUTER))
+ .withName("kubevirt-fipstore")
+ .withApplicationId(appId)
+ .build();
routerStore.addListener(routerMapListener);
+ fipStore.addListener(fipMapListener);
log.info("Started");
}
@Deactivate
protected void deactivate() {
routerStore.removeListener(routerMapListener);
+ fipStore.removeListener(fipMapListener);
eventExecutor.shutdown();
log.info("Stopped");
}
@@ -147,8 +167,48 @@
}
@Override
+ public void createFloatingIp(KubevirtFloatingIp floatingIp) {
+ fipStore.compute(floatingIp.id(), (id, existing) -> {
+ final String error = floatingIp.id() + ERR_DUPLICATE;
+ checkArgument(existing == null, error);
+ return floatingIp;
+ });
+ }
+
+ @Override
+ public void updateFloatingIp(KubevirtFloatingIp floatingIp) {
+ fipStore.compute(floatingIp.id(), (id, existing) -> {
+ final String error = floatingIp.id() + ERR_NOT_FOUND;
+ checkArgument(existing != null, error);
+ return floatingIp;
+ });
+ }
+
+ @Override
+ public KubevirtFloatingIp removeFloatingIp(String id) {
+ Versioned<KubevirtFloatingIp> floatingIp = fipStore.remove(id);
+ if (floatingIp == null) {
+ final String error = id + ERR_NOT_FOUND;
+ throw new IllegalArgumentException(error);
+ }
+ return floatingIp.value();
+ }
+
+ @Override
+ public KubevirtFloatingIp floatingIp(String id) {
+ return fipStore.asJavaMap().get(id);
+ }
+
+ @Override
+ public Set<KubevirtFloatingIp> floatingIps() {
+ return ImmutableSet.copyOf(fipStore.asJavaMap().values());
+ }
+
+
+ @Override
public void clear() {
routerStore.clear();
+ fipStore.clear();
}
private class KubevirtRouterMapListener implements MapEventListener<String, KubevirtRouter> {
@@ -180,4 +240,81 @@
}
}
}
+
+ private class KubevirtFloatingIpMapListener implements MapEventListener<String, KubevirtFloatingIp> {
+
+ @Override
+ public void event(MapEvent<String, KubevirtFloatingIp> event) {
+ switch (event.type()) {
+ case INSERT:
+ eventExecutor.execute(() -> processFloatingIpMapInsertion(event));
+ break;
+ case UPDATE:
+ eventExecutor.execute(() -> processFloatingIpMapUpdate(event));
+ break;
+ case REMOVE:
+ eventExecutor.execute(() -> processFloatingIpMapRemoval(event));
+ break;
+ default:
+ break;
+ }
+ }
+
+ private void processFloatingIpMapInsertion(MapEvent<String, KubevirtFloatingIp> event) {
+ log.debug("Kubevirt floating IP created");
+ KubevirtRouter router = Strings.isNullOrEmpty(
+ event.newValue().value().routerName()) ?
+ null :
+ router(event.newValue().value().routerName());
+ notifyDelegate(new KubevirtRouterEvent(
+ KUBEVIRT_FLOATING_IP_CREATED,
+ router,
+ event.newValue().value()));
+ }
+
+ private void processFloatingIpMapUpdate(MapEvent<String, KubevirtFloatingIp> event) {
+ log.debug("Kubevirt floating IP updated");
+ KubevirtRouter router = Strings.isNullOrEmpty(
+ event.newValue().value().routerName()) ?
+ null :
+ router(event.newValue().value().routerName());
+ notifyDelegate(new KubevirtRouterEvent(
+ KUBEVIRT_FLOATING_IP_UPDATED,
+ router,
+ event.newValue().value()));
+ processFloatingIpUpdate(event, router);
+ }
+
+ private void processFloatingIpMapRemoval(MapEvent<String, KubevirtFloatingIp> event) {
+ log.debug("Kubevirt floating IP removed");
+ KubevirtRouter router = Strings.isNullOrEmpty(
+ event.oldValue().value().routerName()) ?
+ null :
+ router(event.oldValue().value().routerName());
+ notifyDelegate(new KubevirtRouterEvent(
+ KUBEVIRT_FLOATING_IP_REMOVED,
+ router,
+ event.oldValue().value()));
+ }
+
+ private void processFloatingIpUpdate(MapEvent<String, KubevirtFloatingIp> event,
+ KubevirtRouter router) {
+ String oldPodName = event.oldValue().value().podName();
+ String newPodName = event.newValue().value().podName();
+
+ if (Strings.isNullOrEmpty(oldPodName) && !Strings.isNullOrEmpty(newPodName)) {
+ notifyDelegate(new KubevirtRouterEvent(
+ KUBEVIRT_FLOATING_IP_ASSOCIATED,
+ router,
+ event.newValue().value(), newPodName));
+ }
+
+ if (!Strings.isNullOrEmpty(oldPodName) && Strings.isNullOrEmpty(newPodName)) {
+ notifyDelegate(new KubevirtRouterEvent(
+ KUBEVIRT_FLOATING_IP_DISASSOCIATED,
+ router,
+ event.newValue().value(), oldPodName));
+ }
+ }
+ }
}
diff --git a/apps/kubevirt-networking/app/src/main/java/org/onosproject/kubevirtnetworking/impl/KubevirtFloatingIpWatcher.java b/apps/kubevirt-networking/app/src/main/java/org/onosproject/kubevirtnetworking/impl/KubevirtFloatingIpWatcher.java
new file mode 100644
index 0000000..d15a515
--- /dev/null
+++ b/apps/kubevirt-networking/app/src/main/java/org/onosproject/kubevirtnetworking/impl/KubevirtFloatingIpWatcher.java
@@ -0,0 +1,250 @@
+/*
+ * 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.impl;
+
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.fasterxml.jackson.databind.node.ObjectNode;
+import io.fabric8.kubernetes.client.KubernetesClient;
+import io.fabric8.kubernetes.client.Watcher;
+import io.fabric8.kubernetes.client.WatcherException;
+import io.fabric8.kubernetes.client.dsl.base.CustomResourceDefinitionContext;
+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.AbstractWatcher;
+import org.onosproject.kubevirtnetworking.api.KubevirtFloatingIp;
+import org.onosproject.kubevirtnetworking.api.KubevirtRouterAdminService;
+import org.onosproject.kubevirtnode.api.KubevirtApiConfigEvent;
+import org.onosproject.kubevirtnode.api.KubevirtApiConfigListener;
+import org.onosproject.kubevirtnode.api.KubevirtApiConfigService;
+import org.onosproject.mastership.MastershipService;
+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.io.IOException;
+import java.util.Objects;
+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.KUBEVIRT_NETWORKING_APP_ID;
+import static org.onosproject.kubevirtnetworking.util.KubevirtNetworkingUtil.k8sClient;
+import static org.slf4j.LoggerFactory.getLogger;
+
+/**
+ * Kubevirt floating IP watcher used for feeding kubevirt floating IP information.
+ */
+@Component(immediate = true)
+public class KubevirtFloatingIpWatcher extends AbstractWatcher {
+
+ private final Logger log = getLogger(getClass());
+
+ @Reference(cardinality = ReferenceCardinality.MANDATORY)
+ protected CoreService coreService;
+
+ @Reference(cardinality = ReferenceCardinality.MANDATORY)
+ protected MastershipService mastershipService;
+
+ @Reference(cardinality = ReferenceCardinality.MANDATORY)
+ protected ClusterService clusterService;
+
+ @Reference(cardinality = ReferenceCardinality.MANDATORY)
+ protected LeadershipService leadershipService;
+
+ @Reference(cardinality = ReferenceCardinality.MANDATORY)
+ protected KubevirtRouterAdminService adminService;
+
+ @Reference(cardinality = ReferenceCardinality.MANDATORY)
+ protected KubevirtApiConfigService configService;
+
+ private final ExecutorService eventExecutor = newSingleThreadExecutor(
+ groupedThreads(this.getClass().getSimpleName(), "event-handler"));
+
+ private final InternalFloatingIpWatcher watcher = new InternalFloatingIpWatcher();
+ private final InternalKubevirtApiConfigListener configListener =
+ new InternalKubevirtApiConfigListener();
+
+ CustomResourceDefinitionContext fipCrdCxt = new CustomResourceDefinitionContext
+ .Builder()
+ .withGroup("kubevirt.io")
+ .withScope("Cluster")
+ .withVersion("v1")
+ .withPlural("floatingips")
+ .build();
+
+ 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());
+ configService.addListener(configListener);
+
+ log.info("Started");
+ }
+
+ @Deactivate
+ protected void deactivate() {
+ configService.removeListener(configListener);
+ leadershipService.withdraw(appId.name());
+ eventExecutor.shutdown();
+
+ log.info("Stopped");
+ }
+
+ private void instantiateWatcher() {
+ KubernetesClient client = k8sClient(configService);
+
+ if (client != null) {
+ try {
+ client.customResource(fipCrdCxt).watch(watcher);
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ }
+ }
+
+ private KubevirtFloatingIp parseKubevirtFloatingIp(String resource) {
+ try {
+ ObjectMapper mapper = new ObjectMapper();
+ JsonNode json = mapper.readTree(resource);
+ ObjectNode spec = (ObjectNode) json.get("spec");
+ return codec(KubevirtFloatingIp.class).decode(spec, this);
+ } catch (IOException e) {
+ log.error("Failed to parse kubevirt floating IP object");
+ }
+
+ return null;
+ }
+
+ private class InternalKubevirtApiConfigListener implements KubevirtApiConfigListener {
+
+ private boolean isRelevantHelper() {
+ return Objects.equals(localNodeId, leadershipService.getLeader(appId.name()));
+ }
+
+ @Override
+ public void event(KubevirtApiConfigEvent event) {
+
+ switch (event.type()) {
+ case KUBEVIRT_API_CONFIG_UPDATED:
+ eventExecutor.execute(this::processConfigUpdate);
+ break;
+ case KUBEVIRT_API_CONFIG_CREATED:
+ case KUBEVIRT_API_CONFIG_REMOVED:
+ default:
+ // do nothing
+ break;
+ }
+ }
+
+ private void processConfigUpdate() {
+ if (!isRelevantHelper()) {
+ return;
+ }
+
+ instantiateWatcher();
+ }
+ }
+
+ private class InternalFloatingIpWatcher implements Watcher<String> {
+
+ @Override
+ public void eventReceived(Action action, String resource) {
+ switch (action) {
+ case ADDED:
+ eventExecutor.execute(() -> processAddition(resource));
+ break;
+ case MODIFIED:
+ eventExecutor.execute(() -> processModification(resource));
+ break;
+ case DELETED:
+ eventExecutor.execute(() -> processDeletion(resource));
+ break;
+ case ERROR:
+ log.warn("Failures processing floating IP manipulation.");
+ break;
+ default:
+ // do nothing
+ break;
+ }
+ }
+
+ @Override
+ public void onClose(WatcherException e) {
+
+ }
+
+ private void processAddition(String resource) {
+ if (!isMaster()) {
+ return;
+ }
+
+ KubevirtFloatingIp fip = parseKubevirtFloatingIp(resource);
+
+ if (fip != null) {
+ log.trace("Process Floating IP {} creating event from API server.", fip.floatingIp());
+
+ if (adminService.floatingIp(fip.id()) == null) {
+ adminService.createFloatingIp(fip);
+ }
+ }
+ }
+
+ private void processModification(String resource) {
+ if (!isMaster()) {
+ return;
+ }
+
+ KubevirtFloatingIp fip = parseKubevirtFloatingIp(resource);
+
+ if (fip != null) {
+ log.trace("Process Floating IP {} updating event from API server.", fip.floatingIp());
+
+ if (adminService.floatingIp(fip.id()) != null) {
+ adminService.updateFloatingIp(fip);
+ }
+ }
+ }
+
+ private void processDeletion(String resource) {
+ if (!isMaster()) {
+ return;
+ }
+
+ KubevirtFloatingIp fip = parseKubevirtFloatingIp(resource);
+
+ if (fip != null) {
+ log.trace("Process floating IP {} removal event from API server.", fip.floatingIp());
+
+ adminService.removeFloatingIp(fip.id());
+ }
+ }
+
+ private boolean isMaster() {
+ return Objects.equals(localNodeId, leadershipService.getLeader(appId.name()));
+ }
+ }
+}
diff --git a/apps/kubevirt-networking/app/src/main/java/org/onosproject/kubevirtnetworking/impl/KubevirtRouterManager.java b/apps/kubevirt-networking/app/src/main/java/org/onosproject/kubevirtnetworking/impl/KubevirtRouterManager.java
index bae3ff9..e9c06da 100644
--- a/apps/kubevirt-networking/app/src/main/java/org/onosproject/kubevirtnetworking/impl/KubevirtRouterManager.java
+++ b/apps/kubevirt-networking/app/src/main/java/org/onosproject/kubevirtnetworking/impl/KubevirtRouterManager.java
@@ -21,6 +21,7 @@
import org.onosproject.core.ApplicationId;
import org.onosproject.core.CoreService;
import org.onosproject.event.ListenerRegistry;
+import org.onosproject.kubevirtnetworking.api.KubevirtFloatingIp;
import org.onosproject.kubevirtnetworking.api.KubevirtPeerRouter;
import org.onosproject.kubevirtnetworking.api.KubevirtRouter;
import org.onosproject.kubevirtnetworking.api.KubevirtRouterAdminService;
@@ -37,6 +38,7 @@
import org.slf4j.Logger;
import java.util.Set;
+import java.util.stream.Collectors;
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;
@@ -57,12 +59,16 @@
protected final Logger log = getLogger(getClass());
private static final String MSG_ROUTER = "Kubevirt router %s %s";
+ private static final String MSG_FLOATING_IP = "Kubevirt floating IP %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_ROUTER = "Kubevirt router cannot be null";
private static final String ERR_NULL_ROUTER_NAME = "Kubevirt router name cannot be null";
+ private static final String ERR_NULL_FLOATING_IP = "Kubevirt floating IP cannot be null";
+ private static final String ERR_NULL_FLOATING_IP_ID = "Kubevirt floating IP ID cannot be null";
+ private static final String ERR_NULL_POD_NAME = "Kubevirt POD name cannot be null";
private static final String ERR_IN_USE = " still in use";
@Reference(cardinality = ReferenceCardinality.MANDATORY)
@@ -142,6 +148,39 @@
}
@Override
+ public void createFloatingIp(KubevirtFloatingIp floatingIp) {
+ checkNotNull(floatingIp, ERR_NULL_FLOATING_IP);
+ checkArgument(!Strings.isNullOrEmpty(floatingIp.id()), ERR_NULL_FLOATING_IP_ID);
+
+ kubevirtRouterStore.createFloatingIp(floatingIp);
+
+ log.info(String.format(MSG_FLOATING_IP, floatingIp.floatingIp(), MSG_CREATED));
+ }
+
+ @Override
+ public void updateFloatingIp(KubevirtFloatingIp floatingIp) {
+ checkNotNull(floatingIp, ERR_NULL_FLOATING_IP);
+ checkArgument(!Strings.isNullOrEmpty(floatingIp.id()), ERR_NULL_FLOATING_IP_ID);
+
+ kubevirtRouterStore.updateFloatingIp(floatingIp);
+
+ log.info(String.format(MSG_FLOATING_IP, floatingIp.floatingIp(), MSG_UPDATED));
+ }
+
+ @Override
+ public void removeFloatingIp(String id) {
+ checkArgument(!Strings.isNullOrEmpty(id), ERR_NULL_FLOATING_IP_ID);
+
+ synchronized (this) {
+ KubevirtFloatingIp floatingIp = kubevirtRouterStore.removeFloatingIp(id);
+
+ if (floatingIp != null) {
+ log.info(String.format(MSG_FLOATING_IP, floatingIp.floatingIp(), MSG_REMOVED));
+ }
+ }
+ }
+
+ @Override
public void clear() {
kubevirtRouterStore.clear();
}
@@ -157,8 +196,35 @@
return ImmutableSet.copyOf(kubevirtRouterStore.routers());
}
+ @Override
+ public KubevirtFloatingIp floatingIp(String id) {
+ checkArgument(!Strings.isNullOrEmpty(id), ERR_NULL_FLOATING_IP_ID);
+ return kubevirtRouterStore.floatingIp(id);
+ }
+
+ @Override
+ public KubevirtFloatingIp floatingIpByPodName(String podName) {
+ checkArgument(!Strings.isNullOrEmpty(podName), ERR_NULL_POD_NAME);
+ return kubevirtRouterStore.floatingIps().stream()
+ .filter(ips -> podName.equals(ips.podName()))
+ .findAny().orElse(null);
+ }
+
+ @Override
+ public Set<KubevirtFloatingIp> floatingIpsByRouter(String routerName) {
+ checkArgument(!Strings.isNullOrEmpty(routerName), ERR_NULL_ROUTER_NAME);
+ return kubevirtRouterStore.floatingIps().stream()
+ .filter(ips -> routerName.equals(ips.routerName()))
+ .collect(Collectors.toSet());
+ }
+
+ @Override
+ public Set<KubevirtFloatingIp> floatingIps() {
+ return ImmutableSet.copyOf(kubevirtRouterStore.floatingIps());
+ }
+
private boolean isRouterInUse(String name) {
- return false;
+ return floatingIpsByRouter(name).size() > 0;
}
private class InternalRouterStorageDelegate implements KubevirtRouterStoreDelegate {
diff --git a/apps/kubevirt-networking/app/src/main/java/org/onosproject/kubevirtnetworking/web/KubevirtFloatingIpsWebResource.java b/apps/kubevirt-networking/app/src/main/java/org/onosproject/kubevirtnetworking/web/KubevirtFloatingIpsWebResource.java
new file mode 100644
index 0000000..6314a31
--- /dev/null
+++ b/apps/kubevirt-networking/app/src/main/java/org/onosproject/kubevirtnetworking/web/KubevirtFloatingIpsWebResource.java
@@ -0,0 +1,51 @@
+/*
+ * 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.web;
+
+import org.onosproject.kubevirtnetworking.api.KubevirtFloatingIp;
+import org.onosproject.kubevirtnetworking.api.KubevirtRouterService;
+import org.onosproject.rest.AbstractWebResource;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import javax.ws.rs.GET;
+import javax.ws.rs.Path;
+import javax.ws.rs.Produces;
+import javax.ws.rs.core.MediaType;
+import javax.ws.rs.core.Response;
+
+/**
+ * Handles REST API call for kubevirt floating IPs.
+ */
+@Path("floatingips")
+public class KubevirtFloatingIpsWebResource extends AbstractWebResource {
+
+ protected final Logger log = LoggerFactory.getLogger(getClass());
+
+ /**
+ * Returns set of all floating IPs.
+ *
+ * @return 200 OK with set of all floating IPs
+ * @onos.rsModel KubevirtFloatingIps
+ */
+ @GET
+ @Produces(MediaType.APPLICATION_JSON)
+ public Response getFloatingIps() {
+ KubevirtRouterService service = get(KubevirtRouterService.class);
+ final Iterable<KubevirtFloatingIp> fips = service.floatingIps();
+ return ok(encodeArray(KubevirtFloatingIp.class, "floatingips", fips)).build();
+ }
+}
diff --git a/apps/kubevirt-networking/app/src/main/java/org/onosproject/kubevirtnetworking/web/KubevirtNetworkingCodecRegister.java b/apps/kubevirt-networking/app/src/main/java/org/onosproject/kubevirtnetworking/web/KubevirtNetworkingCodecRegister.java
index 2539dff..2754e7c 100644
--- a/apps/kubevirt-networking/app/src/main/java/org/onosproject/kubevirtnetworking/web/KubevirtNetworkingCodecRegister.java
+++ b/apps/kubevirt-networking/app/src/main/java/org/onosproject/kubevirtnetworking/web/KubevirtNetworkingCodecRegister.java
@@ -16,11 +16,13 @@
package org.onosproject.kubevirtnetworking.web;
import org.onosproject.codec.CodecService;
+import org.onosproject.kubevirtnetworking.api.KubevirtFloatingIp;
import org.onosproject.kubevirtnetworking.api.KubevirtHostRoute;
import org.onosproject.kubevirtnetworking.api.KubevirtIpPool;
import org.onosproject.kubevirtnetworking.api.KubevirtNetwork;
import org.onosproject.kubevirtnetworking.api.KubevirtPort;
import org.onosproject.kubevirtnetworking.api.KubevirtRouter;
+import org.onosproject.kubevirtnetworking.codec.KubevirtFloatingIpCodec;
import org.onosproject.kubevirtnetworking.codec.KubevirtHostRouteCodec;
import org.onosproject.kubevirtnetworking.codec.KubevirtIpPoolCodec;
import org.onosproject.kubevirtnetworking.codec.KubevirtNetworkCodec;
@@ -54,6 +56,7 @@
codecService.registerCodec(KubevirtNetwork.class, new KubevirtNetworkCodec());
codecService.registerCodec(KubevirtPort.class, new KubevirtPortCodec());
codecService.registerCodec(KubevirtRouter.class, new KubevirtRouterCodec());
+ codecService.registerCodec(KubevirtFloatingIp.class, new KubevirtFloatingIpCodec());
log.info("Started");
}
@@ -66,6 +69,7 @@
codecService.unregisterCodec(KubevirtNetwork.class);
codecService.unregisterCodec(KubevirtPort.class);
codecService.unregisterCodec(KubevirtRouter.class);
+ codecService.unregisterCodec(KubevirtFloatingIp.class);
log.info("Stopped");
}
diff --git a/apps/kubevirt-networking/app/src/main/resources/definitions/KubevirtFloatingIps.json b/apps/kubevirt-networking/app/src/main/resources/definitions/KubevirtFloatingIps.json
new file mode 100644
index 0000000..bffb67d
--- /dev/null
+++ b/apps/kubevirt-networking/app/src/main/resources/definitions/KubevirtFloatingIps.json
@@ -0,0 +1,54 @@
+{
+ "type": "object",
+ "title": "floatingips",
+ "required": [
+ "floatingips"
+ ],
+ "properties": {
+ "floatingips": {
+ "type": "array",
+ "xml": {
+ "name": "floatingips",
+ "wrapped": true
+ },
+ "items": {
+ "type": "object",
+ "description": "A floating IP object.",
+ "required": [
+ "id",
+ "routerName",
+ "floatingIp",
+ "podName",
+ "fixedIp"
+ ],
+ "properties": {
+ "id": {
+ "type": "string",
+ "example": "6a073105-c735-4e53-8d74-64191c6985fe",
+ "description": "The ID of floating IP."
+ },
+ "routerName": {
+ "type": "string",
+ "example": "router-1",
+ "description": "Name of router."
+ },
+ "floatingIp": {
+ "type": "string",
+ "example": "10.10.10.5",
+ "description": "Floating IP address."
+ },
+ "podName": {
+ "type": "string",
+ "example": "pod-1",
+ "description": "Name of POD associated with this floating IP"
+ },
+ "fixedIp": {
+ "type": "string",
+ "example": "20.20.20.5",
+ "description": "Fixed IP address."
+ }
+ }
+ }
+ }
+ }
+}
\ No newline at end of file
diff --git a/apps/kubevirt-networking/app/src/test/java/org/onosproject/kubevirtnetworking/codec/KubevirtFloatingIpCodecTest.java b/apps/kubevirt-networking/app/src/test/java/org/onosproject/kubevirtnetworking/codec/KubevirtFloatingIpCodecTest.java
new file mode 100644
index 0000000..79182da
--- /dev/null
+++ b/apps/kubevirt-networking/app/src/test/java/org/onosproject/kubevirtnetworking/codec/KubevirtFloatingIpCodecTest.java
@@ -0,0 +1,102 @@
+/*
+ * 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.codec;
+
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.node.ObjectNode;
+import org.hamcrest.MatcherAssert;
+import org.junit.Before;
+import org.junit.Test;
+import org.onlab.packet.IpAddress;
+import org.onosproject.codec.JsonCodec;
+import org.onosproject.codec.impl.MockCodecContext;
+import org.onosproject.core.CoreService;
+import org.onosproject.kubevirtnetworking.api.DefaultKubevirtFloatingIp;
+import org.onosproject.kubevirtnetworking.api.KubevirtFloatingIp;
+
+import java.io.IOException;
+import java.io.InputStream;
+
+import static junit.framework.TestCase.assertEquals;
+import static org.easymock.EasyMock.createMock;
+import static org.easymock.EasyMock.expect;
+import static org.easymock.EasyMock.replay;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.notNullValue;
+import static org.onosproject.kubevirtnetworking.codec.KubevirtFloatingIpJsonMatcher.matchesKubevirtFloatingIp;
+import static org.onosproject.net.NetTestTools.APP_ID;
+
+/**
+ * Unit tests for KubevirtFloatingIp codec.
+ */
+public final class KubevirtFloatingIpCodecTest {
+
+ MockCodecContext context;
+
+ JsonCodec<KubevirtFloatingIp> kubevirtFloatingIpCodec;
+
+ final CoreService mockCoreService = createMock(CoreService.class);
+ private static final String REST_APP_ID = "org.onosproject.rest";
+
+ @Before
+ public void setUp() {
+ context = new MockCodecContext();
+ kubevirtFloatingIpCodec = new KubevirtFloatingIpCodec();
+
+ assertThat(kubevirtFloatingIpCodec, notNullValue());
+ expect(mockCoreService.registerApplication(REST_APP_ID))
+ .andReturn(APP_ID).anyTimes();
+ replay(mockCoreService);
+ context.registerService(CoreService.class, mockCoreService);
+ }
+
+ /**
+ * Tests the kubevirt floating IP encoding.
+ */
+ @Test
+ public void testKubevirtFloatingIpEncode() {
+ KubevirtFloatingIp floatingIp = DefaultKubevirtFloatingIp.builder()
+ .id("fip-id")
+ .routerName("router-1")
+ .floatingIp(IpAddress.valueOf("10.10.10.10"))
+ .podName("pod-1")
+ .fixedIp(IpAddress.valueOf("20.20.20.20"))
+ .build();
+
+ ObjectNode floatingIpJson = kubevirtFloatingIpCodec.encode(floatingIp, context);
+ assertThat(floatingIpJson, matchesKubevirtFloatingIp(floatingIp));
+ }
+
+ @Test
+ public void testKubevirtFloatingIpDecode() throws IOException {
+ KubevirtFloatingIp floatingIp = getKubevirtFloatingIp("KubevirtFloatingIp.json");
+
+ assertEquals("fip-1", floatingIp.id());
+ assertEquals("router-1", floatingIp.routerName());
+ assertEquals("10.10.10.10", floatingIp.floatingIp().toString());
+ assertEquals("pod-1", floatingIp.podName());
+ assertEquals("20.20.20.20", floatingIp.fixedIp().toString());
+ }
+
+ private KubevirtFloatingIp getKubevirtFloatingIp(String resourceName) throws IOException {
+ InputStream jsonStream = KubevirtFloatingIpCodecTest.class.getResourceAsStream(resourceName);
+ JsonNode json = context.mapper().readTree(jsonStream);
+ MatcherAssert.assertThat(json, notNullValue());
+ KubevirtFloatingIp fip = kubevirtFloatingIpCodec.decode((ObjectNode) json, context);
+ assertThat(fip, notNullValue());
+ return fip;
+ }
+}
diff --git a/apps/kubevirt-networking/app/src/test/java/org/onosproject/kubevirtnetworking/codec/KubevirtFloatingIpJsonMatcher.java b/apps/kubevirt-networking/app/src/test/java/org/onosproject/kubevirtnetworking/codec/KubevirtFloatingIpJsonMatcher.java
new file mode 100644
index 0000000..0bcfa0a
--- /dev/null
+++ b/apps/kubevirt-networking/app/src/test/java/org/onosproject/kubevirtnetworking/codec/KubevirtFloatingIpJsonMatcher.java
@@ -0,0 +1,101 @@
+/*
+ * 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.codec;
+
+import com.fasterxml.jackson.databind.JsonNode;
+import org.hamcrest.Description;
+import org.hamcrest.TypeSafeDiagnosingMatcher;
+import org.onosproject.kubevirtnetworking.api.KubevirtFloatingIp;
+
+/**
+ * Hamcrest matcher for kubevirt router interface.
+ */
+public final class KubevirtFloatingIpJsonMatcher extends TypeSafeDiagnosingMatcher<JsonNode> {
+
+ private final KubevirtFloatingIp floatingIp;
+ private static final String ID = "id";
+ private static final String ROUTER_NAME = "routerName";
+ private static final String POD_NAME = "podName";
+ private static final String FLOATING_IP = "floatingIp";
+ private static final String FIXED_IP = "fixedIp";
+
+ private KubevirtFloatingIpJsonMatcher(KubevirtFloatingIp floatingIp) {
+ this.floatingIp = floatingIp;
+ }
+
+ @Override
+ protected boolean matchesSafely(JsonNode jsonNode, Description description) {
+
+ // check ID
+ String jsonId = jsonNode.get(ID).asText();
+ String id = floatingIp.id();
+ if (!jsonId.equals(id)) {
+ description.appendText("ID was " + jsonId);
+ return false;
+ }
+
+ // check router name
+ String jsonRouterName = jsonNode.get(ROUTER_NAME).asText();
+ String routerName = floatingIp.routerName();
+ if (!jsonRouterName.equals(routerName)) {
+ description.appendText("Router name was " + jsonRouterName);
+ return false;
+ }
+
+ // check floating IP
+ String jsonFip = jsonNode.get(FLOATING_IP).asText();
+ String fip = floatingIp.floatingIp().toString();
+ if (!jsonFip.equals(fip)) {
+ description.appendText("Floating IP was " + jsonFip);
+ return false;
+ }
+
+ // check POD name
+ JsonNode jsonPodName = jsonNode.get(POD_NAME);
+ if (jsonPodName != null) {
+ if (!floatingIp.podName().equals(jsonPodName.asText())) {
+ description.appendText("POD name was " + jsonPodName);
+ return false;
+ }
+ }
+
+ // check fixed IP
+ JsonNode jsonFixedIp = jsonNode.get(FIXED_IP);
+ if (jsonFixedIp != null) {
+ if (!floatingIp.fixedIp().toString().equals(jsonFixedIp.asText())) {
+ description.appendText("Fixed IP was " + jsonFixedIp);
+ return false;
+ }
+ }
+
+ return true;
+ }
+
+ @Override
+ public void describeTo(Description description) {
+ description.appendText(floatingIp.toString());
+ }
+
+ /**
+ * Factory to allocate a kubevirt floating IP matcher.
+ *
+ * @param fip kubevirt floating IP object we are looking for
+ * @return matcher
+ */
+ public static KubevirtFloatingIpJsonMatcher matchesKubevirtFloatingIp(KubevirtFloatingIp fip) {
+ return new KubevirtFloatingIpJsonMatcher(fip);
+ }
+}
diff --git a/apps/kubevirt-networking/app/src/test/java/org/onosproject/kubevirtnetworking/impl/KubevirtRouterManagerTest.java b/apps/kubevirt-networking/app/src/test/java/org/onosproject/kubevirtnetworking/impl/KubevirtRouterManagerTest.java
index 8a7e553..2bc222e 100644
--- a/apps/kubevirt-networking/app/src/test/java/org/onosproject/kubevirtnetworking/impl/KubevirtRouterManagerTest.java
+++ b/apps/kubevirt-networking/app/src/test/java/org/onosproject/kubevirtnetworking/impl/KubevirtRouterManagerTest.java
@@ -29,7 +29,9 @@
import org.onosproject.core.CoreServiceAdapter;
import org.onosproject.core.DefaultApplicationId;
import org.onosproject.event.Event;
+import org.onosproject.kubevirtnetworking.api.DefaultKubevirtFloatingIp;
import org.onosproject.kubevirtnetworking.api.DefaultKubevirtRouter;
+import org.onosproject.kubevirtnetworking.api.KubevirtFloatingIp;
import org.onosproject.kubevirtnetworking.api.KubevirtPeerRouter;
import org.onosproject.kubevirtnetworking.api.KubevirtRouter;
import org.onosproject.kubevirtnetworking.api.KubevirtRouterEvent;
@@ -41,6 +43,11 @@
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
+import static org.onosproject.kubevirtnetworking.api.KubevirtRouterEvent.Type.KUBEVIRT_FLOATING_IP_ASSOCIATED;
+import static org.onosproject.kubevirtnetworking.api.KubevirtRouterEvent.Type.KUBEVIRT_FLOATING_IP_CREATED;
+import static org.onosproject.kubevirtnetworking.api.KubevirtRouterEvent.Type.KUBEVIRT_FLOATING_IP_DISASSOCIATED;
+import static org.onosproject.kubevirtnetworking.api.KubevirtRouterEvent.Type.KUBEVIRT_FLOATING_IP_REMOVED;
+import static org.onosproject.kubevirtnetworking.api.KubevirtRouterEvent.Type.KUBEVIRT_FLOATING_IP_UPDATED;
import static org.onosproject.kubevirtnetworking.api.KubevirtRouterEvent.Type.KUBEVIRT_ROUTER_CREATED;
import static org.onosproject.kubevirtnetworking.api.KubevirtRouterEvent.Type.KUBEVIRT_ROUTER_REMOVED;
import static org.onosproject.kubevirtnetworking.api.KubevirtRouterEvent.Type.KUBEVIRT_ROUTER_UPDATED;
@@ -53,10 +60,13 @@
private static final ApplicationId TEST_APP_ID = new DefaultApplicationId(1, "test");
private static final String ROUTER_NAME = "router-1";
+ private static final String POD_NAME = "pod-1";
private static final String UPDATED_DESCRIPTION = "router-updated";
-
private static final MacAddress UPDATED_MAC = MacAddress.valueOf("FF:FF:FF:FF:FF:FF");
+ private static final String FLOATING_IP_ID = "fip-1";
+ private static final String UNKNOWN_ID = "unknown";
+
private static final KubevirtRouter ROUTER = DefaultKubevirtRouter.builder()
.name(ROUTER_NAME)
.description(ROUTER_NAME)
@@ -77,6 +87,20 @@
MacAddress.valueOf("11:22:33:44:55:66")))
.build();
+ private static final KubevirtFloatingIp FLOATING_IP_DISASSOCIATED = DefaultKubevirtFloatingIp.builder()
+ .id(FLOATING_IP_ID)
+ .routerName(ROUTER_NAME)
+ .floatingIp(IpAddress.valueOf("10.10.10.10"))
+ .build();
+
+ private static final KubevirtFloatingIp FLOATING_IP_ASSOCIATED = DefaultKubevirtFloatingIp.builder()
+ .id(FLOATING_IP_ID)
+ .routerName(ROUTER_NAME)
+ .floatingIp(IpAddress.valueOf("10.10.10.10"))
+ .fixedIp(IpAddress.valueOf("20.20.20.20"))
+ .podName(POD_NAME)
+ .build();
+
private final TestKubevirtRouterListener testListener = new TestKubevirtRouterListener();
private KubevirtRouterManager target;
@@ -202,10 +226,140 @@
target.updateRouter(null);
}
+ /**
+ * Tests if getting all floating IPs returns the correct set of floating IPs.
+ */
+ @Test
+ public void testGetFloatingIps() {
+ createBasicFloatingIpDisassociated();
+ assertEquals("Number of floating IPs did not match", 1, target.floatingIps().size());
+ }
+
+ /**
+ * Tests if getting a floating IP with ID returns the correct floating IP.
+ */
+ @Test
+ public void testGetFloatingIpById() {
+ createBasicFloatingIpDisassociated();
+ assertNotNull("Floating IP did not match", target.floatingIp(FLOATING_IP_ID));
+ assertNull("Floating IP did not match", target.floatingIp(UNKNOWN_ID));
+ }
+
+ /**
+ * Tests if getting a floating IP with POD name returns the correct floating IP.
+ */
+ @Test
+ public void testGetFloatingIpByPodName() {
+ createBasicFloatingIpAssociated();
+ assertNotNull("Floating IP did not match", target.floatingIpByPodName(POD_NAME));
+ assertNull("Floating IP did not match", target.floatingIpByPodName(UNKNOWN_ID));
+ }
+
+ /**
+ * Tests if getting floating IPs with router name returns the correct floating IPs.
+ */
+ @Test
+ public void testGetFloatingIpsByRouterName() {
+ createBasicFloatingIpDisassociated();
+ assertEquals("Number of floating IPs did not match", 1,
+ target.floatingIpsByRouter(ROUTER_NAME).size());
+ }
+
+ /**
+ * Tests creating and removing a floating IP, and checks if it triggers proper events.
+ */
+ @Test
+ public void testCreateAndRemoveFloatingIp() {
+ target.createFloatingIp(FLOATING_IP_DISASSOCIATED);
+ assertEquals("Number of floating IP did not match", 1, target.floatingIps().size());
+ assertNotNull("Floating IP was not created", target.floatingIp(FLOATING_IP_ID));
+
+ target.removeFloatingIp(FLOATING_IP_ID);
+ assertEquals("Number of floating IP did not match", 0, target.floatingIps().size());
+ assertNull("Floating IP was not created", target.floatingIp(FLOATING_IP_ID));
+
+ validateEvents(KUBEVIRT_FLOATING_IP_CREATED, KUBEVIRT_FLOATING_IP_REMOVED);
+ }
+
+ /**
+ * Tests associating a floating IP, and checks if it triggers proper events.
+ */
+ @Test
+ public void testAssociateFloatingIp() {
+ target.createFloatingIp(FLOATING_IP_DISASSOCIATED);
+ assertEquals("Number of floating IP did not match", 1, target.floatingIps().size());
+ assertNotNull("Floating IP was not created", target.floatingIp(FLOATING_IP_ID));
+
+ target.updateFloatingIp(FLOATING_IP_ASSOCIATED);
+ assertEquals("Number of floating IP did not match", 1, target.floatingIps().size());
+ assertNotNull("Floating IP was not created", target.floatingIp(FLOATING_IP_ID));
+
+ validateEvents(KUBEVIRT_FLOATING_IP_CREATED, KUBEVIRT_FLOATING_IP_UPDATED,
+ KUBEVIRT_FLOATING_IP_ASSOCIATED);
+ }
+
+ /**
+ * Tests disassociating a floating IP, and checks if it triggers proper events.
+ */
+ @Test
+ public void testDisassociateFloatingIp() {
+ target.createFloatingIp(FLOATING_IP_ASSOCIATED);
+ assertEquals("Number of floating IP did not match", 1, target.floatingIps().size());
+ assertNotNull("Floating IP was not created", target.floatingIp(FLOATING_IP_ID));
+
+ target.updateFloatingIp(FLOATING_IP_DISASSOCIATED);
+ assertEquals("Number of floating IP did not match", 1, target.floatingIps().size());
+ assertNotNull("Floating IP was not created", target.floatingIp(FLOATING_IP_ID));
+
+ validateEvents(KUBEVIRT_FLOATING_IP_CREATED, KUBEVIRT_FLOATING_IP_UPDATED,
+ KUBEVIRT_FLOATING_IP_DISASSOCIATED);
+ }
+
+ /**
+ * Tests if creating a null floating IP fails with an exception.
+ */
+ @Test(expected = NullPointerException.class)
+ public void testCreateNullFloatingIp() {
+ target.createFloatingIp(null);
+ }
+
+ /**
+ * Tests if creating a duplicate floating IP fails with an exception.
+ */
+ @Test(expected = IllegalArgumentException.class)
+ public void testCreateDuplicateFloatingIp() {
+ target.createFloatingIp(FLOATING_IP_ASSOCIATED);
+ target.createFloatingIp(FLOATING_IP_DISASSOCIATED);
+ }
+
+ /**
+ * Tests if removing floating IP with null ID fails with an exception.
+ */
+ @Test(expected = IllegalArgumentException.class)
+ public void testRemoveFloatingIpWithNull() {
+ target.removeFloatingIp(null);
+ }
+
+ /**
+ * Tests if updating an unregistered floating IP fails with an exception.
+ */
+ @Test(expected = IllegalArgumentException.class)
+ public void testUpdateUnregisteredFloatingIp() {
+ target.updateFloatingIp(FLOATING_IP_ASSOCIATED);
+ }
+
private void createBasicRouters() {
target.createRouter(ROUTER);
}
+ private void createBasicFloatingIpDisassociated() {
+ target.createFloatingIp(FLOATING_IP_DISASSOCIATED);
+ }
+
+ private void createBasicFloatingIpAssociated() {
+ target.createFloatingIp(FLOATING_IP_ASSOCIATED);
+ }
+
private static class TestCoreService extends CoreServiceAdapter {
@Override
diff --git a/apps/kubevirt-networking/app/src/test/resources/org/onosproject/kubevirtnetworking/codec/KubevirtFloatingIp.json b/apps/kubevirt-networking/app/src/test/resources/org/onosproject/kubevirtnetworking/codec/KubevirtFloatingIp.json
new file mode 100644
index 0000000..fa85c53
--- /dev/null
+++ b/apps/kubevirt-networking/app/src/test/resources/org/onosproject/kubevirtnetworking/codec/KubevirtFloatingIp.json
@@ -0,0 +1,7 @@
+{
+ "id": "fip-1",
+ "routerName": "router-1",
+ "floatingIp": "10.10.10.10",
+ "podName": "pod-1",
+ "fixedIp": "20.20.20.20"
+}
\ No newline at end of file