Fix config files to remove outdated configuration for hibernate
[policy/models.git] / models-sim / policy-models-simulators / src / main / java / org / onap / policy / models / simulators / Main.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * Copyright (C) 2020-2021 AT&T Intellectual Property. All rights reserved.
4  * Modifications Copyright (C) 2020-2021 Bell Canada. All rights reserved.
5  * Modifications Copyright 2023-2024 Nordix Foundation.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  * SPDX-License-Identifier: Apache-2.0
20  * ============LICENSE_END=========================================================
21  */
22
23 package org.onap.policy.models.simulators;
24
25 import java.io.FileNotFoundException;
26 import java.io.IOException;
27 import java.util.Properties;
28 import java.util.concurrent.atomic.AtomicReference;
29 import lombok.AccessLevel;
30 import lombok.Getter;
31 import org.apache.commons.lang3.StringUtils;
32 import org.onap.policy.common.endpoints.http.server.HttpServletServer;
33 import org.onap.policy.common.endpoints.http.server.HttpServletServerFactoryInstance;
34 import org.onap.policy.common.endpoints.properties.PolicyEndPointProperties;
35 import org.onap.policy.common.gson.GsonMessageBodyHandler;
36 import org.onap.policy.common.parameters.BeanValidationResult;
37 import org.onap.policy.common.utils.coder.Coder;
38 import org.onap.policy.common.utils.coder.CoderException;
39 import org.onap.policy.common.utils.coder.StandardCoder;
40 import org.onap.policy.common.utils.network.NetworkUtil;
41 import org.onap.policy.common.utils.resources.ResourceUtils;
42 import org.onap.policy.common.utils.services.Registry;
43 import org.onap.policy.common.utils.services.ServiceManagerContainer;
44 import org.onap.policy.simulators.CdsSimulator;
45 import org.slf4j.Logger;
46 import org.slf4j.LoggerFactory;
47
48 /**
49  * This class runs all simulators specified in the parameter file.
50  */
51 public class Main extends ServiceManagerContainer {
52     private static final Logger logger = LoggerFactory.getLogger(Main.class);
53
54     private static final String CANNOT_CONNECT = "cannot connect to port ";
55
56     @Getter(AccessLevel.PROTECTED)
57     private static Main instance;
58
59
60     /**
61      * Runs the simulators.
62      *
63      * @param paramFile parameter file name
64      */
65     public Main(String paramFile) {
66         super(Main.class.getPackage().getName());
67
68         SimulatorParameters params = readParameters(paramFile);
69
70         CdsServerParameters cdsServer = params.getGrpcServer();
71
72         // Cds Simulator
73         if (cdsServer != null) {
74             AtomicReference<CdsSimulator> cdsSim = new AtomicReference<>();
75             addAction(cdsServer.getName(), () -> cdsSim.set(buildCdsSimulator(cdsServer)), () -> cdsSim.get().stop());
76         }
77
78         // REST server simulators
79         // @formatter:off
80         for (ClassRestServerParameters restsim : params.getRestServers()) {
81             AtomicReference<HttpServletServer> ref = new AtomicReference<>();
82             if (StringUtils.isNotBlank(restsim.getResourceLocation())) {
83                 String resourceLocationId = restsim.getProviderClass() + "_RESOURCE_LOCATION";
84                 addAction(resourceLocationId,
85                     () -> Registry.register(resourceLocationId, restsim.getResourceLocation()),
86                     () -> Registry.unregister(resourceLocationId));
87             }
88             addAction(restsim.getName(),
89                 () -> ref.set(buildRestServer(restsim)),
90                 () -> ref.get().shutdown());
91         }
92         // @formatter:on
93     }
94
95     /**
96      * The main method. The arguments are validated, thus adding the NOSONAR.
97      *
98      * @param args the arguments, the first of which is the name of the parameter file
99      */
100     public static void main(final String[] args) { // NOSONAR
101         /*
102          * Only one argument is used and is validated implicitly by the constructor (i.e.,
103          * file-not-found), thus sonar is disabled.
104          */
105
106         try {
107             if (args.length != 1) {
108                 throw new IllegalArgumentException("arg(s): parameter-file-name");
109             }
110
111             instance = new Main(args[0]);
112             instance.start();
113
114         } catch (RuntimeException e) {
115             logger.error("failed to start simulators", e);
116         }
117     }
118
119     private SimulatorParameters readParameters(String paramFile) {
120         try {
121             var paramsJson = getResourceAsString(paramFile);
122             if (paramsJson == null) {
123                 throw new IllegalArgumentException(new FileNotFoundException(paramFile));
124             }
125
126             String hostName = NetworkUtil.getHostname();
127             logger.info("replacing 'HOST_NAME' with {} in {}", hostName, paramFile);
128
129             paramsJson = paramsJson.replace("${HOST_NAME}", hostName);
130
131             return makeCoder().decode(paramsJson, SimulatorParameters.class);
132
133         } catch (CoderException e) {
134             throw new IllegalArgumentException("cannot decode " + paramFile, e);
135         }
136     }
137
138     private CdsSimulator buildCdsSimulator(CdsServerParameters params) throws IOException {
139         var cdsSimulator = new CdsSimulator(params.getHost(), params.getPort(), params.getResourceLocation(),
140             params.getSuccessRepeatCount(), params.getRequestedResponseDelayMs());
141         cdsSimulator.start();
142         return cdsSimulator;
143     }
144
145
146     private HttpServletServer buildRestServer(ClassRestServerParameters params) {
147         try {
148             var props = getServerProperties(params);
149             HttpServletServer testServer = makeServer(props);
150             testServer.waitedStart(5000);
151
152             String svcpfx = PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES + "." + params.getName();
153             String hostName = props.getProperty(svcpfx + PolicyEndPointProperties.PROPERTY_HTTP_HOST_SUFFIX);
154
155             if (!isTcpPortOpen(hostName, testServer.getPort())) {
156                 throw new IllegalStateException(CANNOT_CONNECT + testServer.getPort());
157             }
158
159             return testServer;
160
161         } catch (InterruptedException e) {
162             Thread.currentThread().interrupt();
163             throw new IllegalStateException("interrupted while building " + params.getName(), e);
164         }
165     }
166
167
168     /**
169      * Creates a set of properties, suitable for building a REST server, from the
170      * parameters.
171      *
172      * @param params parameters from which to build the properties
173      * @return a Map of properties representing the given parameters
174      */
175     private static Properties getServerProperties(ClassRestServerParameters params) {
176         final var props = new Properties();
177         props.setProperty(PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES, params.getName());
178
179         final String svcpfx = PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES + "." + params.getName();
180
181         props.setProperty(PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES, params.getName());
182         props.setProperty(svcpfx + PolicyEndPointProperties.PROPERTY_HTTP_HOST_SUFFIX, params.getHost());
183         props.setProperty(svcpfx + PolicyEndPointProperties.PROPERTY_HTTP_PORT_SUFFIX,
184                         Integer.toString(params.getPort()));
185         props.setProperty(svcpfx + PolicyEndPointProperties.PROPERTY_HTTP_HTTPS_SUFFIX,
186                         Boolean.toString(params.isHttps()));
187         props.setProperty(svcpfx + PolicyEndPointProperties.PROPERTY_HTTP_REST_CLASSES_SUFFIX,
188                         params.getProviderClass());
189         props.setProperty(svcpfx + PolicyEndPointProperties.PROPERTY_MANAGED_SUFFIX, "false");
190         props.setProperty(svcpfx + PolicyEndPointProperties.PROPERTY_HTTP_SWAGGER_SUFFIX, "false");
191         props.setProperty(svcpfx + PolicyEndPointProperties.PROPERTY_HTTP_SNI_HOST_CHECK_SUFFIX, "false");
192         props.setProperty(svcpfx + PolicyEndPointProperties.PROPERTY_MANAGED_SUFFIX, "true");
193
194         props.setProperty(svcpfx + PolicyEndPointProperties.PROPERTY_HTTP_SERIALIZATION_PROVIDER, String.join(",",
195                             GsonMessageBodyHandler.class.getName(), TextMessageBodyHandler.class.getName()));
196
197
198         return props;
199     }
200
201     // the following methods may be overridden by junit tests
202
203     protected String getResourceAsString(String resourceName) {
204         return ResourceUtils.getResourceAsString(resourceName);
205     }
206
207     protected Coder makeCoder() {
208         return new StandardCoder();
209     }
210
211     protected HttpServletServer makeServer(Properties props) {
212         return HttpServletServerFactoryInstance.getServerFactory().build(props).get(0);
213     }
214
215     protected boolean isTcpPortOpen(String hostName, int port) throws InterruptedException {
216         return NetworkUtil.isTcpPortOpen(hostName, port, 100, 200L);
217     }
218 }