2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2016-2018 Ericsson. All rights reserved.
4 * ================================================================================
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.apex.plugins.event.carrier.restserver;
23 import java.util.LinkedHashMap;
26 import javax.ws.rs.Consumes;
27 import javax.ws.rs.GET;
28 import javax.ws.rs.POST;
29 import javax.ws.rs.PUT;
30 import javax.ws.rs.Path;
31 import javax.ws.rs.PathParam;
32 import javax.ws.rs.Produces;
33 import javax.ws.rs.core.MediaType;
34 import javax.ws.rs.core.Response;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
40 * The Class RestServerEndpoint is the end point servlet class for handling REST requests and responses to and from
43 * @author Liam Fallon (liam.fallon@ericsson.com)
45 @Path("/{eventInput}")
47 { MediaType.APPLICATION_JSON })
49 { MediaType.APPLICATION_JSON })
50 public class RestServerEndpoint {
51 // Get a reference to the logger
52 private static final Logger LOGGER = LoggerFactory.getLogger(RestServerEndpoint.class);
54 // Statistics on the amount of HTTP messages handled
55 private static int getMessagesReceived = 0;
56 private static int postEventMessagesReceived = 0;
57 private static int putEventMessagesReceived = 0;
59 // This map is used to hold all the REST server event inputs. This is used to determine which consumer to send input
61 private static Map<String, ApexRestServerConsumer> consumerMap = new LinkedHashMap<>();
63 // The ID of this event input. This gets injected from the URL.
64 @PathParam("eventInput")
65 private String eventInputId = null;
68 * Register an Apex consumer with the REST server end point.
70 * @param consumerEventInputId The event input ID that indicates this consumer shoud be used
71 * @param consumer The consumer to register
73 public static void registerApexRestServerConsumer(final String consumerEventInputId,
74 final ApexRestServerConsumer consumer) {
75 consumerMap.put(consumerEventInputId, consumer);
79 * Get statistics on apex REST event handling.
81 * @return the response
85 public Response serviceGetStats() {
86 incrementGetMessages();
87 return Response.status(Response.Status.OK.getStatusCode())
88 .entity("{\n" + "\"INPUTS\": \"" + consumerMap.keySet() + "\",\n" + "\"STAT\": "
89 + getMessagesReceived + ",\n" + "\"POST\": " + postEventMessagesReceived + ",\n"
90 + "\"PUT\": " + putEventMessagesReceived + "\n}")
95 * Service post request, an incoming event over RETS to Apex.
97 * @param jsonString the JSON string containing the data coming in on the REST call
98 * @return the response event to the request
102 public Response servicePostRequest(final String jsonString) {
103 incrementPostEventMessages();
105 if (LOGGER.isDebugEnabled()) {
106 String message = "event input " + eventInputId + ", received POST of event \"" + jsonString + "\"";
107 LOGGER.debug(message);
110 // Common handler method for POST and PUT requests
111 return handleEvent(jsonString);
115 * Service put request, an incoming event over RETS to Apex.
117 * @param jsonString the JSON string containing the data coming in on the REST call
118 * @return the response event to the request
122 public Response servicePutRequest(final String jsonString) {
123 incrementPutEventMessages();
125 if (LOGGER.isDebugEnabled()) {
126 String message = "event input \"" + eventInputId + "\", received PUT of event \"" + jsonString + "\"";
127 LOGGER.debug(message);
130 // Common handler method for POST and PUT requests
131 return handleEvent(jsonString);
135 * Common event handler for events received on POST and PUT messages.
137 * @param jsonString the JSON string containing the data coming in on the REST call
138 * @return the response event to the request
140 private Response handleEvent(final String jsonString) {
141 // Find the correct consumer for this REST message
142 final ApexRestServerConsumer eventConsumer = consumerMap.get(eventInputId);
143 if (eventConsumer == null) {
144 final String errorMessage = "event input " + eventInputId
145 + " is not defined in the Apex configuration file";
146 LOGGER.warn(errorMessage);
147 return Response.status(Response.Status.BAD_REQUEST.getStatusCode())
148 .entity("{'errorMessage', '" + errorMessage + "'}").build();
151 return eventConsumer.receiveEvent(jsonString);
155 * Increment number of get messages received.
157 private static void incrementGetMessages() {
158 getMessagesReceived++;
162 * Increment number of get messages received.
164 private static void incrementPutEventMessages() {
165 putEventMessagesReceived++;
169 * Increment number of get messages received.
171 private static void incrementPostEventMessages() {
172 postEventMessagesReceived++;