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