f8524fcfd2f4eab9ba8a7d8a753281f0996c441b
[policy/apex-pdp.git] /
1 /*-
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
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.apex.plugins.event.carrier.restserver;
22
23 import java.util.LinkedHashMap;
24 import java.util.Map;
25
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;
35
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
38
39 /**
40  * The Class RestServerEndpoint is the end point servlet class for handling REST requests and responses to and from
41  * Apex.
42  *
43  * @author Liam Fallon (liam.fallon@ericsson.com)
44  */
45 @Path("/{eventInput}")
46 @Produces({ MediaType.APPLICATION_JSON })
47 @Consumes({ MediaType.APPLICATION_JSON })
48 public class RestServerEndpoint {
49     // Get a reference to the logger
50     private static final Logger LOGGER = LoggerFactory.getLogger(RestServerEndpoint.class);
51
52     // Statistics on the amount of HTTP messages handled
53     private static int getMessagesReceived = 0;
54     private static int postEventMessagesReceived = 0;
55     private static int putEventMessagesReceived = 0;
56
57     // This map is used to hold all the REST server event inputs. This is used to determine which consumer to send input
58     // events to
59     private static Map<String, ApexRestServerConsumer> consumerMap =
60             new LinkedHashMap<>();
61
62     // The ID of this event input. This gets injected from the URL.
63     @PathParam("eventInput")
64     private String eventInputId = null;
65
66     /**
67      * Register an Apex consumer with the REST server end point.
68      *
69      * @param consumerEventInputId The event input ID that indicates this consumer shoud be used
70      * @param consumer The consumer to register
71      */
72     public static void registerApexRestServerConsumer(final String consumerEventInputId,
73             final ApexRestServerConsumer consumer) {
74         consumerMap.put(consumerEventInputId, consumer);
75     }
76
77     /**
78      * Get statistics on apex REST event handling.
79      *
80      * @return the response
81      */
82     @Path("/Status")
83     @GET
84     public Response serviceGetStats() {
85         getMessagesReceived++;
86         return Response.status(Response.Status.OK.getStatusCode())
87                 .entity("{\n" + "\"INPUTS\": \"" + consumerMap.keySet() + "\",\n" + "\"STAT\": " + getMessagesReceived
88                         + ",\n" + "\"POST\": " + postEventMessagesReceived + ",\n" + "\"PUT\":  "
89                         + putEventMessagesReceived + "\n}")
90                 .build();
91     }
92
93     /**
94      * Service post request, an incoming event over RETS to Apex.
95      *
96      * @param jsonString the JSON string containing the data coming in on the REST call
97      * @return the response event to the request
98      */
99     @Path("/EventIn")
100     @POST
101     public Response servicePostRequest(final String jsonString) {
102         postEventMessagesReceived++;
103
104         if (LOGGER.isDebugEnabled()) {
105             String message = "event input " + eventInputId + ", received POST of event \"" + jsonString + "\"";
106             LOGGER.debug(message);
107         }
108
109         // Common handler method for POST and PUT requests
110         return handleEvent(jsonString);
111     }
112
113     /**
114      * Service put request, an incoming event over RETS to Apex.
115      *
116      * @param jsonString the JSON string containing the data coming in on the REST call
117      * @return the response event to the request
118      */
119     @Path("/EventIn")
120     @PUT
121     public Response servicePutRequest(final String jsonString) {
122         putEventMessagesReceived++;
123
124         if (LOGGER.isDebugEnabled()) {
125             String message = "event input \"" + eventInputId + "\", received PUT of event \"" + jsonString + "\"";
126             LOGGER.debug(message);
127         }
128
129         // Common handler method for POST and PUT requests
130         return handleEvent(jsonString);
131     }
132
133     /**
134      * Common event handler for events received on POST and PUT messages.
135      *
136      * @param jsonString the JSON string containing the data coming in on the REST call
137      * @return the response event to the request
138      */
139     private Response handleEvent(final String jsonString) {
140         // Find the correct consumer for this REST message
141         final ApexRestServerConsumer eventConsumer = consumerMap.get(eventInputId);
142         if (eventConsumer == null) {
143             final String errorMessage =
144                     "event input " + eventInputId + " is not defined in the Apex configuration file";
145             LOGGER.warn(errorMessage);
146             return Response.status(Response.Status.BAD_REQUEST.getStatusCode())
147                     .entity("{'errorMessage', '" + errorMessage + "'}").build();
148         }
149
150         return eventConsumer.receiveEvent(jsonString);
151     }
152 }