Initial implementation of k8s networking REST API with unit tests

Change-Id: Ifb11204edb3c1e75b26810c0b104423941b0801d
diff --git a/apps/k8s-networking/api/src/main/java/org/onosproject/k8snetworking/api/DefaultK8sPort.java b/apps/k8s-networking/api/src/main/java/org/onosproject/k8snetworking/api/DefaultK8sPort.java
index e545f69..84548a2 100644
--- a/apps/k8s-networking/api/src/main/java/org/onosproject/k8snetworking/api/DefaultK8sPort.java
+++ b/apps/k8s-networking/api/src/main/java/org/onosproject/k8snetworking/api/DefaultK8sPort.java
@@ -169,9 +169,6 @@
             checkArgument(portId != null, NOT_NULL_MSG, "portId");
             checkArgument(macAddress != null, NOT_NULL_MSG, "macAddress");
             checkArgument(ipAddress != null, NOT_NULL_MSG, "ipAddress");
-            checkArgument(deviceId != null, NOT_NULL_MSG, "deviceId");
-            checkArgument(portNumber != null, NOT_NULL_MSG, "portNumber");
-            checkArgument(state != null, NOT_NULL_MSG, "state");
 
             return new DefaultK8sPort(networkId, portId, macAddress, ipAddress,
                     deviceId, portNumber, state);
diff --git a/apps/k8s-networking/app/BUILD b/apps/k8s-networking/app/BUILD
index 06da173..e6b5753 100644
--- a/apps/k8s-networking/app/BUILD
+++ b/apps/k8s-networking/app/BUILD
@@ -1,16 +1,24 @@
 COMPILE_DEPS = CORE_DEPS + JACKSON + KRYO + CLI + REST + [
     "//core/store/serializers:onos-core-serializers",
+    "//protocols/ovsdb/api:onos-protocols-ovsdb-api",
+    "//protocols/ovsdb/rfc:onos-protocols-ovsdb-rfc",
     "//apps/k8s-node/api:onos-apps-k8s-node-api",
     "//apps/k8s-networking/api:onos-apps-k8s-networking-api",
 ]
 
-TEST_DEPS = TEST_ADAPTERS + [
+TEST_DEPS = TEST_ADAPTERS + TEST_REST + [
     "//core/api:onos-api-tests",
     "//core/common:onos-core-common-tests",
+    "//web/api:onos-rest-tests",
 ]
 
 osgi_jar_with_tests(
+    api_description = "REST API for Kubernetes Networking",
+    api_package = "org.onosproject.k8snetworking.web",
+    api_title = "Kubernetes Networking API",
+    api_version = "1.0",
     karaf_command_packages = ["org.onosproject.k8snetworking.cli"],
     test_deps = TEST_DEPS,
+    web_context = "/onos/k8snetworking",
     deps = COMPILE_DEPS,
 )
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();
+    }
+}
diff --git a/apps/k8s-networking/app/src/main/resources/definitions/K8sNetwork.json b/apps/k8s-networking/app/src/main/resources/definitions/K8sNetwork.json
new file mode 100644
index 0000000..1a5a7f2
--- /dev/null
+++ b/apps/k8s-networking/app/src/main/resources/definitions/K8sNetwork.json
@@ -0,0 +1,50 @@
+{
+  "type": "object",
+  "description": "A network object.",
+  "required": [
+    "networkId",
+    "type",
+    "name",
+    "segmentId",
+    "gatewayIp",
+    "cidr",
+    "mtu"
+  ],
+  "properties": {
+    "networkId": {
+      "type": "string",
+      "example": "a87cc70a-3e15-4acf-8205-9b711a3531b7",
+      "description": "The ID of the attached network."
+    },
+    "type": {
+      "type": "string",
+      "example": "MINION",
+      "description": "Type of kubernetes node."
+    },
+    "name": {
+      "type": "string",
+      "example": "my_network",
+      "description": "The name of network."
+    },
+    "segmentId": {
+      "type": "string",
+      "example": "1",
+      "description": "Segment ID of tenant network."
+    },
+    "gatewayIp": {
+      "type": "string",
+      "example": "10.10.10.1",
+      "description": "The IP address of the gateway."
+    },
+    "cidr": {
+      "type": "string",
+      "example": "32",
+      "description": "The CIDR of this network."
+    },
+    "mtu": {
+      "type": "integer",
+      "example": 1500,
+      "description": "The Maximum Transmission Unit of this network."
+    }
+  }
+}
\ No newline at end of file
diff --git a/apps/k8s-networking/app/src/main/resources/definitions/K8sPort.json b/apps/k8s-networking/app/src/main/resources/definitions/K8sPort.json
new file mode 100644
index 0000000..6c8a069
--- /dev/null
+++ b/apps/k8s-networking/app/src/main/resources/definitions/K8sPort.json
@@ -0,0 +1,32 @@
+{
+  "type": "object",
+  "description": "A port object.",
+  "required": [
+    "portId",
+    "networkId",
+    "macAddress",
+    "ipAddress"
+  ],
+  "properties": {
+    "portId": {
+      "type": "string",
+      "example": "65c0ee9f-d634-4522-8954-51021b570b0d",
+      "description": "The ID of the resource."
+    },
+    "networkId": {
+      "type": "string",
+      "example": "a87cc70a-3e15-4acf-8205-9b711a3531b7",
+      "description": "The ID of the attached network."
+    },
+    "macAddress": {
+      "type": "string",
+      "example": "fa:16:3e:c9:cb:f0",
+      "description": "The MAC address of the port."
+    },
+    "ipAddress": {
+      "type": "string",
+      "example": "10.10.10.10",
+      "description": "The IP address of the port."
+    }
+  }
+}
\ No newline at end of file
diff --git a/apps/k8s-networking/app/src/main/webapp/WEB-INF/web.xml b/apps/k8s-networking/app/src/main/webapp/WEB-INF/web.xml
new file mode 100644
index 0000000..d31e3d8
--- /dev/null
+++ b/apps/k8s-networking/app/src/main/webapp/WEB-INF/web.xml
@@ -0,0 +1,58 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+  ~ Copyright 2018-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.
+  -->
+<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee"
+         xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
+         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
+         id="ONOS" version="2.5">
+    <display-name>Kubernetes Networking REST API v1.0</display-name>
+
+    <security-constraint>
+        <web-resource-collection>
+            <web-resource-name>Secured</web-resource-name>
+            <url-pattern>/*</url-pattern>
+        </web-resource-collection>
+        <auth-constraint>
+            <role-name>admin</role-name>
+            <role-name>viewer</role-name>
+        </auth-constraint>
+    </security-constraint>
+
+    <security-role>
+        <role-name>admin</role-name>
+        <role-name>viewer</role-name>
+    </security-role>
+
+    <login-config>
+        <auth-method>BASIC</auth-method>
+        <realm-name>karaf</realm-name>
+    </login-config>
+
+    <servlet>
+        <servlet-name>JAX-RS Service</servlet-name>
+        <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
+        <init-param>
+            <param-name>javax.ws.rs.Application</param-name>
+            <param-value>org.onosproject.k8snetworking.web.K8sNetworkingWebApplication</param-value>
+        </init-param>
+        <load-on-startup>1</load-on-startup>
+    </servlet>
+
+    <servlet-mapping>
+        <servlet-name>JAX-RS Service</servlet-name>
+        <url-pattern>/*</url-pattern>
+    </servlet-mapping>
+</web-app>
diff --git a/apps/k8s-networking/app/src/test/java/org/onosproject/k8snetworking/codec/K8sPortJsonMatcher.java b/apps/k8s-networking/app/src/test/java/org/onosproject/k8snetworking/codec/K8sPortJsonMatcher.java
index b0f327e..974dfab 100644
--- a/apps/k8s-networking/app/src/test/java/org/onosproject/k8snetworking/codec/K8sPortJsonMatcher.java
+++ b/apps/k8s-networking/app/src/test/java/org/onosproject/k8snetworking/codec/K8sPortJsonMatcher.java
@@ -77,25 +77,31 @@
         // check device ID
         String jsonDeviceId = jsonNode.get(DEVICE_ID).asText();
         String deviceId = port.deviceId().toString();
-        if (!jsonDeviceId.equals(deviceId)) {
-            description.appendText("device ID was " + jsonDeviceId);
-            return false;
+        if (jsonDeviceId != null) {
+            if (!jsonDeviceId.equals(deviceId)) {
+                description.appendText("device ID was " + jsonDeviceId);
+                return false;
+            }
         }
 
         // check port number
         String jsonPortNumber = jsonNode.get(PORT_NUMBER).asText();
         String portNumber = port.portNumber().toString();
-        if (!jsonPortNumber.equals(portNumber)) {
-            description.appendText("port number was " + jsonPortNumber);
-            return false;
+        if (jsonPortNumber != null) {
+            if (!jsonPortNumber.equals(portNumber)) {
+                description.appendText("port number was " + jsonPortNumber);
+                return false;
+            }
         }
 
         // check state
         String jsonState = jsonNode.get(STATE).asText();
         String state = port.state().name();
-        if (!jsonState.equals(state)) {
-            description.appendText("state was " + jsonState);
-            return false;
+        if (jsonState != null) {
+            if (!jsonState.equals(state)) {
+                description.appendText("state was " + jsonState);
+                return false;
+            }
         }
 
         return true;
diff --git a/apps/k8s-networking/app/src/test/java/org/onosproject/k8snetworking/web/K8sNetworkWebResourceTest.java b/apps/k8s-networking/app/src/test/java/org/onosproject/k8snetworking/web/K8sNetworkWebResourceTest.java
new file mode 100644
index 0000000..40299b2
--- /dev/null
+++ b/apps/k8s-networking/app/src/test/java/org/onosproject/k8snetworking/web/K8sNetworkWebResourceTest.java
@@ -0,0 +1,139 @@
+/*
+ * 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.glassfish.jersey.server.ResourceConfig;
+import org.junit.Before;
+import org.junit.Test;
+import org.onlab.osgi.ServiceDirectory;
+import org.onlab.osgi.TestServiceDirectory;
+import org.onosproject.codec.CodecService;
+import org.onosproject.codec.impl.CodecManager;
+import org.onosproject.k8snetworking.api.K8sNetwork;
+import org.onosproject.k8snetworking.api.K8sNetworkAdminService;
+import org.onosproject.k8snetworking.codec.K8sNetworkCodec;
+import org.onosproject.rest.resources.ResourceTest;
+
+import javax.ws.rs.client.Entity;
+import javax.ws.rs.client.WebTarget;
+import javax.ws.rs.core.MediaType;
+import javax.ws.rs.core.Response;
+import java.io.InputStream;
+
+import static org.easymock.EasyMock.anyObject;
+import static org.easymock.EasyMock.anyString;
+import static org.easymock.EasyMock.createMock;
+import static org.easymock.EasyMock.replay;
+import static org.easymock.EasyMock.verify;
+import static org.hamcrest.Matchers.is;
+import static org.junit.Assert.assertThat;
+
+/**
+ * Unit tests for kubernetes network REST API.
+ */
+public class K8sNetworkWebResourceTest extends ResourceTest {
+
+    final K8sNetworkAdminService mockAdminService = createMock(K8sNetworkAdminService.class);
+    private static final String PATH = "network";
+
+    private K8sNetwork k8sNetwork;
+
+    /**
+     * Constructs a kubernetes networking resource test instance.
+     */
+    public K8sNetworkWebResourceTest() {
+        super(ResourceConfig.forApplicationClass(K8sNetworkingWebApplication.class));
+    }
+
+    /**
+     * Sets up the global values for all the tests.
+     */
+    @Before
+    public void setUpTest() {
+        final CodecManager codecService = new CodecManager();
+        codecService.activate();
+        codecService.registerCodec(K8sNetwork.class, new K8sNetworkCodec());
+        ServiceDirectory testDirectory =
+                new TestServiceDirectory()
+                        .add(K8sNetworkAdminService.class, mockAdminService)
+                        .add(CodecService.class, codecService);
+        setServiceDirectory(testDirectory);
+    }
+
+    /**
+     * Tests the results of the REST API POST method with creating new network operation.
+     */
+    @Test
+    public void testCreateNetworkWithCreateOperation() {
+        mockAdminService.createNetwork(anyObject());
+        replay(mockAdminService);
+
+        final WebTarget wt = target();
+        InputStream jsonStream = K8sNetworkWebResourceTest.class
+                .getResourceAsStream("k8s-network.json");
+        Response response = wt.path(PATH).request(MediaType.APPLICATION_JSON_TYPE)
+                .post(Entity.json(jsonStream));
+        final int status = response.getStatus();
+
+        assertThat(status, is(201));
+
+        verify(mockAdminService);
+    }
+
+    /**
+     * Tests the results of the REST API PUT method with modifying the network.
+     */
+    @Test
+    public void testUpdateNetworkWithModifyOperation() {
+        mockAdminService.updateNetwork(anyObject());
+        replay(mockAdminService);
+
+        String location = PATH + "/network-1";
+
+        final WebTarget wt = target();
+        InputStream jsonStream = K8sNetworkWebResourceTest.class
+                .getResourceAsStream("k8s-network.json");
+        Response response = wt.path(location)
+                .request(MediaType.APPLICATION_JSON_TYPE)
+                .put(Entity.json(jsonStream));
+        final int status = response.getStatus();
+
+        assertThat(status, is(200));
+
+        verify(mockAdminService);
+    }
+
+    /**
+     * Tests the results of the REST API DELETE method with deleting the network.
+     */
+    @Test
+    public void testDeleteNetworkWithDeletionOperation() {
+        mockAdminService.removeNetwork(anyString());
+        replay(mockAdminService);
+
+        String location = PATH + "/network-1";
+
+        final WebTarget wt = target();
+        Response response = wt.path(location).request(
+                MediaType.APPLICATION_JSON_TYPE).delete();
+
+        final int status = response.getStatus();
+
+        assertThat(status, is(204));
+
+        verify(mockAdminService);
+    }
+}
diff --git a/apps/k8s-networking/app/src/test/java/org/onosproject/k8snetworking/web/K8sNetworkingCodecRegisterTest.java b/apps/k8s-networking/app/src/test/java/org/onosproject/k8snetworking/web/K8sNetworkingCodecRegisterTest.java
new file mode 100644
index 0000000..c536a3c
--- /dev/null
+++ b/apps/k8s-networking/app/src/test/java/org/onosproject/k8snetworking/web/K8sNetworkingCodecRegisterTest.java
@@ -0,0 +1,86 @@
+/*
+ * 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.google.common.collect.ImmutableSet;
+import com.google.common.collect.Maps;
+import org.junit.Test;
+import org.onlab.junit.TestUtils;
+import org.onosproject.codec.CodecService;
+import org.onosproject.codec.JsonCodec;
+import org.onosproject.k8snetworking.api.K8sNetwork;
+import org.onosproject.k8snetworking.api.K8sPort;
+import org.onosproject.k8snetworking.codec.K8sNetworkCodec;
+import org.onosproject.k8snetworking.codec.K8sPortCodec;
+import org.onosproject.k8snode.api.K8sNode;
+
+import java.util.Map;
+import java.util.Set;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNull;
+
+/**
+ * Unit tests for kubernetes networking codec register.
+ */
+public class K8sNetworkingCodecRegisterTest {
+
+    /**
+     * Tests codec register activation and deactivation.
+     */
+    @Test
+    public void testActivateDeactivate() {
+        K8sNetworkingCodecRegister register = new K8sNetworkingCodecRegister();
+        CodecService codecService = new TestCodecService();
+
+        TestUtils.setField(register, "codecService", codecService);
+        register.activate();
+
+        assertEquals(K8sNetworkCodec.class.getName(),
+                codecService.getCodec(K8sNetwork.class).getClass().getName());
+        assertEquals(K8sPortCodec.class.getName(),
+                codecService.getCodec(K8sPort.class).getClass().getName());
+
+        register.deactivate();
+
+        assertNull(codecService.getCodec(K8sNode.class));
+    }
+
+    private static class TestCodecService implements CodecService {
+
+        private Map<String, JsonCodec> codecMap = Maps.newConcurrentMap();
+
+        @Override
+        public Set<Class<?>> getCodecs() {
+            return ImmutableSet.of();
+        }
+
+        @Override
+        public <T> JsonCodec<T> getCodec(Class<T> entityClass) {
+            return codecMap.get(entityClass.getName());
+        }
+
+        @Override
+        public <T> void registerCodec(Class<T> entityClass, JsonCodec<T> codec) {
+            codecMap.put(entityClass.getName(), codec);
+        }
+
+        @Override
+        public void unregisterCodec(Class<?> entityClass) {
+            codecMap.remove(entityClass.getName());
+        }
+    }
+}
diff --git a/apps/k8s-networking/app/src/test/java/org/onosproject/k8snetworking/web/K8sPortWebResourceTest.java b/apps/k8s-networking/app/src/test/java/org/onosproject/k8snetworking/web/K8sPortWebResourceTest.java
new file mode 100644
index 0000000..2cb2047
--- /dev/null
+++ b/apps/k8s-networking/app/src/test/java/org/onosproject/k8snetworking/web/K8sPortWebResourceTest.java
@@ -0,0 +1,139 @@
+/*
+ * 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.glassfish.jersey.server.ResourceConfig;
+import org.junit.Before;
+import org.junit.Test;
+import org.onlab.osgi.ServiceDirectory;
+import org.onlab.osgi.TestServiceDirectory;
+import org.onosproject.codec.CodecService;
+import org.onosproject.codec.impl.CodecManager;
+import org.onosproject.k8snetworking.api.K8sNetworkAdminService;
+import org.onosproject.k8snetworking.api.K8sPort;
+import org.onosproject.k8snetworking.codec.K8sPortCodec;
+import org.onosproject.rest.resources.ResourceTest;
+
+import javax.ws.rs.client.Entity;
+import javax.ws.rs.client.WebTarget;
+import javax.ws.rs.core.MediaType;
+import javax.ws.rs.core.Response;
+import java.io.InputStream;
+
+import static org.easymock.EasyMock.anyObject;
+import static org.easymock.EasyMock.anyString;
+import static org.easymock.EasyMock.createMock;
+import static org.easymock.EasyMock.replay;
+import static org.easymock.EasyMock.verify;
+import static org.hamcrest.Matchers.is;
+import static org.junit.Assert.assertThat;
+
+/**
+ * Unit test for kubernetes port REST API.
+ */
+public class K8sPortWebResourceTest extends ResourceTest {
+
+    final K8sNetworkAdminService mockAdminService = createMock(K8sNetworkAdminService.class);
+    private static final String PATH = "port";
+
+    private K8sPort k8sPort;
+
+    /**
+     * Constructs a kubernetes networking resource test instance.
+     */
+    public K8sPortWebResourceTest() {
+        super(ResourceConfig.forApplicationClass(K8sNetworkingWebApplication.class));
+    }
+
+    /**
+     * Sets up the global values for all the tests.
+     */
+    @Before
+    public void setUpTest() {
+        final CodecManager codecService = new CodecManager();
+        codecService.activate();
+        codecService.registerCodec(K8sPort.class, new K8sPortCodec());
+        ServiceDirectory testDirectory =
+                new TestServiceDirectory()
+                        .add(K8sNetworkAdminService.class, mockAdminService)
+                        .add(CodecService.class, codecService);
+        setServiceDirectory(testDirectory);
+    }
+
+    /**
+     * Tests the results of the REST API POST method with creating new port operation.
+     */
+    @Test
+    public void testCreatePortWithCreateOperation() {
+        mockAdminService.createPort(anyObject());
+        replay(mockAdminService);
+
+        final WebTarget wt = target();
+        InputStream jsonStream = K8sPortWebResourceTest.class
+                .getResourceAsStream("k8s-port.json");
+        Response response = wt.path(PATH).request(MediaType.APPLICATION_JSON_TYPE)
+                .post(Entity.json(jsonStream));
+        final int status = response.getStatus();
+
+        assertThat(status, is(201));
+
+        verify(mockAdminService);
+    }
+
+    /**
+     * Tests the results of the REST API PUT method with modifying the port.
+     */
+    @Test
+    public void testUpdatePortWithModifyOperation() {
+        mockAdminService.updatePort(anyObject());
+        replay(mockAdminService);
+
+        String location = PATH + "/port-1";
+
+        final WebTarget wt = target();
+        InputStream jsonStream = K8sPortWebResourceTest.class
+                .getResourceAsStream("k8s-port.json");
+        Response response = wt.path(location)
+                .request(MediaType.APPLICATION_JSON_TYPE)
+                .put(Entity.json(jsonStream));
+        final int status = response.getStatus();
+
+        assertThat(status, is(200));
+
+        verify(mockAdminService);
+    }
+
+    /**
+     * Tests the results of the REST API DELETE method with deleting the port.
+     */
+    @Test
+    public void testDeletePortWithDeletionOperation() {
+        mockAdminService.removePort(anyString());
+        replay(mockAdminService);
+
+        String location = PATH + "/port-1";
+
+        final WebTarget wt = target();
+        Response response = wt.path(location).request(
+                MediaType.APPLICATION_JSON_TYPE).delete();
+
+        final int status = response.getStatus();
+
+        assertThat(status, is(204));
+
+        verify(mockAdminService);
+    }
+}
diff --git a/apps/k8s-networking/app/src/test/resources/org/onosproject/k8snetworking/web/k8s-network.json b/apps/k8s-networking/app/src/test/resources/org/onosproject/k8snetworking/web/k8s-network.json
new file mode 100644
index 0000000..6efe965
--- /dev/null
+++ b/apps/k8s-networking/app/src/test/resources/org/onosproject/k8snetworking/web/k8s-network.json
@@ -0,0 +1,9 @@
+{
+  "networkId": "network-1",
+  "name": "network-1",
+  "type": "VXLAN",
+  "mtu": 1500,
+  "segmentId": "1",
+  "gatewayIp": "10.10.10.1",
+  "cidr": "32"
+}
\ No newline at end of file
diff --git a/apps/k8s-networking/app/src/test/resources/org/onosproject/k8snetworking/web/k8s-port.json b/apps/k8s-networking/app/src/test/resources/org/onosproject/k8snetworking/web/k8s-port.json
new file mode 100644
index 0000000..d55ea94
--- /dev/null
+++ b/apps/k8s-networking/app/src/test/resources/org/onosproject/k8snetworking/web/k8s-port.json
@@ -0,0 +1,6 @@
+{
+  "networkId": "network-1",
+  "portId": "port-1",
+  "macAddress": "00:11:22:33:44:55",
+  "ipAddress": "10.10.10.10"
+}
\ No newline at end of file
diff --git a/apps/k8s-node/app/src/test/java/org/onosproject/k8snode/web/K8sNodeWebResourceTest.java b/apps/k8s-node/app/src/test/java/org/onosproject/k8snode/web/K8sNodeWebResourceTest.java
index c52f398..61c2e2d 100644
--- a/apps/k8s-node/app/src/test/java/org/onosproject/k8snode/web/K8sNodeWebResourceTest.java
+++ b/apps/k8s-node/app/src/test/java/org/onosproject/k8snode/web/K8sNodeWebResourceTest.java
@@ -132,7 +132,7 @@
      * Tests the results of the REST API PUT method with modifying the nodes.
      */
     @Test
-    public void testUpdateNodesWithoutModifyOperation() {
+    public void testUpdateNodesWithModifyOperation() {
         expect(mockK8sNodeAdminService.node(anyString())).andReturn(k8sNode).once();
         mockK8sNodeAdminService.updateNode(anyObject());
         replay(mockK8sNodeAdminService);
@@ -153,7 +153,7 @@
      * Tests the results of the REST API PUT method without modifying the nodes.
      */
     @Test
-    public void testUpdateNodesWithModifyOperation() {
+    public void testUpdateNodesWithoutModifyOperation() {
         expect(mockK8sNodeAdminService.node(anyString())).andReturn(null).once();
         replay(mockK8sNodeAdminService);