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

Change-Id: I7bcd8a85cab375786ec72620e5d0d06bce084ded
diff --git a/api/pom.xml b/api/pom.xml
index 3ee690d..74e93d8 100644
--- a/api/pom.xml
+++ b/api/pom.xml
@@ -22,7 +22,7 @@
     <parent>
         <artifactId>olt</artifactId>
         <groupId>org.opencord</groupId>
-        <version>2.0.0-SNAPSHOT</version>
+        <version>2.1.0-SNAPSHOT</version>
         <relativePath>../pom.xml</relativePath>
     </parent>
 
diff --git a/api/src/main/java/org/opencord/olt/AccessDeviceService.java b/api/src/main/java/org/opencord/olt/AccessDeviceService.java
index 361a30d..9b1e574 100644
--- a/api/src/main/java/org/opencord/olt/AccessDeviceService.java
+++ b/api/src/main/java/org/opencord/olt/AccessDeviceService.java
@@ -35,22 +35,40 @@
      * Provisions connectivity for a subscriber on an access device.
      *
      * @param port subscriber's connection point
+     * @return true if successful false otherwise
      */
-    void provisionSubscriber(ConnectPoint port);
+    boolean provisionSubscriber(ConnectPoint port);
 
     /**
      * Removes provisioned connectivity for a subscriber from an access device.
      *
      * @param port subscriber's connection point
+     * @return true if successful false otherwise
      */
-    void removeSubscriber(ConnectPoint port);
+    boolean removeSubscriber(ConnectPoint port);
+
+    /**
+     * Provisions flows for the specific subscriber.
+     *
+     * @param subscriberId Identification of the subscriber
+     * @return true if successful false otherwise
+     */
+    boolean provisionSubscriber(AccessSubscriberId subscriberId);
+
+    /**
+     * Removes flows for the specific subscriber.
+     *
+     * @param subscriberId Identification of the subscriber
+     * @return true if successful false otherwise
+     */
+    boolean removeSubscriber(AccessSubscriberId subscriberId);
 
     /**
      * Returns information about the provisioned subscribers.
      *
      * @return subscribers
      */
-    Collection<Map.Entry<ConnectPoint, VlanId>> getSubscribers();
+    Collection<Map.Entry<ConnectPoint, Map.Entry<VlanId, VlanId>>> getSubscribers();
 
     /**
      * Returns the list of active OLTs.
diff --git a/api/src/main/java/org/opencord/olt/AccessSubscriberId.java b/api/src/main/java/org/opencord/olt/AccessSubscriberId.java
new file mode 100644
index 0000000..e998e67
--- /dev/null
+++ b/api/src/main/java/org/opencord/olt/AccessSubscriberId.java
@@ -0,0 +1,95 @@
+/*
+ * 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.
+ */
+
+package org.opencord.olt;
+
+/**
+ * Class representing the identifier for a subscriber.
+ * It encapsulates the name of the logical port for the subscriber in
+ * ONOS.
+ * For e.g. if in ONOS you see
+ * onos> ports
+ * id=of:00000000c0a83264, available=true, local-status=connected 3m48s ago, role=MASTER, mfr=VOLTHA Projectd,...
+ *   port=32, state=enabled, type=fiber, speed=0 , adminState=enabled, portMac=08:00:00:00:00:20, portName=TWSH80808082
+ *
+ * the subscriber id would encapsulate the string "TWSH80808082"
+ */
+public class AccessSubscriberId {
+    // string representing the identifier
+    String id;
+
+    /**
+     * Creates an AccessSubscriberId with the passed id.
+     *
+     * @param id identifier of the subscriber
+     */
+    public AccessSubscriberId(String id) {
+        this.id = id;
+    }
+
+    /*
+    * (non-Javadoc)
+    *
+    * @see java.lang.Object#hashCode()
+    */
+    @Override
+    public int hashCode() {
+        final int prime = 31;
+        int result = 1;
+        result = prime * result + (this.id == null ? 0 : this.id.hashCode());
+
+        return result;
+    }
+
+    /*
+     * (non-Javadoc)
+     *
+     * @see java.lang.Object#equals(java.lang.Object)
+     */
+    @Override
+    public boolean equals(final Object obj) {
+        if (this == obj) {
+            return true;
+        }
+        if (obj == null) {
+            return false;
+        }
+        if (this.getClass() != obj.getClass()) {
+            return false;
+        }
+
+        final AccessSubscriberId other = (AccessSubscriberId) obj;
+        if (this.id == null) {
+            if (other.id != null) {
+                return false;
+            }
+        } else if (!this.id.equals(other.id)) {
+            return false;
+        }
+
+        return true;
+    }
+
+    /*
+    * (non-Javadoc)
+    *
+    * @see java.lang.Object#toString()
+    */
+    @Override
+    public String toString() {
+        return id;
+    }
+}
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();
+    }
+
 }
diff --git a/pom.xml b/pom.xml
index 7aa1dd9..6fb0c44 100644
--- a/pom.xml
+++ b/pom.xml
@@ -28,7 +28,7 @@
 
     <groupId>org.opencord</groupId>
     <artifactId>olt</artifactId>
-    <version>2.0.0-SNAPSHOT</version>
+    <version>2.1.0-SNAPSHOT</version>
     <packaging>pom</packaging>
 
     <properties>