Delete extra simulator docker images
[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 GUARDSIM_SERVER_NAME = "guardSim";
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 GUARDSIM_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         HttpServletServer testServer = HttpServletServerFactoryInstance.getServerFactory().build(GUARDSIM_SERVER_NAME,
141                 LOCALHOST, GUARDSIM_SERVER_PORT, "/", false, true);
142         testServer.addServletClass("/*", GuardSimulatorJaxRs.class.getName());
143         testServer.waitedStart(5000);
144         waitForServerToListen(testServer.getPort());
145         return testServer;
146     }
147
148     /**
149      * Build a DMaaP simulator.
150      *
151      * @return the simulator
152      * @throws InterruptedException if a thread is interrupted
153      */
154     public static HttpServletServer buildDmaapSim() throws InterruptedException {
155         var json = ResourceUtils.getResourceAsString("org/onap/policy/simulators/dmaap/DmaapParameters.json");
156         DmaapSimParameterGroup params = null;
157         try {
158             params = new StandardCoder().decode(json, DmaapSimParameterGroup.class);
159         } catch (CoderException ce) {
160             throw new ParameterRuntimeException(
161                     CANNOT_PROCESS_PARAMETERS + "org/onap/policy/simulators/dmaap/DmaapParameters.json", ce);
162         }
163
164         DmaapSimProvider.setInstance(new DmaapSimProvider(params));
165
166         var props = params.getRestServerParameters().getServerProperties();
167
168         final String svcpfx = PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES + "."
169                 + params.getRestServerParameters().getName();
170         props.setProperty(svcpfx + PolicyEndPointProperties.PROPERTY_HTTP_PORT_SUFFIX,
171                 Integer.toString(DMAAPSIM_SERVER_PORT));
172         props.setProperty(svcpfx + PolicyEndPointProperties.PROPERTY_MANAGED_SUFFIX, "true");
173
174         HttpServletServer testServer = HttpServletServerFactoryInstance.getServerFactory().build(props).get(0);
175         testServer.waitedStart(5000);
176         waitForServerToListen(testServer.getPort());
177         return testServer;
178     }
179
180     private static void waitForServerToListen(int port) throws InterruptedException {
181         if (!NetworkUtil.isTcpPortOpen(LOCALHOST, port, 200, 250L)) {
182             throw new IllegalStateException(CANNOT_CONNECT + port);
183         }
184     }
185 }