Sanjana Agarwal
Committed by Yuta HIGUCHI

Kafka Codec Integration

Made changes as per the comments on patch set 2.
Made changes as per comments on patch set 3.
Made further changes and event subscription works absolutely fine now.

Change-Id: Icd20195f6c0e045d14beb73584069d486ab9290c
1 +/**
2 + * Copyright 2016-present Open Networking Laboratory
3 + * Licensed under the Apache License, Version 2.0 (the "License");
4 + * you may not use this file except in compliance with the License.
5 + * You may obtain a copy of the License at
6 +
7 + * http://www.apache.org/licenses/LICENSE-2.0
8 +
9 + * Unless required by applicable law or agreed to in writing, software
10 + * distributed under the License is distributed on an "AS IS" BASIS,
11 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 + * See the License for the specific language governing permissions and
13 + * limitations under the License.
14 + */
15 +package org.onosproject.kafkaintegration.api.dto;
16 +
17 +import static com.google.common.base.MoreObjects.toStringHelper;
18 +import static com.google.common.base.Preconditions.checkNotNull;
19 +
20 +import java.util.Objects;
21 +
22 +import org.onosproject.kafkaintegration.api.dto.OnosEvent.Type;
23 +
24 +/**
25 + * Representation of a subscription to an event type.
26 + *
27 + */
28 +public final class DefaultEventSubscriber implements EventSubscriber {
29 + private final String appName;
30 + private final EventSubscriberGroupId subscriberGroupId;
31 + private final Type eventType;
32 +
33 + /**
34 + * Creates a new Event Subscriber.
35 + *
36 + * @param name Application Name
37 + * @param groupId Subscriber group id of the application
38 + * @param eventType ONOS event type
39 + */
40 + public DefaultEventSubscriber(String name, EventSubscriberGroupId groupId,
41 + Type eventType) {
42 + this.appName = checkNotNull(name);
43 + this.subscriberGroupId = checkNotNull(groupId);
44 + this.eventType = checkNotNull(eventType);
45 + }
46 +
47 + @Override
48 + public String appName() {
49 + return appName;
50 + }
51 +
52 + @Override
53 + public EventSubscriberGroupId subscriberGroupId() {
54 + return subscriberGroupId;
55 + }
56 +
57 + @Override
58 + public Type eventType() {
59 + return eventType;
60 + }
61 +
62 + @Override
63 + public int hashCode() {
64 + return Objects.hash(appName, subscriberGroupId, eventType);
65 + }
66 +
67 + @Override
68 + public boolean equals(Object o) {
69 + if (o instanceof DefaultEventSubscriber) {
70 + DefaultEventSubscriber sub = (DefaultEventSubscriber) o;
71 + if (sub.appName.equals(appName)
72 + && sub.subscriberGroupId.equals(subscriberGroupId)
73 + && sub.eventType.equals(eventType)) {
74 + return true;
75 + }
76 + }
77 +
78 + return false;
79 + }
80 +
81 + @Override
82 + public String toString() {
83 + return toStringHelper(this).add("appName", appName)
84 + .addValue(subscriberGroupId.toString())
85 + .add("eventType", eventType).toString();
86 + }
87 + /**
88 + * To create an instance of the builder.
89 + *
90 + * @return instance of builder
91 + */
92 + public static Builder builder() {
93 + return new Builder();
94 + }
95 + /**
96 + * Builder class for Event subscriber.
97 + */
98 + public static final class Builder implements EventSubscriber.Builder {
99 + private String appName;
100 + private EventSubscriberGroupId subscriberGroupId;
101 + private Type eventType;
102 +
103 + @Override
104 + public Builder setAppName(String appName) {
105 + this.appName = appName;
106 + return this;
107 + }
108 +
109 + @Override
110 + public Builder setSubscriberGroupId(EventSubscriberGroupId
111 + subscriberGroupId) {
112 + this.subscriberGroupId = subscriberGroupId;
113 + return this;
114 + }
115 +
116 + @Override
117 + public Builder setEventType(Type eventType) {
118 + this.eventType = eventType;
119 + return this;
120 + }
121 +
122 + @Override
123 + public EventSubscriber build() {
124 + checkNotNull(appName, "App name cannot be null");
125 + checkNotNull(subscriberGroupId, "Subscriber group ID cannot " +
126 + "be " +
127 + "null");
128 + checkNotNull(eventType, "Event type cannot be null");
129 +
130 + return new DefaultEventSubscriber(appName,
131 + subscriberGroupId,
132 + eventType);
133 + }
134 + }
135 +
136 +}
...@@ -13,87 +13,42 @@ ...@@ -13,87 +13,42 @@
13 * limitations under the License. 13 * limitations under the License.
14 */ 14 */
15 package org.onosproject.kafkaintegration.api.dto; 15 package org.onosproject.kafkaintegration.api.dto;
16 -
17 -import static com.google.common.base.MoreObjects.toStringHelper;
18 -import static com.google.common.base.Preconditions.checkNotNull;
19 -
20 -import java.util.Objects;
21 -
22 import org.onosproject.kafkaintegration.api.dto.OnosEvent.Type; 16 import org.onosproject.kafkaintegration.api.dto.OnosEvent.Type;
23 17
24 /** 18 /**
25 - * Representation of a subscription to an event type. 19 + * Abstraction of subscription to an event type.
26 - *
27 */ 20 */
28 -public final class EventSubscriber { 21 +public interface EventSubscriber {
29 - private final String appName;
30 - private final EventSubscriberGroupId subscriberGroupId;
31 - private final Type eventType;
32 -
33 /** 22 /**
34 - * Creates a new Event Subscriber. 23 + * Returns the application name.
35 * 24 *
36 - * @param name Application Name 25 + * @return application name.
37 - * @param groupId Subscriber group id of the application
38 - * @param eventType ONOS event type
39 */ 26 */
40 - public EventSubscriber(String name, EventSubscriberGroupId groupId, 27 + String appName();
41 - Type eventType) {
42 - this.appName = checkNotNull(name);
43 - this.subscriberGroupId = checkNotNull(groupId);
44 - this.eventType = checkNotNull(eventType);
45 - }
46 28
47 /** 29 /**
48 - * Returns the Application Name. 30 + * Returns the subscriber group ID.
49 - * 31 + * @return subscriber group ID.
50 - * @return application name
51 */ 32 */
52 - public String appName() { 33 + EventSubscriberGroupId subscriberGroupId();
53 - return appName;
54 - }
55 -
56 - /**
57 - * Returns the Subscriber Group Id.
58 - *
59 - * @return Subscriber Group Id
60 - */
61 - public EventSubscriberGroupId subscriberGroupId() {
62 - return subscriberGroupId;
63 - }
64 34
65 /** 35 /**
66 * Returns the Event type. 36 * Returns the Event type.
67 * 37 *
68 * @return ONOS Event Type 38 * @return ONOS Event Type
69 */ 39 */
70 - public Type eventType() { 40 + Type eventType();
71 - return eventType;
72 - }
73 41
74 - @Override 42 + /**
75 - public int hashCode() { 43 + * An event subscriber builder.
76 - return Objects.hash(appName, subscriberGroupId, eventType); 44 + */
77 - } 45 + interface Builder {
46 + Builder setAppName(String appName);
78 47
79 - @Override 48 + Builder setSubscriberGroupId(EventSubscriberGroupId subscriberGroupId);
80 - public boolean equals(Object o) {
81 - if (o instanceof EventSubscriber) {
82 - EventSubscriber sub = (EventSubscriber) o;
83 - if (sub.appName.equals(appName)
84 - && sub.subscriberGroupId.equals(subscriberGroupId)
85 - && sub.eventType.equals(eventType)) {
86 - return true;
87 - }
88 - }
89 49
90 - return false; 50 + Builder setEventType(Type eventType);
91 - }
92 51
93 - @Override 52 + EventSubscriber build();
94 - public String toString() {
95 - return toStringHelper(this).add("appName", appName)
96 - .addValue(subscriberGroupId.toString())
97 - .add("eventType", eventType).toString();
98 } 53 }
99 } 54 }
......
...@@ -166,6 +166,7 @@ ...@@ -166,6 +166,7 @@
166 com.fasterxml.jackson.core, 166 com.fasterxml.jackson.core,
167 org.onlab.packet.*, 167 org.onlab.packet.*,
168 org.onosproject.*, 168 org.onosproject.*,
169 + org.onlab.util.*,
169 com.google.common.* 170 com.google.common.*
170 </Import-Package> 171 </Import-Package>
171 <Web-ContextPath>${web.context}</Web-ContextPath> 172 <Web-ContextPath>${web.context}</Web-ContextPath>
......
...@@ -19,6 +19,9 @@ import static com.google.common.base.Preconditions.checkNotNull; ...@@ -19,6 +19,9 @@ import static com.google.common.base.Preconditions.checkNotNull;
19 import static org.onosproject.kafkaintegration.api.dto.OnosEvent.Type.DEVICE; 19 import static org.onosproject.kafkaintegration.api.dto.OnosEvent.Type.DEVICE;
20 import static org.onosproject.kafkaintegration.api.dto.OnosEvent.Type.LINK; 20 import static org.onosproject.kafkaintegration.api.dto.OnosEvent.Type.LINK;
21 21
22 +import org.onosproject.kafkaintegration.api.dto.DefaultEventSubscriber;
23 +import org.onosproject.kafkaintegration.api.dto.EventSubscriber;
24 +
22 import java.util.ArrayList; 25 import java.util.ArrayList;
23 import java.util.List; 26 import java.util.List;
24 import java.util.Map; 27 import java.util.Map;
...@@ -33,8 +36,8 @@ import org.apache.felix.scr.annotations.Service; ...@@ -33,8 +36,8 @@ import org.apache.felix.scr.annotations.Service;
33 import org.onosproject.core.ApplicationId; 36 import org.onosproject.core.ApplicationId;
34 import org.onosproject.core.CoreService; 37 import org.onosproject.core.CoreService;
35 import org.onosproject.kafkaintegration.api.EventExporterService; 38 import org.onosproject.kafkaintegration.api.EventExporterService;
36 -import org.onosproject.kafkaintegration.api.dto.EventSubscriber;
37 import org.onosproject.kafkaintegration.api.dto.EventSubscriberGroupId; 39 import org.onosproject.kafkaintegration.api.dto.EventSubscriberGroupId;
40 +import org.onosproject.kafkaintegration.api.dto.OnosEvent;
38 import org.onosproject.kafkaintegration.api.dto.OnosEvent.Type; 41 import org.onosproject.kafkaintegration.api.dto.OnosEvent.Type;
39 import org.onosproject.kafkaintegration.errors.InvalidApplicationException; 42 import org.onosproject.kafkaintegration.errors.InvalidApplicationException;
40 import org.onosproject.kafkaintegration.errors.InvalidGroupIdException; 43 import org.onosproject.kafkaintegration.errors.InvalidGroupIdException;
...@@ -98,7 +101,12 @@ public class EventExporterManager implements EventExporterService { ...@@ -98,7 +101,12 @@ public class EventExporterManager implements EventExporterService {
98 .<Type, List<EventSubscriber>>consistentMapBuilder() 101 .<Type, List<EventSubscriber>>consistentMapBuilder()
99 .withName(SUBSCRIBED_APPS) 102 .withName(SUBSCRIBED_APPS)
100 .withSerializer(Serializer.using(KryoNamespaces.API, 103 .withSerializer(Serializer.using(KryoNamespaces.API,
101 - EventSubscriber.class)) 104 + EventSubscriber.class,
105 + OnosEvent.class,
106 + OnosEvent.Type.class,
107 + DefaultEventSubscriber.class,
108 + EventSubscriberGroupId.class,
109 + UUID.class))
102 .build().asJavaMap(); 110 .build().asJavaMap();
103 111
104 log.info("Started"); 112 log.info("Started");
...@@ -119,7 +127,6 @@ public class EventExporterManager implements EventExporterService { ...@@ -119,7 +127,6 @@ public class EventExporterManager implements EventExporterService {
119 return registeredApps.computeIfAbsent(externalAppId, 127 return registeredApps.computeIfAbsent(externalAppId,
120 (key) -> new EventSubscriberGroupId(UUID 128 (key) -> new EventSubscriberGroupId(UUID
121 .randomUUID())); 129 .randomUUID()));
122 -
123 } 130 }
124 131
125 @Override 132 @Override
......
1 +/**
2 + * Copyright 2016-present Open Networking Laboratory
3 + * Licensed under the Apache License, Version 2.0 (the "License");
4 + * you may not use this file except in compliance with the License.
5 + * You may obtain a copy of the License at
6 +
7 + * http://www.apache.org/licenses/LICENSE-2.0
8 +
9 + * Unless required by applicable law or agreed to in writing, software
10 + * distributed under the License is distributed on an "AS IS" BASIS,
11 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 + * See the License for the specific language governing permissions and
13 + * limitations under the License.
14 + */
15 +package org.onosproject.kafkaintegration.impl;
16 +
17 +import org.apache.felix.scr.annotations.Activate;
18 +import org.apache.felix.scr.annotations.Component;
19 +import org.apache.felix.scr.annotations.Deactivate;
20 +import org.apache.felix.scr.annotations.Reference;
21 +import org.apache.felix.scr.annotations.ReferenceCardinality;
22 +import org.onosproject.codec.CodecService;
23 +import org.onosproject.kafkaintegration.api.dto.EventSubscriber;
24 +import org.onosproject.kafkaintegration.rest.SubscriberCodec;
25 +import org.slf4j.Logger;
26 +import org.slf4j.LoggerFactory;
27 +
28 +/**
29 + * Implementation of the JSON codec brokering service for Kafka app.
30 + */
31 +@Component(immediate = true)
32 +public class KafkaCodecRegistrator {
33 + private static Logger log = LoggerFactory.getLogger(KafkaCodecRegistrator
34 + .class);
35 + @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
36 + protected CodecService codecService;
37 +
38 + @Activate
39 + public void activate() {
40 + codecService.registerCodec(EventSubscriber.class, new SubscriberCodec());
41 + log.info("Started");
42 + }
43 +
44 + @Deactivate
45 + public void deactivate() {
46 + log.info("Stopped");
47 + }
48 +}
...@@ -28,7 +28,6 @@ import javax.ws.rs.Produces; ...@@ -28,7 +28,6 @@ import javax.ws.rs.Produces;
28 import javax.ws.rs.core.MediaType; 28 import javax.ws.rs.core.MediaType;
29 import javax.ws.rs.core.Response; 29 import javax.ws.rs.core.Response;
30 30
31 -import org.onosproject.codec.JsonCodec;
32 import org.onosproject.kafkaintegration.api.EventExporterService; 31 import org.onosproject.kafkaintegration.api.EventExporterService;
33 import org.onosproject.kafkaintegration.api.dto.EventSubscriber; 32 import org.onosproject.kafkaintegration.api.dto.EventSubscriber;
34 import org.onosproject.kafkaintegration.api.dto.EventSubscriberGroupId; 33 import org.onosproject.kafkaintegration.api.dto.EventSubscriberGroupId;
...@@ -54,9 +53,10 @@ public class EventExporterWebResource extends AbstractWebResource { ...@@ -54,9 +53,10 @@ public class EventExporterWebResource extends AbstractWebResource {
54 "De-Registered Listener successfully"; 53 "De-Registered Listener successfully";
55 public static final String EVENT_SUBSCRIPTION_SUCCESSFUL = 54 public static final String EVENT_SUBSCRIPTION_SUCCESSFUL =
56 "Event Registration successfull"; 55 "Event Registration successfull";
56 + public static final String EVENT_SUBSCRIPTION_UNSUCCESSFUL =
57 + "Event subscription unsuccessful";
57 public static final String EVENT_SUBSCRIPTION_REMOVED = 58 public static final String EVENT_SUBSCRIPTION_REMOVED =
58 "Event De-Registration successfull"; 59 "Event De-Registration successfull";
59 -
60 /** 60 /**
61 * Registers a listener for ONOS Events. 61 * Registers a listener for ONOS Events.
62 * 62 *
...@@ -95,7 +95,7 @@ public class EventExporterWebResource extends AbstractWebResource { ...@@ -95,7 +95,7 @@ public class EventExporterWebResource extends AbstractWebResource {
95 EventExporterService service = get(EventExporterService.class); 95 EventExporterService service = get(EventExporterService.class);
96 96
97 service.unregisterListener(appName); 97 service.unregisterListener(appName);
98 - 98 + log.info("Unregistered app {}", appName);
99 return ok(DEREGISTRATION_SUCCESSFUL).build(); 99 return ok(DEREGISTRATION_SUCCESSFUL).build();
100 } 100 }
101 101
...@@ -107,6 +107,7 @@ public class EventExporterWebResource extends AbstractWebResource { ...@@ -107,6 +107,7 @@ public class EventExporterWebResource extends AbstractWebResource {
107 * @onos.rsModel KafkaSubscription 107 * @onos.rsModel KafkaSubscription
108 */ 108 */
109 @POST 109 @POST
110 + @Consumes(MediaType.APPLICATION_JSON)
110 @Produces(MediaType.APPLICATION_JSON) 111 @Produces(MediaType.APPLICATION_JSON)
111 @Path("subscribe") 112 @Path("subscribe")
112 public Response subscribe(InputStream input) { 113 public Response subscribe(InputStream input) {
...@@ -136,11 +137,10 @@ public class EventExporterWebResource extends AbstractWebResource { ...@@ -136,11 +137,10 @@ public class EventExporterWebResource extends AbstractWebResource {
136 137
137 ObjectMapper mapper = new ObjectMapper(); 138 ObjectMapper mapper = new ObjectMapper();
138 ObjectNode node = (ObjectNode) mapper.readTree(input); 139 ObjectNode node = (ObjectNode) mapper.readTree(input);
139 -
140 checkNotNull(node, JSON_NOT_NULL); 140 checkNotNull(node, JSON_NOT_NULL);
141 - 141 + EventSubscriber codec = codec(EventSubscriber.class).decode(node, this);
142 - JsonCodec<EventSubscriber> codec = codec(EventSubscriber.class); 142 + checkNotNull(codec, JSON_NOT_NULL);
143 - return codec.decode(node, this); 143 + return codec;
144 } 144 }
145 145
146 /** 146 /**
......
...@@ -20,10 +20,10 @@ import java.util.UUID; ...@@ -20,10 +20,10 @@ import java.util.UUID;
20 20
21 import org.onosproject.codec.CodecContext; 21 import org.onosproject.codec.CodecContext;
22 import org.onosproject.codec.JsonCodec; 22 import org.onosproject.codec.JsonCodec;
23 +import org.onosproject.kafkaintegration.api.dto.DefaultEventSubscriber;
23 import org.onosproject.kafkaintegration.api.dto.EventSubscriber; 24 import org.onosproject.kafkaintegration.api.dto.EventSubscriber;
24 import org.onosproject.kafkaintegration.api.dto.EventSubscriberGroupId; 25 import org.onosproject.kafkaintegration.api.dto.EventSubscriberGroupId;
25 import org.onosproject.kafkaintegration.api.dto.OnosEvent.Type; 26 import org.onosproject.kafkaintegration.api.dto.OnosEvent.Type;
26 -
27 import com.fasterxml.jackson.databind.node.ObjectNode; 27 import com.fasterxml.jackson.databind.node.ObjectNode;
28 28
29 /** 29 /**
...@@ -47,13 +47,18 @@ public final class SubscriberCodec extends JsonCodec<EventSubscriber> { ...@@ -47,13 +47,18 @@ public final class SubscriberCodec extends JsonCodec<EventSubscriber> {
47 47
48 @Override 48 @Override
49 public EventSubscriber decode(ObjectNode json, CodecContext context) { 49 public EventSubscriber decode(ObjectNode json, CodecContext context) {
50 - String name = json.path(NAME).asText(); 50 +
51 - String groupId = json.path(GROUP_ID).asText(); 51 + EventSubscriber.Builder resultBuilder = new DefaultEventSubscriber
52 - EventSubscriberGroupId subscriberGroupId = new EventSubscriberGroupId(UUID 52 + .Builder();
53 - .fromString(groupId)); 53 + String appName = json.get(NAME).asText();
54 - String eventType = json.path(EVENT_TYPE).asText(); 54 + resultBuilder.setAppName(appName);
55 - 55 +
56 - return new EventSubscriber(name, subscriberGroupId, 56 + String subscriberGroupId = json.get(GROUP_ID).asText();
57 - Type.valueOf(eventType)); 57 + resultBuilder.setSubscriberGroupId(new EventSubscriberGroupId(UUID.
58 + fromString(subscriberGroupId)));
59 +
60 + String eventType = json.get(EVENT_TYPE).asText();
61 + resultBuilder.setEventType(Type.valueOf(eventType));
62 + return resultBuilder.build();
58 } 63 }
59 -} 64 +}
...\ No newline at end of file ...\ No newline at end of file
......