Kafka app that listens to events from applications and pushes them to a kafka bus

Change-Id: I8026f13f10aa00f6389b89459fa8ebd74de289a3
diff --git a/src/main/java/org/opencord/kafka/EventBusService.java b/src/main/java/org/opencord/kafka/EventBusService.java
new file mode 100644
index 0000000..c9686e7
--- /dev/null
+++ b/src/main/java/org/opencord/kafka/EventBusService.java
@@ -0,0 +1,33 @@
+/*
+ * 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;
+
+import com.fasterxml.jackson.databind.JsonNode;
+
+/**
+ * Service for interacting with an event bus.
+ */
+public interface EventBusService {
+
+    /**
+     * Sends JSON-formatted data to the event bus.
+     *
+     * @param topic topic to send to
+     * @param data JSON data
+     */
+    void send(String topic, JsonNode data);
+}
diff --git a/src/main/java/org/opencord/kafka/impl/KafkaConfig.java b/src/main/java/org/opencord/kafka/impl/KafkaConfig.java
new file mode 100644
index 0000000..6665577
--- /dev/null
+++ b/src/main/java/org/opencord/kafka/impl/KafkaConfig.java
@@ -0,0 +1,91 @@
+/*
+ * 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.impl;
+
+import org.onosproject.core.ApplicationId;
+import org.onosproject.net.config.Config;
+
+/**
+ * Configuration of the Kafka publishing endpoint.
+ */
+public class KafkaConfig extends Config<ApplicationId> {
+
+    private static final String BOOTSTRAP_SERVERS = "bootstrapServers";
+    private static final String RETRIES = "retries";
+    private static final String RECONNECT_BACKOFF = "reconnectBackoff";
+    private static final String INFLIGHT_REQUESTS = "inflightRequests";
+    private static final String ACKS = "acks";
+
+    private static final String DEFAULT_RETRIES = "1";
+    private static final String DEFAULT_RECONNECT_BACKOFF = "500";
+    private static final String DEFAULT_INFLIGHT_REQUESTS = "5";
+    private static final String DEFAULT_ACKS = "1";
+
+    @Override
+    public boolean isValid() {
+        return hasOnlyFields(BOOTSTRAP_SERVERS, RETRIES, RECONNECT_BACKOFF,
+                INFLIGHT_REQUESTS, ACKS) && hasFields(BOOTSTRAP_SERVERS);
+    }
+
+    /**
+     * Returns the Kafka bootstrap servers.
+     * <p>
+     * This can be a hostname/IP and port (e.g. 10.1.1.1:9092).
+     * </p>
+     *
+     * @return Kafka bootstrap servers
+     */
+    public String getBootstrapServers() {
+        return object.path(BOOTSTRAP_SERVERS).asText();
+    }
+
+    /**
+     * Returns the number of retries.
+     *
+     * @return retries
+     */
+    public String getRetries() {
+        return get(RETRIES, DEFAULT_RETRIES);
+    }
+
+    /**
+     * Returns the reconnect backoff in milliseconds.
+     *
+     * @return reconnect backoff
+     */
+    public String getReconnectBackoff() {
+        return get(RECONNECT_BACKOFF, DEFAULT_RECONNECT_BACKOFF);
+    }
+
+    /**
+     * Returns the number of inflight requests.
+     *
+     * @return inflight requests
+     */
+    public String getInflightRequests() {
+        return get(INFLIGHT_REQUESTS, DEFAULT_INFLIGHT_REQUESTS);
+    }
+
+    /**
+     * Returns the number of acks.
+     *
+     * @return acks
+     */
+    public String getAcks() {
+        return get(ACKS, DEFAULT_ACKS);
+    }
+}
diff --git a/src/main/java/org/opencord/kafka/impl/KafkaIntegration.java b/src/main/java/org/opencord/kafka/impl/KafkaIntegration.java
new file mode 100644
index 0000000..7a44872
--- /dev/null
+++ b/src/main/java/org/opencord/kafka/impl/KafkaIntegration.java
@@ -0,0 +1,216 @@
+/*
+ * 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.impl;
+
+import com.fasterxml.jackson.databind.JsonNode;
+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.apache.felix.scr.annotations.Service;
+import org.apache.kafka.clients.producer.KafkaProducer;
+import org.apache.kafka.clients.producer.ProducerRecord;
+import org.apache.kafka.common.serialization.StringSerializer;
+import org.onosproject.core.ApplicationId;
+import org.onosproject.core.CoreService;
+import org.onosproject.net.config.ConfigFactory;
+import org.onosproject.net.config.NetworkConfigEvent;
+import org.onosproject.net.config.NetworkConfigListener;
+import org.onosproject.net.config.NetworkConfigRegistry;
+import org.onosproject.net.config.basics.SubjectFactories;
+import org.opencord.kafka.EventBusService;
+import org.slf4j.Logger;
+
+import java.util.Properties;
+import java.util.concurrent.ExecutorService;
+
+import static com.google.common.base.Preconditions.checkNotNull;
+import static java.util.concurrent.Executors.newSingleThreadExecutor;
+import static org.onlab.util.Tools.groupedThreads;
+import static org.slf4j.LoggerFactory.getLogger;
+
+/**
+ * Sends events to a Kafka event bus.
+ */
+@Service
+@Component(immediate = true)
+public class KafkaIntegration implements EventBusService {
+
+    private final Logger log = getLogger(getClass());
+    private static final Class<KafkaConfig>
+            KAFKA_CONFIG_CLASS = KafkaConfig.class;
+
+    private static final String APP_NAME = "org.opencord.kafka";
+    private ApplicationId appId;
+
+    @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+    protected CoreService coreService;
+
+    @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+    protected NetworkConfigRegistry configRegistry;
+
+    private static StringSerializer stringSerializer = new StringSerializer();
+
+    private KafkaProducer<String, String> kafkaProducer;
+
+    private InternalNetworkConfigListener configListener =
+            new InternalNetworkConfigListener();
+
+    private final ExecutorService executor = newSingleThreadExecutor(
+            groupedThreads(this.getClass().getSimpleName(), "events", log));
+
+    private ConfigFactory<ApplicationId, KafkaConfig> kafkaConfigFactory =
+            new ConfigFactory<ApplicationId, KafkaConfig>(
+                    SubjectFactories.APP_SUBJECT_FACTORY, KAFKA_CONFIG_CLASS,
+                    "kafka") {
+                @Override
+                public KafkaConfig createConfig() {
+                    return new KafkaConfig();
+                }
+            };
+
+    private static final String BOOTSTRAP_SERVERS = "bootstrap.servers";
+    private static final String RETRIES = "retries";
+    private static final String RECONNECT_BACKOFF = "reconnect.backoff.ms";
+    private static final String INFLIGHT_REQUESTS =
+            "max.in.flight.requests.per.connection";
+    private static final String ACKS = "acks";
+    private static final String KEY_SERIALIZER = "key.serializer";
+    private static final String VALUE_SERIALIZER = "value.serializer";
+    private static final String STRING_SERIALIZER =
+            stringSerializer.getClass().getCanonicalName();
+
+    private static final String TIMESTAMP = "timestamp";
+
+    @Activate
+    public void activate() {
+        appId = coreService.registerApplication(APP_NAME);
+        configRegistry.registerConfigFactory(kafkaConfigFactory);
+        configRegistry.addListener(configListener);
+
+        configure();
+
+        log.info("Started");
+    }
+
+    @Deactivate
+    public void deactivate() {
+        configRegistry.removeListener(configListener);
+        configRegistry.unregisterConfigFactory(kafkaConfigFactory);
+
+        executor.shutdownNow();
+
+        shutdownKafka();
+        log.info("Stopped");
+    }
+
+    private void configure() {
+        KafkaConfig config =
+                configRegistry.getConfig(appId, KAFKA_CONFIG_CLASS);
+        if (config == null) {
+            log.info("Kafka configuration not present");
+            return;
+        }
+        configure(config);
+    }
+
+    private void configure(KafkaConfig config) {
+        checkNotNull(config);
+
+        Properties properties = new Properties();
+        properties.put(BOOTSTRAP_SERVERS, config.getBootstrapServers());
+        properties.put(RETRIES, config.getRetries());
+        properties.put(RECONNECT_BACKOFF, config.getReconnectBackoff());
+        properties.put(INFLIGHT_REQUESTS, config.getInflightRequests());
+        properties.put(ACKS, config.getAcks());
+        properties.put(KEY_SERIALIZER, STRING_SERIALIZER);
+        properties.put(VALUE_SERIALIZER, STRING_SERIALIZER);
+
+        startKafka(properties);
+    }
+
+    private void unconfigure() {
+        shutdownKafka();
+    }
+
+    private void startKafka(Properties properties) {
+        shutdownKafka();
+
+        // Kafka client doesn't play nice with the default OSGi classloader
+        // This workaround temporarily changes the thread's classloader so that
+        // the Kafka client can load the serializer classes.
+        ClassLoader original = Thread.currentThread().getContextClassLoader();
+        Thread.currentThread().setContextClassLoader(this.getClass().getClassLoader());
+        try {
+            kafkaProducer = new KafkaProducer<>(properties);
+        } finally {
+            Thread.currentThread().setContextClassLoader(original);
+        }
+    }
+
+    private void shutdownKafka() {
+        if (kafkaProducer != null) {
+            kafkaProducer.close();
+            kafkaProducer = null;
+        }
+    }
+
+    private void logException(Exception e) {
+        if (e != null) {
+            log.error("Exception while sending to Kafka", e);
+        }
+    }
+
+    @Override
+    public void send(String topic, JsonNode data) {
+        if (kafkaProducer == null) {
+            return;
+        }
+
+        if (log.isTraceEnabled()) {
+            log.trace("Sending event to Kafka: {}", data.toString());
+        }
+
+        kafkaProducer.send(new ProducerRecord<>(topic, data.toString()),
+                (r, e) -> logException(e));
+    }
+
+    private class InternalNetworkConfigListener implements NetworkConfigListener {
+
+        @Override
+        public void event(NetworkConfigEvent event) {
+            switch (event.type()) {
+            case CONFIG_ADDED:
+            case CONFIG_UPDATED:
+                configure((KafkaConfig) event.config().get());
+                break;
+            case CONFIG_REMOVED:
+                unconfigure();
+                break;
+            case CONFIG_REGISTERED:
+            case CONFIG_UNREGISTERED:
+            default:
+                break;
+            }
+        }
+
+        @Override
+        public boolean isRelevant(NetworkConfigEvent event) {
+            return event.configClass().equals(KAFKA_CONFIG_CLASS);
+        }
+    }
+}
diff --git a/src/main/java/org/opencord/kafka/impl/package-info.java b/src/main/java/org/opencord/kafka/impl/package-info.java
new file mode 100644
index 0000000..bec54b1
--- /dev/null
+++ b/src/main/java/org/opencord/kafka/impl/package-info.java
@@ -0,0 +1,20 @@
+/*
+ * 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.
+ */
+
+/**
+ * Implementation of EventBusService using Kafka.
+ */
+package org.opencord.kafka.impl;
diff --git a/src/main/java/org/opencord/kafka/integrations/AaaKafkaIntegration.java b/src/main/java/org/opencord/kafka/integrations/AaaKafkaIntegration.java
new file mode 100644
index 0000000..2393b73
--- /dev/null
+++ b/src/main/java/org/opencord/kafka/integrations/AaaKafkaIntegration.java
@@ -0,0 +1,89 @@
+/*
+ * 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.aaa.AuthenticationEvent;
+import org.opencord.aaa.AuthenticationEventListener;
+import org.opencord.aaa.AuthenticationService;
+import org.opencord.kafka.EventBusService;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * Listens for AAA events and pushes them on a Kafka bus.
+ */
+@Component(immediate = true)
+public class AaaKafkaIntegration {
+
+    public Logger log = LoggerFactory.getLogger(getClass());
+
+    @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+    protected EventBusService eventBusService;
+
+    @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+    protected AuthenticationService authenticationService;
+
+    private final AuthenticationEventListener listener = new InternalAuthenticationListener();
+
+    private static final String TOPIC = "authentication.events";
+
+    private static final String DEVICE_ID = "device_id";
+    private static final String PORT_NUMBER = "port_number";
+    private static final String AUTHENTICATION_STATE = "authentication_state";
+
+    @Activate
+    public void activate() {
+        authenticationService.addListener(listener);
+        log.info("Started");
+    }
+
+    @Deactivate
+    public void deactivate() {
+        authenticationService.removeListener(listener);
+        log.info("Stopped");
+    }
+
+    private void handle(AuthenticationEvent event) {
+        eventBusService.send(TOPIC, serialize(event));
+    }
+
+    private JsonNode serialize(AuthenticationEvent event) {
+        ObjectMapper mapper = new ObjectMapper();
+        ObjectNode authEvent = mapper.createObjectNode();
+        authEvent.put(DEVICE_ID, event.subject().deviceId().toString());
+        authEvent.put(PORT_NUMBER, event.subject().port().toString());
+        authEvent.put(AUTHENTICATION_STATE, event.type().toString());
+        return authEvent;
+    }
+
+    private class InternalAuthenticationListener implements
+            AuthenticationEventListener {
+
+        @Override
+        public void event(AuthenticationEvent authenticationEvent) {
+            handle(authenticationEvent);
+        }
+    }
+}
diff --git a/src/main/java/org/opencord/kafka/integrations/AccessDeviceKafkaIntegration.java b/src/main/java/org/opencord/kafka/integrations/AccessDeviceKafkaIntegration.java
new file mode 100644
index 0000000..9a5ddb1
--- /dev/null
+++ b/src/main/java/org/opencord/kafka/integrations/AccessDeviceKafkaIntegration.java
@@ -0,0 +1,108 @@
+/*
+ * 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.onosproject.net.AnnotationKeys;
+import org.onosproject.net.Port;
+import org.opencord.kafka.EventBusService;
+import org.opencord.olt.AccessDeviceEvent;
+import org.opencord.olt.AccessDeviceListener;
+import org.opencord.olt.AccessDeviceService;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.time.Instant;
+
+/**
+ * Listens for access device events and pushes them on a Kafka bus.
+ */
+@Component(immediate = true)
+public class AccessDeviceKafkaIntegration {
+
+    public Logger log = LoggerFactory.getLogger(getClass());
+
+    @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+    protected EventBusService eventBusService;
+
+    @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+    protected AccessDeviceService accessDeviceService;
+
+    private final AccessDeviceListener listener = new InternalAccessDeviceListener();
+
+    private static final String TOPIC = "onu.events";
+
+    private static final String STATUS = "status";
+    private static final String SERIAL_NUMBER = "serial_number";
+    private static final String UNI_PORT_ID = "uni_port_id";
+    private static final String OF_DPID = "of_dpid";
+    private static final String ACTIVATED = "activated";
+    private static final String TIMESTAMP = "timestamp";
+
+    @Activate
+    public void activate() {
+        accessDeviceService.addListener(listener);
+        log.info("Started");
+    }
+
+    @Deactivate
+    public void deactivate() {
+        accessDeviceService.removeListener(listener);
+        log.info("Stopped");
+    }
+
+    private void handle(AccessDeviceEvent event) {
+        eventBusService.send(TOPIC, serialize(event));
+    }
+
+    private JsonNode serialize(AccessDeviceEvent event) {
+        Port port = event.port().get();
+        String serialNumber = port.annotations().value(AnnotationKeys.PORT_NAME);
+
+        ObjectMapper mapper = new ObjectMapper();
+        ObjectNode onuNode = mapper.createObjectNode();
+        onuNode.put(TIMESTAMP, Instant.now().toString());
+        onuNode.put(STATUS, ACTIVATED);
+        onuNode.put(SERIAL_NUMBER, serialNumber);
+        onuNode.put(UNI_PORT_ID, port.number().toLong());
+        onuNode.put(OF_DPID, port.element().id().toString());
+
+        return onuNode;
+    }
+
+    private class InternalAccessDeviceListener implements
+            AccessDeviceListener {
+
+        @Override
+        public void event(AccessDeviceEvent accessDeviceEvent) {
+            switch (accessDeviceEvent.type()) {
+            case UNI_ADDED:
+                handle(accessDeviceEvent);
+                break;
+            default:
+                break;
+            }
+        }
+    }
+}
diff --git a/src/main/java/org/opencord/kafka/integrations/package-info.java b/src/main/java/org/opencord/kafka/integrations/package-info.java
new file mode 100644
index 0000000..3626f5d
--- /dev/null
+++ b/src/main/java/org/opencord/kafka/integrations/package-info.java
@@ -0,0 +1,20 @@
+/*
+ * 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.
+ */
+
+/**
+ * Integrations of other application's events with Kafka.
+ */
+package org.opencord.kafka.integrations;
diff --git a/src/main/java/org/opencord/kafka/package-info.java b/src/main/java/org/opencord/kafka/package-info.java
new file mode 100644
index 0000000..f461440
--- /dev/null
+++ b/src/main/java/org/opencord/kafka/package-info.java
@@ -0,0 +1,20 @@
+/*
+ * 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.
+ */
+
+/**
+ * Integration with Kafka event bus.
+ */
+package org.opencord.kafka;