[ONOS-7905] Add manager and store for k8s network and ports with unit tests
Change-Id: Idd7a8762edbc4bc6d14fd7b22d2218897e7ea0c0
diff --git a/apps/k8s-networking/api/src/main/java/org/onosproject/k8snetworking/api/Constants.java b/apps/k8s-networking/api/src/main/java/org/onosproject/k8snetworking/api/Constants.java
new file mode 100644
index 0000000..aefc940
--- /dev/null
+++ b/apps/k8s-networking/api/src/main/java/org/onosproject/k8snetworking/api/Constants.java
@@ -0,0 +1,27 @@
+/*
+ * Copyright 2016-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.k8snetworking.api;
+
+/**
+ * Provides constants used in OpenStackSwitching.
+ */
+public final class Constants {
+
+ private Constants() {
+ }
+
+ public static final String K8S_NETWORKING_APP_ID = "org.onosproject.k8snetworking";
+}
diff --git a/apps/k8s-networking/api/src/main/java/org/onosproject/k8snetworking/api/DefaultK8sNetwork.java b/apps/k8s-networking/api/src/main/java/org/onosproject/k8snetworking/api/DefaultK8sNetwork.java
index 3ac1230..fef1b1c 100644
--- a/apps/k8s-networking/api/src/main/java/org/onosproject/k8snetworking/api/DefaultK8sNetwork.java
+++ b/apps/k8s-networking/api/src/main/java/org/onosproject/k8snetworking/api/DefaultK8sNetwork.java
@@ -29,6 +29,7 @@
private static final int DEFAULT_MTU = 1500;
private final String networkId;
+ private final String name;
private final Type type;
private final Integer mtu;
private final String segmentId;
@@ -38,9 +39,10 @@
private static final String NOT_NULL_MSG = "Network % cannot be null";
// private constructor not intended for external invocation
- private DefaultK8sNetwork(String networkId, Type type, Integer mtu,
+ private DefaultK8sNetwork(String networkId, String name, Type type, Integer mtu,
String segmentId, IpAddress gatewayIp, String cidr) {
this.networkId = networkId;
+ this.name = name;
this.type = type;
this.mtu = mtu;
this.segmentId = segmentId;
@@ -59,6 +61,11 @@
}
@Override
+ public String name() {
+ return name;
+ }
+
+ @Override
public Integer mtu() {
return mtu;
}
@@ -88,6 +95,7 @@
}
DefaultK8sNetwork that = (DefaultK8sNetwork) o;
return Objects.equal(networkId, that.networkId) &&
+ Objects.equal(name, that.name) &&
type == that.type &&
Objects.equal(mtu, that.mtu) &&
Objects.equal(segmentId, that.segmentId) &&
@@ -97,13 +105,14 @@
@Override
public int hashCode() {
- return Objects.hashCode(networkId, type, mtu, segmentId, gatewayIp, cidr);
+ return Objects.hashCode(networkId, name, type, mtu, segmentId, gatewayIp, cidr);
}
@Override
public String toString() {
return MoreObjects.toStringHelper(this)
.add("networkId", networkId)
+ .add("name", name)
.add("type", type)
.add("mtu", mtu)
.add("segmentId", segmentId)
@@ -127,6 +136,7 @@
public static final class Builder implements K8sNetwork.Builder {
private String networkId;
+ private String name;
private Type type;
private Integer mtu;
private String segmentId;
@@ -136,6 +146,7 @@
@Override
public K8sNetwork build() {
checkArgument(networkId != null, NOT_NULL_MSG, "networkId");
+ checkArgument(name != null, NOT_NULL_MSG, "name");
checkArgument(type != null, NOT_NULL_MSG, "type");
checkArgument(segmentId != null, NOT_NULL_MSG, "segmentId");
checkArgument(gatewayIp != null, NOT_NULL_MSG, "gatewayIp");
@@ -145,7 +156,7 @@
mtu = DEFAULT_MTU;
}
- return new DefaultK8sNetwork(networkId, type, mtu, segmentId, gatewayIp, cidr);
+ return new DefaultK8sNetwork(networkId, name, type, mtu, segmentId, gatewayIp, cidr);
}
@Override
@@ -155,6 +166,12 @@
}
@Override
+ public Builder name(String name) {
+ this.name = name;
+ return this;
+ }
+
+ @Override
public Builder type(Type type) {
this.type = type;
return this;
diff --git a/apps/k8s-networking/api/src/main/java/org/onosproject/k8snetworking/api/K8sNetwork.java b/apps/k8s-networking/api/src/main/java/org/onosproject/k8snetworking/api/K8sNetwork.java
index f971cb9..0ab833e 100644
--- a/apps/k8s-networking/api/src/main/java/org/onosproject/k8snetworking/api/K8sNetwork.java
+++ b/apps/k8s-networking/api/src/main/java/org/onosproject/k8snetworking/api/K8sNetwork.java
@@ -58,6 +58,13 @@
Type type();
/**
+ * Returns kubernetes network name.
+ *
+ * @return kubernetes network name
+ */
+ String name();
+
+ /**
* Returns maximum transmission unit (MTU) value to address fragmentation.
*
* @return maximum transmission unit (MTU) value to address fragmentation
@@ -106,6 +113,14 @@
Builder networkId(String networkId);
/**
+ * Returns network builder with supplied network name.
+ *
+ * @param name network name
+ * @return network builder
+ */
+ Builder name(String name);
+
+ /**
* Returns network builder with supplied network type.
*
* @param type network type
diff --git a/apps/k8s-networking/api/src/main/java/org/onosproject/k8snetworking/api/K8sNetworkAdminService.java b/apps/k8s-networking/api/src/main/java/org/onosproject/k8snetworking/api/K8sNetworkAdminService.java
index e7f5c18..efa8b6f 100644
--- a/apps/k8s-networking/api/src/main/java/org/onosproject/k8snetworking/api/K8sNetworkAdminService.java
+++ b/apps/k8s-networking/api/src/main/java/org/onosproject/k8snetworking/api/K8sNetworkAdminService.java
@@ -38,9 +38,8 @@
* Removes the network.
*
* @param networkId network identifier
- * @return removed network; null if the network does not exist
*/
- K8sNetwork removeNetwork(String networkId);
+ void removeNetwork(String networkId);
/**
* Creates a kubernetes port with the given information.
@@ -60,7 +59,11 @@
* Removes the port.
*
* @param portId port identifier
- * @return removed port; null if the port does not exist
*/
- K8sPort removePort(String portId);
+ void removePort(String portId);
+
+ /**
+ * Clears the existing network and port states.
+ */
+ void clear();
}
diff --git a/apps/k8s-networking/api/src/main/java/org/onosproject/k8snetworking/api/K8sNetworkEvent.java b/apps/k8s-networking/api/src/main/java/org/onosproject/k8snetworking/api/K8sNetworkEvent.java
index ea097bc..a39beca 100644
--- a/apps/k8s-networking/api/src/main/java/org/onosproject/k8snetworking/api/K8sNetworkEvent.java
+++ b/apps/k8s-networking/api/src/main/java/org/onosproject/k8snetworking/api/K8sNetworkEvent.java
@@ -15,22 +15,17 @@
*/
package org.onosproject.k8snetworking.api;
+import org.onlab.util.Tools;
import org.onosproject.event.AbstractEvent;
+import static com.google.common.base.MoreObjects.toStringHelper;
+
/**
* Describes kubernetes network service event.
*/
public class K8sNetworkEvent extends AbstractEvent<K8sNetworkEvent.Type, K8sNetwork> {
- /**
- * Creates an event of a given type for the specified network.
- *
- * @param type kubernetes network event type
- * @param subject kubernetes network
- */
- protected K8sNetworkEvent(Type type, K8sNetwork subject) {
- super(type, subject);
- }
+ private final K8sPort port;
/**
* Kubernetes network events.
@@ -51,5 +46,64 @@
* Signifies that the kubernetes network is removed.
*/
K8S_NETWORK_REMOVED,
+
+ /**
+ * Signifies that a new kubernetes port is created.
+ */
+ K8S_PORT_CREATED,
+
+ /**
+ * Signifies that the kubernetes port is updated.
+ */
+ K8S_PORT_UPDATED,
+
+ /**
+ * Signifies that the kubernetes port is removed.
+ */
+ K8S_PORT_REMOVED,
+ }
+
+ /**
+ * Creates an event of a given type for the specified network.
+ *
+ * @param type kubernetes network event type
+ * @param network kubernetes network
+ */
+ public K8sNetworkEvent(Type type, K8sNetwork network) {
+ super(type, network);
+ this.port = null;
+ }
+
+ /**
+ * Creates an event of a given type for the specified network and port.
+ *
+ * @param type kubernetes network event type
+ * @param network kubernetes network
+ * @param port kubernetes port
+ */
+ public K8sNetworkEvent(Type type, K8sNetwork network, K8sPort port) {
+ super(type, network);
+ this.port = port;
+ }
+
+ /**
+ * Returns the kubernetes port of the network event.
+ *
+ * @return kubernetes port; null if the event is not port specific
+ */
+ public K8sPort port() {
+ return port;
+ }
+
+ @Override
+ public String toString() {
+ if (port == null) {
+ return super.toString();
+ }
+ return toStringHelper(this)
+ .add("time", Tools.defaultOffsetDataTime(time()))
+ .add("port", port)
+ .add("network", subject())
+ .toString();
}
}
diff --git a/apps/k8s-networking/api/src/main/java/org/onosproject/k8snetworking/api/K8sNetworkStore.java b/apps/k8s-networking/api/src/main/java/org/onosproject/k8snetworking/api/K8sNetworkStore.java
index a5255ff..e4c5ee3 100644
--- a/apps/k8s-networking/api/src/main/java/org/onosproject/k8snetworking/api/K8sNetworkStore.java
+++ b/apps/k8s-networking/api/src/main/java/org/onosproject/k8snetworking/api/K8sNetworkStore.java
@@ -62,7 +62,44 @@
Set<K8sNetwork> networks();
/**
- * Removes all kubernetes networks.
+ * Creates a new kubernetes port.
+ *
+ * @param port kubernetes port
+ */
+ void createPort(K8sPort port);
+
+ /**
+ * Update the kubernetes port.
+ *
+ * @param port kubernetes port
+ */
+ void updatePort(K8sPort port);
+
+ /**
+ * Removes the kubernetes port with the given port identifier.
+ *
+ * @param portId port identifier
+ * @return port; null if not found
+ */
+ K8sPort removePort(String portId);
+
+ /**
+ * Returns all kubernetes ports.
+ *
+ * @return set of kubernetes ports
+ */
+ Set<K8sPort> ports();
+
+ /**
+ * Return the kubernetes port with the given port identifier.
+ *
+ * @param portId port identifier
+ * @return kubernetes port
+ */
+ K8sPort port(String portId);
+
+ /**
+ * Removes all kubernetes networks and ports.
*/
void clear();
}
diff --git a/apps/k8s-networking/api/src/test/java/org/onosproject/k8snetworking/api/DefaultK8sNetworkTest.java b/apps/k8s-networking/api/src/test/java/org/onosproject/k8snetworking/api/DefaultK8sNetworkTest.java
index 727b0ad..058bc03 100644
--- a/apps/k8s-networking/api/src/test/java/org/onosproject/k8snetworking/api/DefaultK8sNetworkTest.java
+++ b/apps/k8s-networking/api/src/test/java/org/onosproject/k8snetworking/api/DefaultK8sNetworkTest.java
@@ -30,6 +30,8 @@
private static final String NETWORK_ID_1 = "network-1";
private static final String NETWORK_ID_2 = "network-2";
+ private static final String NAME_1 = "network-1";
+ private static final String NAME_2 = "network-2";
private static final K8sNetwork.Type TYPE_1 = K8sNetwork.Type.VXLAN;
private static final K8sNetwork.Type TYPE_2 = K8sNetwork.Type.GENEVE;
private static final Integer MTU_1 = 1500;
@@ -52,6 +54,7 @@
public void setUp() {
k8sNetwork1 = DefaultK8sNetwork.builder()
.networkId(NETWORK_ID_1)
+ .name(NAME_1)
.type(TYPE_1)
.mtu(MTU_1)
.segmentId(SEGMENT_ID_1)
@@ -61,6 +64,7 @@
sameAsK8sNetwork1 = DefaultK8sNetwork.builder()
.networkId(NETWORK_ID_1)
+ .name(NAME_1)
.type(TYPE_1)
.mtu(MTU_1)
.segmentId(SEGMENT_ID_1)
@@ -70,6 +74,7 @@
k8sNetwork2 = DefaultK8sNetwork.builder()
.networkId(NETWORK_ID_2)
+ .name(NAME_2)
.type(TYPE_2)
.mtu(MTU_2)
.segmentId(SEGMENT_ID_2)
@@ -104,6 +109,7 @@
K8sNetwork k8sNetwork = k8sNetwork1;
assertEquals(NETWORK_ID_1, k8sNetwork.networkId());
+ assertEquals(NAME_1, k8sNetwork.name());
assertEquals(TYPE_1, k8sNetwork.type());
assertEquals(MTU_1, k8sNetwork.mtu());
assertEquals(SEGMENT_ID_1, k8sNetwork.segmentId());
diff --git a/apps/k8s-networking/app/src/main/java/org/onosproject/k8snetworking/codec/K8sNetworkCodec.java b/apps/k8s-networking/app/src/main/java/org/onosproject/k8snetworking/codec/K8sNetworkCodec.java
index e6e646b..b9984bf 100644
--- a/apps/k8s-networking/app/src/main/java/org/onosproject/k8snetworking/codec/K8sNetworkCodec.java
+++ b/apps/k8s-networking/app/src/main/java/org/onosproject/k8snetworking/codec/K8sNetworkCodec.java
@@ -35,6 +35,7 @@
private final Logger log = getLogger(getClass());
private static final String NETWORK_ID = "networkId";
+ private static final String NAME = "name";
private static final String TYPE = "type";
private static final String MTU = "mtu";
private static final String SEGMENT_ID = "segmentId";
@@ -49,6 +50,7 @@
ObjectNode result = context.mapper().createObjectNode()
.put(NETWORK_ID, network.networkId())
+ .put(NAME, network.name())
.put(TYPE, network.type().name())
.put(SEGMENT_ID, network.segmentId())
.put(GATEWAY_IP, network.gatewayIp().toString())
@@ -69,6 +71,8 @@
String networkId = nullIsIllegal(json.get(NETWORK_ID).asText(),
NETWORK_ID + MISSING_MESSAGE);
+ String name = nullIsIllegal(json.get(NAME).asText(),
+ NAME + MISSING_MESSAGE);
String type = nullIsIllegal(json.get(TYPE).asText(),
TYPE + MISSING_MESSAGE);
String segmentId = nullIsIllegal(json.get(SEGMENT_ID).asText(),
@@ -80,6 +84,7 @@
DefaultK8sNetwork.Builder networkBuilder = DefaultK8sNetwork.builder()
.networkId(networkId)
+ .name(name)
.type(K8sNetwork.Type.valueOf(type))
.segmentId(segmentId)
.gatewayIp(IpAddress.valueOf(gatewayIp))
diff --git a/apps/k8s-networking/app/src/main/java/org/onosproject/k8snetworking/impl/DistributedK8sNetworkStore.java b/apps/k8s-networking/app/src/main/java/org/onosproject/k8snetworking/impl/DistributedK8sNetworkStore.java
new file mode 100644
index 0000000..8e54bef
--- /dev/null
+++ b/apps/k8s-networking/app/src/main/java/org/onosproject/k8snetworking/impl/DistributedK8sNetworkStore.java
@@ -0,0 +1,276 @@
+/*
+ * Copyright 2019-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.k8snetworking.impl;
+
+import com.google.common.collect.ImmutableSet;
+import org.onlab.util.KryoNamespace;
+import org.onosproject.core.ApplicationId;
+import org.onosproject.core.CoreService;
+import org.onosproject.k8snetworking.api.DefaultK8sNetwork;
+import org.onosproject.k8snetworking.api.DefaultK8sPort;
+import org.onosproject.k8snetworking.api.K8sNetwork;
+import org.onosproject.k8snetworking.api.K8sNetworkEvent;
+import org.onosproject.k8snetworking.api.K8sNetworkStore;
+import org.onosproject.k8snetworking.api.K8sNetworkStoreDelegate;
+import org.onosproject.k8snetworking.api.K8sPort;
+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.k8snetworking.api.K8sNetworkEvent.Type.K8S_NETWORK_CREATED;
+import static org.onosproject.k8snetworking.api.K8sNetworkEvent.Type.K8S_NETWORK_REMOVED;
+import static org.onosproject.k8snetworking.api.K8sNetworkEvent.Type.K8S_NETWORK_UPDATED;
+import static org.onosproject.k8snetworking.api.K8sNetworkEvent.Type.K8S_PORT_CREATED;
+import static org.onosproject.k8snetworking.api.K8sNetworkEvent.Type.K8S_PORT_REMOVED;
+import static org.onosproject.k8snetworking.api.K8sNetworkEvent.Type.K8S_PORT_UPDATED;
+import static org.slf4j.LoggerFactory.getLogger;
+
+/**
+ * Implementation of kubernetes network store using consistent map.
+ */
+@Component(immediate = true, service = K8sNetworkStore.class)
+public class DistributedK8sNetworkStore
+ extends AbstractStore<K8sNetworkEvent, K8sNetworkStoreDelegate>
+ implements K8sNetworkStore {
+
+ 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.k8snetwork";
+
+ private static final KryoNamespace
+ SERIALIZER_K8S_NETWORK_PORT = KryoNamespace.newBuilder()
+ .register(KryoNamespaces.API)
+ .register(K8sNetwork.class)
+ .register(K8sNetwork.Type.class)
+ .register(DefaultK8sNetwork.class)
+ .register(K8sPort.class)
+ .register(K8sPort.State.class)
+ .register(DefaultK8sPort.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, K8sNetwork> networkMapListener =
+ new K8sNetworkMapListener();
+ private final MapEventListener<String, K8sPort> portMapListener =
+ new K8sPortMapListener();
+
+ private ConsistentMap<String, K8sNetwork> networkStore;
+ private ConsistentMap<String, K8sPort> portStore;
+
+ @Activate
+ protected void activate() {
+ ApplicationId appId = coreService.registerApplication(APP_ID);
+ networkStore = storageService.<String, K8sNetwork>consistentMapBuilder()
+ .withSerializer(Serializer.using(SERIALIZER_K8S_NETWORK_PORT))
+ .withName("k8s-networkstore")
+ .withApplicationId(appId)
+ .build();
+ portStore = storageService.<String, K8sPort>consistentMapBuilder()
+ .withSerializer(Serializer.using(SERIALIZER_K8S_NETWORK_PORT))
+ .withName("k8s-portstore")
+ .withApplicationId(appId)
+ .build();
+ networkStore.addListener(networkMapListener);
+ portStore.addListener(portMapListener);
+ log.info("Started");
+ }
+
+ @Deactivate
+ protected void deactivate() {
+ networkStore.removeListener(networkMapListener);
+ portStore.removeListener(portMapListener);
+ eventExecutor.shutdown();
+ log.info("Stopped");
+ }
+
+ @Override
+ public void createNetwork(K8sNetwork network) {
+ networkStore.compute(network.networkId(), (networkId, existing) -> {
+ final String error = network.networkId() + ERR_DUPLICATE;
+ checkArgument(existing == null, error);
+ return network;
+ });
+ }
+
+ @Override
+ public void updateNetwork(K8sNetwork network) {
+ networkStore.compute(network.networkId(), (networkId, existing) -> {
+ final String error = network.networkId() + ERR_NOT_FOUND;
+ checkArgument(existing != null, error);
+ return network;
+ });
+ }
+
+ @Override
+ public K8sNetwork removeNetwork(String networkId) {
+ Versioned<K8sNetwork> network = networkStore.remove(networkId);
+ if (network == null) {
+ final String error = networkId + ERR_NOT_FOUND;
+ throw new IllegalArgumentException(error);
+ }
+ return network.value();
+ }
+
+ @Override
+ public K8sNetwork network(String networkId) {
+ return networkStore.asJavaMap().get(networkId);
+ }
+
+ @Override
+ public Set<K8sNetwork> networks() {
+ return ImmutableSet.copyOf(networkStore.asJavaMap().values());
+ }
+
+ @Override
+ public void createPort(K8sPort port) {
+ portStore.compute(port.portId(), (portId, existing) -> {
+ final String error = port.portId() + ERR_DUPLICATE;
+ checkArgument(existing == null, error);
+ return port;
+ });
+ }
+
+ @Override
+ public void updatePort(K8sPort port) {
+ portStore.compute(port.portId(), (portId, existing) -> {
+ final String error = port.portId() + ERR_NOT_FOUND;
+ checkArgument(existing != null, error);
+ return port;
+ });
+ }
+
+ @Override
+ public K8sPort removePort(String portId) {
+ Versioned<K8sPort> port = portStore.remove(portId);
+ if (port == null) {
+ final String error = portId + ERR_NOT_FOUND;
+ throw new IllegalArgumentException(error);
+ }
+ return port.value();
+ }
+
+ @Override
+ public Set<K8sPort> ports() {
+ return ImmutableSet.copyOf(portStore.asJavaMap().values());
+ }
+
+ @Override
+ public K8sPort port(String portId) {
+ return portStore.asJavaMap().get(portId);
+ }
+
+ @Override
+ public void clear() {
+ portStore.clear();
+ networkStore.clear();
+ }
+
+ private class K8sNetworkMapListener implements MapEventListener<String, K8sNetwork> {
+
+ @Override
+ public void event(MapEvent<String, K8sNetwork> event) {
+
+ switch (event.type()) {
+ case INSERT:
+ log.debug("Kubernetes network created {}", event.newValue());
+ eventExecutor.execute(() ->
+ notifyDelegate(new K8sNetworkEvent(
+ K8S_NETWORK_CREATED, event.newValue().value())));
+ break;
+ case UPDATE:
+ log.debug("Kubernetes network updated {}", event.newValue());
+ eventExecutor.execute(() ->
+ notifyDelegate(new K8sNetworkEvent(
+ K8S_NETWORK_UPDATED, event.newValue().value())));
+ break;
+ case REMOVE:
+ log.debug("Kubernetes network removed {}", event.oldValue());
+ eventExecutor.execute(() ->
+ notifyDelegate(new K8sNetworkEvent(
+ K8S_NETWORK_REMOVED, event.oldValue().value())));
+ break;
+ default:
+ // do nothing
+ break;
+ }
+ }
+ }
+
+ private class K8sPortMapListener implements MapEventListener<String, K8sPort> {
+
+ @Override
+ public void event(MapEvent<String, K8sPort> event) {
+
+ switch (event.type()) {
+ case INSERT:
+ log.debug("Kubernetes port created");
+ eventExecutor.execute(() ->
+ notifyDelegate(new K8sNetworkEvent(
+ K8S_PORT_CREATED,
+ network(event.newValue().value().networkId()),
+ event.newValue().value())));
+ break;
+ case UPDATE:
+ log.debug("Kubernetes port updated");
+ eventExecutor.execute(() ->
+ notifyDelegate(new K8sNetworkEvent(
+ K8S_PORT_UPDATED,
+ network(event.newValue().value().networkId()),
+ event.newValue().value())));
+ break;
+ case REMOVE:
+ log.debug("Kubernetes port removed");
+ notifyDelegate(new K8sNetworkEvent(
+ K8S_PORT_REMOVED,
+ network(event.oldValue().value().networkId()),
+ event.oldValue().value()));
+ break;
+ default:
+ // do nothing
+ break;
+ }
+ }
+ }
+}
diff --git a/apps/k8s-networking/app/src/main/java/org/onosproject/k8snetworking/impl/K8sNetworkManager.java b/apps/k8s-networking/app/src/main/java/org/onosproject/k8snetworking/impl/K8sNetworkManager.java
new file mode 100644
index 0000000..2a6890e
--- /dev/null
+++ b/apps/k8s-networking/app/src/main/java/org/onosproject/k8snetworking/impl/K8sNetworkManager.java
@@ -0,0 +1,238 @@
+/*
+ * Copyright 2019-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.k8snetworking.impl;
+
+import com.google.common.base.Strings;
+import com.google.common.collect.ImmutableSet;
+import org.onosproject.core.ApplicationId;
+import org.onosproject.core.CoreService;
+import org.onosproject.event.ListenerRegistry;
+import org.onosproject.k8snetworking.api.K8sNetwork;
+import org.onosproject.k8snetworking.api.K8sNetwork.Type;
+import org.onosproject.k8snetworking.api.K8sNetworkAdminService;
+import org.onosproject.k8snetworking.api.K8sNetworkEvent;
+import org.onosproject.k8snetworking.api.K8sNetworkListener;
+import org.onosproject.k8snetworking.api.K8sNetworkService;
+import org.onosproject.k8snetworking.api.K8sNetworkStore;
+import org.onosproject.k8snetworking.api.K8sNetworkStoreDelegate;
+import org.onosproject.k8snetworking.api.K8sPort;
+import org.onosproject.k8snetworking.api.K8sPort.State;
+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.k8snetworking.api.Constants.K8S_NETWORKING_APP_ID;
+import static org.slf4j.LoggerFactory.getLogger;
+
+/**
+ * Provides implementation of administering and interfacing kubernetes network,
+ * and port.
+ */
+@Component(
+ immediate = true,
+ service = {K8sNetworkAdminService.class, K8sNetworkService.class }
+)
+public class K8sNetworkManager
+ extends ListenerRegistry<K8sNetworkEvent, K8sNetworkListener>
+ implements K8sNetworkAdminService, K8sNetworkService {
+
+ protected final Logger log = getLogger(getClass());
+
+ private static final String MSG_NETWORK = "Kubernetes network %s %s";
+ 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_NETWORK = "Kubernetes network cannot be null";
+ private static final String ERR_NULL_NETWORK_ID = "Kubernetes network ID cannot be null";
+ private static final String ERR_NULL_PORT = "Kubernetes port cannot be null";
+ private static final String ERR_NULL_PORT_ID = "Kubernetes port ID cannot be null";
+ private static final String ERR_NULL_PORT_NET_ID = "Kubernetes 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 K8sNetworkStore k8sNetworkStore;
+
+ private final K8sNetworkStoreDelegate
+ delegate = new InternalNetworkStorageDelegate();
+
+ private ApplicationId appId;
+
+ @Activate
+ protected void activate() {
+ appId = coreService.registerApplication(K8S_NETWORKING_APP_ID);
+
+ k8sNetworkStore.setDelegate(delegate);
+ log.info("Started");
+ }
+
+ @Deactivate
+ protected void deactivate() {
+ k8sNetworkStore.unsetDelegate(delegate);
+ log.info("Stopped");
+ }
+
+ @Override
+ public void createNetwork(K8sNetwork network) {
+ checkNotNull(network, ERR_NULL_NETWORK);
+ checkArgument(!Strings.isNullOrEmpty(network.networkId()), ERR_NULL_NETWORK_ID);
+
+ k8sNetworkStore.createNetwork(network);
+
+ log.info(String.format(MSG_NETWORK, network.name(), MSG_CREATED));
+ }
+
+ @Override
+ public void updateNetwork(K8sNetwork network) {
+ checkNotNull(network, ERR_NULL_NETWORK);
+ checkArgument(!Strings.isNullOrEmpty(network.networkId()), ERR_NULL_NETWORK_ID);
+
+ k8sNetworkStore.updateNetwork(network);
+
+ log.info(String.format(MSG_NETWORK, network.networkId(), MSG_UPDATED));
+ }
+
+ @Override
+ public void removeNetwork(String networkId) {
+ checkArgument(!Strings.isNullOrEmpty(networkId), ERR_NULL_NETWORK_ID);
+
+ synchronized (this) {
+ if (isNetworkInUse(networkId)) {
+ final String error = String.format(MSG_NETWORK, networkId, ERR_IN_USE);
+ throw new IllegalStateException(error);
+ }
+ K8sNetwork network = k8sNetworkStore.removeNetwork(networkId);
+
+ if (network != null) {
+ log.info(String.format(MSG_NETWORK, network.name(), MSG_REMOVED));
+ }
+ }
+ }
+
+ @Override
+ public K8sNetwork network(String networkId) {
+ checkArgument(!Strings.isNullOrEmpty(networkId), ERR_NULL_NETWORK_ID);
+ return k8sNetworkStore.network(networkId);
+ }
+
+ @Override
+ public Set<K8sNetwork> networks() {
+ return ImmutableSet.copyOf(k8sNetworkStore.networks());
+ }
+
+ @Override
+ public Set<K8sNetwork> networks(Type type) {
+ return ImmutableSet.copyOf(k8sNetworkStore.networks().stream()
+ .filter(n -> n.type() == type).collect(Collectors.toSet()));
+ }
+
+ @Override
+ public void createPort(K8sPort port) {
+ checkNotNull(port, ERR_NULL_PORT);
+ checkArgument(!Strings.isNullOrEmpty(port.portId()), ERR_NULL_PORT_ID);
+ checkArgument(!Strings.isNullOrEmpty(port.networkId()), ERR_NULL_PORT_NET_ID);
+
+ k8sNetworkStore.createPort(port);
+ log.info(String.format(MSG_PORT, port.portId(), MSG_CREATED));
+ }
+
+ @Override
+ public void updatePort(K8sPort port) {
+ checkNotNull(port, ERR_NULL_PORT);
+ checkArgument(!Strings.isNullOrEmpty(port.portId()), ERR_NULL_PORT_ID);
+ checkArgument(!Strings.isNullOrEmpty(port.networkId()), ERR_NULL_PORT_NET_ID);
+
+ k8sNetworkStore.updatePort(port);
+ log.info(String.format(MSG_PORT, port.portId(), MSG_UPDATED));
+ }
+
+ @Override
+ public void removePort(String portId) {
+ checkArgument(!Strings.isNullOrEmpty(portId), ERR_NULL_PORT_ID);
+ synchronized (this) {
+ if (isPortInUse(portId)) {
+ final String error = String.format(MSG_PORT, portId, ERR_IN_USE);
+ throw new IllegalStateException(error);
+ }
+ K8sPort port = k8sNetworkStore.removePort(portId);
+ if (port != null) {
+ log.info(String.format(MSG_PORT, port.portId(), MSG_REMOVED));
+ }
+ }
+ }
+
+ @Override
+ public K8sPort port(String portId) {
+ checkArgument(!Strings.isNullOrEmpty(portId), ERR_NULL_PORT_ID);
+ return k8sNetworkStore.port(portId);
+ }
+
+ @Override
+ public Set<K8sPort> ports() {
+ return ImmutableSet.copyOf(k8sNetworkStore.ports());
+ }
+
+ @Override
+ public Set<K8sPort> ports(State state) {
+ return ImmutableSet.copyOf(k8sNetworkStore.ports().stream()
+ .filter(p -> p.state() == state).collect(Collectors.toSet()));
+ }
+
+ @Override
+ public Set<K8sPort> ports(String networkId) {
+ checkArgument(!Strings.isNullOrEmpty(networkId), ERR_NULL_PORT_NET_ID);
+ return ImmutableSet.copyOf(k8sNetworkStore.ports().stream()
+ .filter(p -> p.networkId().equals(networkId))
+ .collect(Collectors.toSet()));
+ }
+
+ @Override
+ public void clear() {
+ k8sNetworkStore.clear();
+ }
+
+ private boolean isNetworkInUse(String networkId) {
+ return !ports(networkId).isEmpty();
+ }
+
+ private boolean isPortInUse(String portId) {
+ return false;
+ }
+
+ private class InternalNetworkStorageDelegate implements K8sNetworkStoreDelegate {
+
+ @Override
+ public void notify(K8sNetworkEvent event) {
+ if (event != null) {
+ log.trace("send kubernetes networking event {}", event);
+ process(event);
+ }
+ }
+ }
+}
diff --git a/apps/k8s-networking/app/src/test/java/org/onosproject/k8snetworking/codec/K8sNetworkCodecTest.java b/apps/k8s-networking/app/src/test/java/org/onosproject/k8snetworking/codec/K8sNetworkCodecTest.java
index 9d7fd99..fa894de 100644
--- a/apps/k8s-networking/app/src/test/java/org/onosproject/k8snetworking/codec/K8sNetworkCodecTest.java
+++ b/apps/k8s-networking/app/src/test/java/org/onosproject/k8snetworking/codec/K8sNetworkCodecTest.java
@@ -79,6 +79,7 @@
public void testK8sNetworkEncode() {
K8sNetwork network = DefaultK8sNetwork.builder()
.networkId("network-1")
+ .name("network-1")
.segmentId("1")
.type(K8sNetwork.Type.VXLAN)
.gatewayIp(IpAddress.valueOf("10.10.10.1"))
@@ -98,6 +99,7 @@
K8sNetwork network = getK8sNetwork("K8sNetwork.json");
assertEquals("network-1", network.networkId());
+ assertEquals("network-1", network.name());
assertEquals("1", network.segmentId());
assertEquals("VXLAN", network.type().name());
assertEquals("10.10.10.1", network.gatewayIp().toString());
diff --git a/apps/k8s-networking/app/src/test/java/org/onosproject/k8snetworking/codec/K8sNetworkJsonMatcher.java b/apps/k8s-networking/app/src/test/java/org/onosproject/k8snetworking/codec/K8sNetworkJsonMatcher.java
index 435de0f..73f2e58 100644
--- a/apps/k8s-networking/app/src/test/java/org/onosproject/k8snetworking/codec/K8sNetworkJsonMatcher.java
+++ b/apps/k8s-networking/app/src/test/java/org/onosproject/k8snetworking/codec/K8sNetworkJsonMatcher.java
@@ -28,6 +28,7 @@
private final K8sNetwork network;
private static final String NETWORK_ID = "networkId";
+ private static final String NAME = "name";
private static final String TYPE = "type";
private static final String MTU = "mtu";
private static final String SEGMENT_ID = "segmentId";
@@ -49,6 +50,14 @@
return false;
}
+ // check network name
+ String jsonName = jsonNode.get(NAME).asText();
+ String name = network.name();
+ if (!jsonName.equals(name)) {
+ description.appendText("name was " + jsonName);
+ return false;
+ }
+
// check type
String jsonType = jsonNode.get(TYPE).asText();
String type = network.type().name();
diff --git a/apps/k8s-networking/app/src/test/java/org/onosproject/k8snetworking/impl/K8sNetworkManagerTest.java b/apps/k8s-networking/app/src/test/java/org/onosproject/k8snetworking/impl/K8sNetworkManagerTest.java
new file mode 100644
index 0000000..874899b
--- /dev/null
+++ b/apps/k8s-networking/app/src/test/java/org/onosproject/k8snetworking/impl/K8sNetworkManagerTest.java
@@ -0,0 +1,344 @@
+/*
+ * Copyright 2019-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.k8snetworking.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.k8snetworking.api.DefaultK8sNetwork;
+import org.onosproject.k8snetworking.api.DefaultK8sPort;
+import org.onosproject.k8snetworking.api.K8sNetwork;
+import org.onosproject.k8snetworking.api.K8sNetworkEvent;
+import org.onosproject.k8snetworking.api.K8sNetworkListener;
+import org.onosproject.k8snetworking.api.K8sPort;
+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.k8snetworking.api.K8sNetworkEvent.Type.K8S_NETWORK_CREATED;
+import static org.onosproject.k8snetworking.api.K8sNetworkEvent.Type.K8S_NETWORK_REMOVED;
+import static org.onosproject.k8snetworking.api.K8sNetworkEvent.Type.K8S_NETWORK_UPDATED;
+import static org.onosproject.k8snetworking.api.K8sNetworkEvent.Type.K8S_PORT_CREATED;
+import static org.onosproject.k8snetworking.api.K8sNetworkEvent.Type.K8S_PORT_REMOVED;
+import static org.onosproject.k8snetworking.api.K8sNetworkEvent.Type.K8S_PORT_UPDATED;
+
+/**
+ * Unit tests for kubernetes network manager.
+ */
+public class K8sNetworkManagerTest {
+
+ private static final ApplicationId TEST_APP_ID = new DefaultApplicationId(1, "test");
+
+ private static final String UNKNOWN_ID = "unknown_id";
+ private static final String UPDATED_ID = "updated_id";
+ private static final String UPDATED_NAME = "updated_name";
+
+ private static final String NETWORK_ID = "network_id";
+ private static final String NETWORK_NAME = "network_name";
+ private static final K8sNetwork NETWORK = DefaultK8sNetwork.builder()
+ .networkId(NETWORK_ID)
+ .name(NETWORK_NAME)
+ .type(K8sNetwork.Type.VXLAN)
+ .segmentId("1")
+ .gatewayIp(IpAddress.valueOf("10.10.10.1"))
+ .cidr("32")
+ .mtu(1500)
+ .build();
+ private static final K8sNetwork NETWORK_UPDATED = DefaultK8sNetwork.builder()
+ .networkId(NETWORK_ID)
+ .name(UPDATED_NAME)
+ .type(K8sNetwork.Type.VXLAN)
+ .segmentId("1")
+ .gatewayIp(IpAddress.valueOf("10.10.10.1"))
+ .cidr("32")
+ .mtu(1500)
+ .build();
+
+ private static final String PORT_ID = "port_id";
+ private static final K8sPort PORT = DefaultK8sPort.builder()
+ .portId(PORT_ID)
+ .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"))
+ .state(K8sPort.State.ACTIVE)
+ .build();
+ private static final K8sPort PORT_UPDATED = DefaultK8sPort.builder()
+ .portId(PORT_ID)
+ .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"))
+ .state(K8sPort.State.ACTIVE)
+ .build();
+
+ private final TestK8sNetworkListener testListener = new TestK8sNetworkListener();
+
+ private K8sNetworkManager target;
+ private DistributedK8sNetworkStore k8sNetworkStore;
+
+ @Before
+ public void setUp() throws Exception {
+ k8sNetworkStore = new DistributedK8sNetworkStore();
+ TestUtils.setField(k8sNetworkStore, "coreService", new TestCoreService());
+ TestUtils.setField(k8sNetworkStore, "storageService", new TestStorageService());
+ TestUtils.setField(k8sNetworkStore, "eventExecutor", MoreExecutors.newDirectExecutorService());
+ k8sNetworkStore.activate();
+
+ target = new K8sNetworkManager();
+ TestUtils.setField(target, "coreService", new TestCoreService());
+ target.k8sNetworkStore = k8sNetworkStore;
+ target.addListener(testListener);
+ target.activate();
+ }
+
+ @After
+ public void tearDown() {
+ target.removeListener(testListener);
+ k8sNetworkStore.deactivate();
+ target.deactivate();
+ k8sNetworkStore = null;
+ target = null;
+ }
+
+ /**
+ * Tests if getting all networks returns the correct set of networks.
+ */
+ @Test
+ public void testGetNetworks() {
+ createBasicNetworks();
+ assertEquals("Number of network did not match", 1, target.networks().size());
+ }
+
+ /**
+ * Tests if getting a network with ID returns the correct network.
+ */
+ @Test
+ public void testGetNetworkById() {
+ createBasicNetworks();
+ assertNotNull("Network did not match", target.network(NETWORK_ID));
+ assertNull("Network did not match", target.network(UNKNOWN_ID));
+ }
+
+ /**
+ * Tests creating and removing a network, and checks if it triggers proper events.
+ */
+ @Test
+ public void testCreateAndRemoveNetwork() {
+ target.createNetwork(NETWORK);
+ assertEquals("Number of networks did not match", 1, target.networks().size());
+ assertNotNull("Network was not created", target.network(NETWORK_ID));
+
+ target.removeNetwork(NETWORK_ID);
+ assertEquals("Number of networks did not match", 0, target.networks().size());
+ assertNull("Network was not removed", target.network(NETWORK_ID));
+
+ validateEvents(K8S_NETWORK_CREATED, K8S_NETWORK_REMOVED);
+ }
+
+ /**
+ * Tests updating a network, and checks if it triggers proper events.
+ */
+ @Test
+ public void testCreateAndUpdateNetwork() {
+ target.createNetwork(NETWORK);
+ assertEquals("Number of networks did not match", 1, target.networks().size());
+ assertEquals("Network did not match", NETWORK_NAME, target.network(NETWORK_ID).name());
+
+ target.updateNetwork(NETWORK_UPDATED);
+
+ assertEquals("Number of networks did not match", 1, target.networks().size());
+ assertEquals("Network did not match", UPDATED_NAME, target.network(NETWORK_ID).name());
+ validateEvents(K8S_NETWORK_CREATED, K8S_NETWORK_UPDATED);
+ }
+
+ /**
+ * Tests if creating a null network fails with an exception.
+ */
+ @Test(expected = NullPointerException.class)
+ public void testCreateNullNetwork() {
+ target.createNetwork(null);
+ }
+
+ /**
+ * Tests if creating a duplicate network fails with an exception.
+ */
+ @Test(expected = IllegalArgumentException.class)
+ public void testCreateDuplicateNetwork() {
+ target.createNetwork(NETWORK);
+ target.createNetwork(NETWORK);
+ }
+
+ /**
+ * Tests if removing network with null ID fails with an exception.
+ */
+ @Test(expected = IllegalArgumentException.class)
+ public void testRemoveNetworkWithNull() {
+ target.removeNetwork(null);
+ }
+
+ /**
+ * Tests if updating an unregistered network fails with an exception.
+ */
+ @Test(expected = IllegalArgumentException.class)
+ public void testUpdateUnregisteredNetwork() {
+ target.updateNetwork(NETWORK);
+ }
+
+ /**
+ * Tests if getting all ports returns correct set of values.
+ */
+ @Test
+ public void testGetPorts() {
+ createBasicNetworks();
+ 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() {
+ createBasicNetworks();
+ 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() {
+ createBasicNetworks();
+ assertNotNull("Port did not match", target.port(PORT_ID));
+ assertNull("Port did not match", target.port(UNKNOWN_ID));
+ }
+
+ /**
+ * 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(PORT_ID));
+
+ target.removePort(PORT_ID);
+ assertEquals("Number of port did not match", 0, target.ports().size());
+ assertNull("Port was not created", target.port(PORT_ID));
+
+ validateEvents(K8S_PORT_CREATED, K8S_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(PORT_ID).networkId());
+
+ validateEvents(K8S_PORT_CREATED, K8S_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 createBasicNetworks() {
+ target.createNetwork(NETWORK);
+ target.createPort(PORT);
+ }
+
+ private static class TestCoreService extends CoreServiceAdapter {
+
+ @Override
+ public ApplicationId registerApplication(String name) {
+ return TEST_APP_ID;
+ }
+ }
+
+ private static class TestK8sNetworkListener implements K8sNetworkListener {
+ private List<K8sNetworkEvent> events = Lists.newArrayList();
+
+ @Override
+ public void event(K8sNetworkEvent 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();
+ }
+}
diff --git a/apps/k8s-networking/app/src/test/resources/org/onosproject/k8snetworking/codec/K8sNetwork.json b/apps/k8s-networking/app/src/test/resources/org/onosproject/k8snetworking/codec/K8sNetwork.json
index ce15c92..6efe965 100644
--- a/apps/k8s-networking/app/src/test/resources/org/onosproject/k8snetworking/codec/K8sNetwork.json
+++ b/apps/k8s-networking/app/src/test/resources/org/onosproject/k8snetworking/codec/K8sNetwork.json
@@ -1,5 +1,6 @@
{
"networkId": "network-1",
+ "name": "network-1",
"type": "VXLAN",
"mtu": 1500,
"segmentId": "1",