Update snapshot and/or references of policy/models to latest snapshots
[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, 2021 AT&T Intellectual Property. All rights reserved.
6  * Modifications Copyright (C) 2019, 2023-2024 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 lombok.AccessLevel;
26 import lombok.NoArgsConstructor;
27 import org.onap.policy.common.endpoints.http.server.HttpServletServer;
28 import org.onap.policy.common.endpoints.http.server.HttpServletServerFactoryInstance;
29 import org.onap.policy.common.utils.network.NetworkUtil;
30
31 @NoArgsConstructor(access = AccessLevel.PRIVATE)
32 public final class Util {
33     public static final String AAISIM_SERVER_NAME = "aaiSim";
34     public static final String SOSIM_SERVER_NAME = "soSim";
35     public static final String VFCSIM_SERVER_NAME = "vfcSim";
36     public static final String XACMLSIM_SERVER_NAME = "xacmlSim";
37     public static final String SDNCSIM_SERVER_NAME = "sdncSim";
38
39     public static final int AAISIM_SERVER_PORT = 6666;
40     public static final int SOSIM_SERVER_PORT = 6667;
41     public static final int VFCSIM_SERVER_PORT = 6668;
42     public static final int XACMLSIM_SERVER_PORT = 6669;
43     public static final int SDNCSIM_SERVER_PORT = 6670;
44     public static final int CDSSIM_SERVER_PORT = 6671;
45
46     private static final String CANNOT_PROCESS_PARAMETERS = "cannot parse parameters ";
47     private static final String CANNOT_CONNECT = "cannot connect to port ";
48     public static final String LOCALHOST = "localhost";
49
50     /**
51      * Build an A&AI simulator.
52      *
53      * @return the simulator
54      * @throws InterruptedException if a thread is interrupted
55      */
56     public static HttpServletServer buildAaiSim() throws InterruptedException {
57         final HttpServletServer testServer = HttpServletServerFactoryInstance.getServerFactory()
58                 .build(AAISIM_SERVER_NAME, LOCALHOST, AAISIM_SERVER_PORT, "/", false, true);
59         testServer.addServletClass("/*", AaiSimulatorJaxRs.class.getName());
60         testServer.waitedStart(5000);
61         waitForServerToListen(testServer.getPort());
62         return testServer;
63     }
64
65     /**
66      * Build a CDS simulator.
67      *
68      * @return the simulator
69      * @throws InterruptedException if a thread is interrupted
70      * @throws IOException if an I/O error occurs
71      */
72     public static CdsSimulator buildCdsSim() throws InterruptedException, IOException {
73         final var testServer = new CdsSimulator(LOCALHOST, CDSSIM_SERVER_PORT);
74         testServer.start();
75         waitForServerToListen(testServer.getPort());
76         return testServer;
77     }
78
79     /**
80      * Build an SDNC simulator.
81      *
82      * @return the simulator
83      * @throws InterruptedException if a thread is interrupted
84      */
85     public static HttpServletServer buildSdncSim() throws InterruptedException {
86         final HttpServletServer testServer = HttpServletServerFactoryInstance.getServerFactory()
87                 .build(SDNCSIM_SERVER_NAME, LOCALHOST, SDNCSIM_SERVER_PORT, "/", false, true);
88         testServer.addServletClass("/*", SdncSimulatorJaxRs.class.getName());
89         testServer.waitedStart(5000);
90         waitForServerToListen(testServer.getPort());
91         return testServer;
92     }
93
94
95     /**
96      * Build a SO simulator.
97      *
98      * @return the simulator
99      * @throws InterruptedException if a thread is interrupted
100      */
101     public static HttpServletServer buildSoSim() throws InterruptedException {
102         final HttpServletServer testServer = HttpServletServerFactoryInstance.getServerFactory()
103                 .build(SOSIM_SERVER_NAME, LOCALHOST, SOSIM_SERVER_PORT, "/", false, true);
104         testServer.addServletClass("/*", SoSimulatorJaxRs.class.getName());
105         testServer.waitedStart(5000);
106         waitForServerToListen(testServer.getPort());
107         return testServer;
108     }
109
110     /**
111      * Build a VFC simulator.
112      *
113      * @return the simulator
114      * @throws InterruptedException if a thread is interrupted
115      */
116     public static HttpServletServer buildVfcSim() throws InterruptedException {
117         final HttpServletServer testServer = HttpServletServerFactoryInstance.getServerFactory()
118                 .build(VFCSIM_SERVER_NAME, LOCALHOST, VFCSIM_SERVER_PORT, "/", false, true);
119         testServer.addServletClass("/*", VfcSimulatorJaxRs.class.getName());
120         testServer.waitedStart(5000);
121         waitForServerToListen(testServer.getPort());
122         return testServer;
123     }
124
125     /**
126      * Build a guard simulator.
127      *
128      * @return the simulator
129      * @throws InterruptedException if a thread is interrupted
130      */
131     public static HttpServletServer buildGuardSim() throws InterruptedException {
132         return buildXacmlSim();
133     }
134
135     /**
136      * Build a xacml simulator.
137      *
138      * @return the simulator
139      * @throws InterruptedException if a thread is interrupted
140      */
141     public static HttpServletServer buildXacmlSim() throws InterruptedException {
142         HttpServletServer testServer = HttpServletServerFactoryInstance.getServerFactory().build(XACMLSIM_SERVER_NAME,
143                 LOCALHOST, XACMLSIM_SERVER_PORT, "/", false, true);
144         testServer.addServletClass("/*", XacmlSimulatorJaxRs.class.getName());
145         testServer.waitedStart(5000);
146         waitForServerToListen(testServer.getPort());
147         return testServer;
148     }
149
150     private static void waitForServerToListen(int port) throws InterruptedException {
151         if (!NetworkUtil.isTcpPortOpen(LOCALHOST, port, 200, 250L)) {
152             throw new IllegalStateException(CANNOT_CONNECT + port);
153         }
154     }
155 }