blob: 0a6e91d85e24cd151e3c0712feb0bfbfd80c578f [file] [log] [blame]
Shravan Ambati7d199542016-04-22 16:09:05 -07001/**
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 */
15package org.onosproject.kafkaintegration.rest;
16
Sanjana Agarwalcb4a3db2016-07-14 11:42:48 -070017import com.fasterxml.jackson.databind.ObjectMapper;
18import com.fasterxml.jackson.databind.node.ObjectNode;
19import org.onosproject.kafkaintegration.api.EventSubscriptionService;
20import org.onosproject.kafkaintegration.api.dto.EventSubscriber;
21import org.onosproject.kafkaintegration.api.dto.EventSubscriberGroupId;
22import org.onosproject.rest.AbstractWebResource;
23import org.slf4j.Logger;
24import org.slf4j.LoggerFactory;
Shravan Ambatibb6b4452016-05-04 13:25:28 -070025
Shravan Ambati7d199542016-04-22 16:09:05 -070026
Jian Lic2a542b2016-05-10 11:48:19 -070027import javax.ws.rs.Consumes;
28import javax.ws.rs.DELETE;
29import javax.ws.rs.POST;
30import javax.ws.rs.Path;
31import javax.ws.rs.Produces;
32import javax.ws.rs.core.MediaType;
33import javax.ws.rs.core.Response;
Sanjana Agarwalcb4a3db2016-07-14 11:42:48 -070034import java.io.IOException;
35import java.io.InputStream;
Jian Lic2a542b2016-05-10 11:48:19 -070036
Sanjana Agarwalcb4a3db2016-07-14 11:42:48 -070037import static com.google.common.base.Preconditions.checkNotNull;
38import static javax.ws.rs.core.Response.Status.BAD_REQUEST;
Shravan Ambati7d199542016-04-22 16:09:05 -070039
40/**
41 * Rest Interfaces for subscribing/unsubscribing to event notifications.
42 */
43@Path("kafkaService")
44public class EventExporterWebResource extends AbstractWebResource {
45
46 private final Logger log = LoggerFactory.getLogger(getClass());
Shravan Ambatibb6b4452016-05-04 13:25:28 -070047 public static final String JSON_NOT_NULL =
48 "Registration Data cannot be empty";
49 public static final String REGISTRATION_SUCCESSFUL =
50 "Registered Listener successfully";
51 public static final String DEREGISTRATION_SUCCESSFUL =
52 "De-Registered Listener successfully";
53 public static final String EVENT_SUBSCRIPTION_SUCCESSFUL =
Sanjana Agarwalcb4a3db2016-07-14 11:42:48 -070054 "Event Registration successful";
Sanjana Agarwaleb9f0c52016-06-07 11:10:34 -070055 public static final String EVENT_SUBSCRIPTION_UNSUCCESSFUL =
56 "Event subscription unsuccessful";
Shravan Ambatibb6b4452016-05-04 13:25:28 -070057 public static final String EVENT_SUBSCRIPTION_REMOVED =
Sanjana Agarwalcb4a3db2016-07-14 11:42:48 -070058 "Event De-Registration successful";
59
Shravan Ambati7d199542016-04-22 16:09:05 -070060 /**
Shravan Ambatibb6b4452016-05-04 13:25:28 -070061 * Registers a listener for ONOS Events.
Shravan Ambati7d199542016-04-22 16:09:05 -070062 *
63 * @param appName The application trying to register
64 * @return 200 OK with UUID string which should be used as Kafka Consumer
65 * Group Id
66 * @onos.rsModel KafkaRegistration
67 */
68 @POST
69 @Produces(MediaType.APPLICATION_JSON)
70 @Consumes(MediaType.APPLICATION_JSON)
71 @Path("register")
72 public Response registerKafkaListener(String appName) {
73
Sanjana Agarwalcb4a3db2016-07-14 11:42:48 -070074 EventSubscriptionService service = get(EventSubscriptionService.class);
Shravan Ambati7d199542016-04-22 16:09:05 -070075
76 EventSubscriberGroupId groupId = service.registerListener(appName);
77
78 log.info("Registered app {}", appName);
Shravan Ambati7d199542016-04-22 16:09:05 -070079 // TODO: Should also return Kafka server information.
80 // Will glue this in when we have the config and Kafka modules ready
81 return ok(groupId.getId().toString()).build();
82 }
83
84 /**
Shravan Ambatibb6b4452016-05-04 13:25:28 -070085 * Unregisters a listener for ONOS Events.
Shravan Ambati7d199542016-04-22 16:09:05 -070086 *
87 * @param appName The application trying to unregister
88 * @return 200 OK
89 * @onos.rsModel KafkaRegistration
90 */
91 @DELETE
Shravan Ambati7d199542016-04-22 16:09:05 -070092 @Path("unregister")
93 public Response removeKafkaListener(String appName) {
Sanjana Agarwalcb4a3db2016-07-14 11:42:48 -070094 EventSubscriptionService service = get(EventSubscriptionService.class);
Shravan Ambati7d199542016-04-22 16:09:05 -070095
96 service.unregisterListener(appName);
Sanjana Agarwaleb9f0c52016-06-07 11:10:34 -070097 log.info("Unregistered app {}", appName);
Shravan Ambati7d199542016-04-22 16:09:05 -070098 return ok(DEREGISTRATION_SUCCESSFUL).build();
99 }
100
101 /**
Shravan Ambatibb6b4452016-05-04 13:25:28 -0700102 * Creates subscription to a specific ONOS event.
Shravan Ambati7d199542016-04-22 16:09:05 -0700103 *
Shravan Ambatibb6b4452016-05-04 13:25:28 -0700104 * @param input Subscription Data in JSON format
Shravan Ambati7d199542016-04-22 16:09:05 -0700105 * @return 200 OK if successful or 400 BAD REQUEST
106 * @onos.rsModel KafkaSubscription
107 */
108 @POST
Sanjana Agarwaleb9f0c52016-06-07 11:10:34 -0700109 @Consumes(MediaType.APPLICATION_JSON)
Shravan Ambati7d199542016-04-22 16:09:05 -0700110 @Produces(MediaType.APPLICATION_JSON)
111 @Path("subscribe")
112 public Response subscribe(InputStream input) {
113
Sanjana Agarwalcb4a3db2016-07-14 11:42:48 -0700114 EventSubscriptionService service = get(EventSubscriptionService.class);
Shravan Ambati7d199542016-04-22 16:09:05 -0700115
116 try {
117 EventSubscriber sub = parseSubscriptionData(input);
118 service.subscribe(sub);
Sanjana Agarwalcb4a3db2016-07-14 11:42:48 -0700119 // It will subscribe to all the topics. Not only the one that is sent by the consumer.
Shravan Ambati7d199542016-04-22 16:09:05 -0700120 } catch (Exception e) {
121 log.error(e.getMessage());
122 return Response.status(BAD_REQUEST).entity(e.getMessage()).build();
123 }
124
125 return ok(EVENT_SUBSCRIPTION_SUCCESSFUL).build();
126 }
127
128 /**
Shravan Ambatibb6b4452016-05-04 13:25:28 -0700129 * Parses JSON Subscription Data from the external application.
Shravan Ambati7d199542016-04-22 16:09:05 -0700130 *
Shravan Ambatibb6b4452016-05-04 13:25:28 -0700131 * @param input Subscription Data in JSON format
Shravan Ambati7d199542016-04-22 16:09:05 -0700132 * @return parsed DTO object
133 * @throws IOException
134 */
135 private EventSubscriber parseSubscriptionData(InputStream input)
136 throws IOException {
137
138 ObjectMapper mapper = new ObjectMapper();
139 ObjectNode node = (ObjectNode) mapper.readTree(input);
Shravan Ambati7d199542016-04-22 16:09:05 -0700140 checkNotNull(node, JSON_NOT_NULL);
Sanjana Agarwaleb9f0c52016-06-07 11:10:34 -0700141 EventSubscriber codec = codec(EventSubscriber.class).decode(node, this);
142 checkNotNull(codec, JSON_NOT_NULL);
143 return codec;
Shravan Ambati7d199542016-04-22 16:09:05 -0700144 }
145
146 /**
Shravan Ambatibb6b4452016-05-04 13:25:28 -0700147 * Deletes subscription from a specific ONOS event.
Shravan Ambati7d199542016-04-22 16:09:05 -0700148 *
Shravan Ambatibb6b4452016-05-04 13:25:28 -0700149 * @param input data in JSON format
Shravan Ambati7d199542016-04-22 16:09:05 -0700150 * @return 200 OK if successful or 400 BAD REQUEST
151 * @onos.rsModel KafkaSubscription
152 */
153 @DELETE
Jian Lic2a542b2016-05-10 11:48:19 -0700154 @Consumes(MediaType.APPLICATION_JSON)
Shravan Ambati7d199542016-04-22 16:09:05 -0700155 @Path("unsubscribe")
156 public Response unsubscribe(InputStream input) {
157
Sanjana Agarwalcb4a3db2016-07-14 11:42:48 -0700158 EventSubscriptionService service = get(EventSubscriptionService.class);
Shravan Ambati7d199542016-04-22 16:09:05 -0700159
160 try {
161 EventSubscriber sub = parseSubscriptionData(input);
Shravan Ambatibb6b4452016-05-04 13:25:28 -0700162 service.unsubscribe(sub);
Shravan Ambati7d199542016-04-22 16:09:05 -0700163 } catch (Exception e) {
164 log.error(e.getMessage());
165 return Response.status(BAD_REQUEST).entity(e.getMessage()).build();
166 }
167
168 return ok(EVENT_SUBSCRIPTION_REMOVED).build();
169 }
170}