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