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