Add CDS simulator to 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 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 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.parameters.ParameterRuntimeException;
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;
37
38 public class Util {
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";
44
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 CDSSIM_SERVER_PORT = 6671;
51     public static final int DMAAPSIM_SERVER_PORT = 3904;
52
53     private static final String CANNOT_PROCESS_PARAMETERS = "cannot parse parameters ";
54     private static final String CANNOT_CONNECT = "cannot connect to port ";
55     private static final String LOCALHOST = "localhost";
56
57     private Util() {
58         // Prevent instantiation of this class
59     }
60
61     /**
62      * Build an A&AI simulator.
63      *
64      * @return the simulator
65      * @throws InterruptedException if a thread is interrupted
66      */
67     public static HttpServletServer buildAaiSim() throws InterruptedException {
68         final HttpServletServer testServer = HttpServletServerFactoryInstance.getServerFactory()
69                 .build(AAISIM_SERVER_NAME, LOCALHOST, AAISIM_SERVER_PORT, "/", false, true);
70         testServer.addServletClass("/*", AaiSimulatorJaxRs.class.getName());
71         testServer.waitedStart(5000);
72         waitForServerToListen(testServer.getPort());
73         return testServer;
74     }
75
76     /**
77      * Build a CDS simulator.
78      *
79      * @return the simulator
80      * @throws InterruptedException if a thread is interrupted
81      * @throws IOException if an I/O error occurs
82      */
83     public static CdsSimulator buildCdsSim() throws InterruptedException, IOException {
84         final CdsSimulator testServer = new CdsSimulator(LOCALHOST, CDSSIM_SERVER_PORT);
85         testServer.start();
86         waitForServerToListen(testServer.getPort());
87         return testServer;
88     }
89
90     /**
91      * Build an SDNC simulator.
92      *
93      * @return the simulator
94      * @throws InterruptedException if a thread is interrupted
95      */
96     public static HttpServletServer buildSdncSim() throws InterruptedException {
97         final HttpServletServer testServer = HttpServletServerFactoryInstance.getServerFactory()
98                 .build(SDNCSIM_SERVER_NAME, LOCALHOST, SDNCSIM_SERVER_PORT, "/", false, true);
99         testServer.addServletClass("/*", SdncSimulatorJaxRs.class.getName());
100         testServer.waitedStart(5000);
101         waitForServerToListen(testServer.getPort());
102         return testServer;
103     }
104
105
106     /**
107      * Build an SO simulator.
108      *
109      * @return the simulator
110      * @throws InterruptedException if a thread is interrupted
111      */
112     public static HttpServletServer buildSoSim() throws InterruptedException {
113         final HttpServletServer testServer = HttpServletServerFactoryInstance.getServerFactory()
114                 .build(SOSIM_SERVER_NAME, LOCALHOST, SOSIM_SERVER_PORT, "/", false, true);
115         testServer.addServletClass("/*", SoSimulatorJaxRs.class.getName());
116         testServer.waitedStart(5000);
117         waitForServerToListen(testServer.getPort());
118         return testServer;
119     }
120
121     /**
122      * Build a VFC simulator.
123      *
124      * @return the simulator
125      * @throws InterruptedException if a thread is interrupted
126      */
127     public static HttpServletServer buildVfcSim() throws InterruptedException {
128         final HttpServletServer testServer = HttpServletServerFactoryInstance.getServerFactory()
129                 .build(VFCSIM_SERVER_NAME, LOCALHOST, VFCSIM_SERVER_PORT, "/", false, true);
130         testServer.addServletClass("/*", VfcSimulatorJaxRs.class.getName());
131         testServer.waitedStart(5000);
132         waitForServerToListen(testServer.getPort());
133         return testServer;
134     }
135
136     /**
137      * Build a guard simulator.
138      *
139      * @return the simulator
140      * @throws InterruptedException if a thread is interrupted
141      */
142     public static HttpServletServer buildGuardSim() throws InterruptedException {
143         HttpServletServer testServer = HttpServletServerFactoryInstance.getServerFactory().build(GUARDSIM_SERVER_NAME,
144                 LOCALHOST, GUARDSIM_SERVER_PORT, "/", false, true);
145         testServer.addServletClass("/*", GuardSimulatorJaxRs.class.getName());
146         testServer.waitedStart(5000);
147         waitForServerToListen(testServer.getPort());
148         return testServer;
149     }
150
151     /**
152      * Build a DMaaP simulator.
153      *
154      * @return the simulator
155      * @throws InterruptedException if a thread is interrupted
156      */
157     public static HttpServletServer buildDmaapSim() throws InterruptedException {
158         String json = ResourceUtils.getResourceAsString("org/onap/policy/simulators/dmaap/DmaapParameters.json");
159         DmaapSimParameterGroup params = null;
160         try {
161             params = new StandardCoder().decode(json, DmaapSimParameterGroup.class);
162         } catch (CoderException ce) {
163             throw new ParameterRuntimeException(
164                     CANNOT_PROCESS_PARAMETERS + "org/onap/policy/simulators/dmaap/DmaapParameters.json", ce);
165         }
166
167         DmaapSimProvider.setInstance(new DmaapSimProvider(params));
168
169         Properties props = DmaapSimRestServer.getServerProperties(params.getRestServerParameters());
170
171         final String svcpfx = PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES + "."
172                 + params.getRestServerParameters().getName();
173         props.setProperty(svcpfx + PolicyEndPointProperties.PROPERTY_HTTP_PORT_SUFFIX,
174                 Integer.toString(DMAAPSIM_SERVER_PORT));
175         props.setProperty(svcpfx + PolicyEndPointProperties.PROPERTY_MANAGED_SUFFIX, "true");
176
177         HttpServletServer testServer = HttpServletServerFactoryInstance.getServerFactory().build(props).get(0);
178         testServer.waitedStart(5000);
179         waitForServerToListen(testServer.getPort());
180         return testServer;
181     }
182
183     private static void waitForServerToListen(int port) throws InterruptedException {
184         if (!NetworkUtil.isTcpPortOpen(LOCALHOST, port, 200, 250L)) {
185             throw new IllegalStateException(CANNOT_CONNECT + port);
186         }
187     }
188 }