Support to store and monitor kubevirt port resource
Change-Id: Id1d8c7a9eb2c351ccb9ae27e5f0730aa37a8a720
diff --git a/apps/kubevirt-networking/api/src/main/java/org/onosproject/kubevirtnetworking/api/DefaultKubevirtPort.java b/apps/kubevirt-networking/api/src/main/java/org/onosproject/kubevirtnetworking/api/DefaultKubevirtPort.java
index 410c098..4fe0125 100644
--- a/apps/kubevirt-networking/api/src/main/java/org/onosproject/kubevirtnetworking/api/DefaultKubevirtPort.java
+++ b/apps/kubevirt-networking/api/src/main/java/org/onosproject/kubevirtnetworking/api/DefaultKubevirtPort.java
@@ -83,12 +83,24 @@
@Override
public KubevirtPort updatePortNumber(PortNumber portNumber) {
- return null;
+ return new Builder()
+ .networkId(networkId)
+ .macAddress(macAddress)
+ .ipAddress(ipAddress)
+ .deviceId(deviceId)
+ .portNumber(portNumber)
+ .build();
}
@Override
public KubevirtPort updateDeviceId(DeviceId deviceId) {
- return null;
+ return new Builder()
+ .networkId(networkId)
+ .macAddress(macAddress)
+ .ipAddress(ipAddress)
+ .deviceId(deviceId)
+ .portNumber(portNumber)
+ .build();
}
@Override
diff --git a/apps/kubevirt-networking/api/src/main/java/org/onosproject/kubevirtnetworking/api/KubevirtPortService.java b/apps/kubevirt-networking/api/src/main/java/org/onosproject/kubevirtnetworking/api/KubevirtPortService.java
index 7861bb3..61b6338 100644
--- a/apps/kubevirt-networking/api/src/main/java/org/onosproject/kubevirtnetworking/api/KubevirtPortService.java
+++ b/apps/kubevirt-networking/api/src/main/java/org/onosproject/kubevirtnetworking/api/KubevirtPortService.java
@@ -35,6 +35,14 @@
KubevirtPort port(MacAddress mac);
/**
+ * Returns the kubevirt ports belongs to the given network.
+ *
+ * @param networkId network identifier
+ * @return kubevirt ports
+ */
+ Set<KubevirtPort> ports(String networkId);
+
+ /**
* Returns all kubevirt ports registered.
*
* @return set of kubevirt ports
diff --git a/apps/kubevirt-networking/api/src/main/java/org/onosproject/kubevirtnetworking/api/KubevirtPortStore.java b/apps/kubevirt-networking/api/src/main/java/org/onosproject/kubevirtnetworking/api/KubevirtPortStore.java
index 0daf301..d08f3b5 100644
--- a/apps/kubevirt-networking/api/src/main/java/org/onosproject/kubevirtnetworking/api/KubevirtPortStore.java
+++ b/apps/kubevirt-networking/api/src/main/java/org/onosproject/kubevirtnetworking/api/KubevirtPortStore.java
@@ -38,7 +38,7 @@
*
* @param port kubevirt port
*/
- void updatedPort(KubevirtPort port);
+ void updatePort(KubevirtPort port);
/**
* Removes the kubevirt port with the given MAC address.
diff --git a/apps/kubevirt-networking/app/src/main/java/org/onosproject/kubevirtnetworking/impl/DistributedKubevirtPortStore.java b/apps/kubevirt-networking/app/src/main/java/org/onosproject/kubevirtnetworking/impl/DistributedKubevirtPortStore.java
new file mode 100644
index 0000000..2c5cd3f
--- /dev/null
+++ b/apps/kubevirt-networking/app/src/main/java/org/onosproject/kubevirtnetworking/impl/DistributedKubevirtPortStore.java
@@ -0,0 +1,185 @@
+/*
+ * 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.google.common.collect.ImmutableSet;
+import org.onlab.packet.MacAddress;
+import org.onlab.util.KryoNamespace;
+import org.onosproject.core.ApplicationId;
+import org.onosproject.core.CoreService;
+import org.onosproject.kubevirtnetworking.api.DefaultKubevirtPort;
+import org.onosproject.kubevirtnetworking.api.KubevirtPort;
+import org.onosproject.kubevirtnetworking.api.KubevirtPortEvent;
+import org.onosproject.kubevirtnetworking.api.KubevirtPortStore;
+import org.onosproject.kubevirtnetworking.api.KubevirtPortStoreDelegate;
+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.KubevirtPortEvent.Type.KUBEVIRT_PORT_CREATED;
+import static org.onosproject.kubevirtnetworking.api.KubevirtPortEvent.Type.KUBEVIRT_PORT_REMOVED;
+import static org.onosproject.kubevirtnetworking.api.KubevirtPortEvent.Type.KUBEVIRT_PORT_UPDATED;
+import static org.slf4j.LoggerFactory.getLogger;
+
+/**
+ * Implementation of kubevirt pod store using consistent map.
+ */
+@Component(immediate = true, service = KubevirtPortStore.class)
+public class DistributedKubevirtPortStore
+ extends AbstractStore<KubevirtPortEvent, KubevirtPortStoreDelegate>
+ implements KubevirtPortStore {
+
+ 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_KUBEVIRT_PORT = KryoNamespace.newBuilder()
+ .register(KryoNamespaces.API)
+ .register(KubevirtPort.class)
+ .register(DefaultKubevirtPort.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, KubevirtPort> portMapListener =
+ new KubevirtPortMapListener();
+
+ private ConsistentMap<String, KubevirtPort> portStore;
+
+ @Activate
+ protected void activate() {
+ ApplicationId appId = coreService.registerApplication(APP_ID);
+ portStore = storageService.<String, KubevirtPort>consistentMapBuilder()
+ .withSerializer(Serializer.using(SERIALIZER_KUBEVIRT_PORT))
+ .withName("kubevirt-portstore")
+ .withApplicationId(appId)
+ .build();
+ portStore.addListener(portMapListener);
+ log.info("Started");
+ }
+
+ @Deactivate
+ protected void deactivate() {
+ portStore.removeListener(portMapListener);
+ eventExecutor.shutdown();
+ log.info("Stopped");
+ }
+
+ @Override
+ public void createPort(KubevirtPort port) {
+ portStore.compute(port.macAddress().toString(), (mac, existing) -> {
+ final String error = port.macAddress().toString() + ERR_DUPLICATE;
+ checkArgument(existing == null, error);
+ return port;
+ });
+ }
+
+ @Override
+ public void updatePort(KubevirtPort port) {
+ portStore.compute(port.macAddress().toString(), (mac, existing) -> {
+ final String error = port.macAddress().toString() + ERR_NOT_FOUND;
+ checkArgument(existing != null, error);
+ return port;
+ });
+ }
+
+ @Override
+ public KubevirtPort removePort(MacAddress mac) {
+ Versioned<KubevirtPort> port = portStore.remove(mac.toString());
+ if (port == null) {
+ final String error = mac.toString() + ERR_NOT_FOUND;
+ throw new IllegalArgumentException(error);
+ }
+ return port.value();
+ }
+
+ @Override
+ public KubevirtPort port(MacAddress mac) {
+ return portStore.asJavaMap().get(mac.toString());
+ }
+
+ @Override
+ public Set<KubevirtPort> ports() {
+ return ImmutableSet.copyOf(portStore.asJavaMap().values());
+ }
+
+ @Override
+ public void clear() {
+ portStore.clear();
+ }
+
+ private class KubevirtPortMapListener implements MapEventListener<String, KubevirtPort> {
+
+ @Override
+ public void event(MapEvent<String, KubevirtPort> event) {
+ switch (event.type()) {
+ case INSERT:
+ log.debug("Kubevirt port created");
+ eventExecutor.execute(() ->
+ notifyDelegate(new KubevirtPortEvent(
+ KUBEVIRT_PORT_CREATED, event.newValue().value())));
+ break;
+ case UPDATE:
+ log.debug("Kubevirt port updated");
+ eventExecutor.execute(() ->
+ notifyDelegate(new KubevirtPortEvent(
+ KUBEVIRT_PORT_UPDATED, event.newValue().value())));
+ break;
+ case REMOVE:
+ log.debug("Kubevirt port removed");
+ // if the event object has invalid port value, we do not
+ // propagate KUBEVIRT_PORT_REMOVED event.
+ if (event.oldValue() != null && event.oldValue().value() != null) {
+ notifyDelegate(new KubevirtPortEvent(
+ KUBEVIRT_PORT_REMOVED, event.oldValue().value()));
+ }
+ break;
+ default:
+ // do nothing
+ break;
+ }
+ }
+ }
+}
diff --git a/apps/kubevirt-networking/app/src/main/java/org/onosproject/kubevirtnetworking/impl/KubevirtPortManager.java b/apps/kubevirt-networking/app/src/main/java/org/onosproject/kubevirtnetworking/impl/KubevirtPortManager.java
new file mode 100644
index 0000000..b8160e2
--- /dev/null
+++ b/apps/kubevirt-networking/app/src/main/java/org/onosproject/kubevirtnetworking/impl/KubevirtPortManager.java
@@ -0,0 +1,166 @@
+/*
+ * 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.google.common.base.Strings;
+import com.google.common.collect.ImmutableSet;
+import org.onlab.packet.MacAddress;
+import org.onosproject.core.ApplicationId;
+import org.onosproject.core.CoreService;
+import org.onosproject.event.ListenerRegistry;
+import org.onosproject.kubevirtnetworking.api.KubevirtPort;
+import org.onosproject.kubevirtnetworking.api.KubevirtPortAdminService;
+import org.onosproject.kubevirtnetworking.api.KubevirtPortEvent;
+import org.onosproject.kubevirtnetworking.api.KubevirtPortListener;
+import org.onosproject.kubevirtnetworking.api.KubevirtPortService;
+import org.onosproject.kubevirtnetworking.api.KubevirtPortStore;
+import org.onosproject.kubevirtnetworking.api.KubevirtPortStoreDelegate;
+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 java.util.stream.Collectors;
+
+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 administering and interfacing kubevirt port.
+ */
+@Component(
+ immediate = true,
+ service = {KubevirtPortAdminService.class, KubevirtPortService.class }
+)
+public class KubevirtPortManager
+ extends ListenerRegistry<KubevirtPortEvent, KubevirtPortListener>
+ implements KubevirtPortAdminService, KubevirtPortService {
+
+ protected final Logger log = getLogger(getClass());
+
+ private static final String MSG_PORT = "Kubernetes port %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_PORT = "Kubevirt port cannot be null";
+ private static final String ERR_NULL_PORT_MAC = "Kubevirt port MAC cannot be null";
+ private static final String ERR_NULL_PORT_NET_ID = "Kubevirt port network ID 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 KubevirtPortStore kubevirtPortStore;
+
+ private final InternalPortStorageDelegate delegate = new InternalPortStorageDelegate();
+
+ private ApplicationId appId;
+
+ @Activate
+ protected void activate() {
+ appId = coreService.registerApplication(KUBEVIRT_NETWORKING_APP_ID);
+
+ kubevirtPortStore.setDelegate(delegate);
+ log.info("Started");
+ }
+
+ @Deactivate
+ protected void deactivate() {
+ kubevirtPortStore.unsetDelegate(delegate);
+ log.info("Stopped");
+ }
+
+ @Override
+ public void createPort(KubevirtPort port) {
+ checkNotNull(port, ERR_NULL_PORT);
+ checkArgument(!Strings.isNullOrEmpty(port.macAddress().toString()), ERR_NULL_PORT_MAC);
+ checkArgument(!Strings.isNullOrEmpty(port.networkId()), ERR_NULL_PORT_NET_ID);
+
+ kubevirtPortStore.createPort(port);
+ log.info(String.format(MSG_PORT, port.macAddress().toString(), MSG_CREATED));
+ }
+
+ @Override
+ public void updatePort(KubevirtPort port) {
+ checkNotNull(port, ERR_NULL_PORT);
+ checkArgument(!Strings.isNullOrEmpty(port.macAddress().toString()), ERR_NULL_PORT_MAC);
+ checkArgument(!Strings.isNullOrEmpty(port.networkId()), ERR_NULL_PORT_NET_ID);
+
+ kubevirtPortStore.updatePort(port);
+ log.info(String.format(MSG_PORT, port.macAddress().toString(), MSG_UPDATED));
+ }
+
+ @Override
+ public void removePort(MacAddress mac) {
+ checkArgument(mac != null, ERR_NULL_PORT_MAC);
+ synchronized (this) {
+ if (isPortInUse(mac.toString())) {
+ final String error = String.format(MSG_PORT, mac.toString(), ERR_IN_USE);
+ throw new IllegalStateException(error);
+ }
+ KubevirtPort port = kubevirtPortStore.removePort(mac);
+ if (port != null) {
+ log.info(String.format(MSG_PORT, port.macAddress().toString(), MSG_REMOVED));
+ }
+ }
+ }
+
+ @Override
+ public void clear() {
+ kubevirtPortStore.clear();
+ }
+
+ @Override
+ public KubevirtPort port(MacAddress mac) {
+ checkArgument(mac != null, ERR_NULL_PORT_MAC);
+ return kubevirtPortStore.port(mac);
+ }
+
+ @Override
+ public Set<KubevirtPort> ports(String networkId) {
+ checkArgument(!Strings.isNullOrEmpty(networkId), ERR_NULL_PORT_NET_ID);
+ return ImmutableSet.copyOf(kubevirtPortStore.ports().stream()
+ .filter(p -> p.networkId().equals(networkId))
+ .collect(Collectors.toSet()));
+ }
+
+ @Override
+ public Set<KubevirtPort> ports() {
+ return ImmutableSet.copyOf(kubevirtPortStore.ports());
+ }
+
+ private boolean isPortInUse(String portId) {
+ return false;
+ }
+
+ private class InternalPortStorageDelegate implements KubevirtPortStoreDelegate {
+
+ @Override
+ public void notify(KubevirtPortEvent event) {
+ if (event != null) {
+ log.trace("send kubevirt networking event {}", event);
+ process(event);
+ }
+ }
+ }
+}
diff --git a/apps/kubevirt-networking/app/src/test/java/org/onosproject/kubevirtnetworking/impl/KubevirtPortManagerTest.java b/apps/kubevirt-networking/app/src/test/java/org/onosproject/kubevirtnetworking/impl/KubevirtPortManagerTest.java
new file mode 100644
index 0000000..e6e4219
--- /dev/null
+++ b/apps/kubevirt-networking/app/src/test/java/org/onosproject/kubevirtnetworking/impl/KubevirtPortManagerTest.java
@@ -0,0 +1,228 @@
+/*
+ * 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.google.common.collect.Lists;
+import com.google.common.util.concurrent.MoreExecutors;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+import org.onlab.junit.TestUtils;
+import org.onlab.packet.IpAddress;
+import org.onlab.packet.MacAddress;
+import org.onosproject.core.ApplicationId;
+import org.onosproject.core.CoreServiceAdapter;
+import org.onosproject.core.DefaultApplicationId;
+import org.onosproject.event.Event;
+import org.onosproject.kubevirtnetworking.api.DefaultKubevirtPort;
+import org.onosproject.kubevirtnetworking.api.KubevirtPort;
+import org.onosproject.kubevirtnetworking.api.KubevirtPortEvent;
+import org.onosproject.kubevirtnetworking.api.KubevirtPortListener;
+import org.onosproject.net.DeviceId;
+import org.onosproject.net.PortNumber;
+import org.onosproject.store.service.TestStorageService;
+
+import java.util.List;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
+import static org.onosproject.kubevirtnetworking.api.KubevirtPortEvent.Type.KUBEVIRT_PORT_CREATED;
+import static org.onosproject.kubevirtnetworking.api.KubevirtPortEvent.Type.KUBEVIRT_PORT_REMOVED;
+import static org.onosproject.kubevirtnetworking.api.KubevirtPortEvent.Type.KUBEVIRT_PORT_UPDATED;
+
+/**
+ * Unit tests for kubernetes network manager.
+ */
+public class KubevirtPortManagerTest {
+
+ private static final ApplicationId TEST_APP_ID = new DefaultApplicationId(1, "test");
+
+ private static final String UNKNOWN_ID = "unknown_id";
+ private static final String NETWORK_ID = "network_id";
+ private static final String UPDATED_ID = "updated_id";
+
+ private static final String PORT_MAC = "00:11:22:33:44:55";
+ private static final KubevirtPort PORT = DefaultKubevirtPort.builder()
+ .networkId(NETWORK_ID)
+ .deviceId(DeviceId.deviceId("dev-1"))
+ .ipAddress(IpAddress.valueOf("20.20.20.20"))
+ .macAddress(MacAddress.valueOf("00:11:22:33:44:55"))
+ .portNumber(PortNumber.portNumber("1"))
+ .build();
+ private static final KubevirtPort PORT_UPDATED = DefaultKubevirtPort.builder()
+ .networkId(UPDATED_ID)
+ .deviceId(DeviceId.deviceId("dev-1"))
+ .ipAddress(IpAddress.valueOf("20.20.20.20"))
+ .macAddress(MacAddress.valueOf("00:11:22:33:44:55"))
+ .portNumber(PortNumber.portNumber("1"))
+ .build();
+
+ private final TestKubevirtPortListener testListener = new TestKubevirtPortListener();
+
+ private KubevirtPortManager target;
+ private DistributedKubevirtPortStore kubevirtPortStore;
+
+ @Before
+ public void setUp() throws Exception {
+ kubevirtPortStore = new DistributedKubevirtPortStore();
+ TestUtils.setField(kubevirtPortStore, "coreService", new TestCoreService());
+ TestUtils.setField(kubevirtPortStore, "storageService", new TestStorageService());
+ TestUtils.setField(kubevirtPortStore, "eventExecutor", MoreExecutors.newDirectExecutorService());
+ kubevirtPortStore.activate();
+
+ target = new KubevirtPortManager();
+ TestUtils.setField(target, "coreService", new TestCoreService());
+ target.kubevirtPortStore = kubevirtPortStore;
+ target.addListener(testListener);
+ target.activate();
+ }
+
+ @After
+ public void tearDown() {
+ target.removeListener(testListener);
+ kubevirtPortStore.deactivate();
+ target.deactivate();
+ kubevirtPortStore = null;
+ target = null;
+ }
+
+ /**
+ * Tests if getting all ports returns correct set of values.
+ */
+ @Test
+ public void testGetPorts() {
+ createBasicPorts();
+ assertEquals("Number of port did not match", 1, target.ports().size());
+ }
+
+ /**
+ * Tests if getting a port with network ID returns correct set of values.
+ */
+ @Test
+ public void testGetPortsByNetworkId() {
+ createBasicPorts();
+ assertEquals("Number of port did not match", 1, target.ports(NETWORK_ID).size());
+ assertEquals("Number of port did not match", 0, target.ports(UNKNOWN_ID).size());
+ }
+
+ /**
+ * Tests if getting a port with ID returns correct value.
+ */
+ @Test
+ public void testGetPortById() {
+ createBasicPorts();
+ assertNotNull("Port did not match", target.port(MacAddress.valueOf(PORT_MAC)));
+ }
+
+ /**
+ * Tests creating and removing a port, and checks if proper event is triggered.
+ */
+ @Test
+ public void testCreateAndRemovePort() {
+ target.createPort(PORT);
+ assertEquals("Number of port did not match", 1, target.ports().size());
+ assertNotNull("Port was not created", target.port(MacAddress.valueOf(PORT_MAC)));
+
+ target.removePort(MacAddress.valueOf(PORT_MAC));
+ assertEquals("Number of port did not match", 0, target.ports().size());
+ assertNull("Port was not created", target.port(MacAddress.valueOf(PORT_MAC)));
+
+ validateEvents(KUBEVIRT_PORT_CREATED, KUBEVIRT_PORT_REMOVED);
+ }
+
+ /**
+ * Tests creating and updating a port, and checks if proper event is triggered.
+ */
+ @Test
+ public void testCreateAndUpdatePort() {
+ target.createPort(PORT);
+ assertEquals("Number of port did not match", 1, target.ports().size());
+
+ target.updatePort(PORT_UPDATED);
+
+ assertEquals("Number of port did not match", 1, target.ports().size());
+ assertEquals("Port did not match", UPDATED_ID,
+ target.port(MacAddress.valueOf(PORT_MAC)).networkId());
+
+ validateEvents(KUBEVIRT_PORT_CREATED, KUBEVIRT_PORT_UPDATED);
+ }
+
+ /**
+ * Tests if creating a null port fails with an exception.
+ */
+ @Test(expected = NullPointerException.class)
+ public void testCreateNullPort() {
+ target.createPort(null);
+ }
+
+ /**
+ * Tests if creating a duplicate port fails with an exception.
+ */
+ @Test(expected = IllegalArgumentException.class)
+ public void createDuplicatePort() {
+ target.createPort(PORT);
+ target.createPort(PORT);
+ }
+
+ /**
+ * Tests if updating an unregistered port fails with an exception.
+ */
+ @Test(expected = IllegalArgumentException.class)
+ public void testUpdateUnregisteredPort() {
+ target.updatePort(PORT);
+ }
+
+ /**
+ * Tests if updating a null port fails with an exception.
+ */
+ @Test(expected = NullPointerException.class)
+ public void testUpdateNullPort() {
+ target.updatePort(null);
+ }
+
+ private void createBasicPorts() {
+ target.createPort(PORT);
+ }
+
+ private static class TestCoreService extends CoreServiceAdapter {
+
+ @Override
+ public ApplicationId registerApplication(String name) {
+ return TEST_APP_ID;
+ }
+ }
+
+ private static class TestKubevirtPortListener implements KubevirtPortListener {
+
+ private List<KubevirtPortEvent> events = Lists.newArrayList();
+
+ @Override
+ public void event(KubevirtPortEvent event) {
+ events.add(event);
+ }
+ }
+
+ private void validateEvents(Enum... types) {
+ int i = 0;
+ assertEquals("Number of events did not match", types.length, testListener.events.size());
+ for (Event event : testListener.events) {
+ assertEquals("Incorrect event received", types[i], event.type());
+ i++;
+ }
+ testListener.events.clear();
+ }
+}