2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017-2019, 2021 AT&T Intellectual Property. All rights reserved.
6 * Modifications Copyright (C) 2019, 2023-2024 Nordix Foundation.
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.simulators;
24 import java.io.IOException;
25 import lombok.AccessLevel;
26 import lombok.NoArgsConstructor;
27 import org.onap.policy.common.endpoints.http.server.HttpServletServer;
28 import org.onap.policy.common.endpoints.http.server.HttpServletServerFactoryInstance;
29 import org.onap.policy.common.utils.network.NetworkUtil;
31 @NoArgsConstructor(access = AccessLevel.PRIVATE)
32 public final class Util {
33 public static final String AAISIM_SERVER_NAME = "aaiSim";
34 public static final String SOSIM_SERVER_NAME = "soSim";
35 public static final String VFCSIM_SERVER_NAME = "vfcSim";
36 public static final String XACMLSIM_SERVER_NAME = "xacmlSim";
37 public static final String SDNCSIM_SERVER_NAME = "sdncSim";
39 public static final int AAISIM_SERVER_PORT = 6666;
40 public static final int SOSIM_SERVER_PORT = 6667;
41 public static final int VFCSIM_SERVER_PORT = 6668;
42 public static final int XACMLSIM_SERVER_PORT = 6669;
43 public static final int SDNCSIM_SERVER_PORT = 6670;
44 public static final int CDSSIM_SERVER_PORT = 6671;
46 private static final String CANNOT_PROCESS_PARAMETERS = "cannot parse parameters ";
47 private static final String CANNOT_CONNECT = "cannot connect to port ";
48 public static final String LOCALHOST = "localhost";
51 * Build an A&AI simulator.
53 * @return the simulator
54 * @throws InterruptedException if a thread is interrupted
56 public static HttpServletServer buildAaiSim() throws InterruptedException {
57 final HttpServletServer testServer = HttpServletServerFactoryInstance.getServerFactory()
58 .build(AAISIM_SERVER_NAME, LOCALHOST, AAISIM_SERVER_PORT, "/", false, true);
59 testServer.addServletClass("/*", AaiSimulatorJaxRs.class.getName());
60 testServer.waitedStart(5000);
61 waitForServerToListen(testServer.getPort());
66 * Build a CDS simulator.
68 * @return the simulator
69 * @throws InterruptedException if a thread is interrupted
70 * @throws IOException if an I/O error occurs
72 public static CdsSimulator buildCdsSim() throws InterruptedException, IOException {
73 final var testServer = new CdsSimulator(LOCALHOST, CDSSIM_SERVER_PORT);
75 waitForServerToListen(testServer.getPort());
80 * Build an SDNC simulator.
82 * @return the simulator
83 * @throws InterruptedException if a thread is interrupted
85 public static HttpServletServer buildSdncSim() throws InterruptedException {
86 final HttpServletServer testServer = HttpServletServerFactoryInstance.getServerFactory()
87 .build(SDNCSIM_SERVER_NAME, LOCALHOST, SDNCSIM_SERVER_PORT, "/", false, true);
88 testServer.addServletClass("/*", SdncSimulatorJaxRs.class.getName());
89 testServer.waitedStart(5000);
90 waitForServerToListen(testServer.getPort());
96 * Build a SO simulator.
98 * @return the simulator
99 * @throws InterruptedException if a thread is interrupted
101 public static HttpServletServer buildSoSim() throws InterruptedException {
102 final HttpServletServer testServer = HttpServletServerFactoryInstance.getServerFactory()
103 .build(SOSIM_SERVER_NAME, LOCALHOST, SOSIM_SERVER_PORT, "/", false, true);
104 testServer.addServletClass("/*", SoSimulatorJaxRs.class.getName());
105 testServer.waitedStart(5000);
106 waitForServerToListen(testServer.getPort());
111 * Build a VFC simulator.
113 * @return the simulator
114 * @throws InterruptedException if a thread is interrupted
116 public static HttpServletServer buildVfcSim() throws InterruptedException {
117 final HttpServletServer testServer = HttpServletServerFactoryInstance.getServerFactory()
118 .build(VFCSIM_SERVER_NAME, LOCALHOST, VFCSIM_SERVER_PORT, "/", false, true);
119 testServer.addServletClass("/*", VfcSimulatorJaxRs.class.getName());
120 testServer.waitedStart(5000);
121 waitForServerToListen(testServer.getPort());
126 * Build a guard simulator.
128 * @return the simulator
129 * @throws InterruptedException if a thread is interrupted
131 public static HttpServletServer buildGuardSim() throws InterruptedException {
132 return buildXacmlSim();
136 * Build a xacml simulator.
138 * @return the simulator
139 * @throws InterruptedException if a thread is interrupted
141 public static HttpServletServer buildXacmlSim() throws InterruptedException {
142 HttpServletServer testServer = HttpServletServerFactoryInstance.getServerFactory().build(XACMLSIM_SERVER_NAME,
143 LOCALHOST, XACMLSIM_SERVER_PORT, "/", false, true);
144 testServer.addServletClass("/*", XacmlSimulatorJaxRs.class.getName());
145 testServer.waitedStart(5000);
146 waitForServerToListen(testServer.getPort());
150 private static void waitForServerToListen(int port) throws InterruptedException {
151 if (!NetworkUtil.isTcpPortOpen(LOCALHOST, port, 200, 250L)) {
152 throw new IllegalStateException(CANNOT_CONNECT + port);