CodeReview - Initial Commit for Kafka Integration Application
1. Partial REST API implementation
2. Partial Event Manager backend implementation
Change-Id: Ieaf703f7a3f6e296aea8ffcf155c7a1b603236ca
diff --git a/apps/kafka-integration/src/main/java/org/onosproject/kafkaintegration/rest/EventExporterWebResource.java b/apps/kafka-integration/src/main/java/org/onosproject/kafkaintegration/rest/EventExporterWebResource.java
new file mode 100644
index 0000000..e3b7cc6
--- /dev/null
+++ b/apps/kafka-integration/src/main/java/org/onosproject/kafkaintegration/rest/EventExporterWebResource.java
@@ -0,0 +1,167 @@
+/**
+ * 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.
+ */
+package org.onosproject.kafkaintegration.rest;
+
+import static com.google.common.base.Preconditions.checkNotNull;
+import static javax.ws.rs.core.Response.Status.BAD_REQUEST;
+
+import java.io.IOException;
+import java.io.InputStream;
+
+import javax.ws.rs.Consumes;
+import javax.ws.rs.DELETE;
+import javax.ws.rs.POST;
+import javax.ws.rs.Path;
+import javax.ws.rs.Produces;
+import javax.ws.rs.core.MediaType;
+import javax.ws.rs.core.Response;
+
+import org.onosproject.codec.JsonCodec;
+import org.onosproject.kafkaintegration.api.EventExporterService;
+import org.onosproject.kafkaintegration.api.dto.EventSubscriber;
+import org.onosproject.kafkaintegration.api.dto.EventSubscriberGroupId;
+import org.onosproject.rest.AbstractWebResource;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.fasterxml.jackson.databind.node.ObjectNode;
+
+/**
+ * Rest Interfaces for subscribing/unsubscribing to event notifications.
+ */
+@Path("kafkaService")
+public class EventExporterWebResource extends AbstractWebResource {
+
+ private final Logger log = LoggerFactory.getLogger(getClass());
+ public static final String JSON_NOT_NULL = "Registration Data cannot be empty";
+ public static final String REGISTRATION_SUCCESSFUL = "Registered Listener successfully";
+ public static final String DEREGISTRATION_SUCCESSFUL = "De-Registered Listener successfully";
+ public static final String EVENT_SUBSCRIPTION_SUCCESSFUL = "Event Registration successfull";
+ public static final String EVENT_SUBSCRIPTION_REMOVED = "Event De-Registration successfull";
+
+ /**
+ * Registers a listener for Onos Events.
+ *
+ * @param appName The application trying to register
+ * @return 200 OK with UUID string which should be used as Kafka Consumer
+ * Group Id
+ * @onos.rsModel KafkaRegistration
+ */
+ @POST
+ @Produces(MediaType.APPLICATION_JSON)
+ @Consumes(MediaType.APPLICATION_JSON)
+ @Path("register")
+ public Response registerKafkaListener(String appName) {
+
+ EventExporterService service = get(EventExporterService.class);
+
+ EventSubscriberGroupId groupId = service.registerListener(appName);
+
+ log.info("Registered app {}", appName);
+
+ // TODO: Should also return Kafka server information.
+ // Will glue this in when we have the config and Kafka modules ready
+ return ok(groupId.getId().toString()).build();
+ }
+
+ /**
+ * Unregisters a listener for Onos Events.
+ *
+ * @param appName The application trying to unregister
+ * @return 200 OK
+ * @onos.rsModel KafkaRegistration
+ */
+ @DELETE
+ @Produces(MediaType.APPLICATION_JSON)
+ @Consumes(MediaType.APPLICATION_JSON)
+ @Path("unregister")
+ public Response removeKafkaListener(String appName) {
+ EventExporterService service = get(EventExporterService.class);
+
+ service.unregisterListener(appName);
+
+ return ok(DEREGISTRATION_SUCCESSFUL).build();
+ }
+
+ /**
+ * Creates subscription to a specific Onos event.
+ *
+ * @param input Subscription Data in Json format
+ * @return 200 OK if successful or 400 BAD REQUEST
+ * @onos.rsModel KafkaSubscription
+ */
+ @POST
+ @Produces(MediaType.APPLICATION_JSON)
+ @Path("subscribe")
+ public Response subscribe(InputStream input) {
+
+ EventExporterService service = get(EventExporterService.class);
+
+ try {
+ EventSubscriber sub = parseSubscriptionData(input);
+ service.subscribe(sub);
+ } catch (Exception e) {
+ log.error(e.getMessage());
+ return Response.status(BAD_REQUEST).entity(e.getMessage()).build();
+ }
+
+ return ok(EVENT_SUBSCRIPTION_SUCCESSFUL).build();
+ }
+
+ /**
+ * Parses Json Subscription Data from the external application.
+ *
+ * @param node node within the parsed json tree.
+ * @return parsed DTO object
+ * @throws IOException
+ */
+ private EventSubscriber parseSubscriptionData(InputStream input)
+ throws IOException {
+
+ ObjectMapper mapper = new ObjectMapper();
+ ObjectNode node = (ObjectNode) mapper.readTree(input);
+
+ checkNotNull(node, JSON_NOT_NULL);
+
+ JsonCodec<EventSubscriber> codec = codec(EventSubscriber.class);
+ return codec.decode(node, this);
+ }
+
+ /**
+ * Deletes subscription from a specific Onos event.
+ *
+ * @param input data in json format
+ * @return 200 OK if successful or 400 BAD REQUEST
+ * @onos.rsModel KafkaSubscription
+ */
+ @DELETE
+ @Produces(MediaType.APPLICATION_JSON)
+ @Path("unsubscribe")
+ public Response unsubscribe(InputStream input) {
+
+ EventExporterService service = get(EventExporterService.class);
+
+ try {
+ EventSubscriber sub = parseSubscriptionData(input);
+ service.subscribe(sub);
+ } catch (Exception e) {
+ log.error(e.getMessage());
+ return Response.status(BAD_REQUEST).entity(e.getMessage()).build();
+ }
+
+ return ok(EVENT_SUBSCRIPTION_REMOVED).build();
+ }
+}
diff --git a/apps/kafka-integration/src/main/java/org/onosproject/kafkaintegration/rest/SubscriberCodec.java b/apps/kafka-integration/src/main/java/org/onosproject/kafkaintegration/rest/SubscriberCodec.java
new file mode 100644
index 0000000..f7a02b4
--- /dev/null
+++ b/apps/kafka-integration/src/main/java/org/onosproject/kafkaintegration/rest/SubscriberCodec.java
@@ -0,0 +1,59 @@
+/**
+ * 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.
+ */
+package org.onosproject.kafkaintegration.rest;
+
+import static com.google.common.base.Preconditions.checkNotNull;
+
+import java.util.UUID;
+
+import org.onosproject.codec.CodecContext;
+import org.onosproject.codec.JsonCodec;
+import org.onosproject.kafkaintegration.api.dto.EventSubscriber;
+import org.onosproject.kafkaintegration.api.dto.EventSubscriberGroupId;
+import org.onosproject.kafkaintegration.api.dto.OnosEvent.Type;
+
+import com.fasterxml.jackson.databind.node.ObjectNode;
+
+/**
+ * Codec for encoding/decoding a Subscriber object to/from JSON.
+ *
+ */
+public final class SubscriberCodec extends JsonCodec<EventSubscriber> {
+
+ // JSON field names
+ private static final String NAME = "appName";
+ private static final String GROUP_ID = "groupId";
+ private static final String EVENT_TYPE = "eventType";
+
+ @Override
+ public ObjectNode encode(EventSubscriber data, CodecContext context) {
+ checkNotNull(data, "Subscriber cannot be null");
+ return context.mapper().createObjectNode().put(NAME, data.appName())
+ .put(GROUP_ID, data.subscriberGroupId().getId().toString())
+ .put(EVENT_TYPE, data.eventType().toString());
+ }
+
+ @Override
+ public EventSubscriber decode(ObjectNode json, CodecContext context) {
+ String name = json.path(NAME).asText();
+ String groupId = json.path(GROUP_ID).asText();
+ EventSubscriberGroupId subscriberGroupId = new EventSubscriberGroupId(UUID
+ .fromString(groupId));
+ String eventType = json.path(EVENT_TYPE).asText();
+
+ return new EventSubscriber(name, subscriberGroupId,
+ Type.valueOf(eventType));
+ }
+}
diff --git a/apps/kafka-integration/src/main/java/org/onosproject/kafkaintegration/rest/package-info.java b/apps/kafka-integration/src/main/java/org/onosproject/kafkaintegration/rest/package-info.java
new file mode 100644
index 0000000..424c4a5
--- /dev/null
+++ b/apps/kafka-integration/src/main/java/org/onosproject/kafkaintegration/rest/package-info.java
@@ -0,0 +1,18 @@
+/**
+ * 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.
+ */
+
+/**
+ * REST API Definitions.
+ */
+package org.onosproject.kafkaintegration.rest;