Ray Milkey | 2d572dd | 2017-04-14 10:01:24 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
Ray Milkey | 2d572dd | 2017-04-14 10:01:24 -0700 | [diff] [blame] | 3 | * |
Shravan Ambati | 7d19954 | 2016-04-22 16:09:05 -0700 | [diff] [blame] | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
Ray Milkey | 2d572dd | 2017-04-14 10:01:24 -0700 | [diff] [blame] | 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
Shravan Ambati | 7d19954 | 2016-04-22 16:09:05 -0700 | [diff] [blame] | 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | package org.onosproject.kafkaintegration.rest; |
| 17 | |
Sanjana Agarwal | cb4a3db | 2016-07-14 11:42:48 -0700 | [diff] [blame] | 18 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 19 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 20 | import org.onosproject.kafkaintegration.api.EventSubscriptionService; |
| 21 | import org.onosproject.kafkaintegration.api.dto.EventSubscriber; |
Shravan Ambati | 5a11e17 | 2016-07-21 15:55:28 -0700 | [diff] [blame] | 22 | import org.onosproject.kafkaintegration.api.dto.RegistrationResponse; |
Sanjana Agarwal | cb4a3db | 2016-07-14 11:42:48 -0700 | [diff] [blame] | 23 | import org.onosproject.rest.AbstractWebResource; |
| 24 | import org.slf4j.Logger; |
| 25 | import org.slf4j.LoggerFactory; |
Shravan Ambati | bb6b445 | 2016-05-04 13:25:28 -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 |
Shravan Ambati | 5a11e17 | 2016-07-21 15:55:28 -0700 | [diff] [blame] | 65 | * Group Id and Kafka Server, port information. |
Shravan Ambati | 7d19954 | 2016-04-22 16:09:05 -0700 | [diff] [blame] | 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 | |
Shravan Ambati | 5a11e17 | 2016-07-21 15:55:28 -0700 | [diff] [blame] | 76 | RegistrationResponse response = service.registerListener(appName); |
| 77 | |
| 78 | ObjectNode result = mapper().createObjectNode(); |
| 79 | result.put("groupId", response.getGroupId().getId().toString()); |
| 80 | result.put("ipAddress", response.getIpAddress()); |
| 81 | result.put("port", response.getPort()); |
Shravan Ambati | 7d19954 | 2016-04-22 16:09:05 -0700 | [diff] [blame] | 82 | |
| 83 | log.info("Registered app {}", appName); |
Shravan Ambati | 5a11e17 | 2016-07-21 15:55:28 -0700 | [diff] [blame] | 84 | |
| 85 | return ok(result.toString()).build(); |
Shravan Ambati | 7d19954 | 2016-04-22 16:09:05 -0700 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | /** |
Shravan Ambati | bb6b445 | 2016-05-04 13:25:28 -0700 | [diff] [blame] | 89 | * Unregisters a listener for ONOS Events. |
Shravan Ambati | 7d19954 | 2016-04-22 16:09:05 -0700 | [diff] [blame] | 90 | * |
| 91 | * @param appName The application trying to unregister |
| 92 | * @return 200 OK |
| 93 | * @onos.rsModel KafkaRegistration |
| 94 | */ |
| 95 | @DELETE |
Shravan Ambati | 7d19954 | 2016-04-22 16:09:05 -0700 | [diff] [blame] | 96 | @Path("unregister") |
| 97 | public Response removeKafkaListener(String appName) { |
Sanjana Agarwal | cb4a3db | 2016-07-14 11:42:48 -0700 | [diff] [blame] | 98 | EventSubscriptionService service = get(EventSubscriptionService.class); |
Shravan Ambati | 7d19954 | 2016-04-22 16:09:05 -0700 | [diff] [blame] | 99 | |
| 100 | service.unregisterListener(appName); |
Sanjana Agarwal | eb9f0c5 | 2016-06-07 11:10:34 -0700 | [diff] [blame] | 101 | log.info("Unregistered app {}", appName); |
Shravan Ambati | 7d19954 | 2016-04-22 16:09:05 -0700 | [diff] [blame] | 102 | return ok(DEREGISTRATION_SUCCESSFUL).build(); |
| 103 | } |
| 104 | |
| 105 | /** |
Shravan Ambati | bb6b445 | 2016-05-04 13:25:28 -0700 | [diff] [blame] | 106 | * Creates subscription to a specific ONOS event. |
Shravan Ambati | 7d19954 | 2016-04-22 16:09:05 -0700 | [diff] [blame] | 107 | * |
Shravan Ambati | bb6b445 | 2016-05-04 13:25:28 -0700 | [diff] [blame] | 108 | * @param input Subscription Data in JSON format |
Shravan Ambati | 7d19954 | 2016-04-22 16:09:05 -0700 | [diff] [blame] | 109 | * @return 200 OK if successful or 400 BAD REQUEST |
| 110 | * @onos.rsModel KafkaSubscription |
| 111 | */ |
| 112 | @POST |
Sanjana Agarwal | eb9f0c5 | 2016-06-07 11:10:34 -0700 | [diff] [blame] | 113 | @Consumes(MediaType.APPLICATION_JSON) |
Shravan Ambati | 7d19954 | 2016-04-22 16:09:05 -0700 | [diff] [blame] | 114 | @Produces(MediaType.APPLICATION_JSON) |
| 115 | @Path("subscribe") |
| 116 | public Response subscribe(InputStream input) { |
| 117 | |
Sanjana Agarwal | cb4a3db | 2016-07-14 11:42:48 -0700 | [diff] [blame] | 118 | EventSubscriptionService service = get(EventSubscriptionService.class); |
Shravan Ambati | 7d19954 | 2016-04-22 16:09:05 -0700 | [diff] [blame] | 119 | |
| 120 | try { |
| 121 | EventSubscriber sub = parseSubscriptionData(input); |
| 122 | service.subscribe(sub); |
Sanjana Agarwal | cb4a3db | 2016-07-14 11:42:48 -0700 | [diff] [blame] | 123 | // 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] | 124 | } catch (Exception e) { |
| 125 | log.error(e.getMessage()); |
| 126 | return Response.status(BAD_REQUEST).entity(e.getMessage()).build(); |
| 127 | } |
| 128 | |
| 129 | return ok(EVENT_SUBSCRIPTION_SUCCESSFUL).build(); |
| 130 | } |
| 131 | |
| 132 | /** |
Shravan Ambati | bb6b445 | 2016-05-04 13:25:28 -0700 | [diff] [blame] | 133 | * Parses JSON Subscription Data from the external application. |
Shravan Ambati | 7d19954 | 2016-04-22 16:09:05 -0700 | [diff] [blame] | 134 | * |
Shravan Ambati | bb6b445 | 2016-05-04 13:25:28 -0700 | [diff] [blame] | 135 | * @param input Subscription Data in JSON format |
Shravan Ambati | 7d19954 | 2016-04-22 16:09:05 -0700 | [diff] [blame] | 136 | * @return parsed DTO object |
| 137 | * @throws IOException |
| 138 | */ |
| 139 | private EventSubscriber parseSubscriptionData(InputStream input) |
| 140 | throws IOException { |
| 141 | |
| 142 | ObjectMapper mapper = new ObjectMapper(); |
| 143 | ObjectNode node = (ObjectNode) mapper.readTree(input); |
Shravan Ambati | 7d19954 | 2016-04-22 16:09:05 -0700 | [diff] [blame] | 144 | checkNotNull(node, JSON_NOT_NULL); |
Sanjana Agarwal | eb9f0c5 | 2016-06-07 11:10:34 -0700 | [diff] [blame] | 145 | EventSubscriber codec = codec(EventSubscriber.class).decode(node, this); |
| 146 | checkNotNull(codec, JSON_NOT_NULL); |
| 147 | return codec; |
Shravan Ambati | 7d19954 | 2016-04-22 16:09:05 -0700 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | /** |
Shravan Ambati | bb6b445 | 2016-05-04 13:25:28 -0700 | [diff] [blame] | 151 | * Deletes subscription from a specific ONOS event. |
Shravan Ambati | 7d19954 | 2016-04-22 16:09:05 -0700 | [diff] [blame] | 152 | * |
Shravan Ambati | bb6b445 | 2016-05-04 13:25:28 -0700 | [diff] [blame] | 153 | * @param input data in JSON format |
Shravan Ambati | 7d19954 | 2016-04-22 16:09:05 -0700 | [diff] [blame] | 154 | * @return 200 OK if successful or 400 BAD REQUEST |
| 155 | * @onos.rsModel KafkaSubscription |
| 156 | */ |
| 157 | @DELETE |
Jian Li | c2a542b | 2016-05-10 11:48:19 -0700 | [diff] [blame] | 158 | @Consumes(MediaType.APPLICATION_JSON) |
Shravan Ambati | 7d19954 | 2016-04-22 16:09:05 -0700 | [diff] [blame] | 159 | @Path("unsubscribe") |
| 160 | public Response unsubscribe(InputStream input) { |
| 161 | |
Sanjana Agarwal | cb4a3db | 2016-07-14 11:42:48 -0700 | [diff] [blame] | 162 | EventSubscriptionService service = get(EventSubscriptionService.class); |
Shravan Ambati | 7d19954 | 2016-04-22 16:09:05 -0700 | [diff] [blame] | 163 | |
| 164 | try { |
| 165 | EventSubscriber sub = parseSubscriptionData(input); |
Shravan Ambati | bb6b445 | 2016-05-04 13:25:28 -0700 | [diff] [blame] | 166 | service.unsubscribe(sub); |
Shravan Ambati | 7d19954 | 2016-04-22 16:09:05 -0700 | [diff] [blame] | 167 | } catch (Exception e) { |
| 168 | log.error(e.getMessage()); |
| 169 | return Response.status(BAD_REQUEST).entity(e.getMessage()).build(); |
| 170 | } |
| 171 | |
| 172 | return ok(EVENT_SUBSCRIPTION_REMOVED).build(); |
| 173 | } |
| 174 | } |