2 * ============LICENSE_START=======================================================
3 * feature-state-management
4 * ================================================================================
5 * Copyright (C) 2017-2019 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 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 url the JMX URL of the MBean server
61 * @param properties properties used locally, as well as by 'IntegrityMonitor'
62 * @throws IntegrityMonitorException (passed from superclass)
64 private DroolsPDPIntegrityMonitor(String resourceName, Properties consolidatedProperties)
65 throws IntegrityMonitorException {
66 super(resourceName, consolidatedProperties);
69 private static void missingProperty(String prop) throws IntegrityMonitorException {
70 String msg = "init: missing IntegrityMonitor property: ".concat(prop);
72 throw new IntegrityMonitorException(msg);
75 private static void logPropertyValue(String prop, String val) {
76 if (logger.isInfoEnabled()) {
77 String msg = "\n\n init: property: " + prop + " = " + val + "\n";
83 * Static initialization -- create Drools Integrity Monitor, and an HTTP server to handle REST
86 * @throws IntegrityMonitorException exception
88 public static DroolsPDPIntegrityMonitor init(String configDir) throws IntegrityMonitorException {
90 logger.info("init: Entering and invoking PropertyUtil.getProperties() on '{}'", configDir);
93 Properties stateManagementProperties = getProperties(configDir);
95 // fetch and verify definitions of some properties, adding defaults where
97 // (the 'IntegrityMonitor' constructor does some additional verification)
99 checkPropError(stateManagementProperties, StateManagementProperties.TEST_HOST);
100 checkPropError(stateManagementProperties, StateManagementProperties.TEST_PORT);
102 addDefaultPropError(stateManagementProperties, StateManagementProperties.TEST_SERVICES,
103 StateManagementProperties.TEST_SERVICES_DEFAULT);
105 addDefaultPropError(stateManagementProperties, StateManagementProperties.TEST_REST_CLASSES,
106 StateManagementProperties.TEST_REST_CLASSES_DEFAULT);
108 addDefaultPropWarn(stateManagementProperties, StateManagementProperties.TEST_MANAGED,
109 StateManagementProperties.TEST_MANAGED_DEFAULT);
111 addDefaultPropWarn(stateManagementProperties, StateManagementProperties.TEST_SWAGGER,
112 StateManagementProperties.TEST_SWAGGER_DEFAULT);
114 checkPropError(stateManagementProperties, StateManagementProperties.RESOURCE_NAME);
115 checkPropError(stateManagementProperties, StateManagementProperties.FP_MONITOR_INTERVAL);
116 checkPropError(stateManagementProperties, StateManagementProperties.FAILED_COUNTER_THRESHOLD);
117 checkPropError(stateManagementProperties, StateManagementProperties.TEST_TRANS_INTERVAL);
118 checkPropError(stateManagementProperties, StateManagementProperties.WRITE_FPC_INTERVAL);
119 checkPropError(stateManagementProperties, StateManagementProperties.SITE_NAME);
120 checkPropError(stateManagementProperties, StateManagementProperties.NODE_TYPE);
121 checkPropError(stateManagementProperties, StateManagementProperties.DEPENDENCY_GROUPS);
122 checkPropError(stateManagementProperties, StateManagementProperties.DB_DRIVER);
123 checkPropError(stateManagementProperties, StateManagementProperties.DB_URL);
124 checkPropError(stateManagementProperties, StateManagementProperties.DB_USER);
125 checkPropError(stateManagementProperties, StateManagementProperties.DB_PWD);
127 final String testHost = stateManagementProperties.getProperty(StateManagementProperties.TEST_HOST);
128 final String testPort = stateManagementProperties.getProperty(StateManagementProperties.TEST_PORT);
129 final String resourceName = stateManagementProperties.getProperty(StateManagementProperties.RESOURCE_NAME);
131 subsystemTestProperties = stateManagementProperties;
133 // Now that we've validated the properties, create Drools Integrity Monitor
134 // with these properties.
135 im = makeMonitor(resourceName, stateManagementProperties);
136 logger.info("init: New DroolsPDPIntegrityMonitor instantiated, resourceName = {}", resourceName);
138 // create http server
139 makeRestServer(testHost, testPort, stateManagementProperties);
140 logger.info("init: Exiting and returning DroolsPDPIntegrityMonitor");
146 * Makes an Integrity Monitor.
148 * @param resourceName unique name of this Integrity Monitor
149 * @param properties properties used to configure the Integrity Monitor
150 * @return monitor object
151 * @throws IntegrityMonitorException exception
153 private static DroolsPDPIntegrityMonitor makeMonitor(String resourceName, Properties properties)
154 throws IntegrityMonitorException {
157 return new DroolsPDPIntegrityMonitor(resourceName, properties);
159 } catch (Exception e) {
160 throw new IntegrityMonitorException(e);
165 * Makes a rest server for the Integrity Monitor.
167 * @param testHost host name
168 * @param testPort port
169 * @param properties properties used to configure the rest server
170 * @throws IntegrityMonitorException exception
172 private static void makeRestServer(String testHost, String testPort, Properties properties)
173 throws IntegrityMonitorException {
176 logger.info("init: Starting HTTP server, addr= {}:{}", testHost, testPort);
178 IntegrityMonitorRestServer server = new IntegrityMonitorRestServer();
179 server.init(properties);
181 } catch (Exception e) {
182 logger.error("init: Caught Exception attempting to start server on testPort= {} message:", testPort, e);
183 throw new IntegrityMonitorException(e);
188 * Gets the properties from the property file.
190 * @param configDir directory containing the property file
191 * @return the properties
192 * @throws IntegrityMonitorException exception
194 private static Properties getProperties(String configDir) throws IntegrityMonitorException {
196 return PropertyUtil.getProperties(configDir + "/" + PROPERTIES_NAME);
198 } catch (IOException e) {
199 throw new IntegrityMonitorException(e);
204 * Checks that a property is defined.
206 * @param props set of properties
207 * @param name name of the property to check
208 * @throws IntegrityMonitorException exception
210 private static void checkPropError(Properties props, String name) throws IntegrityMonitorException {
211 String val = props.getProperty(name);
213 missingProperty(name);
216 logPropertyValue(name, val);
220 * Checks a property's value to verify that it matches the expected value. If the property is
221 * not defined, then it is added to the property set, with the expected value. Logs an error if
222 * the property is defined, but does not have the expected value.
224 * @param props set of properties
225 * @param name name of the property to check
226 * @param expected expected/default value
228 private static void addDefaultPropError(Properties props, String name, String expected) {
229 String val = props.getProperty(name);
231 props.setProperty(name, expected);
233 } else if (!val.equals(expected)) {
234 logger.error(INVALID_PROPERTY_VALUE, name, expected);
237 logPropertyValue(name, val);
241 * Checks a property's value to verify that it matches the expected value. If the property is
242 * not defined, then it is added to the property set, with the expected value. Logs a warning if
243 * the property is defined, but does not have the expected value.
245 * @param props set of properties
246 * @param name name of the property to check
247 * @param expected expected/default value
249 private static void addDefaultPropWarn(Properties props, String name, String dflt) {
250 String val = props.getProperty(name);
252 props.setProperty(name, dflt);
254 } else if (!val.equals(dflt)) {
255 logger.warn(INVALID_PROPERTY_VALUE, name, dflt);
258 logPropertyValue(name, val);
262 * Run tests (audits) unique to Drools PDP VM (Database + Repository).
265 public void subsystemTest() throws IntegrityMonitorException {
266 logger.info("DroolsPDPIntegrityMonitor.subsystemTest called");
268 // clear all responses (non-null values indicate an error)
269 for (AuditBase audit : audits) {
270 audit.setResponse(null);
273 // invoke all of the audits
274 for (AuditBase audit : audits) {
276 // invoke the audit (responses are stored within the audit object)
277 audit.invoke(subsystemTestProperties);
278 } catch (Exception e) {
279 logger.error("{} audit error", audit.getName(), e);
280 if (audit.getResponse() == null) {
281 // if there is no current response, use the exception message
282 audit.setResponse(e.getMessage());
287 // will contain list of subsystems where the audit failed
288 String responseMsg = "";
290 // Loop through all of the audits, and see which ones have failed.
291 // NOTE: response information is stored within the audit objects
292 // themselves -- only one can run at a time.
293 for (AuditBase audit : audits) {
294 String response = audit.getResponse();
295 if (response != null) {
296 // the audit has failed -- add subsystem and
297 // and 'responseValue' with the new information
298 responseMsg = responseMsg.concat("\n" + audit.getName() + ": " + response);
302 if (!responseMsg.isEmpty()) {
303 throw new IntegrityMonitorException(responseMsg);
307 /* ============================================================ */
310 * This is the base class for audits invoked in 'subsystemTest'.
312 public abstract static class AuditBase {
314 protected String name;
316 // non-null indicates the error response
317 protected String response;
320 * Constructor - initialize the name, and clear the initial response.
322 * @param name name of the audit
324 public AuditBase(String name) {
326 this.response = null;
332 * @return the name of this audit
334 public String getName() {
341 * @return the response String (non-null indicates the error message)
343 public String getResponse() {
348 * Set the response string to the specified value.
350 * @param value the new value of the response string (null = no errors)
352 public void setResponse(String value) {
357 * Abstract method to invoke the audit.
359 * @param persistenceProperties Used for DB access
360 * @throws Exception passed in by the audit
362 abstract void invoke(Properties persistenceProperties) throws Exception;
365 public static class IntegrityMonitorRestServer implements Startable {
366 protected volatile HttpServletServer server = null;
367 protected volatile Properties integrityMonitorRestServerProperties = null;
369 public void init(Properties props) {
370 this.integrityMonitorRestServerProperties = props;
375 public boolean start() {
377 List<HttpServletServer> servers = HttpServletServerFactoryInstance.getServerFactory()
378 .build(integrityMonitorRestServerProperties);
380 if (!servers.isEmpty()) {
381 server = servers.get(0);
385 } catch (Exception e) {
386 logger.error("Exception building servers", e);
393 private void waitServerStart() {
395 server.waitedStart(5);
396 } catch (Exception e) {
397 logger.error("Exception waiting for servers to start: ", e);
402 public boolean stop() {
405 } catch (Exception e) {
406 logger.error("Exception during stop", e);
413 public void shutdown() {
418 public synchronized boolean isAlive() {
419 return this.integrityMonitorRestServerProperties != null;
424 * Returns the instance.
426 * @return DroolsPDPIntegrityMonitor object
427 * @throws IntegrityMonitorException exception
429 public static DroolsPDPIntegrityMonitor getInstance() throws IntegrityMonitorException {
430 if (logger.isDebugEnabled()) {
431 logger.debug("getInstance() called");
434 String msg = "No DroolsPDPIntegrityMonitor instance exists."
435 + " Please use the method DroolsPDPIntegrityMonitor init(String configDir)";
436 throw new IntegrityMonitorException(msg);