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;
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 = "localhost";
59 * Build an A&AI simulator.
61 * @return the simulator
62 * @throws InterruptedException if a thread is interrupted
64 public static HttpServletServer buildAaiSim() throws InterruptedException {
65 final HttpServletServer testServer = HttpServletServerFactoryInstance.getServerFactory()
66 .build(AAISIM_SERVER_NAME, LOCALHOST, AAISIM_SERVER_PORT, "/", false, true);
67 testServer.addServletClass("/*", AaiSimulatorJaxRs.class.getName());
68 testServer.waitedStart(5000);
69 waitForServerToListen(testServer.getPort());
74 * Build a CDS simulator.
76 * @return the simulator
77 * @throws InterruptedException if a thread is interrupted
78 * @throws IOException if an I/O error occurs
80 public static CdsSimulator buildCdsSim() throws InterruptedException, IOException {
81 final var testServer = new CdsSimulator(LOCALHOST, CDSSIM_SERVER_PORT);
83 waitForServerToListen(testServer.getPort());
88 * Build an SDNC simulator.
90 * @return the simulator
91 * @throws InterruptedException if a thread is interrupted
93 public static HttpServletServer buildSdncSim() throws InterruptedException {
94 final HttpServletServer testServer = HttpServletServerFactoryInstance.getServerFactory()
95 .build(SDNCSIM_SERVER_NAME, LOCALHOST, SDNCSIM_SERVER_PORT, "/", false, true);
96 testServer.addServletClass("/*", SdncSimulatorJaxRs.class.getName());
97 testServer.waitedStart(5000);
98 waitForServerToListen(testServer.getPort());
104 * Build an SO simulator.
106 * @return the simulator
107 * @throws InterruptedException if a thread is interrupted
109 public static HttpServletServer buildSoSim() throws InterruptedException {
110 final HttpServletServer testServer = HttpServletServerFactoryInstance.getServerFactory()
111 .build(SOSIM_SERVER_NAME, LOCALHOST, SOSIM_SERVER_PORT, "/", false, true);
112 testServer.addServletClass("/*", SoSimulatorJaxRs.class.getName());
113 testServer.waitedStart(5000);
114 waitForServerToListen(testServer.getPort());
119 * Build a VFC simulator.
121 * @return the simulator
122 * @throws InterruptedException if a thread is interrupted
124 public static HttpServletServer buildVfcSim() throws InterruptedException {
125 final HttpServletServer testServer = HttpServletServerFactoryInstance.getServerFactory()
126 .build(VFCSIM_SERVER_NAME, LOCALHOST, VFCSIM_SERVER_PORT, "/", false, true);
127 testServer.addServletClass("/*", VfcSimulatorJaxRs.class.getName());
128 testServer.waitedStart(5000);
129 waitForServerToListen(testServer.getPort());
134 * Build a guard simulator.
136 * @return the simulator
137 * @throws InterruptedException if a thread is interrupted
139 public static HttpServletServer buildGuardSim() throws InterruptedException {
140 return buildXacmlSim();
144 * Build a xacml simulator.
146 * @return the simulator
147 * @throws InterruptedException if a thread is interrupted
149 public static HttpServletServer buildXacmlSim() throws InterruptedException {
150 HttpServletServer testServer = HttpServletServerFactoryInstance.getServerFactory().build(XACMLSIM_SERVER_NAME,
151 LOCALHOST, XACMLSIM_SERVER_PORT, "/", false, true);
152 testServer.addServletClass("/*", XacmlSimulatorJaxRs.class.getName());
153 testServer.waitedStart(5000);
154 waitForServerToListen(testServer.getPort());
159 * Build a DMaaP simulator.
161 * @return the simulator
162 * @throws InterruptedException if a thread is interrupted
164 public static HttpServletServer buildDmaapSim() throws InterruptedException {
165 var json = ResourceUtils.getResourceAsString("org/onap/policy/simulators/dmaap/DmaapParameters.json");
166 DmaapSimParameterGroup params = null;
168 params = new StandardCoder().decode(json, DmaapSimParameterGroup.class);
169 } catch (CoderException ce) {
170 throw new ParameterRuntimeException(
171 CANNOT_PROCESS_PARAMETERS + "org/onap/policy/simulators/dmaap/DmaapParameters.json", ce);
174 DmaapSimProvider.setInstance(new DmaapSimProvider(params));
176 var props = params.getRestServerParameters().getServerProperties();
178 final String svcpfx = PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES + "."
179 + params.getRestServerParameters().getName();
180 props.setProperty(svcpfx + PolicyEndPointProperties.PROPERTY_HTTP_PORT_SUFFIX,
181 Integer.toString(DMAAPSIM_SERVER_PORT));
182 props.setProperty(svcpfx + PolicyEndPointProperties.PROPERTY_MANAGED_SUFFIX, "true");
184 HttpServletServer testServer = HttpServletServerFactoryInstance.getServerFactory().build(props).get(0);
185 testServer.waitedStart(5000);
186 waitForServerToListen(testServer.getPort());
190 private static void waitForServerToListen(int port) throws InterruptedException {
191 if (!NetworkUtil.isTcpPortOpen(LOCALHOST, port, 200, 250L)) {
192 throw new IllegalStateException(CANNOT_CONNECT + port);