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