REST interface for VPLS application

Change-Id: I2cab5bd6ff0ce026d0ef844bba6199fdd7f3e50d
This repository contains the files that provide a REST interface for VPLS
application.I create a new package in org.onosproject.vpls
called rest that contains the java classes VplsWebApplication and
VplsWebResource. The VplsWebResource provides create/update/read/delete
(CURD) functionality, leveraging the methods defined in the Vpls java
interface. I create a new folder called resources that contains
the json definitions and the files for the "web page".
diff --git a/apps/vpls/src/main/java/org/onosproject/vpls/VplsManager.java b/apps/vpls/src/main/java/org/onosproject/vpls/VplsManager.java
index 425f77d..293f92b 100644
--- a/apps/vpls/src/main/java/org/onosproject/vpls/VplsManager.java
+++ b/apps/vpls/src/main/java/org/onosproject/vpls/VplsManager.java
@@ -16,6 +16,7 @@
 package org.onosproject.vpls;
 
 import com.google.common.collect.ImmutableSet;
+import org.onosproject.codec.CodecService;
 import org.onosproject.net.EncapsulationType;
 import org.onosproject.net.Host;
 import org.onosproject.net.host.HostEvent;
@@ -29,6 +30,7 @@
 import org.onosproject.vpls.api.VplsOperation;
 import org.onosproject.vpls.api.VplsOperationService;
 import org.onosproject.vpls.api.VplsStore;
+import org.onosproject.vpls.rest.VplsCodec;
 import org.onosproject.vpls.store.VplsStoreEvent;
 import org.osgi.service.component.annotations.Activate;
 import org.osgi.service.component.annotations.Component;
@@ -65,9 +67,13 @@
     @Reference(cardinality = ReferenceCardinality.MANDATORY)
     protected VplsOperationService operationService;
 
+    @Reference(cardinality = ReferenceCardinality.MANDATORY)
+    protected CodecService codecService;
+
     private StoreDelegate<VplsStoreEvent> vplsStoreDelegate;
     private HostListener vplsHostListener;
 
+
     @Activate
     public void activate() {
         vplsStoreDelegate = new VplsStoreDelegate();
@@ -75,12 +81,14 @@
 
         vplsStore.setDelegate(vplsStoreDelegate);
         hostService.addListener(vplsHostListener);
+        codecService.registerCodec(VplsData.class, new VplsCodec());
     }
 
     @Deactivate
     public void deactivate() {
         vplsStore.unsetDelegate(vplsStoreDelegate);
         hostService.removeListener(vplsHostListener);
+        codecService.unregisterCodec(VplsData.class);
     }
 
     @Override
diff --git a/apps/vpls/src/main/java/org/onosproject/vpls/rest/VplsCodec.java b/apps/vpls/src/main/java/org/onosproject/vpls/rest/VplsCodec.java
new file mode 100644
index 0000000..8f8bf3f
--- /dev/null
+++ b/apps/vpls/src/main/java/org/onosproject/vpls/rest/VplsCodec.java
@@ -0,0 +1,84 @@
+/*
+ * Copyright 2020-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.vpls.rest;
+
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.node.ArrayNode;
+import com.fasterxml.jackson.databind.node.ObjectNode;
+import org.onosproject.codec.CodecContext;
+import org.onosproject.codec.JsonCodec;
+import org.onosproject.net.EncapsulationType;
+import org.onosproject.net.intf.Interface;
+import org.onosproject.vpls.api.VplsData;
+import org.slf4j.Logger;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.stream.IntStream;
+
+import static com.google.common.base.Preconditions.checkNotNull;
+import static org.slf4j.LoggerFactory.getLogger;
+/**
+ * Vpls JSON codec.
+ */
+public final class VplsCodec extends JsonCodec<VplsData> {
+
+    // JSON field names
+    private static final String NAME = "name";
+    private static final String ENCAPSULATION_TYPE = "encapsulation";
+    private static final String INTERFACES = "interfaces";
+
+    private final Logger log = getLogger(getClass());
+
+    @Override
+    public ObjectNode encode(VplsData vplsData, CodecContext context) {
+        checkNotNull(vplsData, "Vpls cannot be null");
+        ObjectNode result = context.mapper().createObjectNode()
+                .put(NAME, vplsData.name())
+                .put(ENCAPSULATION_TYPE, vplsData.encapsulationType().toString());
+        ArrayNode interfaces = context.mapper().createArrayNode();
+        vplsData.interfaces().forEach(interf -> {
+            ObjectNode bandJson = context.codec(Interface.class).encode(interf, context);
+            interfaces.add(bandJson);
+        });
+        result.set(INTERFACES, interfaces);
+        return result;
+    }
+    @Override
+    public VplsData decode(ObjectNode json, CodecContext context) {
+        if (json == null || !json.isObject()) {
+            return null;
+        }
+
+        String vplsName = json.findValue(NAME).asText();
+        EncapsulationType encap = EncapsulationType.enumFromString(json.findValue(ENCAPSULATION_TYPE).asText());
+        VplsData vplsData = VplsData.of(vplsName, encap);
+
+        Collection<Interface> interfaceList = new ArrayList<>();
+        JsonNode interfacesJeson = json.findValue(INTERFACES);
+        JsonCodec<Interface> interfaceCodec = context.codec(Interface.class);
+        if (interfacesJeson != null) {
+            IntStream.range(0, interfacesJeson.size())
+                    .forEach(i -> interfaceList.add(
+                            interfaceCodec.decode(get(interfacesJeson, i),
+                                    context)));
+            vplsData.addInterfaces(interfaceList);
+        }
+        return vplsData;
+    }
+
+}
+
diff --git a/apps/vpls/src/main/java/org/onosproject/vpls/rest/VplsWebApplication.java b/apps/vpls/src/main/java/org/onosproject/vpls/rest/VplsWebApplication.java
new file mode 100644
index 0000000..0581130
--- /dev/null
+++ b/apps/vpls/src/main/java/org/onosproject/vpls/rest/VplsWebApplication.java
@@ -0,0 +1,31 @@
+/*
+ * Copyright 2020-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.vpls.rest;
+
+import org.onlab.rest.AbstractWebApplication;
+
+import java.util.Set;
+
+/**
+ * VPLS web application.
+ */
+public class VplsWebApplication extends AbstractWebApplication {
+    @Override
+    public Set<Class<?>> getClasses() {
+        return getClasses(VplsWebResource.class);
+    }
+}
diff --git a/apps/vpls/src/main/java/org/onosproject/vpls/rest/VplsWebResource.java b/apps/vpls/src/main/java/org/onosproject/vpls/rest/VplsWebResource.java
new file mode 100644
index 0000000..ae227ed
--- /dev/null
+++ b/apps/vpls/src/main/java/org/onosproject/vpls/rest/VplsWebResource.java
@@ -0,0 +1,222 @@
+/*
+ * Copyright 2020-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.vpls.rest;
+
+
+import com.fasterxml.jackson.databind.node.ArrayNode;
+import com.fasterxml.jackson.databind.node.ObjectNode;
+import org.onosproject.net.intf.Interface;
+import org.onosproject.net.intf.InterfaceAdminService;
+import org.onosproject.rest.AbstractWebResource;
+import org.onosproject.vpls.api.Vpls;
+import org.onosproject.vpls.api.VplsData;
+
+import org.slf4j.Logger;
+
+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.PathParam;
+import javax.ws.rs.Produces;
+import javax.ws.rs.core.Context;
+import javax.ws.rs.core.Response;
+import javax.ws.rs.core.UriInfo;
+import javax.ws.rs.core.UriBuilder;
+import javax.ws.rs.core.MediaType;
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.Collection;
+import java.util.ArrayList;
+
+
+import static org.onlab.util.Tools.readTreeFromStream;
+import static org.onlab.util.Tools.nullIsNotFound;
+import static org.slf4j.LoggerFactory.getLogger;
+
+/**
+ * Query and program Vplss.
+ */
+@Path("vpls")
+public class VplsWebResource extends AbstractWebResource {
+    @Context
+    private UriInfo uriInfo;
+
+    private static final String VPLS_NOT_FOUND = "Vpls is not found for ";
+    private static final String VPLSS = "vplss";
+    private static final String VPLS = "vpls";
+    private static final String INTERFACES = "interfaces";
+
+    private final ObjectNode root = mapper().createObjectNode();
+    private final Logger log = getLogger(getClass());
+    /**
+     * Gets all Vplss. Returns array of all Vplss in the system.
+     *
+     * @return 200 OK with a collection of Vplss
+     * @onos.rsModel Vplss
+     */
+    @GET
+    @Produces(MediaType.APPLICATION_JSON)
+    public Response getVplss() {
+        ArrayNode vplssNode = root.putArray(VPLSS);
+        Vpls service = get(Vpls.class);
+        Collection<VplsData> vplsDatas = service.getAllVpls();
+        if (!vplsDatas.isEmpty()) {
+            for (VplsData entry : vplsDatas) {
+                vplssNode.add(codec(VplsData.class).encode(entry, this));
+            }
+        }
+
+        return ok(root).build();
+    }
+
+    /**
+     * Gets Vpls. Returns a Vpls by vplsName.
+     * @param  vplsName  vpls name
+     * @return 200 OK with a vpls, return 404 if no entry has been found
+     * @onos.rsModel Vpls
+     */
+    @GET
+    @Produces(MediaType.APPLICATION_JSON)
+    @Path("{vplsName}")
+    public Response getVpls(@PathParam("vplsName") String vplsName) {
+        ArrayNode vplsNode = root.putArray(VPLS);
+        Vpls service = get(Vpls.class);
+        final VplsData vplsData = nullIsNotFound(service.getVpls(vplsName),
+                VPLS_NOT_FOUND + vplsName);
+        vplsNode.add(codec(VplsData.class).encode(vplsData, this));
+
+        return ok(root).build();
+    }
+    /**
+     * Creates new vpls. Creates and installs a new Vplps.<br>
+     *
+     * @param stream Vpls JSON
+     * @return status of the request - CREATED if the JSON is correct,
+     * BAD_REQUEST if the JSON is invalid
+     * @onos.rsModel VplsPost
+     */
+    @POST
+    @Consumes(MediaType.APPLICATION_JSON)
+    @Produces(MediaType.APPLICATION_JSON)
+    public Response createVpls(InputStream stream) {
+        Vpls service = get(Vpls.class);
+        InterfaceAdminService interfaceService = get(InterfaceAdminService.class);
+        try {
+            ObjectNode jsonTree = readTreeFromStream(mapper(), stream);
+
+            VplsData vplsData = codec(VplsData.class).decode(jsonTree, this);
+            vplsData.interfaces().forEach(interf -> {
+                    interfaceService.add(interf);
+            });
+            service.addInterfaces(vplsData, vplsData.interfaces());
+
+            UriBuilder locationBuilder = uriInfo.getBaseUriBuilder()
+                    .path(VPLS);
+            return Response
+                    .created(locationBuilder.build())
+                    .build();
+
+            } catch (IOException e) {
+            throw new IllegalArgumentException(e.getMessage());
+        }
+
+    }
+
+    /**
+     * Add new interfaces. Add new interfaces to a Vpls.<br>
+     *
+     * @param stream interfaces JSON
+     * @param vplsName Vpls name
+     * @return status of the request - CREATED if the JSON is correct,
+     * BAD_REQUEST if the JSON is invalid
+     * @onos.rsModel InterfacesPost
+     */
+    @POST
+    @Path("interfaces/{vplsName}")
+    @Consumes(MediaType.APPLICATION_JSON)
+    @Produces(MediaType.APPLICATION_JSON)
+    public Response addInterfaces(@PathParam("vplsName") String vplsName, InputStream stream) {
+        Vpls service = get(Vpls.class);
+        InterfaceAdminService interfaceService = get(InterfaceAdminService.class);
+        final VplsData vplsData = nullIsNotFound(service.getVpls(vplsName),
+                VPLS_NOT_FOUND + vplsName);
+        try {
+            ObjectNode jsonTree = readTreeFromStream(mapper(), stream);
+            Collection<Interface> interfaceList = new ArrayList<>();
+            jsonTree.forEach(interf -> {
+                Interface inter = codec(Interface.class).decode(jsonTree, this);
+                interfaceList.add(inter);
+                interfaceService.add(inter);
+            });
+            service.addInterfaces(vplsData, interfaceList);
+            UriBuilder locationBuilder = uriInfo.getBaseUriBuilder()
+                    .path(INTERFACES)
+                    .path(vplsName);
+            return Response
+                    .created(locationBuilder.build())
+                    .build();
+
+        } catch (IOException e) {
+            throw new IllegalArgumentException(e.getMessage());
+        }
+
+    }
+
+    /**
+     * Removes the specified vpls.
+     *
+     * @param vplsName Vpls name
+     * @return 204 NO CONTENT
+     */
+    @DELETE
+    @Path("{vplsName}")
+    public Response deleteVpls(@PathParam("vplsName") String vplsName) {
+        Vpls service = get(Vpls.class);
+        final VplsData vplsData = nullIsNotFound(service.getVpls(vplsName),
+                VPLS_NOT_FOUND + vplsName);
+        service.removeVpls(vplsData);
+        return Response.noContent().build();
+    }
+
+    /**
+     * Removes a specified interface.
+     *
+     * @param vplsName Vpls name
+     * @param interfaceName interface name
+     * @return 204 NO CONTENT
+     *
+     */
+    @DELETE
+    @Path("interface/{vplsName}/{interfaceName}")
+    public Response deleteInterface(@PathParam("vplsName") String vplsName,
+                                    @PathParam("interfaceName") String interfaceName) {
+        Vpls service = get(Vpls.class);
+        InterfaceAdminService interfaceService = get(InterfaceAdminService.class);
+        final VplsData vplsData = nullIsNotFound(service.getVpls(vplsName),
+                VPLS_NOT_FOUND + vplsName);
+        vplsData.interfaces().forEach(anInterface -> {
+            if (anInterface.name().equals(interfaceName)) {
+                interfaceService.remove(anInterface.connectPoint(), anInterface.name());
+                service.removeInterface(vplsData, anInterface);
+            }
+        });
+
+        return Response.noContent().build();
+    }
+
+}
diff --git a/apps/vpls/src/main/java/org/onosproject/vpls/rest/package-info.java b/apps/vpls/src/main/java/org/onosproject/vpls/rest/package-info.java
new file mode 100644
index 0000000..f8adcdf
--- /dev/null
+++ b/apps/vpls/src/main/java/org/onosproject/vpls/rest/package-info.java
@@ -0,0 +1,20 @@
+/*
+ * Copyright 2017-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.
+ */
+
+/**
+ * Implementation REST for VPLS services.
+ */
+package org.onosproject.vpls.rest;
\ No newline at end of file