KafkaApp to export BNG related events

Kafka app export PPPoE related events and BNG statistics events on the Kafka bus

Change-Id: Icea64d376abf65f3c2e38b56936b9a87d8c40ff7
diff --git a/app.xml b/app.xml
index 586cbd8..b96cdac 100644
--- a/app.xml
+++ b/app.xml
@@ -24,5 +24,6 @@
     <artifact>mvn:${project.groupId}/dhcpl2relay-api/${dhcpl2relay.api.version}</artifact>
     <artifact>mvn:${project.groupId}/olt-api/${olt.api.version}</artifact>
     <artifact>mvn:${project.groupId}/sadis-api/${sadis.api.version}</artifact>
+    <artifact>mvn:${project.groupId}/bng-api/${bng.api.version}</artifact>
     <artifact>mvn:org.apache.servicemix.bundles/org.apache.servicemix.bundles.kafka-clients/1.1.1_1</artifact>
 </app>
diff --git a/features.xml b/features.xml
index 70492f0..0cc601f 100644
--- a/features.xml
+++ b/features.xml
@@ -23,6 +23,7 @@
         <bundle>mvn:${project.groupId}/dhcpl2relay-api/${dhcpl2relay.api.version}</bundle>
         <bundle>mvn:${project.groupId}/olt-api/${olt.api.version}</bundle>
         <bundle>mvn:${project.groupId}/sadis-api/${sadis.api.version}</bundle>
+        <bundle>mvn:${project.groupId}/bng-api/${bng.api.version}</bundle>
         <bundle>mvn:org.apache.servicemix.bundles/org.apache.servicemix.bundles.kafka-clients/1.1.1_1</bundle>
     </feature>
 </features>
diff --git a/pom.xml b/pom.xml
index 99798f7..6607425 100644
--- a/pom.xml
+++ b/pom.xml
@@ -40,6 +40,7 @@
         <aaa.api.version>2.0.0</aaa.api.version>
         <olt.api.version>4.0.0</olt.api.version>
         <dhcpl2relay.api.version>2.0.0</dhcpl2relay.api.version>
+        <bng.api.version>1.0-SNAPSHOT</bng.api.version>
         <sadis.api.version>4.0.1</sadis.api.version>
     </properties>
 
@@ -91,6 +92,13 @@
             <scope>provided</scope>
         </dependency>
 
+        <dependency>
+            <groupId>org.opencord</groupId>
+            <artifactId>bng-api</artifactId>
+            <version>${bng.api.version}</version>
+            <scope>provided</scope>
+        </dependency>
+
     </dependencies>
 
     <build>
diff --git a/src/main/java/org/opencord/kafka/integrations/BngPppoeKafkaIntegration.java b/src/main/java/org/opencord/kafka/integrations/BngPppoeKafkaIntegration.java
new file mode 100644
index 0000000..ea91c8d
--- /dev/null
+++ b/src/main/java/org/opencord/kafka/integrations/BngPppoeKafkaIntegration.java
@@ -0,0 +1,108 @@
+/*
+ * Copyright 2019-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.kafka.integrations;
+
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.fasterxml.jackson.databind.node.ObjectNode;
+import org.opencord.bng.PppoeBngControlHandler;
+import org.opencord.bng.PppoeEvent;
+import org.opencord.bng.PppoeEventListener;
+import org.opencord.kafka.EventBusService;
+import org.osgi.service.component.annotations.Activate;
+import org.osgi.service.component.annotations.Component;
+import org.osgi.service.component.annotations.Deactivate;
+import org.osgi.service.component.annotations.Reference;
+import org.osgi.service.component.annotations.ReferenceCardinality;
+import org.osgi.service.component.annotations.ReferencePolicy;
+
+import java.time.Instant;
+import java.util.concurrent.atomic.AtomicReference;
+
+/**
+ * Listens for PPPoE handler events from the BNG app and pushes them on a Kafka
+ * bus.
+ */
+@Component(immediate = true)
+public class BngPppoeKafkaIntegration extends AbstractKafkaIntegration {
+
+    private static final String TOPIC_PPPOE = "bng.pppoe";
+    private static final String TIMESTAMP = "timestamp";
+    private static final String EVENT_TYPE = "eventType";
+    private static final String OLT_DEVICE_ID = "deviceId";
+    private static final String OLT_PORT_NUMBER = "portNumber";
+    private static final String MAC_ADDRESS = "macAddress";
+    private static final String IP_ADDRESS = "ipAddress";
+    private static final String SERIAL_NUMBER = "serialNumber";
+    private static final String SESSION_ID = "sessionId";
+
+    private final PppoeEventListener pppoeEventListener = new InternalPppoeListener();
+
+    private final AtomicReference<PppoeBngControlHandler> pppoeBngControlRef = new AtomicReference<>();
+
+    @Reference(cardinality = ReferenceCardinality.MANDATORY)
+    protected EventBusService eventBusService;
+
+    @Reference(cardinality = ReferenceCardinality.OPTIONAL,
+            policy = ReferencePolicy.DYNAMIC,
+            bind = "bindPppoeBngControl",
+            unbind = "unbindPppoeBngControl")
+    protected volatile PppoeBngControlHandler pppoeBngControlHandler;
+
+    protected void bindPppoeBngControl(PppoeBngControlHandler incomingService) {
+        bindAndAddListener(incomingService, pppoeBngControlRef, pppoeEventListener);
+    }
+
+    protected void unbindPppoeBngControl(PppoeBngControlHandler outgoingService) {
+        unbindAndRemoveListener(outgoingService, pppoeBngControlRef, pppoeEventListener);
+    }
+
+    @Activate
+    public void activate() {
+        log.info("Started PppoeKafkaIntegration");
+    }
+
+    @Deactivate
+    public void deactivate() {
+        unbindPppoeBngControl(pppoeBngControlRef.get());
+        log.info("Stopped PppoeKafkaIntegration");
+    }
+
+    private JsonNode serializePppoeEvent(PppoeEvent event) {
+        // Serialize PPPoE event in a JSON node
+        ObjectMapper mapper = new ObjectMapper();
+        ObjectNode pppoeEvent = mapper.createObjectNode();
+        pppoeEvent.put(TIMESTAMP, Instant.now().toString());
+        pppoeEvent.put(EVENT_TYPE, event.type().toString());
+        pppoeEvent.put(OLT_DEVICE_ID, event.subject().getOltConnectPoint().deviceId().toString());
+        pppoeEvent.put(OLT_PORT_NUMBER, event.subject().getOltConnectPoint().port().toString());
+        pppoeEvent.put(MAC_ADDRESS, event.subject().getMacAddress().toString());
+        pppoeEvent.put(IP_ADDRESS, event.subject().getIpAddress().toString());
+        pppoeEvent.put(SERIAL_NUMBER, event.subject().getOnuSerialNumber());
+        pppoeEvent.put(SESSION_ID, event.subject().getSessionId() == 0 ?
+                "" : String.valueOf(event.subject().getSessionId()));
+        return pppoeEvent;
+    }
+
+    private class InternalPppoeListener implements PppoeEventListener {
+
+        @Override
+        public void event(PppoeEvent event) {
+            eventBusService.send(TOPIC_PPPOE, serializePppoeEvent(event));
+        }
+    }
+}
diff --git a/src/main/java/org/opencord/kafka/integrations/BngStatsKafkaIntegration.java b/src/main/java/org/opencord/kafka/integrations/BngStatsKafkaIntegration.java
new file mode 100644
index 0000000..e7cffd8
--- /dev/null
+++ b/src/main/java/org/opencord/kafka/integrations/BngStatsKafkaIntegration.java
@@ -0,0 +1,179 @@
+/*
+ * Copyright 2019-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.kafka.integrations;
+
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.fasterxml.jackson.databind.node.ObjectNode;
+import com.google.common.collect.ImmutableMap;
+import org.apache.commons.lang3.tuple.Pair;
+import org.onosproject.net.behaviour.BngProgrammable;
+import org.onosproject.net.pi.runtime.PiCounterCellData;
+import org.opencord.bng.BngStatsEvent;
+import org.opencord.bng.BngStatsEventListener;
+import org.opencord.bng.BngStatsEventSubject;
+import org.opencord.bng.BngStatsService;
+import org.opencord.kafka.EventBusService;
+import org.osgi.service.component.annotations.Activate;
+import org.osgi.service.component.annotations.Component;
+import org.osgi.service.component.annotations.Deactivate;
+import org.osgi.service.component.annotations.Reference;
+import org.osgi.service.component.annotations.ReferenceCardinality;
+import org.osgi.service.component.annotations.ReferencePolicy;
+
+import java.time.Instant;
+import java.util.Map;
+import java.util.concurrent.atomic.AtomicReference;
+
+/**
+ * Listens for statistic events from the BNG app and pushes them on a Kafka
+ * bus.
+ */
+@Component(immediate = true)
+public class BngStatsKafkaIntegration extends AbstractKafkaIntegration {
+
+    private static final String TOPIC_STATS = "bng.stats";
+    private static final String SUBSCRIBER_S_TAG = "sTag";
+    private static final String SUBSCRIBER_C_TAG = "cTag";
+
+    private static final String UP_TX_BYTES = "upTermBytes";
+    private static final String UP_TX_PACKETS = "upTermPackets";
+
+    private static final String UP_RX_BYTES = "upRxBytes";
+    private static final String UP_RX_PACKETS = "upRxPackets";
+
+    private static final String UP_DROP_BYTES = "upDropBytes";
+    private static final String UP_DROP_PACKETS = "upDropPackets";
+
+    private static final String DOWN_RX_BYTES = "downRxBytes";
+    private static final String DOWN_RX_PACKETS = "downRxPackets";
+
+    private static final String DOWN_TX_BYTES = "downTxBytes";
+    private static final String DOWN_TX_PACKETS = "downTxPackets";
+
+    private static final String DOWN_DROP_BYTES = "downDropBytes";
+    private static final String DOWN_DROP_PACKETS = "downDropPackets";
+
+    private static final String CONTROL_PACKETS = "controlPackets";
+
+    private static final ImmutableMap<BngProgrammable.BngCounterType, Pair<String, String>> MAP_COUNTERS =
+            ImmutableMap.<BngProgrammable.BngCounterType, Pair<String, String>>builder()
+                    .put(BngProgrammable.BngCounterType.UPSTREAM_RX, Pair.of(UP_RX_BYTES, UP_RX_PACKETS))
+                    .put(BngProgrammable.BngCounterType.UPSTREAM_TX, Pair.of(UP_TX_BYTES, UP_TX_PACKETS))
+                    .put(BngProgrammable.BngCounterType.UPSTREAM_DROPPED, Pair.of(UP_DROP_BYTES, UP_DROP_PACKETS))
+
+                    .put(BngProgrammable.BngCounterType.DOWNSTREAM_RX, Pair.of(DOWN_RX_BYTES, DOWN_RX_PACKETS))
+                    .put(BngProgrammable.BngCounterType.DOWNSTREAM_TX, Pair.of(DOWN_TX_BYTES, DOWN_TX_PACKETS))
+                    .put(BngProgrammable.BngCounterType.DOWNSTREAM_DROPPED, Pair.of(DOWN_DROP_BYTES, DOWN_DROP_PACKETS))
+                    .build();
+
+    private static final String TIMESTAMP = "timestamp";
+    private static final String ATTACHMENT_TYPE = "attachmentType";
+    private static final String DEVICE_ID = "deviceId";
+    private static final String PORT_NUMBER = "portNumber";
+    private static final String MAC_ADDRESS = "macAddress";
+    private static final String IP_ADDRESS = "ipAddress";
+    private static final String ONU_SERIAL_NUMBER = "onuSerialNumber";
+    private static final String PPPOE_SESSION_ID = "pppoeSessionId";
+
+    private final BngStatsEventListener statsListener = new InternalStatsListener();
+
+    private final AtomicReference<BngStatsService> bngStatsServiceRef = new AtomicReference<>();
+
+    @Reference(cardinality = ReferenceCardinality.OPTIONAL,
+            policy = ReferencePolicy.DYNAMIC,
+            bind = "bindBngStatsService",
+            unbind = "unbindBngStatsService")
+    protected volatile BngStatsService bngStatsService;
+
+    @Reference(cardinality = ReferenceCardinality.MANDATORY)
+    protected EventBusService eventBusService;
+
+
+    protected void bindBngStatsService(BngStatsService incomingService) {
+        bindAndAddListener(incomingService, bngStatsServiceRef, statsListener);
+    }
+
+    protected void unbindBngStatsService(BngStatsService outgoingService) {
+        unbindAndRemoveListener(outgoingService, bngStatsServiceRef, statsListener);
+    }
+
+    @Activate
+    public void activate() {
+        log.info("Started BngKafkaIntegration");
+    }
+
+    @Deactivate
+    public void deactivate() {
+        unbindBngStatsService(bngStatsServiceRef.get());
+        log.info("Stopped BngKafkaIntegration");
+    }
+
+    private JsonNode serializeBngStatsEvent(BngStatsEventSubject eventSubject) {
+        // Serialize stats in a JSON node
+        ObjectMapper mapper = new ObjectMapper();
+        ObjectNode attStatsNode = mapper.createObjectNode();
+
+        // Exposing statistics only for PPPoE attachment
+        var attachmentStats = eventSubject.getAttachmentStats();
+        var attachment = eventSubject.getBngAttachment();
+
+        attStatsNode.put(MAC_ADDRESS, attachment.macAddress().toString());
+        attStatsNode.put(IP_ADDRESS, attachment.ipAddress().toString());
+        attStatsNode.put(PPPOE_SESSION_ID, attachment.pppoeSessionId());
+
+        attStatsNode.put(SUBSCRIBER_S_TAG, attachment.sTag().toShort());
+        attStatsNode.put(SUBSCRIBER_C_TAG, attachment.cTag().toShort());
+
+        attStatsNode.put(ONU_SERIAL_NUMBER, attachment.onuSerial());
+        attStatsNode.put(ATTACHMENT_TYPE, attachment.type().toString());
+
+        attStatsNode.put(DEVICE_ID, attachment.oltConnectPoint().deviceId().toString());
+        attStatsNode.put(PORT_NUMBER, attachment.oltConnectPoint().port().toString());
+
+        // Add the statistics to the JSON
+        attStatsNode = createNodesStats(attachmentStats, attStatsNode);
+
+        // Control stats are different, only packets statistics
+        attStatsNode.put(CONTROL_PACKETS,
+                attachmentStats.get(BngProgrammable.BngCounterType.CONTROL_PLANE).packets());
+
+        attStatsNode.put(TIMESTAMP, Instant.now().toString());
+        return attStatsNode;
+    }
+
+    private ObjectNode createNodesStats(Map<BngProgrammable.BngCounterType,
+            PiCounterCellData> attStats, ObjectNode node) {
+        MAP_COUNTERS.forEach((counterType, pairStats) -> {
+            if (attStats.containsKey(counterType)) {
+                node.put(pairStats.getLeft(),
+                        attStats.get(counterType).bytes());
+                node.put(pairStats.getRight(),
+                        attStats.get(counterType).packets());
+            }
+        });
+        return node;
+    }
+
+    private class InternalStatsListener implements BngStatsEventListener {
+
+        @Override
+        public void event(BngStatsEvent event) {
+            eventBusService.send(TOPIC_STATS, serializeBngStatsEvent(event.subject()));
+        }
+    }
+}