ce32fd407b0605963b4a0c2a462711916bede1f0
[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.utils.coder.Coder;
37 import org.onap.policy.common.utils.coder.CoderException;
38 import org.onap.policy.common.utils.coder.StandardCoder;
39 import org.onap.policy.common.utils.network.NetworkUtil;
40 import org.onap.policy.common.utils.resources.ResourceUtils;
41 import org.onap.policy.common.utils.services.Registry;
42 import org.onap.policy.common.utils.services.ServiceManagerContainer;
43 import org.onap.policy.simulators.CdsSimulator;
44 import org.slf4j.Logger;
45 import org.slf4j.LoggerFactory;
46
47 /**
48  * This class runs all simulators specified in the parameter file.
49  */
50 public class Main extends ServiceManagerContainer {
51     private static final Logger logger = LoggerFactory.getLogger(Main.class);
52
53     private static final String CANNOT_CONNECT = "cannot connect to port ";
54
55     @Getter(AccessLevel.PROTECTED)
56     private static Main instance;
57
58
59     /**
60      * Runs the simulators.
61      *
62      * @param paramFile parameter file name
63      */
64     public Main(String paramFile) {
65         super(Main.class.getPackage().getName());
66
67         SimulatorParameters params = readParameters(paramFile);
68
69         CdsServerParameters cdsServer = params.getGrpcServer();
70
71         // Cds Simulator
72         if (cdsServer != null) {
73             AtomicReference<CdsSimulator> cdsSim = new AtomicReference<>();
74             addAction(cdsServer.getName(), () -> cdsSim.set(buildCdsSimulator(cdsServer)), () -> cdsSim.get().stop());
75         }
76
77         // REST server simulators
78         // @formatter:off
79         for (ClassRestServerParameters restsim : params.getRestServers()) {
80             AtomicReference<HttpServletServer> ref = new AtomicReference<>();
81             if (StringUtils.isNotBlank(restsim.getResourceLocation())) {
82                 String resourceLocationId = restsim.getProviderClass() + "_RESOURCE_LOCATION";
83                 addAction(resourceLocationId,
84                     () -> Registry.register(resourceLocationId, restsim.getResourceLocation()),
85                     () -> Registry.unregister(resourceLocationId));
86             }
87             addAction(restsim.getName(),
88                 () -> ref.set(buildRestServer(restsim)),
89                 () -> ref.get().shutdown());
90         }
91         // @formatter:on
92     }
93
94     /**
95      * The main method. The arguments are validated, thus adding the NOSONAR.
96      *
97      * @param args the arguments, the first of which is the name of the parameter file
98      */
99     public static void main(final String[] args) { // NOSONAR
100         /*
101          * Only one argument is used and is validated implicitly by the constructor (i.e.,
102          * file-not-found), thus sonar is disabled.
103          */
104
105         try {
106             if (args.length != 1) {
107                 throw new IllegalArgumentException("arg(s): parameter-file-name");
108             }
109
110             instance = new Main(args[0]);
111             instance.start();
112
113         } catch (RuntimeException e) {
114             logger.error("failed to start simulators", e);
115         }
116     }
117
118     private SimulatorParameters readParameters(String paramFile) {
119         try {
120             var paramsJson = getResourceAsString(paramFile);
121             if (paramsJson == null) {
122                 throw new IllegalArgumentException(new FileNotFoundException(paramFile));
123             }
124
125             String hostName = NetworkUtil.getHostname();
126             logger.info("replacing 'HOST_NAME' with {} in {}", hostName, paramFile);
127
128             paramsJson = paramsJson.replace("${HOST_NAME}", hostName);
129
130             return makeCoder().decode(paramsJson, SimulatorParameters.class);
131
132         } catch (CoderException e) {
133             throw new IllegalArgumentException("cannot decode " + paramFile, e);
134         }
135     }
136
137     private CdsSimulator buildCdsSimulator(CdsServerParameters params) throws IOException {
138         var cdsSimulator = new CdsSimulator(params.getHost(), params.getPort(), params.getResourceLocation(),
139             params.getSuccessRepeatCount(), params.getRequestedResponseDelayMs());
140         cdsSimulator.start();
141         return cdsSimulator;
142     }
143
144
145     private HttpServletServer buildRestServer(ClassRestServerParameters params) {
146         try {
147             var props = getServerProperties(params);
148             HttpServletServer testServer = makeServer(props);
149             testServer.waitedStart(5000);
150
151             String svcpfx = PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES + "." + params.getName();
152             String hostName = props.getProperty(svcpfx + PolicyEndPointProperties.PROPERTY_HTTP_HOST_SUFFIX);
153
154             if (!isTcpPortOpen(hostName, testServer.getPort())) {
155                 throw new IllegalStateException(CANNOT_CONNECT + testServer.getPort());
156             }
157
158             return testServer;
159
160         } catch (InterruptedException e) {
161             Thread.currentThread().interrupt();
162             throw new IllegalStateException("interrupted while building " + params.getName(), e);
163         }
164     }
165
166
167     /**
168      * Creates a set of properties, suitable for building a REST server, from the
169      * parameters.
170      *
171      * @param params parameters from which to build the properties
172      * @return a Map of properties representing the given parameters
173      */
174     private static Properties getServerProperties(ClassRestServerParameters params) {
175         final var props = new Properties();
176         props.setProperty(PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES, params.getName());
177
178         final String svcpfx = PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES + "." + params.getName();
179
180         props.setProperty(PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES, params.getName());
181         props.setProperty(svcpfx + PolicyEndPointProperties.PROPERTY_HTTP_HOST_SUFFIX, params.getHost());
182         props.setProperty(svcpfx + PolicyEndPointProperties.PROPERTY_HTTP_PORT_SUFFIX,
183                         Integer.toString(params.getPort()));
184         props.setProperty(svcpfx + PolicyEndPointProperties.PROPERTY_HTTP_HTTPS_SUFFIX,
185                         Boolean.toString(params.isHttps()));
186         props.setProperty(svcpfx + PolicyEndPointProperties.PROPERTY_HTTP_REST_CLASSES_SUFFIX,
187                         params.getProviderClass());
188         props.setProperty(svcpfx + PolicyEndPointProperties.PROPERTY_MANAGED_SUFFIX, "false");
189         props.setProperty(svcpfx + PolicyEndPointProperties.PROPERTY_HTTP_SWAGGER_SUFFIX, "false");
190         props.setProperty(svcpfx + PolicyEndPointProperties.PROPERTY_HTTP_SNI_HOST_CHECK_SUFFIX, "false");
191         props.setProperty(svcpfx + PolicyEndPointProperties.PROPERTY_MANAGED_SUFFIX, "true");
192
193         props.setProperty(svcpfx + PolicyEndPointProperties.PROPERTY_HTTP_SERIALIZATION_PROVIDER, String.join(",",
194                             GsonMessageBodyHandler.class.getName(), TextMessageBodyHandler.class.getName()));
195
196
197         return props;
198     }
199
200     // the following methods may be overridden by junit tests
201
202     protected String getResourceAsString(String resourceName) {
203         return ResourceUtils.getResourceAsString(resourceName);
204     }
205
206     protected Coder makeCoder() {
207         return new StandardCoder();
208     }
209
210     protected HttpServletServer makeServer(Properties props) {
211         return HttpServletServerFactoryInstance.getServerFactory().build(props).get(0);
212     }
213
214     protected boolean isTcpPortOpen(String hostName, int port) throws InterruptedException {
215         return NetworkUtil.isTcpPortOpen(hostName, port, 100, 200L);
216     }
217 }