2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017-2019, 2021 AT&T Intellectual Property. All rights reserved.
6 * Modifications Copyright (C) 2019, 2023 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.endpoints.properties.PolicyEndPointProperties;
30 import org.onap.policy.common.parameters.ParameterRuntimeException;
31 import org.onap.policy.common.utils.coder.CoderException;
32 import org.onap.policy.common.utils.coder.StandardCoder;
33 import org.onap.policy.common.utils.network.NetworkUtil;
34 import org.onap.policy.common.utils.resources.ResourceUtils;
35 import org.onap.policy.models.sim.dmaap.parameters.DmaapSimParameterGroup;
36 import org.onap.policy.models.sim.dmaap.provider.DmaapSimProvider;
38 @NoArgsConstructor(access = AccessLevel.PRIVATE)
39 public final class Util {
40 public static final String AAISIM_SERVER_NAME = "aaiSim";
41 public static final String SOSIM_SERVER_NAME = "soSim";
42 public static final String VFCSIM_SERVER_NAME = "vfcSim";
43 public static final String XACMLSIM_SERVER_NAME = "xacmlSim";
44 public static final String SDNCSIM_SERVER_NAME = "sdncSim";
46 public static final int AAISIM_SERVER_PORT = 6666;
47 public static final int SOSIM_SERVER_PORT = 6667;
48 public static final int VFCSIM_SERVER_PORT = 6668;
49 public static final int XACMLSIM_SERVER_PORT = 6669;
50 public static final int SDNCSIM_SERVER_PORT = 6670;
51 public static final int CDSSIM_SERVER_PORT = 6671;
52 public static final int DMAAPSIM_SERVER_PORT = 3904;
54 private static final String CANNOT_PROCESS_PARAMETERS = "cannot parse parameters ";
55 private static final String CANNOT_CONNECT = "cannot connect to port ";
56 public static final String LOCALHOST = "127.0.0.1";
59 * Build an A&AI simulator.
61 * @throws InterruptedException if a thread is interrupted
63 public static void buildAaiSim() throws InterruptedException {
64 final HttpServletServer testServer = HttpServletServerFactoryInstance.getServerFactory()
65 .build(AAISIM_SERVER_NAME, LOCALHOST, AAISIM_SERVER_PORT, "/", false, true);
66 testServer.addServletClass("/*", AaiSimulatorJaxRs.class.getName());
67 testServer.waitedStart(5000);
68 waitForServerToListen(testServer.getPort());
72 * Build a CDS simulator.
74 * @return the simulator
75 * @throws InterruptedException if a thread is interrupted
76 * @throws IOException if an I/O error occurs
78 public static CdsSimulator buildCdsSim() throws InterruptedException, IOException {
79 final var testServer = new CdsSimulator(LOCALHOST, CDSSIM_SERVER_PORT);
81 waitForServerToListen(testServer.getPort());
86 * Build an SDNC simulator.
88 * @throws InterruptedException if a thread is interrupted
90 public static void buildSdncSim() throws InterruptedException {
91 final HttpServletServer testServer = HttpServletServerFactoryInstance.getServerFactory()
92 .build(SDNCSIM_SERVER_NAME, LOCALHOST, SDNCSIM_SERVER_PORT, "/", false, true);
93 testServer.addServletClass("/*", SdncSimulatorJaxRs.class.getName());
94 testServer.waitedStart(5000);
95 waitForServerToListen(testServer.getPort());
100 * Build an SO simulator.
102 * @throws InterruptedException if a thread is interrupted
104 public static void buildSoSim() throws InterruptedException {
105 final HttpServletServer testServer = HttpServletServerFactoryInstance.getServerFactory()
106 .build(SOSIM_SERVER_NAME, LOCALHOST, SOSIM_SERVER_PORT, "/", false, true);
107 testServer.addServletClass("/*", SoSimulatorJaxRs.class.getName());
108 testServer.waitedStart(5000);
109 waitForServerToListen(testServer.getPort());
113 * Build a VFC simulator.
115 * @throws InterruptedException if a thread is interrupted
117 public static void buildVfcSim() throws InterruptedException {
118 final HttpServletServer testServer = HttpServletServerFactoryInstance.getServerFactory()
119 .build(VFCSIM_SERVER_NAME, LOCALHOST, VFCSIM_SERVER_PORT, "/", false, true);
120 testServer.addServletClass("/*", VfcSimulatorJaxRs.class.getName());
121 testServer.waitedStart(5000);
122 waitForServerToListen(testServer.getPort());
126 * Build a xacml simulator.
128 * @throws InterruptedException if a thread is interrupted
130 public static void buildXacmlSim() throws InterruptedException {
131 HttpServletServer testServer = HttpServletServerFactoryInstance.getServerFactory().build(XACMLSIM_SERVER_NAME,
132 LOCALHOST, XACMLSIM_SERVER_PORT, "/", false, true);
133 testServer.addServletClass("/*", XacmlSimulatorJaxRs.class.getName());
134 testServer.waitedStart(5000);
135 waitForServerToListen(testServer.getPort());
139 * Builds an unauthenticated HTTP DMaaP simulator.
141 * @return the simulator
142 * @throws InterruptedException if a thread is interrupted
144 public static HttpServletServer buildDmaapSim() throws InterruptedException {
145 return buildDmaapSim("org/onap/policy/simulators/dmaap/DmaapParameters.json");
149 * Build a DMaaP simulator from a properties file.
151 * @param resourceName the name of the properties file
152 * @return the simulator
153 * @throws InterruptedException if a thread is interrupted
155 public static HttpServletServer buildDmaapSim(String resourceName) throws InterruptedException {
156 var json = ResourceUtils.getResourceAsString(resourceName);
157 DmaapSimParameterGroup params;
159 params = new StandardCoder().decode(json, DmaapSimParameterGroup.class);
160 } catch (CoderException ce) {
161 throw new ParameterRuntimeException(
162 CANNOT_PROCESS_PARAMETERS + resourceName, ce);
165 DmaapSimProvider.setInstance(new DmaapSimProvider(params));
167 var props = params.getRestServerParameters().getServerProperties();
169 final String svcpfx = PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES + "."
170 + params.getRestServerParameters().getName();
171 props.setProperty(svcpfx + PolicyEndPointProperties.PROPERTY_MANAGED_SUFFIX, "true");
173 HttpServletServer testServer = HttpServletServerFactoryInstance.getServerFactory().build(props).get(0);
174 testServer.waitedStart(5000);
175 waitForServerToListen(testServer.getPort());
179 private static void waitForServerToListen(int port) throws InterruptedException {
180 if (!NetworkUtil.isTcpPortOpen(LOCALHOST, port, 200, 250L)) {
181 throw new IllegalStateException(CANNOT_CONNECT + port);