2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017-2019, 2021 AT&T Intellectual Property. All rights reserved.
6 * Modifications Copyright (C) 2019 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;
37 import org.onap.policy.models.sim.dmaap.rest.DmaapSimRestServer;
39 @NoArgsConstructor(access = AccessLevel.PRIVATE)
40 public final class Util {
41 public static final String AAISIM_SERVER_NAME = "aaiSim";
42 public static final String SOSIM_SERVER_NAME = "soSim";
43 public static final String VFCSIM_SERVER_NAME = "vfcSim";
44 public static final String GUARDSIM_SERVER_NAME = "guardSim";
45 public static final String SDNCSIM_SERVER_NAME = "sdncSim";
47 public static final int AAISIM_SERVER_PORT = 6666;
48 public static final int SOSIM_SERVER_PORT = 6667;
49 public static final int VFCSIM_SERVER_PORT = 6668;
50 public static final int GUARDSIM_SERVER_PORT = 6669;
51 public static final int SDNCSIM_SERVER_PORT = 6670;
52 public static final int CDSSIM_SERVER_PORT = 6671;
53 public static final int DMAAPSIM_SERVER_PORT = 3904;
55 private static final String CANNOT_PROCESS_PARAMETERS = "cannot parse parameters ";
56 private static final String CANNOT_CONNECT = "cannot connect to port ";
57 public static final String LOCALHOST = "localhost";
60 * Build an A&AI simulator.
62 * @return the simulator
63 * @throws InterruptedException if a thread is interrupted
65 public static HttpServletServer buildAaiSim() throws InterruptedException {
66 final HttpServletServer testServer = HttpServletServerFactoryInstance.getServerFactory()
67 .build(AAISIM_SERVER_NAME, LOCALHOST, AAISIM_SERVER_PORT, "/", false, true);
68 testServer.addServletClass("/*", AaiSimulatorJaxRs.class.getName());
69 testServer.waitedStart(5000);
70 waitForServerToListen(testServer.getPort());
75 * Build a CDS simulator.
77 * @return the simulator
78 * @throws InterruptedException if a thread is interrupted
79 * @throws IOException if an I/O error occurs
81 public static CdsSimulator buildCdsSim() throws InterruptedException, IOException {
82 final var testServer = new CdsSimulator(LOCALHOST, CDSSIM_SERVER_PORT);
84 waitForServerToListen(testServer.getPort());
89 * Build an SDNC simulator.
91 * @return the simulator
92 * @throws InterruptedException if a thread is interrupted
94 public static HttpServletServer buildSdncSim() throws InterruptedException {
95 final HttpServletServer testServer = HttpServletServerFactoryInstance.getServerFactory()
96 .build(SDNCSIM_SERVER_NAME, LOCALHOST, SDNCSIM_SERVER_PORT, "/", false, true);
97 testServer.addServletClass("/*", SdncSimulatorJaxRs.class.getName());
98 testServer.waitedStart(5000);
99 waitForServerToListen(testServer.getPort());
105 * Build an SO simulator.
107 * @return the simulator
108 * @throws InterruptedException if a thread is interrupted
110 public static HttpServletServer buildSoSim() throws InterruptedException {
111 final HttpServletServer testServer = HttpServletServerFactoryInstance.getServerFactory()
112 .build(SOSIM_SERVER_NAME, LOCALHOST, SOSIM_SERVER_PORT, "/", false, true);
113 testServer.addServletClass("/*", SoSimulatorJaxRs.class.getName());
114 testServer.waitedStart(5000);
115 waitForServerToListen(testServer.getPort());
120 * Build a VFC simulator.
122 * @return the simulator
123 * @throws InterruptedException if a thread is interrupted
125 public static HttpServletServer buildVfcSim() throws InterruptedException {
126 final HttpServletServer testServer = HttpServletServerFactoryInstance.getServerFactory()
127 .build(VFCSIM_SERVER_NAME, LOCALHOST, VFCSIM_SERVER_PORT, "/", false, true);
128 testServer.addServletClass("/*", VfcSimulatorJaxRs.class.getName());
129 testServer.waitedStart(5000);
130 waitForServerToListen(testServer.getPort());
135 * Build a guard simulator.
137 * @return the simulator
138 * @throws InterruptedException if a thread is interrupted
140 public static HttpServletServer buildGuardSim() throws InterruptedException {
141 HttpServletServer testServer = HttpServletServerFactoryInstance.getServerFactory().build(GUARDSIM_SERVER_NAME,
142 LOCALHOST, GUARDSIM_SERVER_PORT, "/", false, true);
143 testServer.addServletClass("/*", GuardSimulatorJaxRs.class.getName());
144 testServer.waitedStart(5000);
145 waitForServerToListen(testServer.getPort());
150 * Build a DMaaP simulator.
152 * @return the simulator
153 * @throws InterruptedException if a thread is interrupted
155 public static HttpServletServer buildDmaapSim() throws InterruptedException {
156 var json = ResourceUtils.getResourceAsString("org/onap/policy/simulators/dmaap/DmaapParameters.json");
157 DmaapSimParameterGroup params = null;
159 params = new StandardCoder().decode(json, DmaapSimParameterGroup.class);
160 } catch (CoderException ce) {
161 throw new ParameterRuntimeException(
162 CANNOT_PROCESS_PARAMETERS + "org/onap/policy/simulators/dmaap/DmaapParameters.json", ce);
165 DmaapSimProvider.setInstance(new DmaapSimProvider(params));
167 var props = DmaapSimRestServer.getServerProperties(params.getRestServerParameters());
169 final String svcpfx = PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES + "."
170 + params.getRestServerParameters().getName();
171 props.setProperty(svcpfx + PolicyEndPointProperties.PROPERTY_HTTP_PORT_SUFFIX,
172 Integer.toString(DMAAPSIM_SERVER_PORT));
173 props.setProperty(svcpfx + PolicyEndPointProperties.PROPERTY_MANAGED_SUFFIX, "true");
175 HttpServletServer testServer = HttpServletServerFactoryInstance.getServerFactory().build(props).get(0);
176 testServer.waitedStart(5000);
177 waitForServerToListen(testServer.getPort());
181 private static void waitForServerToListen(int port) throws InterruptedException {
182 if (!NetworkUtil.isTcpPortOpen(LOCALHOST, port, 200, 250L)) {
183 throw new IllegalStateException(CANNOT_CONNECT + port);