4e88afe83b6208af75a42d354e0b2e043f6e4199
[policy/apex-pdp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2020 Nordix Foundation.
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.examples.grpc;
22
23 import java.io.IOException;
24 import java.nio.file.Files;
25 import java.nio.file.Paths;
26 import javax.ws.rs.GET;
27 import javax.ws.rs.POST;
28 import javax.ws.rs.Path;
29 import javax.ws.rs.QueryParam;
30 import javax.ws.rs.core.Response;
31 import org.slf4j.ext.XLogger;
32 import org.slf4j.ext.XLoggerFactory;
33
34 /**
35  * The Class GrpcTestRestSimEndpoint creates rest server endpoints for simulating sending/receiving events on DMaaP.
36  */
37 @Path("/sim")
38 public class GrpcTestRestSimEndpoint {
39     private static final XLogger LOGGER = XLoggerFactory.getXLogger(GrpcTestRestSimEndpoint.class);
40     private static String loggedOutputEvent = null;
41     private final Object lock = new Object();
42
43     /**
44      * DCAE input of events (simulation of DMaaP). This input event triggers the policy
45      *
46      * @param timeout the timeout to wait for
47      * @return the response
48      * @throws IOException on I/O errors
49      */
50     @Path("events/unauthenticated.DCAE_CL_OUTPUT/APEX/1")
51     @GET
52     public Response dcaeClOutput(@QueryParam("timeout") final int timeout) throws IOException {
53         String createSubscriptionRequest =
54             Files.readString(Paths.get("src/main/resources/examples/events/APEXgRPC/CreateSubscriptionEvent.json"));
55         LOGGER.info("Create subscription request received: \n {}", createSubscriptionRequest);
56
57         return Response.status(200).entity(createSubscriptionRequest).build();
58     }
59
60     /**
61      * Post to Policy management log (Simulation of DMaaP).
62      *
63      * @param jsonString the json string
64      * @return the response
65      */
66     @Path("/events/POLICY_CL_MGT")
67     @POST
68     public Response policyLogRequest(final String jsonString) {
69         LOGGER.info("\n*** POLICY LOG ENTRY START ***\n {} \n *** POLICY LOG ENTRY END ***", jsonString);
70         synchronized (lock) {
71             loggedOutputEvent = jsonString;
72         }
73         return Response.status(200).build();
74     }
75
76     /**
77      * Get the logged event for test verification.
78      *
79      * @return the response
80      */
81     @Path("/event/getLoggedEvent")
82     @GET
83     public Response getDetails() {
84         String loggedEvent;
85         synchronized (lock) {
86             loggedEvent = loggedOutputEvent;
87         }
88         if (null == loggedEvent) {
89             return Response.status(500).entity("Error: Log event not yet generated.").build();
90         }
91         return Response.status(200).entity(loggedEvent).build();
92     }
93 }