Fix models due to sonar changes in common
[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
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.gson.GsonMessageBodyHandler;
29 import org.onap.policy.common.utils.network.NetworkUtil;
30
31 public class Util {
32     public static final String AAISIM_SERVER_NAME = "aaiSim";
33     public static final String SOSIM_SERVER_NAME = "soSim";
34     public static final String VFCSIM_SERVER_NAME = "vfcSim";
35     public static final String GUARDSIM_SERVER_NAME = "guardSim";
36     public static final String SDNCSIM_SERVER_NAME = "sdncSim";
37
38     public static final int AAISIM_SERVER_PORT = 6666;
39     public static final int SOSIM_SERVER_PORT = 6667;
40     public static final int VFCSIM_SERVER_PORT = 6668;
41     public static final int GUARDSIM_SERVER_PORT = 6669;
42     public static final int SDNCSIM_SERVER_PORT = 6670;
43
44     private static final String CANNOT_CONNECT = "cannot connect to port ";
45     private static final String LOCALHOST = "localhost";
46
47     private Util() {
48         // Prevent instantiation of thic class
49     }
50
51     /**
52      * Build an A&AI simulator.
53      *
54      * @return the simulator
55      * @throws InterruptedException if a thread is interrupted
56      * @throws IOException if an IO errror occurs
57      */
58     public static HttpServletServer buildAaiSim() throws InterruptedException, IOException {
59         final HttpServletServer testServer = HttpServletServerFactoryInstance.getServerFactory()
60                         .build(AAISIM_SERVER_NAME, LOCALHOST, AAISIM_SERVER_PORT, "/", false, true);
61         testServer.addServletClass("/*", AaiSimulatorJaxRs.class.getName());
62         testServer.waitedStart(5000);
63         if (!NetworkUtil.isTcpPortOpen(LOCALHOST, testServer.getPort(), 5, 10000L)) {
64             throw new IllegalStateException(CANNOT_CONNECT + testServer.getPort());
65         }
66         return testServer;
67     }
68
69     /**
70      * Build an SDNC simulator.
71      *
72      * @return the simulator
73      * @throws InterruptedException if a thread is interrupted
74      * @throws IOException if an IO errror occurs
75      */
76     public static HttpServletServer buildSdncSim() throws InterruptedException, IOException {
77         final HttpServletServer testServer = HttpServletServerFactoryInstance.getServerFactory()
78                         .build(SDNCSIM_SERVER_NAME, LOCALHOST, SDNCSIM_SERVER_PORT, "/", false, true);
79         testServer.addServletClass("/*", SdncSimulatorJaxRs.class.getName());
80         testServer.waitedStart(5000);
81         if (!NetworkUtil.isTcpPortOpen(LOCALHOST, testServer.getPort(), 5, 10000L)) {
82             throw new IllegalStateException(CANNOT_CONNECT + testServer.getPort());
83         }
84         return testServer;
85     }
86
87
88     /**
89      * Build an SO simulator.
90      *
91      * @return the simulator
92      * @throws InterruptedException if a thread is interrupted
93      * @throws IOException if an IO errror occurs
94      */
95     public static HttpServletServer buildSoSim() throws InterruptedException, IOException {
96         final HttpServletServer testServer = HttpServletServerFactoryInstance.getServerFactory()
97                         .build(SOSIM_SERVER_NAME, LOCALHOST, SOSIM_SERVER_PORT, "/", false, true);
98         testServer.addServletClass("/*", SoSimulatorJaxRs.class.getName());
99         testServer.waitedStart(5000);
100         if (!NetworkUtil.isTcpPortOpen(LOCALHOST, testServer.getPort(), 5, 10000L)) {
101             throw new IllegalStateException(CANNOT_CONNECT + testServer.getPort());
102         }
103         return testServer;
104     }
105
106     /**
107      * Build a VFC simulator.
108      *
109      * @return the simulator
110      * @throws InterruptedException if a thread is interrupted
111      * @throws IOException if an IO errror occurs
112      */
113     public static HttpServletServer buildVfcSim() throws InterruptedException, IOException {
114         final HttpServletServer testServer = HttpServletServerFactoryInstance.getServerFactory()
115                         .build(VFCSIM_SERVER_NAME, LOCALHOST, VFCSIM_SERVER_PORT, "/", false, true);
116         testServer.addServletClass("/*", VfcSimulatorJaxRs.class.getName());
117         testServer.waitedStart(5000);
118         if (!NetworkUtil.isTcpPortOpen(LOCALHOST, testServer.getPort(), 5, 10000L)) {
119             throw new IllegalStateException(CANNOT_CONNECT + testServer.getPort());
120         }
121         return testServer;
122     }
123
124     /**
125      * Build a guard simulator.
126      *
127      * @return the simulator
128      * @throws InterruptedException if a thread is interrupted
129      * @throws IOException if an IO errror occurs
130      */
131     public static HttpServletServer buildGuardSim() throws InterruptedException, IOException {
132         HttpServletServer testServer = HttpServletServerFactoryInstance.getServerFactory().build(GUARDSIM_SERVER_NAME,
133                         LOCALHOST, GUARDSIM_SERVER_PORT, "/", false, true);
134         testServer.setSerializationProvider(GsonMessageBodyHandler.class.getName());
135         testServer.addServletClass("/*", GuardSimulatorJaxRs.class.getName());
136         testServer.waitedStart(5000);
137         if (!NetworkUtil.isTcpPortOpen(LOCALHOST, testServer.getPort(), 5, 10000L)) {
138             throw new IllegalStateException(CANNOT_CONNECT + testServer.getPort());
139         }
140         return testServer;
141     }
142 }