2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017-2019 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 java.util.Properties;
26 import org.onap.policy.common.endpoints.http.server.HttpServletServer;
27 import org.onap.policy.common.endpoints.http.server.HttpServletServerFactoryInstance;
28 import org.onap.policy.common.endpoints.properties.PolicyEndPointProperties;
29 import org.onap.policy.common.gson.GsonMessageBodyHandler;
30 import org.onap.policy.common.utils.coder.CoderException;
31 import org.onap.policy.common.utils.coder.StandardCoder;
32 import org.onap.policy.common.utils.network.NetworkUtil;
33 import org.onap.policy.common.utils.resources.ResourceUtils;
34 import org.onap.policy.models.sim.dmaap.parameters.DmaapSimParameterGroup;
35 import org.onap.policy.models.sim.dmaap.provider.DmaapSimProvider;
36 import org.onap.policy.models.sim.dmaap.rest.DmaapSimRestServer;
39 public static final String AAISIM_SERVER_NAME = "aaiSim";
40 public static final String SOSIM_SERVER_NAME = "soSim";
41 public static final String VFCSIM_SERVER_NAME = "vfcSim";
42 public static final String GUARDSIM_SERVER_NAME = "guardSim";
43 public static final String SDNCSIM_SERVER_NAME = "sdncSim";
45 public static final int AAISIM_SERVER_PORT = 6666;
46 public static final int SOSIM_SERVER_PORT = 6667;
47 public static final int VFCSIM_SERVER_PORT = 6668;
48 public static final int GUARDSIM_SERVER_PORT = 6669;
49 public static final int SDNCSIM_SERVER_PORT = 6670;
50 public static final int DMAAPSIM_SERVER_PORT = 3904;
52 private static final String CANNOT_CONNECT = "cannot connect to port ";
53 private static final String LOCALHOST = "localhost";
56 // Prevent instantiation of thic class
60 * Build an A&AI simulator.
62 * @return the simulator
63 * @throws InterruptedException if a thread is interrupted
64 * @throws IOException if an IO errror occurs
66 public static HttpServletServer buildAaiSim() throws InterruptedException, IOException {
67 final HttpServletServer testServer = HttpServletServerFactoryInstance.getServerFactory()
68 .build(AAISIM_SERVER_NAME, LOCALHOST, AAISIM_SERVER_PORT, "/", false, true);
69 testServer.addServletClass("/*", AaiSimulatorJaxRs.class.getName());
70 testServer.waitedStart(5000);
71 if (!NetworkUtil.isTcpPortOpen(LOCALHOST, testServer.getPort(), 5, 10000L)) {
72 throw new IllegalStateException(CANNOT_CONNECT + testServer.getPort());
78 * Build an SDNC simulator.
80 * @return the simulator
81 * @throws InterruptedException if a thread is interrupted
82 * @throws IOException if an IO errror occurs
84 public static HttpServletServer buildSdncSim() throws InterruptedException, IOException {
85 final HttpServletServer testServer = HttpServletServerFactoryInstance.getServerFactory()
86 .build(SDNCSIM_SERVER_NAME, LOCALHOST, SDNCSIM_SERVER_PORT, "/", false, true);
87 testServer.addServletClass("/*", SdncSimulatorJaxRs.class.getName());
88 testServer.waitedStart(5000);
89 if (!NetworkUtil.isTcpPortOpen(LOCALHOST, testServer.getPort(), 5, 10000L)) {
90 throw new IllegalStateException(CANNOT_CONNECT + testServer.getPort());
97 * Build an SO simulator.
99 * @return the simulator
100 * @throws InterruptedException if a thread is interrupted
101 * @throws IOException if an IO errror occurs
103 public static HttpServletServer buildSoSim() throws InterruptedException, IOException {
104 final HttpServletServer testServer = HttpServletServerFactoryInstance.getServerFactory()
105 .build(SOSIM_SERVER_NAME, LOCALHOST, SOSIM_SERVER_PORT, "/", false, true);
106 testServer.addServletClass("/*", SoSimulatorJaxRs.class.getName());
107 testServer.waitedStart(5000);
108 if (!NetworkUtil.isTcpPortOpen(LOCALHOST, testServer.getPort(), 5, 10000L)) {
109 throw new IllegalStateException(CANNOT_CONNECT + testServer.getPort());
115 * Build a VFC simulator.
117 * @return the simulator
118 * @throws InterruptedException if a thread is interrupted
119 * @throws IOException if an IO errror occurs
121 public static HttpServletServer buildVfcSim() throws InterruptedException, IOException {
122 final HttpServletServer testServer = HttpServletServerFactoryInstance.getServerFactory()
123 .build(VFCSIM_SERVER_NAME, LOCALHOST, VFCSIM_SERVER_PORT, "/", false, true);
124 testServer.addServletClass("/*", VfcSimulatorJaxRs.class.getName());
125 testServer.waitedStart(5000);
126 if (!NetworkUtil.isTcpPortOpen(LOCALHOST, testServer.getPort(), 5, 10000L)) {
127 throw new IllegalStateException(CANNOT_CONNECT + testServer.getPort());
133 * Build a guard simulator.
135 * @return the simulator
136 * @throws InterruptedException if a thread is interrupted
137 * @throws IOException if an IO errror occurs
139 public static HttpServletServer buildGuardSim() throws InterruptedException, IOException {
140 HttpServletServer testServer = HttpServletServerFactoryInstance.getServerFactory().build(GUARDSIM_SERVER_NAME,
141 LOCALHOST, GUARDSIM_SERVER_PORT, "/", false, true);
142 testServer.setSerializationProvider(GsonMessageBodyHandler.class.getName());
143 testServer.addServletClass("/*", GuardSimulatorJaxRs.class.getName());
144 testServer.waitedStart(5000);
145 if (!NetworkUtil.isTcpPortOpen(LOCALHOST, testServer.getPort(), 5, 10000L)) {
146 throw new IllegalStateException(CANNOT_CONNECT + testServer.getPort());
152 * Build a DMaaP simulator.
154 * @return the simulator
155 * @throws InterruptedException if a thread is interrupted
156 * @throws IOException if an IO errror occurs
157 * @throws CoderException if the server parameters cannot be loaded
159 public static HttpServletServer buildDmaapSim() throws InterruptedException, IOException, CoderException {
160 String json = ResourceUtils.getResourceAsString("org/onap/policy/simulators/dmaap/DmaapParameters.json");
161 DmaapSimParameterGroup params = new StandardCoder().decode(json, DmaapSimParameterGroup.class);
163 DmaapSimProvider.setInstance(new DmaapSimProvider(params));
165 Properties props = DmaapSimRestServer.getServerProperties(params.getRestServerParameters());
167 final String svcpfx = PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES + "."
168 + params.getRestServerParameters().getName();
169 props.setProperty(svcpfx + PolicyEndPointProperties.PROPERTY_HTTP_PORT_SUFFIX,
170 Integer.toString(DMAAPSIM_SERVER_PORT));
171 props.setProperty(svcpfx + PolicyEndPointProperties.PROPERTY_MANAGED_SUFFIX, "true");
173 HttpServletServer testServer = HttpServletServerFactoryInstance.getServerFactory().build(props).get(0);
174 testServer.waitedStart(5000);
175 if (!NetworkUtil.isTcpPortOpen(LOCALHOST, testServer.getPort(), 50, 1000L)) {
176 throw new IllegalStateException(CANNOT_CONNECT + testServer.getPort());