2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2020,2023 Nordix Foundation.
4 * Modifications Copyright (C) 2020 AT&T Intellectual Property. All rights reserved.
5 * ================================================================================
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.apex.examples.grpc;
24 import java.io.IOException;
25 import org.onap.policy.common.endpoints.http.server.HttpServletServer;
26 import org.onap.policy.common.endpoints.http.server.HttpServletServerFactoryInstance;
27 import org.onap.policy.common.gson.GsonMessageBodyHandler;
28 import org.onap.policy.common.utils.network.NetworkUtil;
29 import org.onap.policy.simulators.CdsSimulator;
32 * The Class GrpcTestServerSim that manages test servers for REST and gRPC requests for the test.
34 public class GrpcTestServerSim {
35 private static final String HOST = "localhost";
36 private HttpServletServer restServer;
37 private CdsSimulator grpcServer;
40 * Instantiates a new REST simulator for DMaaP requests.
42 * @throws InterruptedException interrupted exception
43 * @throws IOException io exception
45 public GrpcTestServerSim() throws InterruptedException, IOException {
46 int restServerPort = 54321;
47 restServer = HttpServletServerFactoryInstance.getServerFactory().build("GrpcTestRestSimEndpoint", false, HOST,
48 restServerPort, false, "/GrpcTestRestSim", false, false);
49 restServer.addServletClass(null, GrpcTestRestSimEndpoint.class.getName());
50 restServer.setSerializationProvider(GsonMessageBodyHandler.class.getName());
52 if (!NetworkUtil.isTcpPortOpen(HOST, restServerPort, 50, 200L)) {
53 throw new IllegalStateException("port " + restServerPort + " is still not in use");
56 int grpcServerPort = 54322;
57 grpcServer = new CdsSimulator(HOST, grpcServerPort);
59 if (!NetworkUtil.isTcpPortOpen(HOST, grpcServerPort, 50, 200L)) {
60 throw new IllegalStateException("port " + grpcServerPort + " is still not in use");
67 public void tearDown() {
68 if (restServer != null) {
71 if (grpcServer != null) {