155107bbd07540e43183f3b0b64b8e2dc31a8f24
[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 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 = "localhost";
57
58     /**
59      * Build an A&AI simulator.
60      *
61      * @return the simulator
62      * @throws InterruptedException if a thread is interrupted
63      */
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());
70         return testServer;
71     }
72
73     /**
74      * Build a CDS simulator.
75      *
76      * @return the simulator
77      * @throws InterruptedException if a thread is interrupted
78      * @throws IOException if an I/O error occurs
79      */
80     public static CdsSimulator buildCdsSim() throws InterruptedException, IOException {
81         final var testServer = new CdsSimulator(LOCALHOST, CDSSIM_SERVER_PORT);
82         testServer.start();
83         waitForServerToListen(testServer.getPort());
84         return testServer;
85     }
86
87     /**
88      * Build an SDNC simulator.
89      *
90      * @return the simulator
91      * @throws InterruptedException if a thread is interrupted
92      */
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());
99         return testServer;
100     }
101
102
103     /**
104      * Build an SO simulator.
105      *
106      * @return the simulator
107      * @throws InterruptedException if a thread is interrupted
108      */
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());
115         return testServer;
116     }
117
118     /**
119      * Build a VFC simulator.
120      *
121      * @return the simulator
122      * @throws InterruptedException if a thread is interrupted
123      */
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());
130         return testServer;
131     }
132
133     /**
134      * Build a guard simulator.
135      *
136      * @return the simulator
137      * @throws InterruptedException if a thread is interrupted
138      */
139     public static HttpServletServer buildGuardSim() throws InterruptedException {
140         return buildXacmlSim();
141     }
142
143     /**
144      * Build a xacml simulator.
145      *
146      * @return the simulator
147      * @throws InterruptedException if a thread is interrupted
148      */
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());
155         return testServer;
156     }
157
158     /**
159      * Builds an unauthenticated HTTP DMaaP simulator.
160      *
161      * @return the simulator
162      * @throws InterruptedException if a thread is interrupted
163      */
164     public static HttpServletServer buildDmaapSim() throws InterruptedException {
165         return buildDmaapSim("org/onap/policy/simulators/dmaap/DmaapParameters.json");
166     }
167
168     /**
169      * Build a DMaaP simulator from a properties file.
170      *
171      * @param resourceName the name of the properties file
172      * @return the simulator
173      * @throws InterruptedException if a thread is interrupted
174      */
175     public static HttpServletServer buildDmaapSim(String resourceName) throws InterruptedException {
176         var json = ResourceUtils.getResourceAsString(resourceName);
177         DmaapSimParameterGroup params = null;
178         try {
179             params = new StandardCoder().decode(json, DmaapSimParameterGroup.class);
180         } catch (CoderException ce) {
181             throw new ParameterRuntimeException(
182                     CANNOT_PROCESS_PARAMETERS + resourceName, ce);
183         }
184
185         DmaapSimProvider.setInstance(new DmaapSimProvider(params));
186
187         var props = params.getRestServerParameters().getServerProperties();
188
189         final String svcpfx = PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES + "."
190                 + params.getRestServerParameters().getName();
191         props.setProperty(svcpfx + PolicyEndPointProperties.PROPERTY_MANAGED_SUFFIX, "true");
192
193         HttpServletServer testServer = HttpServletServerFactoryInstance.getServerFactory().build(props).get(0);
194         testServer.waitedStart(5000);
195         waitForServerToListen(testServer.getPort());
196         return testServer;
197     }
198
199     private static void waitForServerToListen(int port) throws InterruptedException {
200         if (!NetworkUtil.isTcpPortOpen(LOCALHOST, port, 200, 250L)) {
201             throw new IllegalStateException(CANNOT_CONNECT + port);
202         }
203     }
204 }