Implement CLI and REST API for Xconnect

Deprecate the old way of configuring Xconnect via network config

Change-Id: I5b9ac7852517c25805bcbfc0e7b3bec3a52eed9f
diff --git a/apps/segmentrouting/web/BUCK b/apps/segmentrouting/web/BUCK
index bf6864c..9706438 100644
--- a/apps/segmentrouting/web/BUCK
+++ b/apps/segmentrouting/web/BUCK
@@ -9,7 +9,7 @@
 osgi_jar_with_tests (
     deps = COMPILE_DEPS,
     web_context = '/onos/segmentrouting',
-    api_title = 'Segment Routing Rest Server',
+    api_title = 'Segment Routing REST API',
     api_version = '1.0',
     api_description = 'REST API for Segment Routing Application',
     api_package = 'org.onosproject.segmentrouting.web',
diff --git a/apps/segmentrouting/web/pom.xml b/apps/segmentrouting/web/pom.xml
index 8c3901b..e7345e9 100644
--- a/apps/segmentrouting/web/pom.xml
+++ b/apps/segmentrouting/web/pom.xml
@@ -29,12 +29,12 @@
 
     <url>http://onosproject.org</url>
 
-    <description>Segment Routing REST Server</description>
+    <description>Segment Routing REST API</description>
 
     <properties>
         <web.context>/onos/segmentrouting</web.context>
         <api.version>1.0</api.version>
-        <api.title>Segment Routing Rest Server</api.title>
+        <api.title>Segment Routing REST APIr</api.title>
         <api.description>
             REST API for Segment Routing Application
         </api.description>
diff --git a/apps/segmentrouting/web/src/main/java/org/onosproject/segmentrouting/web/SegmentRoutingWebApplication.java b/apps/segmentrouting/web/src/main/java/org/onosproject/segmentrouting/web/SegmentRoutingWebApplication.java
index 75646d1..6704030 100644
--- a/apps/segmentrouting/web/src/main/java/org/onosproject/segmentrouting/web/SegmentRoutingWebApplication.java
+++ b/apps/segmentrouting/web/src/main/java/org/onosproject/segmentrouting/web/SegmentRoutingWebApplication.java
@@ -21,11 +21,15 @@
 import java.util.Set;
 
 /**
- * Segment Routing Web application.
+ * Segment Routing REST API.
  */
 public class SegmentRoutingWebApplication extends AbstractWebApplication {
     @Override
     public Set<Class<?>> getClasses() {
-        return getClasses(PseudowireWebResource.class, McastWebResource.class);
+        return getClasses(
+                PseudowireWebResource.class,
+                McastWebResource.class,
+                XconnectWebResource.class
+        );
     }
 }
diff --git a/apps/segmentrouting/web/src/main/java/org/onosproject/segmentrouting/web/XconnectWebResource.java b/apps/segmentrouting/web/src/main/java/org/onosproject/segmentrouting/web/XconnectWebResource.java
new file mode 100644
index 0000000..edaea9e
--- /dev/null
+++ b/apps/segmentrouting/web/src/main/java/org/onosproject/segmentrouting/web/XconnectWebResource.java
@@ -0,0 +1,109 @@
+/*
+ * 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.
+ */
+package org.onosproject.segmentrouting.web;
+
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.fasterxml.jackson.databind.node.ObjectNode;
+import org.onosproject.rest.AbstractWebResource;
+import org.onosproject.segmentrouting.xconnect.api.XconnectDesc;
+import org.onosproject.segmentrouting.xconnect.api.XconnectService;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import javax.ws.rs.Consumes;
+import javax.ws.rs.DELETE;
+import javax.ws.rs.GET;
+import javax.ws.rs.POST;
+import javax.ws.rs.Path;
+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.util.Set;
+
+import static org.onlab.util.Tools.readTreeFromStream;
+
+/**
+ * Query, create and remove Xconnects.
+ */
+@Path("xconnect")
+public class XconnectWebResource extends AbstractWebResource {
+    private static final String XCONNECTS = "xconnects";
+    private static Logger log = LoggerFactory.getLogger(XconnectWebResource.class);
+
+    /**
+     * Gets all Xconnects.
+     *
+     * @return an array of xconnects
+     */
+    @GET
+    @Produces(MediaType.APPLICATION_JSON)
+    public Response getXconnects() {
+        XconnectService xconnectService = get(XconnectService.class);
+        Set<XconnectDesc> xconnects = xconnectService.getXconnects();
+
+        ObjectNode result = encodeArray(XconnectDesc.class, XCONNECTS, xconnects);
+        return ok(result).build();
+    }
+
+    /**
+     * Create a new Xconnect.
+     *
+     * @param input JSON stream for xconnect to create
+     * @return 200 OK
+     * @throws IOException Throws IO exception
+     * @onos.rsModel XconnectCreate
+     */
+    @POST
+    @Consumes(MediaType.APPLICATION_JSON)
+    public Response addOrUpdateXconnect(InputStream input) throws IOException {
+        ObjectMapper mapper = new ObjectMapper();
+        ObjectNode json = readTreeFromStream(mapper, input);
+        XconnectDesc desc = codec(XconnectDesc.class).decode(json, this);
+
+        if (desc.ports().size() != 2) {
+            throw new IllegalArgumentException("Ports should have only two items.");
+        }
+
+        XconnectService xconnectService = get(XconnectService.class);
+        xconnectService.addOrUpdateXconnect(desc.key().deviceId(), desc.key().vlanId(), desc.ports());
+
+        return Response.ok().build();
+    }
+
+
+    /**
+     * Delete an existing Xconnect.
+     *
+     * @param input JSON stream for xconnect to remove
+     * @return 204 NO CONTENT
+     * @throws IOException Throws IO exception
+     * @onos.rsModel XconnectDelete
+     */
+    @DELETE
+    @Consumes(MediaType.APPLICATION_JSON)
+    public Response removeXconnect(InputStream input) throws IOException {
+        ObjectMapper mapper = new ObjectMapper();
+        ObjectNode json = readTreeFromStream(mapper, input);
+        XconnectDesc desc = codec(XconnectDesc.class).decode(json, this);
+
+        XconnectService xconnectService = get(XconnectService.class);
+        xconnectService.removeXonnect(desc.key().deviceId(), desc.key().vlanId());
+
+        return Response.noContent().build();
+    }
+}
diff --git a/apps/segmentrouting/web/src/main/resources/definitions/XconnectCreate.json b/apps/segmentrouting/web/src/main/resources/definitions/XconnectCreate.json
new file mode 100644
index 0000000..8d2099f
--- /dev/null
+++ b/apps/segmentrouting/web/src/main/resources/definitions/XconnectCreate.json
@@ -0,0 +1,29 @@
+{
+  "type": "object",
+  "title": "xconnect-creation",
+  "required": [
+    "deviceId",
+    "vlanId",
+    "ports"
+  ],
+  "properties": {
+    "deviceId": {
+      "type": "string",
+      "example": "of:0000000000000201",
+      "description": "Device ID"
+    },
+    "vlanId": {
+      "type": "string",
+      "example": "94",
+      "description": "VLAN ID"
+    },
+    "ports": {
+      "type": "array",
+      "items": {
+        "type": "int8",
+        "description": "Port number"
+      },
+      "example": [1, 2]
+    }
+  }
+}
\ No newline at end of file
diff --git a/apps/segmentrouting/web/src/main/resources/definitions/XconnectDelete.json b/apps/segmentrouting/web/src/main/resources/definitions/XconnectDelete.json
new file mode 100644
index 0000000..fcd5f66
--- /dev/null
+++ b/apps/segmentrouting/web/src/main/resources/definitions/XconnectDelete.json
@@ -0,0 +1,20 @@
+{
+  "type": "object",
+  "title": "xconnect-deletion",
+  "required": [
+    "deviceId",
+    "vlanId"
+  ],
+  "properties": {
+    "deviceId": {
+      "type": "string",
+      "example": "of:0000000000000201",
+      "description": "Device ID"
+    },
+    "vlanId": {
+      "type": "string",
+      "example": "94",
+      "description": "VLAN ID"
+    }
+  }
+}
\ No newline at end of file