Java 17 Upgrade
[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, 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
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 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
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";
45
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;
53
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";
57
58     /**
59      * Build an A&AI simulator.
60      *
61      * @throws InterruptedException if a thread is interrupted
62      */
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());
69     }
70
71     /**
72      * Build a CDS simulator.
73      *
74      * @return the simulator
75      * @throws InterruptedException if a thread is interrupted
76      * @throws IOException if an I/O error occurs
77      */
78     public static CdsSimulator buildCdsSim() throws InterruptedException, IOException {
79         final var testServer = new CdsSimulator(LOCALHOST, CDSSIM_SERVER_PORT);
80         testServer.start();
81         waitForServerToListen(testServer.getPort());
82         return testServer;
83     }
84
85     /**
86      * Build an SDNC simulator.
87      *
88      * @throws InterruptedException if a thread is interrupted
89      */
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());
96     }
97
98
99     /**
100      * Build an SO simulator.
101      *
102      * @throws InterruptedException if a thread is interrupted
103      */
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());
110     }
111
112     /**
113      * Build a VFC simulator.
114      *
115      * @throws InterruptedException if a thread is interrupted
116      */
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());
123     }
124
125     /**
126      * Build a xacml simulator.
127      *
128      * @throws InterruptedException if a thread is interrupted
129      */
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());
136     }
137
138     /**
139      * Builds an unauthenticated HTTP DMaaP simulator.
140      *
141      * @return the simulator
142      * @throws InterruptedException if a thread is interrupted
143      */
144     public static HttpServletServer buildDmaapSim() throws InterruptedException {
145         return buildDmaapSim("org/onap/policy/simulators/dmaap/DmaapParameters.json");
146     }
147
148     /**
149      * Build a DMaaP simulator from a properties file.
150      *
151      * @param resourceName the name of the properties file
152      * @return the simulator
153      * @throws InterruptedException if a thread is interrupted
154      */
155     public static HttpServletServer buildDmaapSim(String resourceName) throws InterruptedException {
156         var json = ResourceUtils.getResourceAsString(resourceName);
157         DmaapSimParameterGroup params;
158         try {
159             params = new StandardCoder().decode(json, DmaapSimParameterGroup.class);
160         } catch (CoderException ce) {
161             throw new ParameterRuntimeException(
162                     CANNOT_PROCESS_PARAMETERS + resourceName, ce);
163         }
164
165         DmaapSimProvider.setInstance(new DmaapSimProvider(params));
166
167         var props = params.getRestServerParameters().getServerProperties();
168
169         final String svcpfx = PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES + "."
170                 + params.getRestServerParameters().getName();
171         props.setProperty(svcpfx + PolicyEndPointProperties.PROPERTY_MANAGED_SUFFIX, "true");
172
173         HttpServletServer testServer = HttpServletServerFactoryInstance.getServerFactory().build(props).get(0);
174         testServer.waitedStart(5000);
175         waitForServerToListen(testServer.getPort());
176         return testServer;
177     }
178
179     private static void waitForServerToListen(int port) throws InterruptedException {
180         if (!NetworkUtil.isTcpPortOpen(LOCALHOST, port, 200, 250L)) {
181             throw new IllegalStateException(CANNOT_CONNECT + port);
182         }
183     }
184 }