VOL-540: REST interface for provisioning/removal of HSIA service - rebased

Change-Id: I7bcd8a85cab375786ec72620e5d0d06bce084ded
diff --git a/app/pom.xml b/app/pom.xml
index 3a93474..6a97b0a 100644
--- a/app/pom.xml
+++ b/app/pom.xml
@@ -20,7 +20,7 @@
     <parent>
         <groupId>org.opencord</groupId>
         <artifactId>olt</artifactId>
-        <version>2.0.0-SNAPSHOT</version>
+        <version>2.1.0-SNAPSHOT</version>
         <relativePath>../pom.xml</relativePath>
     </parent>
     <modelVersion>4.0.0</modelVersion>
diff --git a/app/src/main/java/org/opencord/olt/cli/ShowSubscribersCommand.java b/app/src/main/java/org/opencord/olt/cli/ShowSubscribersCommand.java
index 7747fb1..4298cd6 100644
--- a/app/src/main/java/org/opencord/olt/cli/ShowSubscribersCommand.java
+++ b/app/src/main/java/org/opencord/olt/cli/ShowSubscribersCommand.java
@@ -31,7 +31,7 @@
         description = "Shows provisioned subscribers")
 public class ShowSubscribersCommand extends AbstractShellCommand {
 
-    private static final String FORMAT = "port=%s, cvlan=%s";
+    private static final String FORMAT = "port=%s, svlan=%s, cvlan=%s";
 
     @Override
     protected void execute() {
@@ -39,7 +39,8 @@
         service.getSubscribers().forEach(this::display);
     }
 
-    private void display(Map.Entry<ConnectPoint, VlanId> subscriber) {
-        print(FORMAT, subscriber.getKey(), subscriber.getValue());
+    private void display(Map.Entry<ConnectPoint, Map.Entry<VlanId, VlanId>> subscriber) {
+        print(FORMAT, subscriber.getKey(), subscriber.getValue().getKey(),
+                subscriber.getValue().getValue());
     }
 }
diff --git a/app/src/main/java/org/opencord/olt/impl/Olt.java b/app/src/main/java/org/opencord/olt/impl/Olt.java
index 755c178..3c5108e 100644
--- a/app/src/main/java/org/opencord/olt/impl/Olt.java
+++ b/app/src/main/java/org/opencord/olt/impl/Olt.java
@@ -58,6 +58,7 @@
 import org.opencord.olt.AccessDeviceEvent;
 import org.opencord.olt.AccessDeviceListener;
 import org.opencord.olt.AccessDeviceService;
+import org.opencord.olt.AccessSubscriberId;
 import org.opencord.sadis.SubscriberAndDeviceInformation;
 import org.opencord.sadis.SubscriberAndDeviceInformationService;
 import org.osgi.service.component.ComponentContext;
@@ -189,21 +190,21 @@
     }
 
     @Override
-    public void provisionSubscriber(ConnectPoint port) {
+    public boolean provisionSubscriber(ConnectPoint port) {
         checkNotNull(deviceService.getPort(port.deviceId(), port.port()),
                 "Invalid connect point");
         // Find the subscriber on this connect point
         SubscriberAndDeviceInformation sub = getSubscriber(port);
         if (sub == null) {
             log.warn("No subscriber found for {}", port);
-            return;
+            return false;
         }
 
         // Get the uplink port
         Port uplinkPort = getUplinkPort(deviceService.getDevice(port.deviceId()));
         if (uplinkPort == null) {
             log.warn("No uplink port found for OLT device {}", port.deviceId());
-            return;
+            return false;
         }
 
         if (enableDhcpOnProvisioning) {
@@ -217,22 +218,23 @@
         if (enableIgmpOnProvisioning) {
             processIgmpFilteringObjectives(port.deviceId(), port.port(), true);
         }
+        return true;
     }
 
     @Override
-    public void removeSubscriber(ConnectPoint port) {
+    public boolean removeSubscriber(ConnectPoint port) {
         // Get the subscriber connected to this port from Sadis
         SubscriberAndDeviceInformation subscriber = getSubscriber(port);
         if (subscriber == null) {
             log.warn("Subscriber on port {} not found", port);
-            return;
+            return false;
         }
 
         // Get the uplink port
         Port uplinkPort = getUplinkPort(deviceService.getDevice(port.deviceId()));
         if (uplinkPort == null) {
             log.warn("No uplink port found for OLT device {}", port.deviceId());
-            return;
+            return false;
         }
 
         if (enableDhcpOnProvisioning) {
@@ -246,12 +248,36 @@
         if (enableIgmpOnProvisioning) {
             processIgmpFilteringObjectives(port.deviceId(), port.port(), false);
         }
+        return true;
     }
 
+    @Override
+    public boolean provisionSubscriber(AccessSubscriberId subscriberId) {
+        // Check if we can find the connect point to which this subscriber is connected
+        ConnectPoint subsPort = findSubscriberConnectPoint(subscriberId.toString());
+        if (subsPort == null) {
+            log.warn("ConnectPoint for {} not found", subscriberId);
+            return false;
+        }
+
+        return provisionSubscriber(subsPort);
+    }
 
     @Override
-    public Collection<Map.Entry<ConnectPoint, VlanId>> getSubscribers() {
-        ArrayList<Map.Entry<ConnectPoint, VlanId>> subs = new ArrayList<>();
+    public boolean removeSubscriber(AccessSubscriberId subscriberId) {
+        // Check if we can find the connect point to which this subscriber is connected
+        ConnectPoint subsPort = findSubscriberConnectPoint(subscriberId.toString());
+        if (subsPort == null) {
+            log.warn("ConnectPoint for {} not found", subscriberId);
+            return false;
+        }
+
+        return removeSubscriber(subsPort);
+    }
+
+    @Override
+    public Collection<Map.Entry<ConnectPoint, Map.Entry<VlanId, VlanId>>> getSubscribers() {
+        ArrayList<Map.Entry<ConnectPoint, Map.Entry<VlanId, VlanId>>> subs = new ArrayList<>();
 
         // Get the subscribers for all the devices
         // If the port is UNI, is enabled and exists in Sadis then copy it
@@ -262,7 +288,8 @@
 
                     SubscriberAndDeviceInformation sub = getSubscriber(cp);
                     if (sub != null) {
-                        subs.add(new AbstractMap.SimpleEntry(cp, sub.cTag()));
+                        Map.Entry<VlanId, VlanId> vlans = new AbstractMap.SimpleEntry(sub.sTag(), sub.cTag());
+                        subs.add(new AbstractMap.SimpleEntry(cp, vlans));
                     }
                 }
             }
@@ -288,6 +315,27 @@
         return olts;
     }
 
+    /**
+     * Finds the connect point to which a subscriber is connected.
+     *
+     * @param id The id of the subscriber, this is the same ID as in Sadis
+     * @return Subscribers ConnectPoint if found else null
+     */
+    private ConnectPoint findSubscriberConnectPoint(String id) {
+
+        Iterable<Device> devices = deviceService.getDevices();
+        for (Device d : devices) {
+            for (Port p : deviceService.getPorts(d.id())) {
+                log.trace("Comparing {} with {}", p.annotations().value(AnnotationKeys.PORT_NAME), id);
+                if (p.annotations().value(AnnotationKeys.PORT_NAME).equals(id)) {
+                    log.debug("Found on device {} port {}", d.id(), p.number());
+                    return new ConnectPoint(d.id(), p.number());
+                }
+            }
+        }
+        return null;
+    }
+
     private void initializeUni(Port port) {
         DeviceId deviceId = (DeviceId) port.element().id();
 
diff --git a/app/src/main/java/org/opencord/olt/rest/OltWebResource.java b/app/src/main/java/org/opencord/olt/rest/OltWebResource.java
index cc75b27..4a786f0 100644
--- a/app/src/main/java/org/opencord/olt/rest/OltWebResource.java
+++ b/app/src/main/java/org/opencord/olt/rest/OltWebResource.java
@@ -20,6 +20,7 @@
 import org.onosproject.net.PortNumber;
 import org.onosproject.rest.AbstractWebResource;
 import org.opencord.olt.AccessDeviceService;
+import org.opencord.olt.AccessSubscriberId;
 
 import javax.ws.rs.DELETE;
 import javax.ws.rs.POST;
@@ -29,6 +30,8 @@
 import javax.ws.rs.core.MediaType;
 import javax.ws.rs.core.Response;
 
+import static javax.ws.rs.core.Response.Status.NOT_FOUND;
+
 /**
  * OLT REST APIs.
  */
@@ -76,4 +79,43 @@
         service.removeSubscriber(connectPoint);
         return Response.noContent().build();
     }
+
+    /**
+     * Provision service for a subscriber.
+     *
+     * @param portName Name of the port on which the subscriber is connected
+     * @return 200 OK or 404 NOT_FOUND
+     */
+    @POST
+    @Produces(MediaType.APPLICATION_JSON)
+    @Path("services/{portName}")
+    public Response provisionServices(
+            @PathParam("portName")String portName) {
+        AccessDeviceService service = get(AccessDeviceService.class);
+
+        if (service.provisionSubscriber(new AccessSubscriberId(portName))) {
+            return ok("").build();
+        }
+        return Response.status(NOT_FOUND).build();
+    }
+
+    /**
+     * Removes services for a subscriber.
+     *
+     * @param portName Name of the port on which the subscriber is connected
+     * @return 200 OK or 404 NOT_FOUND
+     */
+    @DELETE
+    @Produces(MediaType.APPLICATION_JSON)
+    @Path("services/{portName}")
+    public Response deleteServices(
+            @PathParam("portName")String portName) {
+        AccessDeviceService service = get(AccessDeviceService.class);
+
+        if (service.removeSubscriber(new AccessSubscriberId(portName))) {
+            return ok("").build();
+        }
+        return Response.status(NOT_FOUND).build();
+    }
+
 }