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; |
Ray Milkey | cde7e47 | 2018-04-02 15:33:07 -0700 | [diff] [blame] | 39 | import static org.onlab.util.Tools.readTreeFromStream; |
Shravan Ambati | 7d19954 | 2016-04-22 16:09:05 -0700 | [diff] [blame] | 40 | |
| 41 | /** |
| 42 | * Rest Interfaces for subscribing/unsubscribing to event notifications. |
| 43 | */ |
| 44 | @Path("kafkaService") |
| 45 | public class EventExporterWebResource extends AbstractWebResource { |
| 46 | |
| 47 | private final Logger log = LoggerFactory.getLogger(getClass()); |
Shravan Ambati | bb6b445 | 2016-05-04 13:25:28 -0700 | [diff] [blame] | 48 | public static final String JSON_NOT_NULL = |
| 49 | "Registration Data cannot be empty"; |
| 50 | public static final String REGISTRATION_SUCCESSFUL = |
| 51 | "Registered Listener successfully"; |
| 52 | public static final String DEREGISTRATION_SUCCESSFUL = |
| 53 | "De-Registered Listener successfully"; |
| 54 | public static final String EVENT_SUBSCRIPTION_SUCCESSFUL = |
Sanjana Agarwal | cb4a3db | 2016-07-14 11:42:48 -0700 | [diff] [blame] | 55 | "Event Registration successful"; |
Sanjana Agarwal | eb9f0c5 | 2016-06-07 11:10:34 -0700 | [diff] [blame] | 56 | public static final String EVENT_SUBSCRIPTION_UNSUCCESSFUL = |
| 57 | "Event subscription unsuccessful"; |
Shravan Ambati | bb6b445 | 2016-05-04 13:25:28 -0700 | [diff] [blame] | 58 | public static final String EVENT_SUBSCRIPTION_REMOVED = |
Sanjana Agarwal | cb4a3db | 2016-07-14 11:42:48 -0700 | [diff] [blame] | 59 | "Event De-Registration successful"; |
| 60 | |
Shravan Ambati | 7d19954 | 2016-04-22 16:09:05 -0700 | [diff] [blame] | 61 | /** |
Shravan Ambati | bb6b445 | 2016-05-04 13:25:28 -0700 | [diff] [blame] | 62 | * Registers a listener for ONOS Events. |
Shravan Ambati | 7d19954 | 2016-04-22 16:09:05 -0700 | [diff] [blame] | 63 | * |
| 64 | * @param appName The application trying to register |
| 65 | * @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] | 66 | * Group Id and Kafka Server, port information. |
Shravan Ambati | 7d19954 | 2016-04-22 16:09:05 -0700 | [diff] [blame] | 67 | * @onos.rsModel KafkaRegistration |
| 68 | */ |
| 69 | @POST |
| 70 | @Produces(MediaType.APPLICATION_JSON) |
| 71 | @Consumes(MediaType.APPLICATION_JSON) |
| 72 | @Path("register") |
| 73 | public Response registerKafkaListener(String appName) { |
| 74 | |
Sanjana Agarwal | cb4a3db | 2016-07-14 11:42:48 -0700 | [diff] [blame] | 75 | EventSubscriptionService service = get(EventSubscriptionService.class); |
Shravan Ambati | 7d19954 | 2016-04-22 16:09:05 -0700 | [diff] [blame] | 76 | |
Shravan Ambati | 5a11e17 | 2016-07-21 15:55:28 -0700 | [diff] [blame] | 77 | RegistrationResponse response = service.registerListener(appName); |
| 78 | |
| 79 | ObjectNode result = mapper().createObjectNode(); |
| 80 | result.put("groupId", response.getGroupId().getId().toString()); |
| 81 | result.put("ipAddress", response.getIpAddress()); |
| 82 | result.put("port", response.getPort()); |
Shravan Ambati | 7d19954 | 2016-04-22 16:09:05 -0700 | [diff] [blame] | 83 | |
| 84 | log.info("Registered app {}", appName); |
Shravan Ambati | 5a11e17 | 2016-07-21 15:55:28 -0700 | [diff] [blame] | 85 | |
| 86 | return ok(result.toString()).build(); |
Shravan Ambati | 7d19954 | 2016-04-22 16:09:05 -0700 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | /** |
Shravan Ambati | bb6b445 | 2016-05-04 13:25:28 -0700 | [diff] [blame] | 90 | * Unregisters a listener for ONOS Events. |
Shravan Ambati | 7d19954 | 2016-04-22 16:09:05 -0700 | [diff] [blame] | 91 | * |
| 92 | * @param appName The application trying to unregister |
| 93 | * @return 200 OK |
| 94 | * @onos.rsModel KafkaRegistration |
| 95 | */ |
| 96 | @DELETE |
Shravan Ambati | 7d19954 | 2016-04-22 16:09:05 -0700 | [diff] [blame] | 97 | @Path("unregister") |
| 98 | public Response removeKafkaListener(String appName) { |
Sanjana Agarwal | cb4a3db | 2016-07-14 11:42:48 -0700 | [diff] [blame] | 99 | EventSubscriptionService service = get(EventSubscriptionService.class); |
Shravan Ambati | 7d19954 | 2016-04-22 16:09:05 -0700 | [diff] [blame] | 100 | |
| 101 | service.unregisterListener(appName); |
Sanjana Agarwal | eb9f0c5 | 2016-06-07 11:10:34 -0700 | [diff] [blame] | 102 | log.info("Unregistered app {}", appName); |
Shravan Ambati | 7d19954 | 2016-04-22 16:09:05 -0700 | [diff] [blame] | 103 | return ok(DEREGISTRATION_SUCCESSFUL).build(); |
| 104 | } |
| 105 | |
| 106 | /** |
Shravan Ambati | bb6b445 | 2016-05-04 13:25:28 -0700 | [diff] [blame] | 107 | * Creates subscription to a specific ONOS event. |
Shravan Ambati | 7d19954 | 2016-04-22 16:09:05 -0700 | [diff] [blame] | 108 | * |
Shravan Ambati | bb6b445 | 2016-05-04 13:25:28 -0700 | [diff] [blame] | 109 | * @param input Subscription Data in JSON format |
Shravan Ambati | 7d19954 | 2016-04-22 16:09:05 -0700 | [diff] [blame] | 110 | * @return 200 OK if successful or 400 BAD REQUEST |
| 111 | * @onos.rsModel KafkaSubscription |
| 112 | */ |
| 113 | @POST |
Sanjana Agarwal | eb9f0c5 | 2016-06-07 11:10:34 -0700 | [diff] [blame] | 114 | @Consumes(MediaType.APPLICATION_JSON) |
Shravan Ambati | 7d19954 | 2016-04-22 16:09:05 -0700 | [diff] [blame] | 115 | @Produces(MediaType.APPLICATION_JSON) |
| 116 | @Path("subscribe") |
| 117 | public Response subscribe(InputStream input) { |
| 118 | |
Sanjana Agarwal | cb4a3db | 2016-07-14 11:42:48 -0700 | [diff] [blame] | 119 | EventSubscriptionService service = get(EventSubscriptionService.class); |
Shravan Ambati | 7d19954 | 2016-04-22 16:09:05 -0700 | [diff] [blame] | 120 | |
| 121 | try { |
| 122 | EventSubscriber sub = parseSubscriptionData(input); |
| 123 | service.subscribe(sub); |
Sanjana Agarwal | cb4a3db | 2016-07-14 11:42:48 -0700 | [diff] [blame] | 124 | // 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] | 125 | } catch (Exception e) { |
| 126 | log.error(e.getMessage()); |
| 127 | return Response.status(BAD_REQUEST).entity(e.getMessage()).build(); |
| 128 | } |
| 129 | |
| 130 | return ok(EVENT_SUBSCRIPTION_SUCCESSFUL).build(); |
| 131 | } |
| 132 | |
| 133 | /** |
Shravan Ambati | bb6b445 | 2016-05-04 13:25:28 -0700 | [diff] [blame] | 134 | * Parses JSON Subscription Data from the external application. |
Shravan Ambati | 7d19954 | 2016-04-22 16:09:05 -0700 | [diff] [blame] | 135 | * |
Shravan Ambati | bb6b445 | 2016-05-04 13:25:28 -0700 | [diff] [blame] | 136 | * @param input Subscription Data in JSON format |
Shravan Ambati | 7d19954 | 2016-04-22 16:09:05 -0700 | [diff] [blame] | 137 | * @return parsed DTO object |
| 138 | * @throws IOException |
| 139 | */ |
| 140 | private EventSubscriber parseSubscriptionData(InputStream input) |
| 141 | throws IOException { |
| 142 | |
| 143 | ObjectMapper mapper = new ObjectMapper(); |
Ray Milkey | cde7e47 | 2018-04-02 15:33:07 -0700 | [diff] [blame] | 144 | ObjectNode node = readTreeFromStream(mapper, input); |
Shravan Ambati | 7d19954 | 2016-04-22 16:09:05 -0700 | [diff] [blame] | 145 | checkNotNull(node, JSON_NOT_NULL); |
Sanjana Agarwal | eb9f0c5 | 2016-06-07 11:10:34 -0700 | [diff] [blame] | 146 | EventSubscriber codec = codec(EventSubscriber.class).decode(node, this); |
| 147 | checkNotNull(codec, JSON_NOT_NULL); |
| 148 | return codec; |
Shravan Ambati | 7d19954 | 2016-04-22 16:09:05 -0700 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | /** |
Shravan Ambati | bb6b445 | 2016-05-04 13:25:28 -0700 | [diff] [blame] | 152 | * Deletes subscription from a specific ONOS event. |
Shravan Ambati | 7d19954 | 2016-04-22 16:09:05 -0700 | [diff] [blame] | 153 | * |
Shravan Ambati | bb6b445 | 2016-05-04 13:25:28 -0700 | [diff] [blame] | 154 | * @param input data in JSON format |
Shravan Ambati | 7d19954 | 2016-04-22 16:09:05 -0700 | [diff] [blame] | 155 | * @return 200 OK if successful or 400 BAD REQUEST |
| 156 | * @onos.rsModel KafkaSubscription |
| 157 | */ |
| 158 | @DELETE |
Jian Li | c2a542b | 2016-05-10 11:48:19 -0700 | [diff] [blame] | 159 | @Consumes(MediaType.APPLICATION_JSON) |
Shravan Ambati | 7d19954 | 2016-04-22 16:09:05 -0700 | [diff] [blame] | 160 | @Path("unsubscribe") |
| 161 | public Response unsubscribe(InputStream input) { |
| 162 | |
Sanjana Agarwal | cb4a3db | 2016-07-14 11:42:48 -0700 | [diff] [blame] | 163 | EventSubscriptionService service = get(EventSubscriptionService.class); |
Shravan Ambati | 7d19954 | 2016-04-22 16:09:05 -0700 | [diff] [blame] | 164 | |
| 165 | try { |
| 166 | EventSubscriber sub = parseSubscriptionData(input); |
Shravan Ambati | bb6b445 | 2016-05-04 13:25:28 -0700 | [diff] [blame] | 167 | service.unsubscribe(sub); |
Shravan Ambati | 7d19954 | 2016-04-22 16:09:05 -0700 | [diff] [blame] | 168 | } catch (Exception e) { |
| 169 | log.error(e.getMessage()); |
| 170 | return Response.status(BAD_REQUEST).entity(e.getMessage()).build(); |
| 171 | } |
| 172 | |
| 173 | return ok(EVENT_SUBSCRIPTION_REMOVED).build(); |
| 174 | } |
| 175 | } |