adding Access device events and a listener

Change-Id: I0e9b4dae4231e7057443e7ed52723e29e06747cc
diff --git a/src/main/java/org/onosproject/olt/AccessDeviceService.java b/src/main/java/org/onosproject/olt/AccessDeviceService.java
index bd82f48..868f877 100644
--- a/src/main/java/org/onosproject/olt/AccessDeviceService.java
+++ b/src/main/java/org/onosproject/olt/AccessDeviceService.java
@@ -17,12 +17,16 @@
 package org.onosproject.olt;
 
 import org.onlab.packet.VlanId;
+import org.onosproject.event.ListenerService;
 import org.onosproject.net.ConnectPoint;
+import org.onosproject.olt.api.AccessDeviceEvent;
+import org.onosproject.olt.api.AccessDeviceListener;
 
 /**
  * Service for interacting with an access device (OLT).
  */
-public interface AccessDeviceService {
+public interface AccessDeviceService
+        extends ListenerService<AccessDeviceEvent, AccessDeviceListener> {
 
     /**
      * Provisions connectivity for a subscriber on an access device.
@@ -38,4 +42,5 @@
      * @param port subscriber's connection point
      */
     void removeSubscriber(ConnectPoint port);
+
 }
diff --git a/src/main/java/org/onosproject/olt/Olt.java b/src/main/java/org/onosproject/olt/Olt.java
index ffc4705..b956f3a 100644
--- a/src/main/java/org/onosproject/olt/Olt.java
+++ b/src/main/java/org/onosproject/olt/Olt.java
@@ -28,9 +28,9 @@
 import org.onlab.util.Tools;
 import org.onosproject.core.ApplicationId;
 import org.onosproject.core.CoreService;
+import org.onosproject.event.AbstractListenerManager;
 import org.onosproject.net.ConnectPoint;
 import org.onosproject.net.DeviceId;
-import org.onosproject.net.Port;
 import org.onosproject.net.PortNumber;
 import org.onosproject.net.config.ConfigFactory;
 import org.onosproject.net.config.NetworkConfigEvent;
@@ -47,6 +47,8 @@
 import org.onosproject.net.flowobjective.DefaultForwardingObjective;
 import org.onosproject.net.flowobjective.FlowObjectiveService;
 import org.onosproject.net.flowobjective.ForwardingObjective;
+import org.onosproject.olt.api.AccessDeviceEvent;
+import org.onosproject.olt.api.AccessDeviceListener;
 import org.osgi.service.component.ComponentContext;
 import org.slf4j.Logger;
 
@@ -62,7 +64,9 @@
  */
 @Service
 @Component(immediate = true)
-public class Olt implements AccessDeviceService {
+public class Olt
+        extends AbstractListenerManager<AccessDeviceEvent, AccessDeviceListener>
+        implements AccessDeviceService {
     private final Logger log = getLogger(getClass());
 
     @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
@@ -98,7 +102,7 @@
             label = "The OLT's uplink port number")
     private int gfastUplink = GFAST_UPLINK_PORT;
 
-    //TODO: replace this with an annotation lookup
+    //TODO: replace this information with info comming for net cfg service.
     @Property(name = "oltDevice", value = OLT_DEVICE,
             label = "The OLT device id")
     private String oltDevice = OLT_DEVICE;
@@ -127,6 +131,8 @@
     public void activate() {
         appId = coreService.registerApplication("org.onosproject.olt");
 
+        eventDispatcher.addSink(AccessDeviceEvent.class, listenerRegistry);
+
         networkConfig.registerConfigFactory(configFactory);
         networkConfig.addListener(configListener);
 
@@ -140,31 +146,6 @@
                 }
         );
 
-        /*deviceService.addListener(deviceListener);
-
-        deviceService.getPorts(DeviceId.deviceId(oltDevice)).stream().forEach(
-                port -> {
-                    if (!port.number().isLogical() && port.isEnabled()) {
-                        short vlanId = fetchVlanId(port.number());
-                        if (vlanId > 0) {
-                            provisionVlanOnPort(oltDevice, uplinkPort, port.number(), (short) 7);
-                            provisionVlanOnPort(oltDevice, uplinkPort, port.number(), vlanId);
-                        }
-                    }
-                }
-        );*/
-
-
-        deviceService.getPorts(DeviceId.deviceId(gfastDevice)).stream()
-                .filter(port -> !port.number().isLogical())
-                .filter(Port::isEnabled)
-                .forEach(port -> {
-                            short vlanId = (short) (fetchVlanId(port.number()) + OFFSET);
-                            if (vlanId > 0) {
-                                provisionVlanOnPort(gfastDevice, gfastUplink, port.number(), vlanId);
-                            }
-                        }
-                );
         log.info("Started with Application ID {}", appId.id());
     }
 
@@ -312,17 +293,28 @@
         @Override
         public void event(DeviceEvent event) {
             DeviceId devId = DeviceId.deviceId(oltDevice);
+            if (!devId.equals(event.subject().id())) {
+                return;
+            }
             switch (event.type()) {
                 case PORT_ADDED:
                 case PORT_UPDATED:
-                    if (devId.equals(event.subject().id()) && event.port().isEnabled()) {
+                    if (event.port().isEnabled()) {
                         short vlanId = fetchVlanId(event.port().number());
                         provisionVlanOnPort(gfastDevice, uplinkPort, event.port().number(), vlanId);
                     }
                     break;
                 case DEVICE_ADDED:
-                case DEVICE_UPDATED:
+                    post(new AccessDeviceEvent(
+                            AccessDeviceEvent.Type.DEVICE_CONNECTED, devId,
+                            null, null));
+                    break;
                 case DEVICE_REMOVED:
+                    post(new AccessDeviceEvent(
+                            AccessDeviceEvent.Type.DEVICE_DISCONNECTED, devId,
+                            null, null));
+                    break;
+                case DEVICE_UPDATED:
                 case DEVICE_SUSPENDED:
                 case DEVICE_AVAILABILITY_CHANGED:
                 case PORT_REMOVED:
diff --git a/src/main/java/org/onosproject/olt/api/AccessDeviceEvent.java b/src/main/java/org/onosproject/olt/api/AccessDeviceEvent.java
new file mode 100644
index 0000000..212fd37
--- /dev/null
+++ b/src/main/java/org/onosproject/olt/api/AccessDeviceEvent.java
@@ -0,0 +1,88 @@
+package org.onosproject.olt.api;
+
+import org.onlab.packet.VlanId;
+import org.onosproject.event.AbstractEvent;
+import org.onosproject.net.DeviceId;
+
+import java.util.Optional;
+
+/**
+ * Describes an access device event.
+ */
+public class AccessDeviceEvent extends AbstractEvent<AccessDeviceEvent.Type, DeviceId> {
+
+    private final Optional<VlanId> sVlan;
+    private final Optional<VlanId> cVlan;
+
+    public enum Type {
+        /**
+         * A subscriber was registered and provisioned.
+         */
+        SUBSCRIBER_REGISTERED,
+
+        /**
+         * A subscriber was unregistered and deprovisioned.
+         */
+        SUBSCRIBER_UNREGISTERED,
+
+        /**
+         * An access device connected.
+         */
+        DEVICE_CONNECTED,
+
+        /**
+         * An access device disconnected.
+         */
+        DEVICE_DISCONNECTED
+
+    }
+
+    /**
+     *
+     * Creates an event of a given type and for the specified device,
+     * along with the cVlanId and sVlanId. The vlan fields may not be provisioned
+     * if the event is related to the access device (dis)connection.
+     *
+     * @param type the event type
+     * @param deviceId the device id
+     * @param sVlanId the service vlan
+     * @param cVlanId the customer vlan
+     */
+    public AccessDeviceEvent(Type type, DeviceId deviceId,
+                             VlanId sVlanId,
+                             VlanId cVlanId) {
+        super(type, deviceId);
+        this.sVlan = Optional.ofNullable(sVlanId);
+        this.cVlan = Optional.ofNullable(cVlanId);
+    }
+
+    /**
+     *
+     * Creates an event of a given type and for the specified device, and timestamp
+     * along with the cVlanId and sVlanId. The vlan fields may not be provisioned
+     * if the event is related to the access device (dis)connection.
+     *
+     * @param type the event type
+     * @param deviceId the device id
+     * @param time a timestamp
+     * @param sVlanId the service vlan
+     * @param cVlanId the customer vlan
+     */
+    protected AccessDeviceEvent(Type type, DeviceId deviceId, long time,
+                                VlanId sVlanId,
+                                VlanId cVlanId) {
+        super(type, deviceId, time);
+        this.sVlan = Optional.ofNullable(sVlanId);
+        this.cVlan = Optional.ofNullable(cVlanId);
+
+    }
+
+    public Optional<VlanId> sVlanId() {
+        return sVlan;
+    }
+
+    public Optional<VlanId> cVlanId() {
+        return cVlan;
+    }
+
+}
diff --git a/src/main/java/org/onosproject/olt/api/AccessDeviceListener.java b/src/main/java/org/onosproject/olt/api/AccessDeviceListener.java
new file mode 100644
index 0000000..f652a10
--- /dev/null
+++ b/src/main/java/org/onosproject/olt/api/AccessDeviceListener.java
@@ -0,0 +1,9 @@
+package org.onosproject.olt.api;
+
+import org.onosproject.event.EventListener;
+
+/**
+ * Entity capable of receiving access device related events.
+ */
+public interface AccessDeviceListener extends EventListener<AccessDeviceEvent> {
+}
diff --git a/src/main/java/org/onosproject/olt/api/package-info.java b/src/main/java/org/onosproject/olt/api/package-info.java
new file mode 100644
index 0000000..f99b4e7
--- /dev/null
+++ b/src/main/java/org/onosproject/olt/api/package-info.java
@@ -0,0 +1,20 @@
+/*
+ * Copyright 2016 Open Networking Laboratory
+ *
+ * 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.
+ */
+
+/**
+ * OLT event api.
+ */
+package org.onosproject.olt.api;