blob: 59b9c7e9ab01b02ac889446f43f223218d66e81f [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;
Shravan Ambati5a11e172016-07-21 15:55:28 -070021import org.onosproject.kafkaintegration.api.dto.RegistrationResponse;
Sanjana Agarwalcb4a3db2016-07-14 11:42:48 -070022import org.onosproject.rest.AbstractWebResource;
23import org.slf4j.Logger;
24import org.slf4j.LoggerFactory;
Shravan Ambatibb6b4452016-05-04 13:25:28 -070025
Jian Lic2a542b2016-05-10 11:48:19 -070026import javax.ws.rs.Consumes;
27import javax.ws.rs.DELETE;
28import javax.ws.rs.POST;
29import javax.ws.rs.Path;
30import javax.ws.rs.Produces;
31import javax.ws.rs.core.MediaType;
32import javax.ws.rs.core.Response;
Sanjana Agarwalcb4a3db2016-07-14 11:42:48 -070033import java.io.IOException;
34import java.io.InputStream;
Jian Lic2a542b2016-05-10 11:48:19 -070035
Sanjana Agarwalcb4a3db2016-07-14 11:42:48 -070036import static com.google.common.base.Preconditions.checkNotNull;
37import static javax.ws.rs.core.Response.Status.BAD_REQUEST;
Shravan Ambati7d199542016-04-22 16:09:05 -070038
39/**
40 * Rest Interfaces for subscribing/unsubscribing to event notifications.
41 */
42@Path("kafkaService")
43public class EventExporterWebResource extends AbstractWebResource {
44
45 private final Logger log = LoggerFactory.getLogger(getClass());
Shravan Ambatibb6b4452016-05-04 13:25:28 -070046 public static final String JSON_NOT_NULL =
47 "Registration Data cannot be empty";
48 public static final String REGISTRATION_SUCCESSFUL =
49 "Registered Listener successfully";
50 public static final String DEREGISTRATION_SUCCESSFUL =
51 "De-Registered Listener successfully";
52 public static final String EVENT_SUBSCRIPTION_SUCCESSFUL =
Sanjana Agarwalcb4a3db2016-07-14 11:42:48 -070053 "Event Registration successful";
Sanjana Agarwaleb9f0c52016-06-07 11:10:34 -070054 public static final String EVENT_SUBSCRIPTION_UNSUCCESSFUL =
55 "Event subscription unsuccessful";
Shravan Ambatibb6b4452016-05-04 13:25:28 -070056 public static final String EVENT_SUBSCRIPTION_REMOVED =
Sanjana Agarwalcb4a3db2016-07-14 11:42:48 -070057 "Event De-Registration successful";
58
Shravan Ambati7d199542016-04-22 16:09:05 -070059 /**
Shravan Ambatibb6b4452016-05-04 13:25:28 -070060 * Registers a listener for ONOS Events.
Shravan Ambati7d199542016-04-22 16:09:05 -070061 *
62 * @param appName The application trying to register
63 * @return 200 OK with UUID string which should be used as Kafka Consumer
Shravan Ambati5a11e172016-07-21 15:55:28 -070064 * Group Id and Kafka Server, port information.
Shravan Ambati7d199542016-04-22 16:09:05 -070065 * @onos.rsModel KafkaRegistration
66 */
67 @POST
68 @Produces(MediaType.APPLICATION_JSON)
69 @Consumes(MediaType.APPLICATION_JSON)
70 @Path("register")
71 public Response registerKafkaListener(String appName) {
72
Sanjana Agarwalcb4a3db2016-07-14 11:42:48 -070073 EventSubscriptionService service = get(EventSubscriptionService.class);
Shravan Ambati7d199542016-04-22 16:09:05 -070074
Shravan Ambati5a11e172016-07-21 15:55:28 -070075 RegistrationResponse response = service.registerListener(appName);
76
77 ObjectNode result = mapper().createObjectNode();
78 result.put("groupId", response.getGroupId().getId().toString());
79 result.put("ipAddress", response.getIpAddress());
80 result.put("port", response.getPort());
Shravan Ambati7d199542016-04-22 16:09:05 -070081
82 log.info("Registered app {}", appName);
Shravan Ambati5a11e172016-07-21 15:55:28 -070083
84 return ok(result.toString()).build();
Shravan Ambati7d199542016-04-22 16:09:05 -070085 }
86
87 /**
Shravan Ambatibb6b4452016-05-04 13:25:28 -070088 * Unregisters a listener for ONOS Events.
Shravan Ambati7d199542016-04-22 16:09:05 -070089 *
90 * @param appName The application trying to unregister
91 * @return 200 OK
92 * @onos.rsModel KafkaRegistration
93 */
94 @DELETE
Shravan Ambati7d199542016-04-22 16:09:05 -070095 @Path("unregister")
96 public Response removeKafkaListener(String appName) {
Sanjana Agarwalcb4a3db2016-07-14 11:42:48 -070097 EventSubscriptionService service = get(EventSubscriptionService.class);
Shravan Ambati7d199542016-04-22 16:09:05 -070098
99 service.unregisterListener(appName);
Sanjana Agarwaleb9f0c52016-06-07 11:10:34 -0700100 log.info("Unregistered app {}", appName);
Shravan Ambati7d199542016-04-22 16:09:05 -0700101 return ok(DEREGISTRATION_SUCCESSFUL).build();
102 }
103
104 /**
Shravan Ambatibb6b4452016-05-04 13:25:28 -0700105 * Creates subscription to a specific ONOS event.
Shravan Ambati7d199542016-04-22 16:09:05 -0700106 *
Shravan Ambatibb6b4452016-05-04 13:25:28 -0700107 * @param input Subscription Data in JSON format
Shravan Ambati7d199542016-04-22 16:09:05 -0700108 * @return 200 OK if successful or 400 BAD REQUEST
109 * @onos.rsModel KafkaSubscription
110 */
111 @POST
Sanjana Agarwaleb9f0c52016-06-07 11:10:34 -0700112 @Consumes(MediaType.APPLICATION_JSON)
Shravan Ambati7d199542016-04-22 16:09:05 -0700113 @Produces(MediaType.APPLICATION_JSON)
114 @Path("subscribe")
115 public Response subscribe(InputStream input) {
116
Sanjana Agarwalcb4a3db2016-07-14 11:42:48 -0700117 EventSubscriptionService service = get(EventSubscriptionService.class);
Shravan Ambati7d199542016-04-22 16:09:05 -0700118
119 try {
120 EventSubscriber sub = parseSubscriptionData(input);
121 service.subscribe(sub);
Sanjana Agarwalcb4a3db2016-07-14 11:42:48 -0700122 // It will subscribe to all the topics. Not only the one that is sent by the consumer.
Shravan Ambati7d199542016-04-22 16:09:05 -0700123 } catch (Exception e) {
124 log.error(e.getMessage());
125 return Response.status(BAD_REQUEST).entity(e.getMessage()).build();
126 }
127
128 return ok(EVENT_SUBSCRIPTION_SUCCESSFUL).build();
129 }
130
131 /**
Shravan Ambatibb6b4452016-05-04 13:25:28 -0700132 * Parses JSON Subscription Data from the external application.
Shravan Ambati7d199542016-04-22 16:09:05 -0700133 *
Shravan Ambatibb6b4452016-05-04 13:25:28 -0700134 * @param input Subscription Data in JSON format
Shravan Ambati7d199542016-04-22 16:09:05 -0700135 * @return parsed DTO object
136 * @throws IOException
137 */
138 private EventSubscriber parseSubscriptionData(InputStream input)
139 throws IOException {
140
141 ObjectMapper mapper = new ObjectMapper();
142 ObjectNode node = (ObjectNode) mapper.readTree(input);
Shravan Ambati7d199542016-04-22 16:09:05 -0700143 checkNotNull(node, JSON_NOT_NULL);
Sanjana Agarwaleb9f0c52016-06-07 11:10:34 -0700144 EventSubscriber codec = codec(EventSubscriber.class).decode(node, this);
145 checkNotNull(codec, JSON_NOT_NULL);
146 return codec;
Shravan Ambati7d199542016-04-22 16:09:05 -0700147 }
148
149 /**
Shravan Ambatibb6b4452016-05-04 13:25:28 -0700150 * Deletes subscription from a specific ONOS event.
Shravan Ambati7d199542016-04-22 16:09:05 -0700151 *
Shravan Ambatibb6b4452016-05-04 13:25:28 -0700152 * @param input data in JSON format
Shravan Ambati7d199542016-04-22 16:09:05 -0700153 * @return 200 OK if successful or 400 BAD REQUEST
154 * @onos.rsModel KafkaSubscription
155 */
156 @DELETE
Jian Lic2a542b2016-05-10 11:48:19 -0700157 @Consumes(MediaType.APPLICATION_JSON)
Shravan Ambati7d199542016-04-22 16:09:05 -0700158 @Path("unsubscribe")
159 public Response unsubscribe(InputStream input) {
160
Sanjana Agarwalcb4a3db2016-07-14 11:42:48 -0700161 EventSubscriptionService service = get(EventSubscriptionService.class);
Shravan Ambati7d199542016-04-22 16:09:05 -0700162
163 try {
164 EventSubscriber sub = parseSubscriptionData(input);
Shravan Ambatibb6b4452016-05-04 13:25:28 -0700165 service.unsubscribe(sub);
Shravan Ambati7d199542016-04-22 16:09:05 -0700166 } catch (Exception e) {
167 log.error(e.getMessage());
168 return Response.status(BAD_REQUEST).entity(e.getMessage()).build();
169 }
170
171 return ok(EVENT_SUBSCRIPTION_REMOVED).build();
172 }
173}