Initial implementation of k8s networking REST API with unit tests
Change-Id: Ifb11204edb3c1e75b26810c0b104423941b0801d
diff --git a/apps/k8s-networking/app/src/main/java/org/onosproject/k8snetworking/codec/K8sPortCodec.java b/apps/k8s-networking/app/src/main/java/org/onosproject/k8snetworking/codec/K8sPortCodec.java
index 9ca74cb..6b20a59 100644
--- a/apps/k8s-networking/app/src/main/java/org/onosproject/k8snetworking/codec/K8sPortCodec.java
+++ b/apps/k8s-networking/app/src/main/java/org/onosproject/k8snetworking/codec/K8sPortCodec.java
@@ -15,6 +15,7 @@
*/
package org.onosproject.k8snetworking.codec;
+import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import org.onlab.packet.IpAddress;
import org.onlab.packet.MacAddress;
@@ -52,14 +53,25 @@
public ObjectNode encode(K8sPort port, CodecContext context) {
checkNotNull(port, "Kubernetes port cannot be null");
- return context.mapper().createObjectNode()
+ ObjectNode result = context.mapper().createObjectNode()
.put(NETWORK_ID, port.networkId())
.put(PORT_ID, port.portId())
.put(MAC_ADDRESS, port.macAddress().toString())
- .put(IP_ADDRESS, port.ipAddress().toString())
- .put(DEVICE_ID, port.deviceId().toString())
- .put(PORT_NUMBER, port.portNumber().toString())
- .put(STATE, port.state().name());
+ .put(IP_ADDRESS, port.ipAddress().toString());
+
+ if (port.deviceId() != null) {
+ result.put(DEVICE_ID, port.deviceId().toString());
+ }
+
+ if (port.portNumber() != null) {
+ result.put(PORT_NUMBER, port.portNumber().toString());
+ }
+
+ if (port.state() != null) {
+ result.put(STATE, port.state().name());
+ }
+
+ return result;
}
@Override
@@ -76,21 +88,28 @@
MAC_ADDRESS + MISSING_MESSAGE);
String ipAddress = nullIsIllegal(json.get(IP_ADDRESS).asText(),
IP_ADDRESS + MISSING_MESSAGE);
- String deviceId = nullIsIllegal(json.get(DEVICE_ID).asText(),
- DEVICE_ID + MISSING_MESSAGE);
- String portNumber = nullIsIllegal(json.get(PORT_NUMBER).asText(),
- PORT_NUMBER + MISSING_MESSAGE);
- String state = nullIsIllegal(json.get(STATE).asText(),
- STATE + MISSING_MESSAGE);
- return DefaultK8sPort.builder()
+ K8sPort.Builder builder = DefaultK8sPort.builder()
.networkId(networkId)
.portId(portId)
.macAddress(MacAddress.valueOf(macAddress))
- .ipAddress(IpAddress.valueOf(ipAddress))
- .deviceId(DeviceId.deviceId(deviceId))
- .portNumber(PortNumber.portNumber(portNumber))
- .state(State.valueOf(state))
- .build();
+ .ipAddress(IpAddress.valueOf(ipAddress));
+
+ JsonNode deviceIdJson = json.get(DEVICE_ID);
+ if (deviceIdJson != null) {
+ builder.deviceId(DeviceId.deviceId(deviceIdJson.asText()));
+ }
+
+ JsonNode portNumberJson = json.get(PORT_NUMBER);
+ if (portNumberJson != null) {
+ builder.portNumber(PortNumber.portNumber(portNumberJson.asText()));
+ }
+
+ JsonNode stateJson = json.get(STATE);
+ if (stateJson != null) {
+ builder.state(State.valueOf(stateJson.asText()));
+ }
+
+ return builder.build();
}
}
diff --git a/apps/k8s-networking/app/src/main/java/org/onosproject/k8snetworking/web/K8sNetworkWebResource.java b/apps/k8s-networking/app/src/main/java/org/onosproject/k8snetworking/web/K8sNetworkWebResource.java
new file mode 100644
index 0000000..f582e4b
--- /dev/null
+++ b/apps/k8s-networking/app/src/main/java/org/onosproject/k8snetworking/web/K8sNetworkWebResource.java
@@ -0,0 +1,129 @@
+/*
+ * 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.web;
+
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.node.ObjectNode;
+import org.onosproject.k8snetworking.api.K8sNetwork;
+import org.onosproject.k8snetworking.api.K8sNetworkAdminService;
+import org.onosproject.rest.AbstractWebResource;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import javax.ws.rs.Consumes;
+import javax.ws.rs.DELETE;
+import javax.ws.rs.POST;
+import javax.ws.rs.PUT;
+import javax.ws.rs.Path;
+import javax.ws.rs.PathParam;
+import javax.ws.rs.Produces;
+import javax.ws.rs.core.MediaType;
+import javax.ws.rs.core.Response;
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.URI;
+import java.net.URISyntaxException;
+
+import static org.onlab.util.Tools.readTreeFromStream;
+
+/**
+ * Handles REST API call from CNI plugin.
+ */
+@Path("network")
+public class K8sNetworkWebResource extends AbstractWebResource {
+
+ protected final Logger log = LoggerFactory.getLogger(getClass());
+
+ private static final String MESSAGE = "Received network %s request";
+ private static final String NETWORK_INVALID = "Invalid networkId in network update request";
+
+ private final K8sNetworkAdminService adminService = get(K8sNetworkAdminService.class);
+
+ /**
+ * Creates a network from the JSON input stream.
+ *
+ * @param input network JSON input stream
+ * @return 201 CREATED if the JSON is correct, 400 BAD_REQUEST if the JSON
+ * is invalid or duplicated network already exists
+ * @onos.rsModel K8sNetwork
+ */
+ @POST
+ @Consumes(MediaType.APPLICATION_JSON)
+ @Produces(MediaType.APPLICATION_JSON)
+ public Response createNetwork(InputStream input) {
+ log.trace(String.format(MESSAGE, "CREATE"));
+ URI location;
+
+ try {
+ ObjectNode jsonTree = readTreeFromStream(mapper(), input);
+ final K8sNetwork network = codec(K8sNetwork.class).decode(jsonTree, this);
+ adminService.createNetwork(network);
+ location = new URI(network.networkId());
+ } catch (IOException | URISyntaxException e) {
+ throw new IllegalArgumentException(e);
+ }
+
+ return Response.created(location).build();
+ }
+
+ /**
+ * Updates the network with the specified identifier.
+ *
+ * @param id network identifier
+ * @param input network JSON input stream
+ * @return 200 OK with the updated network, 400 BAD_REQUEST if the requested
+ * network does not exist
+ * @onos.rsModel K8sNetwork
+ */
+ @PUT
+ @Path("{id}")
+ @Consumes(MediaType.APPLICATION_JSON)
+ @Produces(MediaType.APPLICATION_JSON)
+ public Response updateNetwork(@PathParam("id") String id, InputStream input) {
+ log.trace(String.format(MESSAGE, "UPDATED"));
+
+ try {
+ ObjectNode jsonTree = readTreeFromStream(mapper(), input);
+ JsonNode specifiedNetworkId = jsonTree.get("networkId");
+
+ if (specifiedNetworkId != null && !specifiedNetworkId.asText().equals(id)) {
+ throw new IllegalArgumentException(NETWORK_INVALID);
+ }
+
+ final K8sNetwork network = codec(K8sNetwork.class).decode(jsonTree, this);
+ adminService.updateNetwork(network);
+ } catch (IOException e) {
+ throw new IllegalArgumentException(e);
+ }
+
+ return Response.ok().build();
+ }
+
+ /**
+ * Removes the network with the given id.
+ *
+ * @param id network identifier
+ * @return 204 NO_CONTENT, 400 BAD_REQUEST if the network does not exist
+ */
+ @DELETE
+ @Path("{id}")
+ public Response removeNetwork(@PathParam("id") String id) {
+ log.trace(String.format(MESSAGE, "DELETE " + id));
+
+ adminService.removeNetwork(id);
+ return Response.noContent().build();
+ }
+}
diff --git a/apps/k8s-networking/app/src/main/java/org/onosproject/k8snetworking/web/K8sNetworkingCodecRegister.java b/apps/k8s-networking/app/src/main/java/org/onosproject/k8snetworking/web/K8sNetworkingCodecRegister.java
new file mode 100644
index 0000000..e482d52
--- /dev/null
+++ b/apps/k8s-networking/app/src/main/java/org/onosproject/k8snetworking/web/K8sNetworkingCodecRegister.java
@@ -0,0 +1,64 @@
+/*
+ * 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.web;
+
+import org.onosproject.codec.CodecService;
+import org.onosproject.k8snetworking.api.K8sIpam;
+import org.onosproject.k8snetworking.api.K8sNetwork;
+import org.onosproject.k8snetworking.api.K8sPort;
+import org.onosproject.k8snetworking.codec.K8sIpamCodec;
+import org.onosproject.k8snetworking.codec.K8sNetworkCodec;
+import org.onosproject.k8snetworking.codec.K8sPortCodec;
+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 static org.slf4j.LoggerFactory.getLogger;
+
+/**
+ * Implementation of the JSON codec brokering service for K8sNetworking.
+ */
+@Component(immediate = true)
+public class K8sNetworkingCodecRegister {
+
+ private final Logger log = getLogger(getClass());
+
+ @Reference(cardinality = ReferenceCardinality.MANDATORY)
+ protected CodecService codecService;
+
+ @Activate
+ protected void activate() {
+
+ codecService.registerCodec(K8sIpam.class, new K8sIpamCodec());
+ codecService.registerCodec(K8sNetwork.class, new K8sNetworkCodec());
+ codecService.registerCodec(K8sPort.class, new K8sPortCodec());
+
+ log.info("Started");
+ }
+
+ @Deactivate
+ protected void deactivate() {
+
+ codecService.unregisterCodec(K8sIpam.class);
+ codecService.unregisterCodec(K8sNetwork.class);
+ codecService.unregisterCodec(K8sPort.class);
+
+ log.info("Stopped");
+ }
+}
diff --git a/apps/k8s-networking/app/src/main/java/org/onosproject/k8snetworking/web/K8sNetworkingWebApplication.java b/apps/k8s-networking/app/src/main/java/org/onosproject/k8snetworking/web/K8sNetworkingWebApplication.java
new file mode 100644
index 0000000..ff268d8
--- /dev/null
+++ b/apps/k8s-networking/app/src/main/java/org/onosproject/k8snetworking/web/K8sNetworkingWebApplication.java
@@ -0,0 +1,33 @@
+/*
+ * 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.web;
+
+import org.onlab.rest.AbstractWebApplication;
+
+import java.util.Set;
+
+/**
+ * Kubernetes networking REST APIs web application.
+ */
+public class K8sNetworkingWebApplication extends AbstractWebApplication {
+ @Override
+ public Set<Class<?>> getClasses() {
+ return getClasses(
+ K8sNetworkWebResource.class,
+ K8sPortWebResource.class
+ );
+ }
+}
diff --git a/apps/k8s-networking/app/src/main/java/org/onosproject/k8snetworking/web/K8sPortWebResource.java b/apps/k8s-networking/app/src/main/java/org/onosproject/k8snetworking/web/K8sPortWebResource.java
new file mode 100644
index 0000000..f636b1a
--- /dev/null
+++ b/apps/k8s-networking/app/src/main/java/org/onosproject/k8snetworking/web/K8sPortWebResource.java
@@ -0,0 +1,129 @@
+/*
+ * 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.web;
+
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.node.ObjectNode;
+import org.onosproject.k8snetworking.api.K8sNetworkAdminService;
+import org.onosproject.k8snetworking.api.K8sPort;
+import org.onosproject.rest.AbstractWebResource;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import javax.ws.rs.Consumes;
+import javax.ws.rs.DELETE;
+import javax.ws.rs.POST;
+import javax.ws.rs.PUT;
+import javax.ws.rs.Path;
+import javax.ws.rs.PathParam;
+import javax.ws.rs.Produces;
+import javax.ws.rs.core.MediaType;
+import javax.ws.rs.core.Response;
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.URI;
+import java.net.URISyntaxException;
+
+import static org.onlab.util.Tools.readTreeFromStream;
+
+/**
+ * Handles port related REST API call from CNI plugin.
+ */
+@Path("port")
+public class K8sPortWebResource extends AbstractWebResource {
+
+ protected final Logger log = LoggerFactory.getLogger(getClass());
+
+ private static final String MESSAGE = "Received port %s request";
+ private static final String PORT_INVALID = "Invalid portId in port update request";
+
+ private final K8sNetworkAdminService adminService = get(K8sNetworkAdminService.class);
+
+ /**
+ * Creates a port from the JSON input stream.
+ *
+ * @param input port JSON input stream
+ * @return 201 CREATED if the JSON is correct, 400 BAD_REQUEST if the JSON
+ * is invalid or duplicated port already exists
+ * @onos.rsModel K8sPort
+ */
+ @POST
+ @Consumes(MediaType.APPLICATION_JSON)
+ @Produces(MediaType.APPLICATION_JSON)
+ public Response createPort(InputStream input) {
+ log.trace(String.format(MESSAGE, "CREATE"));
+ URI location;
+
+ try {
+ ObjectNode jsonTree = readTreeFromStream(mapper(), input);
+ final K8sPort port = codec(K8sPort.class).decode(jsonTree, this);
+ adminService.createPort(port);
+ location = new URI(port.portId());
+ } catch (IOException | URISyntaxException e) {
+ throw new IllegalArgumentException(e);
+ }
+
+ return Response.created(location).build();
+ }
+
+ /**
+ * Updates the port with the specified identifier.
+ *
+ * @param id port identifier
+ * @param input port JSON input stream
+ * @return 200 OK with the updated port, 400 BAD_REQUEST if the requested
+ * port does not exist
+ * @onos.rsModel K8sPort
+ */
+ @PUT
+ @Path("{id}")
+ @Consumes(MediaType.APPLICATION_JSON)
+ @Produces(MediaType.APPLICATION_JSON)
+ public Response updatePort(@PathParam("id") String id, InputStream input) {
+ log.trace(String.format(MESSAGE, "UPDATED"));
+
+ try {
+ ObjectNode jsonTree = readTreeFromStream(mapper(), input);
+ JsonNode specifiedPortId = jsonTree.get("portId");
+
+ if (specifiedPortId != null && !specifiedPortId.asText().equals(id)) {
+ throw new IllegalArgumentException(PORT_INVALID);
+ }
+
+ final K8sPort port = codec(K8sPort.class).decode(jsonTree, this);
+ adminService.updatePort(port);
+ } catch (IOException e) {
+ throw new IllegalArgumentException(e);
+ }
+
+ return Response.ok().build();
+ }
+
+ /**
+ * Removes the port with the given id.
+ *
+ * @param id port identifier
+ * @return 204 NO_CONTENT, 400 BAD_REQUEST if the port does not exist
+ */
+ @DELETE
+ @Path("{id}")
+ public Response removePort(@PathParam("id") String id) {
+ log.trace(String.format(MESSAGE, "DELETE " + id));
+
+ adminService.removePort(id);
+ return Response.noContent().build();
+ }
+}