2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017-2021 AT&T Intellectual Property. All rights reserved.
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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 * ============LICENSE_END=========================================================
21 package org.onap.policy.drools.statemanagement;
23 import java.io.IOException;
24 import java.util.List;
25 import java.util.Properties;
26 import org.onap.policy.common.capabilities.Startable;
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.im.IntegrityMonitor;
30 import org.onap.policy.common.im.IntegrityMonitorException;
31 import org.onap.policy.drools.utils.PropertyUtil;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
36 * This class extends 'IntegrityMonitor' for use in the 'Drools PDP' virtual machine. The included
37 * audits are 'Database' and 'Repository'.
39 public class DroolsPdpIntegrityMonitor extends IntegrityMonitor {
41 private static final String INVALID_PROPERTY_VALUE = "init: property {} does not have the expected value of {}";
43 // get an instance of logger
44 private static final Logger logger = LoggerFactory.getLogger(DroolsPdpIntegrityMonitor.class);
46 // static global instance
47 private static DroolsPdpIntegrityMonitor im = null;
49 // list of audits to run
50 private static final AuditBase[] audits = new AuditBase[] {DbAudit.getInstance(), RepositoryAudit.getInstance()};
52 private static Properties subsystemTestProperties = null;
54 private static final String PROPERTIES_NAME = "feature-state-management.properties";
57 * Constructor - pass arguments to superclass, but remember properties.
59 * @param resourceName unique name of this Integrity Monitor
60 * @param consolidatedProperties properties used locally, as well as by 'IntegrityMonitor'
61 * @throws IntegrityMonitorException (passed from superclass)
63 private DroolsPdpIntegrityMonitor(String resourceName, Properties consolidatedProperties)
64 throws IntegrityMonitorException {
65 super(resourceName, consolidatedProperties);
68 private static void missingProperty(String prop) throws IntegrityMonitorException {
69 String msg = "init: missing IntegrityMonitor property: ".concat(prop);
71 throw new IntegrityMonitorException(msg);
74 private static void logPropertyValue(String prop, String val) {
75 logger.info("\n\n init: property: {} = {}\n", prop, val);
79 * Static initialization -- create Drools Integrity Monitor, and an HTTP server to handle REST
82 * @throws IntegrityMonitorException exception
84 public static DroolsPdpIntegrityMonitor init(String configDir) throws IntegrityMonitorException {
86 logger.info("init: Entering and invoking PropertyUtil.getProperties() on '{}'", configDir);
89 var stateManagementProperties = getProperties(configDir);
91 // fetch and verify definitions of some properties, adding defaults where
93 // (the 'IntegrityMonitor' constructor does some additional verification)
95 checkPropError(stateManagementProperties, StateManagementProperties.TEST_HOST);
96 checkPropError(stateManagementProperties, StateManagementProperties.TEST_PORT);
98 addDefaultPropError(stateManagementProperties, StateManagementProperties.TEST_SERVICES,
99 StateManagementProperties.TEST_SERVICES_DEFAULT);
101 addDefaultPropError(stateManagementProperties, StateManagementProperties.TEST_REST_CLASSES,
102 StateManagementProperties.TEST_REST_CLASSES_DEFAULT);
104 addDefaultPropWarn(stateManagementProperties, StateManagementProperties.TEST_MANAGED,
105 StateManagementProperties.TEST_MANAGED_DEFAULT);
107 addDefaultPropWarn(stateManagementProperties, StateManagementProperties.TEST_SWAGGER,
108 StateManagementProperties.TEST_SWAGGER_DEFAULT);
110 checkPropError(stateManagementProperties, StateManagementProperties.RESOURCE_NAME);
111 checkPropError(stateManagementProperties, StateManagementProperties.FP_MONITOR_INTERVAL);
112 checkPropError(stateManagementProperties, StateManagementProperties.FAILED_COUNTER_THRESHOLD);
113 checkPropError(stateManagementProperties, StateManagementProperties.TEST_TRANS_INTERVAL);
114 checkPropError(stateManagementProperties, StateManagementProperties.WRITE_FPC_INTERVAL);
115 checkPropError(stateManagementProperties, StateManagementProperties.SITE_NAME);
116 checkPropError(stateManagementProperties, StateManagementProperties.NODE_TYPE);
117 checkPropError(stateManagementProperties, StateManagementProperties.DEPENDENCY_GROUPS);
118 checkPropError(stateManagementProperties, StateManagementProperties.DB_TYPE);
119 checkPropError(stateManagementProperties, StateManagementProperties.DB_DRIVER);
120 checkPropError(stateManagementProperties, StateManagementProperties.DB_URL);
121 checkPropError(stateManagementProperties, StateManagementProperties.DB_USER);
122 checkPropError(stateManagementProperties, StateManagementProperties.DB_PWD);
124 final String testHost = stateManagementProperties.getProperty(StateManagementProperties.TEST_HOST);
125 final String testPort = stateManagementProperties.getProperty(StateManagementProperties.TEST_PORT);
126 final String resourceName = stateManagementProperties.getProperty(StateManagementProperties.RESOURCE_NAME);
128 subsystemTestProperties = stateManagementProperties;
130 // Now that we've validated the properties, create Drools Integrity Monitor
131 // with these properties.
132 im = makeMonitor(resourceName, stateManagementProperties);
133 logger.info("init: New DroolsPDPIntegrityMonitor instantiated, resourceName = {}", resourceName);
135 // create http server
136 makeRestServer(testHost, testPort, stateManagementProperties);
137 logger.info("init: Exiting and returning DroolsPDPIntegrityMonitor");
143 * Makes an Integrity Monitor.
145 * @param resourceName unique name of this Integrity Monitor
146 * @param properties properties used to configure the Integrity Monitor
147 * @return monitor object
148 * @throws IntegrityMonitorException exception
150 private static DroolsPdpIntegrityMonitor makeMonitor(String resourceName, Properties properties)
151 throws IntegrityMonitorException {
154 return new DroolsPdpIntegrityMonitor(resourceName, properties);
156 } catch (Exception e) {
157 throw new IntegrityMonitorException(e);
162 * Makes a rest server for the Integrity Monitor.
164 * @param testHost host name
165 * @param testPort port
166 * @param properties properties used to configure the rest server
167 * @throws IntegrityMonitorException exception
169 private static void makeRestServer(String testHost, String testPort, Properties properties)
170 throws IntegrityMonitorException {
173 logger.info("init: Starting HTTP server, addr= {}:{}", testHost, testPort);
175 new IntegrityMonitorRestServer(properties);
177 } catch (Exception e) {
178 logger.error("init: Caught Exception attempting to start server on testPort={}", testPort);
179 throw new IntegrityMonitorException(e);
184 * Gets the properties from the property file.
186 * @param configDir directory containing the property file
187 * @return the properties
188 * @throws IntegrityMonitorException exception
190 private static Properties getProperties(String configDir) throws IntegrityMonitorException {
192 return PropertyUtil.getProperties(configDir + "/" + PROPERTIES_NAME);
194 } catch (IOException e) {
195 throw new IntegrityMonitorException(e);
200 * Checks that a property is defined.
202 * @param props set of properties
203 * @param name name of the property to check
204 * @throws IntegrityMonitorException exception
206 private static void checkPropError(Properties props, String name) throws IntegrityMonitorException {
207 String val = props.getProperty(name);
209 missingProperty(name);
212 logPropertyValue(name, val);
216 * Checks a property's value to verify that it matches the expected value. If the property is
217 * not defined, then it is added to the property set, with the expected value. Logs an error if
218 * the property is defined, but does not have the expected value.
220 * @param props set of properties
221 * @param name name of the property to check
222 * @param expected expected/default value
224 private static void addDefaultPropError(Properties props, String name, String expected) {
225 String val = props.getProperty(name);
227 props.setProperty(name, expected);
229 } else if (!val.equals(expected)) {
230 logger.error(INVALID_PROPERTY_VALUE, name, expected);
233 logPropertyValue(name, val);
237 * Checks a property's value to verify that it matches the expected value. If the property is
238 * not defined, then it is added to the property set, with the expected value. Logs a warning if
239 * the property is defined, but does not have the expected value.
241 * @param props set of properties
242 * @param name name of the property to check
243 * @param dflt expected/default value
245 private static void addDefaultPropWarn(Properties props, String name, String dflt) {
246 String val = props.getProperty(name);
248 props.setProperty(name, dflt);
250 } else if (!val.equals(dflt)) {
251 logger.warn(INVALID_PROPERTY_VALUE, name, dflt);
254 logPropertyValue(name, val);
258 * Run tests (audits) unique to Drools PDP VM (Database + Repository).
261 public void subsystemTest() throws IntegrityMonitorException {
262 logger.info("DroolsPDPIntegrityMonitor.subsystemTest called");
264 // clear all responses (non-null values indicate an error)
265 for (AuditBase audit : audits) {
266 audit.setResponse(null);
269 // invoke all of the audits
270 for (AuditBase audit : audits) {
272 // invoke the audit (responses are stored within the audit object)
273 audit.invoke(subsystemTestProperties);
274 } catch (Exception e) {
275 logger.error("{} audit error", audit.getName(), e);
276 if (audit.getResponse() == null) {
277 // if there is no current response, use the exception message
278 audit.setResponse(e.getMessage());
283 // will contain list of subsystems where the audit failed
284 var responseMsg = "";
286 // Loop through all of the audits, and see which ones have failed.
287 // NOTE: response information is stored within the audit objects
288 // themselves -- only one can run at a time.
289 for (AuditBase audit : audits) {
290 String response = audit.getResponse();
291 if (response != null) {
292 // the audit has failed -- add subsystem and
293 // and 'responseValue' with the new information
294 responseMsg = responseMsg.concat("\n" + audit.getName() + ": " + response);
298 if (!responseMsg.isEmpty()) {
299 throw new IntegrityMonitorException(responseMsg);
303 /* ============================================================ */
306 * This is the base class for audits invoked in 'subsystemTest'.
308 public abstract static class AuditBase {
310 protected String name;
312 // non-null indicates the error response
313 protected String response;
316 * Constructor - initialize the name, and clear the initial response.
318 * @param name name of the audit
320 protected AuditBase(String name) {
322 this.response = null;
328 * @return the name of this audit
330 public String getName() {
337 * @return the response String (non-null indicates the error message)
339 public String getResponse() {
344 * Set the response string to the specified value.
346 * @param value the new value of the response string (null = no errors)
348 public void setResponse(String value) {
353 * Abstract method to invoke the audit.
355 * @param persistenceProperties Used for DB access
356 * @throws IntegrityMonitorException passed in by the audit
358 abstract void invoke(Properties persistenceProperties) throws IntegrityMonitorException;
361 public static class IntegrityMonitorRestServer implements Startable {
362 protected HttpServletServer server = null;
363 protected final Properties integrityMonitorRestServerProperties;
365 public IntegrityMonitorRestServer(Properties props) {
366 this.integrityMonitorRestServerProperties = props;
371 public boolean start() {
373 List<HttpServletServer> servers = HttpServletServerFactoryInstance.getServerFactory()
374 .build(integrityMonitorRestServerProperties);
376 if (!servers.isEmpty()) {
377 server = servers.get(0);
381 } catch (Exception e) {
382 logger.error("Exception building servers", e);
389 private void waitServerStart() {
391 server.waitedStart(5);
392 } catch (Exception e) {
393 logger.error("Exception waiting for servers to start: ", e);
394 Thread.currentThread().interrupt();
399 public boolean stop() {
402 } catch (Exception e) {
403 logger.error("Exception during stop", e);
410 public void shutdown() {
415 public synchronized boolean isAlive() {
416 return this.integrityMonitorRestServerProperties != null;
421 * Returns the instance.
423 * @return DroolsPDPIntegrityMonitor object
424 * @throws IntegrityMonitorException exception
426 public static DroolsPdpIntegrityMonitor getInstance() throws IntegrityMonitorException {
427 logger.debug("getInstance() called");
429 String msg = "No DroolsPDPIntegrityMonitor instance exists."
430 + " Please use the method DroolsPDPIntegrityMonitor init(String configDir)";
431 throw new IntegrityMonitorException(msg);