Add Kafka integration for DHCP L2 relay app

Change-Id: Ieda0784ca2cc7ba3898cb734f3ff8f2c754bb3fb
diff --git a/pom.xml b/pom.xml
index c3af919..224bc0a 100644
--- a/pom.xml
+++ b/pom.xml
@@ -41,6 +41,7 @@
         <onos.app.readme>Integration with Kafka event bus</onos.app.readme>
         <aaa.api.version>1.8.0-SNAPSHOT</aaa.api.version>
         <olt.api.version>2.0.0-SNAPSHOT</olt.api.version>
+        <dhcpl2relay.api.version>1.5.0-SNAPSHOT</dhcpl2relay.api.version>
     </properties>
 
     <dependencies>
@@ -83,6 +84,13 @@
             <version>${olt.api.version}</version>
         </dependency>
 
+        <dependency>
+            <groupId>org.opencord</groupId>
+            <artifactId>dhcpl2relay</artifactId>
+            <version>${dhcpl2relay.api.version}</version>
+        </dependency>
+
+
     </dependencies>
 
     <build>
diff --git a/src/main/java/org/opencord/kafka/integrations/AaaKafkaIntegration.java b/src/main/java/org/opencord/kafka/integrations/AaaKafkaIntegration.java
index 471792f..e68e307 100644
--- a/src/main/java/org/opencord/kafka/integrations/AaaKafkaIntegration.java
+++ b/src/main/java/org/opencord/kafka/integrations/AaaKafkaIntegration.java
@@ -31,6 +31,8 @@
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
+import java.time.Instant;
+
 /**
  * Listens for AAA events and pushes them on a Kafka bus.
  */
@@ -49,6 +51,7 @@
 
     private static final String TOPIC = "authentication.events";
 
+    private static final String TIMESTAMP = "timestamp";
     private static final String DEVICE_ID = "deviceId";
     private static final String PORT_NUMBER = "portNumber";
     private static final String AUTHENTICATION_STATE = "authenticationState";
@@ -72,6 +75,7 @@
     private JsonNode serialize(AuthenticationEvent event) {
         ObjectMapper mapper = new ObjectMapper();
         ObjectNode authEvent = mapper.createObjectNode();
+        authEvent.put(TIMESTAMP, Instant.now().toString());
         authEvent.put(DEVICE_ID, event.subject().deviceId().toString());
         authEvent.put(PORT_NUMBER, event.subject().port().toString());
         authEvent.put(AUTHENTICATION_STATE, event.type().toString());
diff --git a/src/main/java/org/opencord/kafka/integrations/DhcpL2RelayKafkaIntegration.java b/src/main/java/org/opencord/kafka/integrations/DhcpL2RelayKafkaIntegration.java
new file mode 100644
index 0000000..786be87
--- /dev/null
+++ b/src/main/java/org/opencord/kafka/integrations/DhcpL2RelayKafkaIntegration.java
@@ -0,0 +1,101 @@
+/*
+ * 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.
+ * 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.apache.felix.scr.annotations.Activate;
+import org.apache.felix.scr.annotations.Component;
+import org.apache.felix.scr.annotations.Deactivate;
+import org.apache.felix.scr.annotations.Reference;
+import org.apache.felix.scr.annotations.ReferenceCardinality;
+import org.opencord.dhcpl2relay.DhcpAllocationInfo;
+import org.opencord.dhcpl2relay.DhcpL2RelayEvent;
+import org.opencord.dhcpl2relay.DhcpL2RelayListener;
+import org.opencord.dhcpl2relay.DhcpL2RelayService;
+import org.opencord.kafka.EventBusService;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.time.Instant;
+
+/**
+ * Listens for DHCP L2 relay events and pushes them on a Kafka bus.
+ */
+@Component(immediate = true)
+public class DhcpL2RelayKafkaIntegration {
+
+    public Logger log = LoggerFactory.getLogger(getClass());
+
+    @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+    protected EventBusService eventBusService;
+
+    @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+    protected DhcpL2RelayService authenticationService;
+
+    private final DhcpL2RelayListener listener = new InternalDhcpL2RelayListener();
+
+    private static final String TOPIC = "dhcp.events";
+
+    private static final String TIMESTAMP = "timestamp";
+    private static final String DEVICE_ID = "deviceId";
+    private static final String TYPE = "type";
+    private static final String MESSAGE_TYPE = "messageType";
+    private static final String PORT_NUMBER = "portNumber";
+    private static final String MAC_ADDRESS = "macAddress";
+    private static final String IP_ADDRESS = "ipAddress";
+
+    @Activate
+    public void activate() {
+        authenticationService.addListener(listener);
+        log.info("Started");
+    }
+
+    @Deactivate
+    public void deactivate() {
+        authenticationService.removeListener(listener);
+        log.info("Stopped");
+    }
+
+    private void handle(DhcpL2RelayEvent event) {
+        eventBusService.send(TOPIC, serialize(event));
+    }
+
+    private JsonNode serialize(DhcpL2RelayEvent event) {
+        ObjectMapper mapper = new ObjectMapper();
+        ObjectNode dhcpEvent = mapper.createObjectNode();
+        DhcpAllocationInfo allocationInfo = event.subject();
+        dhcpEvent.put(TYPE, event.type().toString());
+        dhcpEvent.put(TIMESTAMP, Instant.now().toString());
+        dhcpEvent.put(DEVICE_ID, event.connectPoint().deviceId().toString());
+        dhcpEvent.put(MESSAGE_TYPE, allocationInfo.type().toString());
+        dhcpEvent.put(PORT_NUMBER, event.connectPoint().port().toString());
+        dhcpEvent.put(MAC_ADDRESS, allocationInfo.macAddress().toString());
+        dhcpEvent.put(IP_ADDRESS, allocationInfo.ipAddress().toString());
+        return dhcpEvent;
+    }
+
+    private class InternalDhcpL2RelayListener implements
+            DhcpL2RelayListener {
+
+        @Override
+        public void event(DhcpL2RelayEvent dhcpL2RelayEvent) {
+            handle(dhcpL2RelayEvent);
+        }
+    }
+}