Shravan Ambati | 7d19954 | 2016-04-22 16:09:05 -0700 | [diff] [blame] | 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.rest; |
| 16 | |
Sanjana Agarwal | cb4a3db | 2016-07-14 11:42:48 -0700 | [diff] [blame] | 17 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 18 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 19 | import org.onosproject.kafkaintegration.api.EventSubscriptionService; |
| 20 | import org.onosproject.kafkaintegration.api.dto.EventSubscriber; |
| 21 | import org.onosproject.kafkaintegration.api.dto.EventSubscriberGroupId; |
| 22 | import org.onosproject.rest.AbstractWebResource; |
| 23 | import org.slf4j.Logger; |
| 24 | import org.slf4j.LoggerFactory; |
Shravan Ambati | bb6b445 | 2016-05-04 13:25:28 -0700 | [diff] [blame] | 25 | |
Shravan Ambati | 7d19954 | 2016-04-22 16:09:05 -0700 | [diff] [blame] | 26 | |
Jian Li | c2a542b | 2016-05-10 11:48:19 -0700 | [diff] [blame] | 27 | import javax.ws.rs.Consumes; |
| 28 | import javax.ws.rs.DELETE; |
| 29 | import javax.ws.rs.POST; |
| 30 | import javax.ws.rs.Path; |
| 31 | import javax.ws.rs.Produces; |
| 32 | import javax.ws.rs.core.MediaType; |
| 33 | import javax.ws.rs.core.Response; |
Sanjana Agarwal | cb4a3db | 2016-07-14 11:42:48 -0700 | [diff] [blame] | 34 | import java.io.IOException; |
| 35 | import java.io.InputStream; |
Jian Li | c2a542b | 2016-05-10 11:48:19 -0700 | [diff] [blame] | 36 | |
Sanjana Agarwal | cb4a3db | 2016-07-14 11:42:48 -0700 | [diff] [blame] | 37 | import static com.google.common.base.Preconditions.checkNotNull; |
| 38 | import static javax.ws.rs.core.Response.Status.BAD_REQUEST; |
Shravan Ambati | 7d19954 | 2016-04-22 16:09:05 -0700 | [diff] [blame] | 39 | |
| 40 | /** |
| 41 | * Rest Interfaces for subscribing/unsubscribing to event notifications. |
| 42 | */ |
| 43 | @Path("kafkaService") |
| 44 | public class EventExporterWebResource extends AbstractWebResource { |
| 45 | |
| 46 | private final Logger log = LoggerFactory.getLogger(getClass()); |
Shravan Ambati | bb6b445 | 2016-05-04 13:25:28 -0700 | [diff] [blame] | 47 | 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 Agarwal | cb4a3db | 2016-07-14 11:42:48 -0700 | [diff] [blame] | 54 | "Event Registration successful"; |
Sanjana Agarwal | eb9f0c5 | 2016-06-07 11:10:34 -0700 | [diff] [blame] | 55 | public static final String EVENT_SUBSCRIPTION_UNSUCCESSFUL = |
| 56 | "Event subscription unsuccessful"; |
Shravan Ambati | bb6b445 | 2016-05-04 13:25:28 -0700 | [diff] [blame] | 57 | public static final String EVENT_SUBSCRIPTION_REMOVED = |
Sanjana Agarwal | cb4a3db | 2016-07-14 11:42:48 -0700 | [diff] [blame] | 58 | "Event De-Registration successful"; |
| 59 | |
Shravan Ambati | 7d19954 | 2016-04-22 16:09:05 -0700 | [diff] [blame] | 60 | /** |
Shravan Ambati | bb6b445 | 2016-05-04 13:25:28 -0700 | [diff] [blame] | 61 | * Registers a listener for ONOS Events. |
Shravan Ambati | 7d19954 | 2016-04-22 16:09:05 -0700 | [diff] [blame] | 62 | * |
| 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 Agarwal | cb4a3db | 2016-07-14 11:42:48 -0700 | [diff] [blame] | 74 | EventSubscriptionService service = get(EventSubscriptionService.class); |
Shravan Ambati | 7d19954 | 2016-04-22 16:09:05 -0700 | [diff] [blame] | 75 | |
| 76 | EventSubscriberGroupId groupId = service.registerListener(appName); |
| 77 | |
| 78 | log.info("Registered app {}", appName); |
Shravan Ambati | 7d19954 | 2016-04-22 16:09:05 -0700 | [diff] [blame] | 79 | // 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 Ambati | bb6b445 | 2016-05-04 13:25:28 -0700 | [diff] [blame] | 85 | * Unregisters a listener for ONOS Events. |
Shravan Ambati | 7d19954 | 2016-04-22 16:09:05 -0700 | [diff] [blame] | 86 | * |
| 87 | * @param appName The application trying to unregister |
| 88 | * @return 200 OK |
| 89 | * @onos.rsModel KafkaRegistration |
| 90 | */ |
| 91 | @DELETE |
Shravan Ambati | 7d19954 | 2016-04-22 16:09:05 -0700 | [diff] [blame] | 92 | @Path("unregister") |
| 93 | public Response removeKafkaListener(String appName) { |
Sanjana Agarwal | cb4a3db | 2016-07-14 11:42:48 -0700 | [diff] [blame] | 94 | EventSubscriptionService service = get(EventSubscriptionService.class); |
Shravan Ambati | 7d19954 | 2016-04-22 16:09:05 -0700 | [diff] [blame] | 95 | |
| 96 | service.unregisterListener(appName); |
Sanjana Agarwal | eb9f0c5 | 2016-06-07 11:10:34 -0700 | [diff] [blame] | 97 | log.info("Unregistered app {}", appName); |
Shravan Ambati | 7d19954 | 2016-04-22 16:09:05 -0700 | [diff] [blame] | 98 | return ok(DEREGISTRATION_SUCCESSFUL).build(); |
| 99 | } |
| 100 | |
| 101 | /** |
Shravan Ambati | bb6b445 | 2016-05-04 13:25:28 -0700 | [diff] [blame] | 102 | * Creates subscription to a specific ONOS event. |
Shravan Ambati | 7d19954 | 2016-04-22 16:09:05 -0700 | [diff] [blame] | 103 | * |
Shravan Ambati | bb6b445 | 2016-05-04 13:25:28 -0700 | [diff] [blame] | 104 | * @param input Subscription Data in JSON format |
Shravan Ambati | 7d19954 | 2016-04-22 16:09:05 -0700 | [diff] [blame] | 105 | * @return 200 OK if successful or 400 BAD REQUEST |
| 106 | * @onos.rsModel KafkaSubscription |
| 107 | */ |
| 108 | @POST |
Sanjana Agarwal | eb9f0c5 | 2016-06-07 11:10:34 -0700 | [diff] [blame] | 109 | @Consumes(MediaType.APPLICATION_JSON) |
Shravan Ambati | 7d19954 | 2016-04-22 16:09:05 -0700 | [diff] [blame] | 110 | @Produces(MediaType.APPLICATION_JSON) |
| 111 | @Path("subscribe") |
| 112 | public Response subscribe(InputStream input) { |
| 113 | |
Sanjana Agarwal | cb4a3db | 2016-07-14 11:42:48 -0700 | [diff] [blame] | 114 | EventSubscriptionService service = get(EventSubscriptionService.class); |
Shravan Ambati | 7d19954 | 2016-04-22 16:09:05 -0700 | [diff] [blame] | 115 | |
| 116 | try { |
| 117 | EventSubscriber sub = parseSubscriptionData(input); |
| 118 | service.subscribe(sub); |
Sanjana Agarwal | cb4a3db | 2016-07-14 11:42:48 -0700 | [diff] [blame] | 119 | // It will subscribe to all the topics. Not only the one that is sent by the consumer. |
Shravan Ambati | 7d19954 | 2016-04-22 16:09:05 -0700 | [diff] [blame] | 120 | } 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 Ambati | bb6b445 | 2016-05-04 13:25:28 -0700 | [diff] [blame] | 129 | * Parses JSON Subscription Data from the external application. |
Shravan Ambati | 7d19954 | 2016-04-22 16:09:05 -0700 | [diff] [blame] | 130 | * |
Shravan Ambati | bb6b445 | 2016-05-04 13:25:28 -0700 | [diff] [blame] | 131 | * @param input Subscription Data in JSON format |
Shravan Ambati | 7d19954 | 2016-04-22 16:09:05 -0700 | [diff] [blame] | 132 | * @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 Ambati | 7d19954 | 2016-04-22 16:09:05 -0700 | [diff] [blame] | 140 | checkNotNull(node, JSON_NOT_NULL); |
Sanjana Agarwal | eb9f0c5 | 2016-06-07 11:10:34 -0700 | [diff] [blame] | 141 | EventSubscriber codec = codec(EventSubscriber.class).decode(node, this); |
| 142 | checkNotNull(codec, JSON_NOT_NULL); |
| 143 | return codec; |
Shravan Ambati | 7d19954 | 2016-04-22 16:09:05 -0700 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | /** |
Shravan Ambati | bb6b445 | 2016-05-04 13:25:28 -0700 | [diff] [blame] | 147 | * Deletes subscription from a specific ONOS event. |
Shravan Ambati | 7d19954 | 2016-04-22 16:09:05 -0700 | [diff] [blame] | 148 | * |
Shravan Ambati | bb6b445 | 2016-05-04 13:25:28 -0700 | [diff] [blame] | 149 | * @param input data in JSON format |
Shravan Ambati | 7d19954 | 2016-04-22 16:09:05 -0700 | [diff] [blame] | 150 | * @return 200 OK if successful or 400 BAD REQUEST |
| 151 | * @onos.rsModel KafkaSubscription |
| 152 | */ |
| 153 | @DELETE |
Jian Li | c2a542b | 2016-05-10 11:48:19 -0700 | [diff] [blame] | 154 | @Consumes(MediaType.APPLICATION_JSON) |
Shravan Ambati | 7d19954 | 2016-04-22 16:09:05 -0700 | [diff] [blame] | 155 | @Path("unsubscribe") |
| 156 | public Response unsubscribe(InputStream input) { |
| 157 | |
Sanjana Agarwal | cb4a3db | 2016-07-14 11:42:48 -0700 | [diff] [blame] | 158 | EventSubscriptionService service = get(EventSubscriptionService.class); |
Shravan Ambati | 7d19954 | 2016-04-22 16:09:05 -0700 | [diff] [blame] | 159 | |
| 160 | try { |
| 161 | EventSubscriber sub = parseSubscriptionData(input); |
Shravan Ambati | bb6b445 | 2016-05-04 13:25:28 -0700 | [diff] [blame] | 162 | service.unsubscribe(sub); |
Shravan Ambati | 7d19954 | 2016-04-22 16:09:05 -0700 | [diff] [blame] | 163 | } 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 | } |