Merge "Fix sonars in policy models"
[policy/models.git] / models-interactions / model-simulators / src / main / java / org / onap / policy / simulators / Util.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * simulators
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
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
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=========================================================
20  */
21
22 package org.onap.policy.simulators;
23
24 import java.io.IOException;
25 import org.onap.policy.common.endpoints.http.server.HttpServletServer;
26 import org.onap.policy.common.endpoints.http.server.HttpServletServerFactoryInstance;
27 import org.onap.policy.common.endpoints.properties.PolicyEndPointProperties;
28 import org.onap.policy.common.parameters.ParameterRuntimeException;
29 import org.onap.policy.common.utils.coder.CoderException;
30 import org.onap.policy.common.utils.coder.StandardCoder;
31 import org.onap.policy.common.utils.network.NetworkUtil;
32 import org.onap.policy.common.utils.resources.ResourceUtils;
33 import org.onap.policy.models.sim.dmaap.parameters.DmaapSimParameterGroup;
34 import org.onap.policy.models.sim.dmaap.provider.DmaapSimProvider;
35 import org.onap.policy.models.sim.dmaap.rest.DmaapSimRestServer;
36
37 public class Util {
38     public static final String AAISIM_SERVER_NAME = "aaiSim";
39     public static final String SOSIM_SERVER_NAME = "soSim";
40     public static final String VFCSIM_SERVER_NAME = "vfcSim";
41     public static final String GUARDSIM_SERVER_NAME = "guardSim";
42     public static final String SDNCSIM_SERVER_NAME = "sdncSim";
43
44     public static final int AAISIM_SERVER_PORT = 6666;
45     public static final int SOSIM_SERVER_PORT = 6667;
46     public static final int VFCSIM_SERVER_PORT = 6668;
47     public static final int GUARDSIM_SERVER_PORT = 6669;
48     public static final int SDNCSIM_SERVER_PORT = 6670;
49     public static final int CDSSIM_SERVER_PORT = 6671;
50     public static final int DMAAPSIM_SERVER_PORT = 3904;
51
52     private static final String CANNOT_PROCESS_PARAMETERS = "cannot parse parameters ";
53     private static final String CANNOT_CONNECT = "cannot connect to port ";
54     public static final String LOCALHOST = "localhost";
55
56     private Util() {
57         // Prevent instantiation of this class
58     }
59
60     /**
61      * Build an A&AI simulator.
62      *
63      * @return the simulator
64      * @throws InterruptedException if a thread is interrupted
65      */
66     public static HttpServletServer buildAaiSim() throws InterruptedException {
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         waitForServerToListen(testServer.getPort());
72         return testServer;
73     }
74
75     /**
76      * Build a CDS simulator.
77      *
78      * @return the simulator
79      * @throws InterruptedException if a thread is interrupted
80      * @throws IOException if an I/O error occurs
81      */
82     public static CdsSimulator buildCdsSim() throws InterruptedException, IOException {
83         final var testServer = new CdsSimulator(LOCALHOST, CDSSIM_SERVER_PORT);
84         testServer.start();
85         waitForServerToListen(testServer.getPort());
86         return testServer;
87     }
88
89     /**
90      * Build an SDNC simulator.
91      *
92      * @return the simulator
93      * @throws InterruptedException if a thread is interrupted
94      */
95     public static HttpServletServer buildSdncSim() throws InterruptedException {
96         final HttpServletServer testServer = HttpServletServerFactoryInstance.getServerFactory()
97                 .build(SDNCSIM_SERVER_NAME, LOCALHOST, SDNCSIM_SERVER_PORT, "/", false, true);
98         testServer.addServletClass("/*", SdncSimulatorJaxRs.class.getName());
99         testServer.waitedStart(5000);
100         waitForServerToListen(testServer.getPort());
101         return testServer;
102     }
103
104
105     /**
106      * Build an SO simulator.
107      *
108      * @return the simulator
109      * @throws InterruptedException if a thread is interrupted
110      */
111     public static HttpServletServer buildSoSim() throws InterruptedException {
112         final HttpServletServer testServer = HttpServletServerFactoryInstance.getServerFactory()
113                 .build(SOSIM_SERVER_NAME, LOCALHOST, SOSIM_SERVER_PORT, "/", false, true);
114         testServer.addServletClass("/*", SoSimulatorJaxRs.class.getName());
115         testServer.waitedStart(5000);
116         waitForServerToListen(testServer.getPort());
117         return testServer;
118     }
119
120     /**
121      * Build a VFC simulator.
122      *
123      * @return the simulator
124      * @throws InterruptedException if a thread is interrupted
125      */
126     public static HttpServletServer buildVfcSim() throws InterruptedException {
127         final HttpServletServer testServer = HttpServletServerFactoryInstance.getServerFactory()
128                 .build(VFCSIM_SERVER_NAME, LOCALHOST, VFCSIM_SERVER_PORT, "/", false, true);
129         testServer.addServletClass("/*", VfcSimulatorJaxRs.class.getName());
130         testServer.waitedStart(5000);
131         waitForServerToListen(testServer.getPort());
132         return testServer;
133     }
134
135     /**
136      * Build a guard simulator.
137      *
138      * @return the simulator
139      * @throws InterruptedException if a thread is interrupted
140      */
141     public static HttpServletServer buildGuardSim() throws InterruptedException {
142         HttpServletServer testServer = HttpServletServerFactoryInstance.getServerFactory().build(GUARDSIM_SERVER_NAME,
143                 LOCALHOST, GUARDSIM_SERVER_PORT, "/", false, true);
144         testServer.addServletClass("/*", GuardSimulatorJaxRs.class.getName());
145         testServer.waitedStart(5000);
146         waitForServerToListen(testServer.getPort());
147         return testServer;
148     }
149
150     /**
151      * Build a DMaaP simulator.
152      *
153      * @return the simulator
154      * @throws InterruptedException if a thread is interrupted
155      */
156     public static HttpServletServer buildDmaapSim() throws InterruptedException {
157         var json = ResourceUtils.getResourceAsString("org/onap/policy/simulators/dmaap/DmaapParameters.json");
158         DmaapSimParameterGroup params = null;
159         try {
160             params = new StandardCoder().decode(json, DmaapSimParameterGroup.class);
161         } catch (CoderException ce) {
162             throw new ParameterRuntimeException(
163                     CANNOT_PROCESS_PARAMETERS + "org/onap/policy/simulators/dmaap/DmaapParameters.json", ce);
164         }
165
166         DmaapSimProvider.setInstance(new DmaapSimProvider(params));
167
168         var props = DmaapSimRestServer.getServerProperties(params.getRestServerParameters());
169
170         final String svcpfx = PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES + "."
171                 + params.getRestServerParameters().getName();
172         props.setProperty(svcpfx + PolicyEndPointProperties.PROPERTY_HTTP_PORT_SUFFIX,
173                 Integer.toString(DMAAPSIM_SERVER_PORT));
174         props.setProperty(svcpfx + PolicyEndPointProperties.PROPERTY_MANAGED_SUFFIX, "true");
175
176         HttpServletServer testServer = HttpServletServerFactoryInstance.getServerFactory().build(props).get(0);
177         testServer.waitedStart(5000);
178         waitForServerToListen(testServer.getPort());
179         return testServer;
180     }
181
182     private static void waitForServerToListen(int port) throws InterruptedException {
183         if (!NetworkUtil.isTcpPortOpen(LOCALHOST, port, 200, 250L)) {
184             throw new IllegalStateException(CANNOT_CONNECT + port);
185         }
186     }
187 }