VOL-743: Send ONU discover events to Kafka message bus

Change-Id: Ia2d1e3e0ae4f2b5384ae1a9d3c0adf8fbe47c321
diff --git a/app/app.xml b/app/app.xml
index ec55db7..5d912fa 100644
--- a/app/app.xml
+++ b/app/app.xml
@@ -1,6 +1,6 @@
 <?xml version="1.0" encoding="UTF-8"?>
 <!--
-  ~ Copyright 2016-present Open Networking Foundation
+  ~ 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.
@@ -21,4 +21,6 @@
     <description>${project.description}</description>
     <artifact>mvn:${project.groupId}/${project.artifactId}/${project.version}</artifact>
     <artifact>mvn:${project.groupId}/olt-api/${project.version}</artifact>
+    <artifact>mvn:${project.groupId}/olt-kafka/${project.version}</artifact>
+    <artifact>mvn:org.apache.servicemix.bundles/org.apache.servicemix.bundles.kafka-clients/1.1.0_1</artifact>
 </app>
diff --git a/app/features.xml b/app/features.xml
index a19b531..4c1b5f9 100644
--- a/app/features.xml
+++ b/app/features.xml
@@ -1,6 +1,6 @@
 <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
 <!--
-  ~ Copyright 2016-present Open Networking Foundation
+  ~ 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.
@@ -20,5 +20,7 @@
         <feature>onos-api</feature>
         <bundle>mvn:${project.groupId}/olt-api/${project.version}</bundle>
         <bundle>mvn:${project.groupId}/${project.artifactId}/${project.version}</bundle>
+        <bundle>mvn:${project.groupId}/olt-kafka/${project.version}</bundle>
+        <bundle>mvn:org.apache.servicemix.bundles/org.apache.servicemix.bundles.kafka-clients/1.1.0_1</bundle>
     </feature>
 </features>
diff --git a/app/pom.xml b/app/pom.xml
index 7c99f45..62ee855 100644
--- a/app/pom.xml
+++ b/app/pom.xml
@@ -37,8 +37,14 @@
         <api.description>
             APIs for interacting with the CORD OLT application.
         </api.description>
+        <onos.app.name>org.opencord.olt</onos.app.name>
+        <onos.app.title>OLT Access Management</onos.app.title>
+        <onos.app.origin>OpenCord</onos.app.origin>
+        <onos.app.category>default</onos.app.category>
+        <onos.app.url>http://opencord.org</onos.app.url>
+        <onos.app.readme>CORD OLT Access management application</onos.app.readme>
 	<api.package>org.onosproject.olt.rest</api.package>
-	<olt.api.version>1.4.0-SNAPSHOT</olt.api.version>
+	<olt.api.version>${project.version}</olt.api.version>
     </properties>
 
     <dependencies>
@@ -49,6 +55,12 @@
         </dependency>
 
         <dependency>
+            <groupId>org.opencord</groupId>
+            <artifactId>olt-kafka</artifactId>
+            <version>${olt.api.version}</version>
+        </dependency>
+
+        <dependency>
             <groupId>org.onosproject</groupId>
             <artifactId>onos-cli</artifactId>
             <version>${onos.version}</version>
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 8b71a0a..29f070f 100644
--- a/app/src/main/java/org/opencord/olt/impl/Olt.java
+++ b/app/src/main/java/org/opencord/olt/impl/Olt.java
@@ -182,10 +182,8 @@
 
         oltData.keySet().stream()
                 .flatMap(did -> deviceService.getPorts(did).stream())
-                .filter(p -> !oltData.get(p.element().id()).uplink().equals(p.number()))
-                .filter(p -> p.isEnabled())
-                .forEach(p -> processFilteringObjectives((DeviceId) p.element().id(),
-                                                         p.number(), true));
+                .filter(this::isUni)
+                .forEach(this::initializeUni);
 
         subscribers = storageService.<ConnectPoint, VlanId>consistentMapBuilder()
                 .withName(SUBSCRIBERS)
@@ -285,6 +283,16 @@
         return Maps.newHashMap(oltData);
     }
 
+    private void initializeUni(Port port) {
+        DeviceId deviceId = (DeviceId) port.element().id();
+
+        post(new AccessDeviceEvent(AccessDeviceEvent.Type.UNI_ADDED, deviceId, port));
+
+        if (port.isEnabled()) {
+            processFilteringObjectives(deviceId, port.number(), true);
+        }
+    }
+
     private void unprovisionSubscriber(DeviceId deviceId, PortNumber uplink,
                                        PortNumber subscriberPort, VlanId subscriberVlan,
                                        VlanId deviceVlan, Optional<VlanId> defaultVlan) {
@@ -559,6 +567,10 @@
         flowObjectiveService.filter(devId, igmp);
     }
 
+    private boolean isUni(Port port) {
+        return !oltData.get(port.element().id()).uplink().equals(port.number());
+    }
+
     private class InternalDeviceListener implements DeviceListener {
         @Override
         public void event(DeviceEvent event) {
@@ -570,9 +582,12 @@
                 //TODO: Port handling and bookkeeping should be improved once
                 // olt firmware handles correct behaviour.
                 case PORT_ADDED:
-                    if (!oltData.get(devId).uplink().equals(event.port().number()) &&
-                            event.port().isEnabled()) {
-                        processFilteringObjectives(devId, event.port().number(), true);
+                    if (!oltData.get(devId).uplink().equals(event.port().number())) {
+                        post(new AccessDeviceEvent(AccessDeviceEvent.Type.UNI_ADDED, devId, event.port()));
+
+                        if (event.port().isEnabled()) {
+                            processFilteringObjectives(devId, event.port().number(), true);
+                        }
                     }
                     break;
                 case PORT_REMOVED:
@@ -584,10 +599,14 @@
                                 event.port().number(),
                                 vlan, olt.vlan(), olt.defaultVlan());
                     }
-                    if (!oltData.get(devId).uplink().equals(event.port().number()) &&
-                            event.port().isEnabled()) {
-                        processFilteringObjectives(devId, event.port().number(), false);
+                    if (!oltData.get(devId).uplink().equals(event.port().number())) {
+                        if (event.port().isEnabled()) {
+                            processFilteringObjectives(devId, event.port().number(), false);
+                        }
+
+                        post(new AccessDeviceEvent(AccessDeviceEvent.Type.UNI_REMOVED, devId, event.port()));
                     }
+
                     break;
                 case PORT_UPDATED:
                     if (oltData.get(devId).uplink().equals(event.port().number())) {
@@ -603,9 +622,21 @@
                     post(new AccessDeviceEvent(
                             AccessDeviceEvent.Type.DEVICE_CONNECTED, devId,
                             null, null));
+
+                    // Send UNI_ADDED events for all existing ports
+                    deviceService.getPorts(devId).stream()
+                            .filter(Olt.this::isUni)
+                            .forEach(p -> post(new AccessDeviceEvent(
+                                    AccessDeviceEvent.Type.UNI_ADDED, devId, p)));
+
                     provisionDefaultFlows(devId);
                     break;
                 case DEVICE_REMOVED:
+                    deviceService.getPorts(devId).stream()
+                            .filter(Olt.this::isUni)
+                            .forEach(p -> post(new AccessDeviceEvent(
+                                    AccessDeviceEvent.Type.UNI_REMOVED, devId, p)));
+
                     post(new AccessDeviceEvent(
                             AccessDeviceEvent.Type.DEVICE_DISCONNECTED, devId,
                             null, null));