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
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.examples.grpc;
23 import static org.awaitility.Awaitility.await;
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;
38 * The Class GrpcTestRestSimEndpoint creates rest server endpoints for simulating sending/receiving events on DMaaP.
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();
47 * DCAE input of events (simulation of DMaaP). This input event triggers the policy
49 * @param timeout the timeout to wait for
50 * @return the response
51 * @throws IOException on I/O errors
53 @Path("events/unauthenticated.DCAE_CL_OUTPUT/APEX/1")
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);
61 await().pollDelay(4, TimeUnit.SECONDS)
62 .atMost(5, TimeUnit.SECONDS)
64 return Response.status(200).entity(createSubscriptionRequest).build();
68 * Post to Policy management log (Simulation of DMaaP).
70 * @param jsonString the json string
71 * @return the response
73 @Path("/events/POLICY_CL_MGT")
75 public Response policyLogRequest(final String jsonString) {
76 LOGGER.info("\n*** POLICY LOG ENTRY START ***\n {} \n *** POLICY LOG ENTRY END ***", jsonString);
78 loggedOutputEvent += jsonString + "\n";
80 return Response.status(200).build();
84 * Get the logged event for test verification.
86 * @return the response
88 @Path("/event/getLoggedEvent")
90 public Response getDetails() {
93 loggedEvent = loggedOutputEvent;
95 if (null == loggedEvent) {
96 return Response.status(500).entity("Error: Log event not yet generated.").build();
98 return Response.status(200).entity(loggedEvent).build();