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; |
Shravan Ambati | 5a11e17 | 2016-07-21 15:55:28 -0700 | [diff] [blame] | 21 | import org.onosproject.kafkaintegration.api.dto.RegistrationResponse; |
Sanjana Agarwal | cb4a3db | 2016-07-14 11:42:48 -0700 | [diff] [blame] | 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 | |
Jian Li | c2a542b | 2016-05-10 11:48:19 -0700 | [diff] [blame] | 26 | import javax.ws.rs.Consumes; |
| 27 | import javax.ws.rs.DELETE; |
| 28 | import javax.ws.rs.POST; |
| 29 | import javax.ws.rs.Path; |
| 30 | import javax.ws.rs.Produces; |
| 31 | import javax.ws.rs.core.MediaType; |
| 32 | import javax.ws.rs.core.Response; |
Sanjana Agarwal | cb4a3db | 2016-07-14 11:42:48 -0700 | [diff] [blame] | 33 | import java.io.IOException; |
| 34 | import java.io.InputStream; |
Jian Li | c2a542b | 2016-05-10 11:48:19 -0700 | [diff] [blame] | 35 | |
Sanjana Agarwal | cb4a3db | 2016-07-14 11:42:48 -0700 | [diff] [blame] | 36 | import static com.google.common.base.Preconditions.checkNotNull; |
| 37 | import static javax.ws.rs.core.Response.Status.BAD_REQUEST; |
Shravan Ambati | 7d19954 | 2016-04-22 16:09:05 -0700 | [diff] [blame] | 38 | |
| 39 | /** |
| 40 | * Rest Interfaces for subscribing/unsubscribing to event notifications. |
| 41 | */ |
| 42 | @Path("kafkaService") |
| 43 | public class EventExporterWebResource extends AbstractWebResource { |
| 44 | |
| 45 | private final Logger log = LoggerFactory.getLogger(getClass()); |
Shravan Ambati | bb6b445 | 2016-05-04 13:25:28 -0700 | [diff] [blame] | 46 | 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 Agarwal | cb4a3db | 2016-07-14 11:42:48 -0700 | [diff] [blame] | 53 | "Event Registration successful"; |
Sanjana Agarwal | eb9f0c5 | 2016-06-07 11:10:34 -0700 | [diff] [blame] | 54 | public static final String EVENT_SUBSCRIPTION_UNSUCCESSFUL = |
| 55 | "Event subscription unsuccessful"; |
Shravan Ambati | bb6b445 | 2016-05-04 13:25:28 -0700 | [diff] [blame] | 56 | public static final String EVENT_SUBSCRIPTION_REMOVED = |
Sanjana Agarwal | cb4a3db | 2016-07-14 11:42:48 -0700 | [diff] [blame] | 57 | "Event De-Registration successful"; |
| 58 | |
Shravan Ambati | 7d19954 | 2016-04-22 16:09:05 -0700 | [diff] [blame] | 59 | /** |
Shravan Ambati | bb6b445 | 2016-05-04 13:25:28 -0700 | [diff] [blame] | 60 | * Registers a listener for ONOS Events. |
Shravan Ambati | 7d19954 | 2016-04-22 16:09:05 -0700 | [diff] [blame] | 61 | * |
| 62 | * @param appName The application trying to register |
| 63 | * @return 200 OK with UUID string which should be used as Kafka Consumer |
Shravan Ambati | 5a11e17 | 2016-07-21 15:55:28 -0700 | [diff] [blame] | 64 | * Group Id and Kafka Server, port information. |
Shravan Ambati | 7d19954 | 2016-04-22 16:09:05 -0700 | [diff] [blame] | 65 | * @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 Agarwal | cb4a3db | 2016-07-14 11:42:48 -0700 | [diff] [blame] | 73 | EventSubscriptionService service = get(EventSubscriptionService.class); |
Shravan Ambati | 7d19954 | 2016-04-22 16:09:05 -0700 | [diff] [blame] | 74 | |
Shravan Ambati | 5a11e17 | 2016-07-21 15:55:28 -0700 | [diff] [blame] | 75 | 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 Ambati | 7d19954 | 2016-04-22 16:09:05 -0700 | [diff] [blame] | 81 | |
| 82 | log.info("Registered app {}", appName); |
Shravan Ambati | 5a11e17 | 2016-07-21 15:55:28 -0700 | [diff] [blame] | 83 | |
| 84 | return ok(result.toString()).build(); |
Shravan Ambati | 7d19954 | 2016-04-22 16:09:05 -0700 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | /** |
Shravan Ambati | bb6b445 | 2016-05-04 13:25:28 -0700 | [diff] [blame] | 88 | * Unregisters a listener for ONOS Events. |
Shravan Ambati | 7d19954 | 2016-04-22 16:09:05 -0700 | [diff] [blame] | 89 | * |
| 90 | * @param appName The application trying to unregister |
| 91 | * @return 200 OK |
| 92 | * @onos.rsModel KafkaRegistration |
| 93 | */ |
| 94 | @DELETE |
Shravan Ambati | 7d19954 | 2016-04-22 16:09:05 -0700 | [diff] [blame] | 95 | @Path("unregister") |
| 96 | public Response removeKafkaListener(String appName) { |
Sanjana Agarwal | cb4a3db | 2016-07-14 11:42:48 -0700 | [diff] [blame] | 97 | EventSubscriptionService service = get(EventSubscriptionService.class); |
Shravan Ambati | 7d19954 | 2016-04-22 16:09:05 -0700 | [diff] [blame] | 98 | |
| 99 | service.unregisterListener(appName); |
Sanjana Agarwal | eb9f0c5 | 2016-06-07 11:10:34 -0700 | [diff] [blame] | 100 | log.info("Unregistered app {}", appName); |
Shravan Ambati | 7d19954 | 2016-04-22 16:09:05 -0700 | [diff] [blame] | 101 | return ok(DEREGISTRATION_SUCCESSFUL).build(); |
| 102 | } |
| 103 | |
| 104 | /** |
Shravan Ambati | bb6b445 | 2016-05-04 13:25:28 -0700 | [diff] [blame] | 105 | * Creates subscription to a specific ONOS event. |
Shravan Ambati | 7d19954 | 2016-04-22 16:09:05 -0700 | [diff] [blame] | 106 | * |
Shravan Ambati | bb6b445 | 2016-05-04 13:25:28 -0700 | [diff] [blame] | 107 | * @param input Subscription Data in JSON format |
Shravan Ambati | 7d19954 | 2016-04-22 16:09:05 -0700 | [diff] [blame] | 108 | * @return 200 OK if successful or 400 BAD REQUEST |
| 109 | * @onos.rsModel KafkaSubscription |
| 110 | */ |
| 111 | @POST |
Sanjana Agarwal | eb9f0c5 | 2016-06-07 11:10:34 -0700 | [diff] [blame] | 112 | @Consumes(MediaType.APPLICATION_JSON) |
Shravan Ambati | 7d19954 | 2016-04-22 16:09:05 -0700 | [diff] [blame] | 113 | @Produces(MediaType.APPLICATION_JSON) |
| 114 | @Path("subscribe") |
| 115 | public Response subscribe(InputStream input) { |
| 116 | |
Sanjana Agarwal | cb4a3db | 2016-07-14 11:42:48 -0700 | [diff] [blame] | 117 | EventSubscriptionService service = get(EventSubscriptionService.class); |
Shravan Ambati | 7d19954 | 2016-04-22 16:09:05 -0700 | [diff] [blame] | 118 | |
| 119 | try { |
| 120 | EventSubscriber sub = parseSubscriptionData(input); |
| 121 | service.subscribe(sub); |
Sanjana Agarwal | cb4a3db | 2016-07-14 11:42:48 -0700 | [diff] [blame] | 122 | // 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] | 123 | } 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 Ambati | bb6b445 | 2016-05-04 13:25:28 -0700 | [diff] [blame] | 132 | * Parses JSON Subscription Data from the external application. |
Shravan Ambati | 7d19954 | 2016-04-22 16:09:05 -0700 | [diff] [blame] | 133 | * |
Shravan Ambati | bb6b445 | 2016-05-04 13:25:28 -0700 | [diff] [blame] | 134 | * @param input Subscription Data in JSON format |
Shravan Ambati | 7d19954 | 2016-04-22 16:09:05 -0700 | [diff] [blame] | 135 | * @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 Ambati | 7d19954 | 2016-04-22 16:09:05 -0700 | [diff] [blame] | 143 | checkNotNull(node, JSON_NOT_NULL); |
Sanjana Agarwal | eb9f0c5 | 2016-06-07 11:10:34 -0700 | [diff] [blame] | 144 | EventSubscriber codec = codec(EventSubscriber.class).decode(node, this); |
| 145 | checkNotNull(codec, JSON_NOT_NULL); |
| 146 | return codec; |
Shravan Ambati | 7d19954 | 2016-04-22 16:09:05 -0700 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | /** |
Shravan Ambati | bb6b445 | 2016-05-04 13:25:28 -0700 | [diff] [blame] | 150 | * Deletes subscription from a specific ONOS event. |
Shravan Ambati | 7d19954 | 2016-04-22 16:09:05 -0700 | [diff] [blame] | 151 | * |
Shravan Ambati | bb6b445 | 2016-05-04 13:25:28 -0700 | [diff] [blame] | 152 | * @param input data in JSON format |
Shravan Ambati | 7d19954 | 2016-04-22 16:09:05 -0700 | [diff] [blame] | 153 | * @return 200 OK if successful or 400 BAD REQUEST |
| 154 | * @onos.rsModel KafkaSubscription |
| 155 | */ |
| 156 | @DELETE |
Jian Li | c2a542b | 2016-05-10 11:48:19 -0700 | [diff] [blame] | 157 | @Consumes(MediaType.APPLICATION_JSON) |
Shravan Ambati | 7d19954 | 2016-04-22 16:09:05 -0700 | [diff] [blame] | 158 | @Path("unsubscribe") |
| 159 | public Response unsubscribe(InputStream input) { |
| 160 | |
Sanjana Agarwal | cb4a3db | 2016-07-14 11:42:48 -0700 | [diff] [blame] | 161 | EventSubscriptionService service = get(EventSubscriptionService.class); |
Shravan Ambati | 7d19954 | 2016-04-22 16:09:05 -0700 | [diff] [blame] | 162 | |
| 163 | try { |
| 164 | EventSubscriber sub = parseSubscriptionData(input); |
Shravan Ambati | bb6b445 | 2016-05-04 13:25:28 -0700 | [diff] [blame] | 165 | service.unsubscribe(sub); |
Shravan Ambati | 7d19954 | 2016-04-22 16:09:05 -0700 | [diff] [blame] | 166 | } 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 | } |