Made changes as per comments.
kafkaProducer is now non-static.
TODO: KafkaPublisherManager Service and not Singleton.
Kafka event publishing.
Change-Id: I5ec20a6e4950c38e822468d343521ab77475b7d3
diff --git a/apps/kafka-integration/core/src/main/java/org/onosproject/kafkaintegration/converter/ConversionFactory.java b/apps/kafka-integration/core/src/main/java/org/onosproject/kafkaintegration/converter/ConversionFactory.java
new file mode 100644
index 0000000..26b5060
--- /dev/null
+++ b/apps/kafka-integration/core/src/main/java/org/onosproject/kafkaintegration/converter/ConversionFactory.java
@@ -0,0 +1,68 @@
+/**
+ * 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.
+ */
+package org.onosproject.kafkaintegration.converter;
+
+import org.onosproject.kafkaintegration.api.dto.OnosEvent.Type;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import static org.onosproject.kafkaintegration.api.dto.OnosEvent.Type.DEVICE;
+import static org.onosproject.kafkaintegration.api.dto.OnosEvent.Type.LINK;
+
+/**
+ * Returns the appropriate converter object based on the ONOS event type.
+ *
+ */
+public final class ConversionFactory {
+
+ // Store converters for all supported events
+ private Map<Type, EventConverter> converters =
+ new HashMap<Type, EventConverter>() {
+ {
+ put(DEVICE, new DeviceEventConverter());
+ put(LINK, new LinkEventConverter());
+ }
+ };
+
+ // Exists to defeat instantiation
+ private ConversionFactory() {
+ }
+
+ private static class SingletonHolder {
+ private static final ConversionFactory INSTANCE =
+ new ConversionFactory();
+ }
+
+ /**
+ * Returns a static reference to the Conversion Factory.
+ *
+ * @return singleton object
+ */
+ public static ConversionFactory getInstance() {
+ return SingletonHolder.INSTANCE;
+ }
+
+ /**
+ * Returns an Event converter object for the specified ONOS event type.
+ *
+ * @param event ONOS event type
+ * @return Event Converter object
+ */
+ public EventConverter getConverter(Type event) {
+ return converters.get(event);
+ }
+
+}
diff --git a/apps/kafka-integration/core/src/main/java/org/onosproject/kafkaintegration/converter/DeviceEventConverter.java b/apps/kafka-integration/core/src/main/java/org/onosproject/kafkaintegration/converter/DeviceEventConverter.java
new file mode 100644
index 0000000..6b2ee24
--- /dev/null
+++ b/apps/kafka-integration/core/src/main/java/org/onosproject/kafkaintegration/converter/DeviceEventConverter.java
@@ -0,0 +1,122 @@
+/**
+ * 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.
+ */
+package org.onosproject.kafkaintegration.converter;
+
+import com.google.protobuf.GeneratedMessage;
+import org.onosproject.event.Event;
+import org.onosproject.grpc.net.Device.DeviceCore;
+import org.onosproject.grpc.net.Device.DeviceType;
+import org.onosproject.grpc.net.DeviceEvent.DeviceEventType;
+import org.onosproject.grpc.net.DeviceEvent.DeviceNotification;
+import org.onosproject.grpc.net.Port.PortCore;
+import org.onosproject.grpc.net.Port.PortType;
+import org.onosproject.net.device.DeviceEvent;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * Converts ONOS Device event message to protobuf format.
+ */
+public class DeviceEventConverter implements EventConverter {
+
+ private final Logger log = LoggerFactory.getLogger(getClass());
+
+ @Override
+ public GeneratedMessage convertToProtoMessage(Event<?, ?> event) {
+
+ DeviceEvent deviceEvent = (DeviceEvent) event;
+
+ if (!deviceEventTypeSupported(deviceEvent)) {
+ log.error("Unsupported Onos Device Event {}. There is no matching"
+ + "proto Device Event type", deviceEvent.type().toString());
+ return null;
+ }
+
+ return buildDeviceProtoMessage(deviceEvent);
+ }
+
+ /**
+ * Checks if the ONOS Device Event type is supported.
+ *
+ * @param event ONOS Device event
+ * @return true if there is a match and false otherwise
+ */
+ private boolean deviceEventTypeSupported(DeviceEvent event) {
+ DeviceEventType[] deviceEvents = DeviceEventType.values();
+ for (DeviceEventType deviceEventType : deviceEvents) {
+ if (deviceEventType.name().equals(event.type().name())) {
+ return true;
+ }
+ }
+
+ return false;
+ }
+
+ private DeviceNotification buildDeviceProtoMessage(DeviceEvent deviceEvent) {
+ DeviceNotification.Builder notificationBuilder =
+ DeviceNotification.newBuilder();
+
+ DeviceCore deviceCore =
+ DeviceCore.newBuilder()
+ .setChassisId(deviceEvent.subject().chassisId().id()
+ .toString())
+ .setDeviceId(deviceEvent.subject().id().toString())
+ .setHwVersion(deviceEvent.subject().hwVersion())
+ .setManufacturer(deviceEvent.subject().manufacturer())
+ .setSerialNumber(deviceEvent.subject().serialNumber())
+ .setSwVersion(deviceEvent.subject().swVersion())
+ .setType(DeviceType
+ .valueOf(deviceEvent.subject().type().name()))
+ .build();
+
+ PortCore portCore = null;
+ if (deviceEvent.port() != null) {
+ portCore =
+ PortCore.newBuilder()
+ .setIsEnabled(deviceEvent.port().isEnabled())
+ .setPortNumber(deviceEvent.port().number()
+ .toString())
+ .setPortSpeed(deviceEvent.port().portSpeed())
+ .setType(PortType
+ .valueOf(deviceEvent.port().type().name()))
+ .build();
+
+ notificationBuilder.setPort(portCore);
+ }
+
+ notificationBuilder.setDeviceEventType(getProtoType(deviceEvent))
+ .setDevice(deviceCore);
+
+ return notificationBuilder.build();
+ }
+
+ /**
+ * Retrieves the protobuf generated device event type.
+ *
+ * @param event ONOS Device Event
+ * @return generated Device Event Type
+ */
+ private DeviceEventType getProtoType(DeviceEvent event) {
+ DeviceEventType protobufEventType = null;
+ DeviceEventType[] deviceEvents = DeviceEventType.values();
+ for (DeviceEventType deviceEventType : deviceEvents) {
+ if (deviceEventType.name().equals(event.type().name())) {
+ protobufEventType = deviceEventType;
+ }
+ }
+
+ return protobufEventType;
+ }
+}
diff --git a/apps/kafka-integration/core/src/main/java/org/onosproject/kafkaintegration/converter/EventConverter.java b/apps/kafka-integration/core/src/main/java/org/onosproject/kafkaintegration/converter/EventConverter.java
new file mode 100644
index 0000000..c1e7739
--- /dev/null
+++ b/apps/kafka-integration/core/src/main/java/org/onosproject/kafkaintegration/converter/EventConverter.java
@@ -0,0 +1,36 @@
+/**
+ * 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.
+ */
+package org.onosproject.kafkaintegration.converter;
+
+import org.onosproject.event.Event;
+
+import com.google.protobuf.GeneratedMessage;
+
+/**
+ *
+ * APIs for converting between ONOS event objects and protobuf data objects.
+ *
+ */
+public interface EventConverter {
+
+ /**
+ * Converts ONOS specific event data to a format that is suitable for export
+ * to Kafka.
+ *
+ * @param event ONOS Event object
+ * @return converted data in protobuf format.
+ */
+ GeneratedMessage convertToProtoMessage(Event<?, ?> event);
+}
diff --git a/apps/kafka-integration/core/src/main/java/org/onosproject/kafkaintegration/converter/LinkEventConverter.java b/apps/kafka-integration/core/src/main/java/org/onosproject/kafkaintegration/converter/LinkEventConverter.java
new file mode 100644
index 0000000..febe020
--- /dev/null
+++ b/apps/kafka-integration/core/src/main/java/org/onosproject/kafkaintegration/converter/LinkEventConverter.java
@@ -0,0 +1,101 @@
+/**
+ * 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.
+ */
+package org.onosproject.kafkaintegration.converter;
+
+import org.onosproject.event.Event;
+import org.onosproject.grpc.net.Link.ConnectPoint;
+import org.onosproject.grpc.net.Link.LinkCore;
+import org.onosproject.grpc.net.Link.LinkState;
+import org.onosproject.grpc.net.Link.LinkType;
+import org.onosproject.grpc.net.LinkEvent.LinkEventType;
+import org.onosproject.grpc.net.LinkEvent.LinkNotification;
+import org.onosproject.net.link.LinkEvent;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import com.google.protobuf.GeneratedMessage;
+
+/**
+ * Converts for ONOS Link event message to protobuf format.
+ */
+public class LinkEventConverter implements EventConverter {
+
+ private final Logger log = LoggerFactory.getLogger(getClass());
+
+ @Override
+ public GeneratedMessage convertToProtoMessage(Event<?, ?> event) {
+
+ LinkEvent linkEvent = (LinkEvent) event;
+
+ if (!linkEventTypeSupported(linkEvent)) {
+ log.error("Unsupported Onos Event {}. There is no matching "
+ + "proto Event type", linkEvent.type().toString());
+ return null;
+ }
+
+ return buildDeviceProtoMessage(linkEvent);
+ }
+
+ private boolean linkEventTypeSupported(LinkEvent event) {
+ LinkEventType[] kafkaLinkEvents = LinkEventType.values();
+ for (LinkEventType linkEventType : kafkaLinkEvents) {
+ if (linkEventType.name().equals(event.type().name())) {
+ return true;
+ }
+ }
+ return false;
+ }
+
+ private LinkNotification buildDeviceProtoMessage(LinkEvent linkEvent) {
+ LinkNotification notification = LinkNotification.newBuilder()
+ .setLinkEventType(getProtoType(linkEvent))
+ .setLink(LinkCore.newBuilder()
+ .setState(LinkState
+ .valueOf(linkEvent.subject().state().name()))
+ .setType(LinkType.valueOf(linkEvent.subject().type().name()))
+ .setDst(ConnectPoint.newBuilder()
+ .setDeviceId(linkEvent.subject().dst()
+ .deviceId().toString())
+ .setPortNumber(linkEvent.subject().dst().port()
+ .toString()))
+ .setSrc(ConnectPoint.newBuilder()
+ .setDeviceId(linkEvent.subject().src()
+ .deviceId().toString())
+ .setPortNumber(linkEvent.subject().src().port()
+ .toString())))
+ .build();
+
+ return notification;
+ }
+
+ /**
+ * Returns the specific Kafka Device Event Type for the corresponding ONOS
+ * Device Event Type.
+ *
+ * @param event ONOS Device Event
+ * @return Kafka Device Event Type
+ */
+ private LinkEventType getProtoType(LinkEvent event) {
+ LinkEventType generatedEventType = null;
+ LinkEventType[] kafkaEvents = LinkEventType.values();
+ for (LinkEventType linkEventType : kafkaEvents) {
+ if (linkEventType.name().equals(event.type().name())) {
+ generatedEventType = linkEventType;
+ }
+ }
+
+ return generatedEventType;
+ }
+}
diff --git a/apps/kafka-integration/core/src/main/java/org/onosproject/kafkaintegration/converter/package-info.java b/apps/kafka-integration/core/src/main/java/org/onosproject/kafkaintegration/converter/package-info.java
new file mode 100644
index 0000000..d33c0d7
--- /dev/null
+++ b/apps/kafka-integration/core/src/main/java/org/onosproject/kafkaintegration/converter/package-info.java
@@ -0,0 +1,20 @@
+/**
+ * Copyright 2016-present 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.
+ */
+
+/**
+ * Converters for converting various ONOS events to their corresponding Protocol
+ * Buffer format.
+ *
+ */
+package org.onosproject.kafkaintegration.converter;