2 * ============LICENSE_START=======================================================
3 * feature-state-management
4 * ================================================================================
5 * Copyright (C) 2017 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 org.onap.policy.drools.statemanagement.StateManagementProperties;
25 import java.util.ArrayList;
26 import java.util.Properties;
28 import org.onap.policy.common.im.IntegrityMonitor;
29 import org.onap.policy.common.im.IntegrityMonitorException;
30 import org.onap.policy.drools.http.server.HttpServletServer;
31 import org.onap.policy.drools.properties.Startable;
32 import org.onap.policy.drools.utils.PropertyUtil;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
37 * This class extends 'IntegrityMonitor' for use in the 'Drools PDP'
38 * virtual machine. The included audits are 'Database' and 'Repository'.
40 public class DroolsPDPIntegrityMonitor extends IntegrityMonitor
43 private static final String INVALID_PROPERTY_VALUE = "init: property {} does not have the expected value of {}";
45 // get an instance of logger
46 private static final Logger logger = LoggerFactory.getLogger(DroolsPDPIntegrityMonitor.class);
48 // static global instance
49 private static DroolsPDPIntegrityMonitor im = null;
51 // list of audits to run
52 private static AuditBase[] audits =
53 new AuditBase[]{DbAudit.getInstance(), RepositoryAudit.getInstance()};
55 private static Properties subsystemTestProperties = null;
57 private static final String PROPERTIES_NAME = "feature-state-management.properties";
60 * Constructor - pass arguments to superclass, but remember properties
61 * @param resourceName unique name of this Integrity Monitor
62 * @param url the JMX URL of the MBean server
63 * @param properties properties used locally, as well as by
65 * @throws Exception (passed from superclass)
67 private DroolsPDPIntegrityMonitor(String resourceName,
68 Properties consolidatedProperties
70 super(resourceName, consolidatedProperties);
73 private static void missingProperty(String prop) throws StateManagementPropertiesException{
74 String msg = "init: missing IntegrityMonitor property: ".concat(prop);
76 throw new StateManagementPropertiesException(msg);
79 private static void logPropertyValue(String prop, String val){
80 if(logger.isInfoEnabled()){
81 String msg = "\n\n init: property: " + prop + " = " + val + "\n";
86 * Static initialization -- create Drools Integrity Monitor, and
87 * an HTTP server to handle REST 'test' requests
89 public static DroolsPDPIntegrityMonitor init(String configDir) throws Exception
92 logger.info("init: Entering and invoking PropertyUtil.getProperties() on '{}'", configDir);
95 Properties stateManagementProperties =
96 PropertyUtil.getProperties(configDir + "/" + PROPERTIES_NAME);
97 // fetch and verify definitions of some properties
98 // (the 'IntegrityMonitor' constructor does some additional verification)
99 String testHost = stateManagementProperties.getProperty(StateManagementProperties.TEST_HOST);
100 String testPort = stateManagementProperties.getProperty(StateManagementProperties.TEST_PORT);
101 String testServices = stateManagementProperties.getProperty(StateManagementProperties.TEST_SERVICES,
102 StateManagementProperties.TEST_SERVICES_DEFAULT);
103 String testRestClasses = stateManagementProperties.getProperty(StateManagementProperties.TEST_REST_CLASSES,
104 StateManagementProperties.TEST_REST_CLASSES_DEFAULT);
105 String testManaged = stateManagementProperties.getProperty(StateManagementProperties.TEST_MANAGED,
106 StateManagementProperties.TEST_MANAGED_DEFAULT);
107 String testSwagger = stateManagementProperties.getProperty(StateManagementProperties.TEST_SWAGGER,
108 StateManagementProperties.TEST_SWAGGER_DEFAULT);
109 String resourceName = stateManagementProperties.getProperty(StateManagementProperties.RESOURCE_NAME);
110 String fpMonitorInterval = stateManagementProperties.getProperty(StateManagementProperties.FP_MONITOR_INTERVAL);
111 String failedCounterThreshold = stateManagementProperties.getProperty(StateManagementProperties.FAILED_COUNTER_THRESHOLD);
112 String testTransInterval = stateManagementProperties.getProperty(StateManagementProperties.TEST_TRANS_INTERVAL);
113 String writeFpcInterval = stateManagementProperties.getProperty(StateManagementProperties.WRITE_FPC_INTERVAL);
114 String siteName = stateManagementProperties.getProperty(StateManagementProperties.SITE_NAME);
115 String nodeType = stateManagementProperties.getProperty(StateManagementProperties.NODE_TYPE);
116 String dependencyGroups = stateManagementProperties.getProperty(StateManagementProperties.DEPENDENCY_GROUPS);
117 String javaxPersistenceJdbcDriver = stateManagementProperties.getProperty(StateManagementProperties.DB_DRIVER);
118 String javaxPersistenceJdbcUrl = stateManagementProperties.getProperty(StateManagementProperties.DB_URL);
119 String javaxPersistenceJdbcUser = stateManagementProperties.getProperty(StateManagementProperties.DB_USER);
120 String javaxPersistenceJdbcPassword = stateManagementProperties.getProperty(StateManagementProperties.DB_PWD);
122 if (testHost == null){
123 missingProperty(StateManagementProperties.TEST_HOST);
125 if (testPort == null){
126 missingProperty(StateManagementProperties.TEST_PORT);
128 if (!testServices.equals(StateManagementProperties.TEST_SERVICES_DEFAULT)){
129 logger.error(INVALID_PROPERTY_VALUE,
130 StateManagementProperties.TEST_SERVICES,
131 StateManagementProperties.TEST_SERVICES_DEFAULT);
133 if (!testRestClasses.equals(StateManagementProperties.TEST_REST_CLASSES_DEFAULT)){
134 logger.error(INVALID_PROPERTY_VALUE,
135 StateManagementProperties.TEST_REST_CLASSES,
136 StateManagementProperties.TEST_REST_CLASSES_DEFAULT);
138 if (!testManaged.equals(StateManagementProperties.TEST_MANAGED_DEFAULT)){
139 logger.warn(INVALID_PROPERTY_VALUE,
140 StateManagementProperties.TEST_MANAGED,
141 StateManagementProperties.TEST_MANAGED_DEFAULT);
143 if (!testSwagger.equals(StateManagementProperties.TEST_SWAGGER_DEFAULT)){
144 logger.warn(INVALID_PROPERTY_VALUE,
145 StateManagementProperties.TEST_SWAGGER,
146 StateManagementProperties.TEST_SWAGGER_DEFAULT);
148 if (resourceName == null){
149 missingProperty(StateManagementProperties.RESOURCE_NAME);
151 if (fpMonitorInterval == null){
152 missingProperty(StateManagementProperties.FP_MONITOR_INTERVAL);
154 if (failedCounterThreshold == null){
155 missingProperty(StateManagementProperties.FAILED_COUNTER_THRESHOLD);
157 if (testTransInterval == null){
158 missingProperty(StateManagementProperties.TEST_TRANS_INTERVAL);
160 if (writeFpcInterval == null){
161 missingProperty(StateManagementProperties.WRITE_FPC_INTERVAL);
163 if (siteName == null){
164 missingProperty(StateManagementProperties.SITE_NAME);
166 if (nodeType == null){
167 missingProperty(StateManagementProperties.NODE_TYPE);
169 if (dependencyGroups == null){
170 missingProperty(StateManagementProperties.DEPENDENCY_GROUPS);
172 if (javaxPersistenceJdbcDriver == null){
173 missingProperty(StateManagementProperties.DB_DRIVER);
175 if (javaxPersistenceJdbcUrl == null){
176 missingProperty(StateManagementProperties.DB_URL);
178 if (javaxPersistenceJdbcUser == null){
179 missingProperty(StateManagementProperties.DB_USER);
181 if (javaxPersistenceJdbcPassword == null){
182 missingProperty(StateManagementProperties.DB_PWD);
185 //Log the values so we can diagnose any issues
186 logPropertyValue(StateManagementProperties.TEST_HOST,testHost);
187 logPropertyValue(StateManagementProperties.TEST_PORT,testPort);
188 logPropertyValue(StateManagementProperties.TEST_SERVICES,testServices);
189 logPropertyValue(StateManagementProperties.TEST_REST_CLASSES,testRestClasses);
190 logPropertyValue(StateManagementProperties.TEST_MANAGED,testManaged);
191 logPropertyValue(StateManagementProperties.TEST_SWAGGER,testSwagger);
192 logPropertyValue(StateManagementProperties.RESOURCE_NAME,resourceName);
193 logPropertyValue(StateManagementProperties.FP_MONITOR_INTERVAL,fpMonitorInterval);
194 logPropertyValue(StateManagementProperties.FAILED_COUNTER_THRESHOLD,failedCounterThreshold);
195 logPropertyValue(StateManagementProperties.TEST_TRANS_INTERVAL,testTransInterval);
196 logPropertyValue(StateManagementProperties.WRITE_FPC_INTERVAL,writeFpcInterval);
197 logPropertyValue(StateManagementProperties.SITE_NAME,siteName);
198 logPropertyValue(StateManagementProperties.NODE_TYPE,nodeType);
199 logPropertyValue(StateManagementProperties.DEPENDENCY_GROUPS,dependencyGroups);
200 logPropertyValue(StateManagementProperties.DB_DRIVER,javaxPersistenceJdbcDriver);
201 logPropertyValue(StateManagementProperties.DB_URL,javaxPersistenceJdbcUrl);
202 logPropertyValue(StateManagementProperties.DB_USER,javaxPersistenceJdbcUser);
203 logPropertyValue(StateManagementProperties.DB_PWD,javaxPersistenceJdbcPassword);
205 subsystemTestProperties = stateManagementProperties;
207 // Now that we've validated the properties, create Drools Integrity Monitor
208 // with these properties.
209 im = new DroolsPDPIntegrityMonitor(resourceName,
210 stateManagementProperties);
211 logger.info("init: New DroolsPDPIntegrityMonitor instantiated, resourceName = ", resourceName);
213 // create http server
215 logger.info("init: Starting HTTP server, addr= {}", testHost+":"+testPort);
216 IntegrityMonitorRestServer server = new IntegrityMonitorRestServer();
218 server.init(stateManagementProperties);
219 } catch (Exception e) {
220 logger.error("init: Caught Exception attempting to start server on testPort= {} message:",
225 logger.info("init: Exiting and returning DroolsPDPIntegrityMonitor");
230 * Run tests (audits) unique to Drools PDP VM (Database + Repository)
233 public void subsystemTest() throws IntegrityMonitorException
235 logger.info("DroolsPDPIntegrityMonitor.subsystemTest called");
237 // clear all responses (non-null values indicate an error)
238 for (AuditBase audit : audits)
240 audit.setResponse(null);
243 // invoke all of the audits
244 for (AuditBase audit : audits)
248 // invoke the audit (responses are stored within the audit object)
249 audit.invoke(subsystemTestProperties);
253 logger.error("{} audit error", audit.getName(), e);
254 if (audit.getResponse() == null)
256 // if there is no current response, use the exception message
257 audit.setResponse(e.getMessage());
262 // will contain list of subsystems where the audit failed
263 String responseMsg = "";
265 // Loop through all of the audits, and see which ones have failed.
266 // NOTE: response information is stored within the audit objects
267 // themselves -- only one can run at a time.
268 for (AuditBase audit : audits)
270 String response = audit.getResponse();
271 if (response != null)
273 // the audit has failed -- add subsystem and
274 // and 'responseValue' with the new information
275 responseMsg = responseMsg.concat("\n" + audit.getName() + ": " + response);
279 if(!responseMsg.isEmpty()){
280 throw new IntegrityMonitorException(responseMsg);
284 /* ============================================================ */
287 * This is the base class for audits invoked in 'subsystemTest'
289 public abstract static class AuditBase
292 protected String name;
294 // non-null indicates the error response
295 protected String response;
298 * Constructor - initialize the name, and clear the initial response
299 * @param name name of the audit
301 public AuditBase(String name)
304 this.response = null;
308 * @return the name of this audit
310 public String getName()
316 * @return the response String (non-null indicates the error message)
318 public String getResponse()
324 * Set the response string to the specified value
325 * @param value the new value of the response string (null = no errors)
327 public void setResponse(String value)
333 * Abstract method to invoke the audit
334 * @param persistenceProperties Used for DB access
335 * @throws Exception passed in by the audit
337 abstract void invoke(Properties persistenceProperties) throws Exception;
340 public static class IntegrityMonitorRestServer implements Startable {
341 protected volatile HttpServletServer server = null;
342 protected volatile Properties integrityMonitorRestServerProperties = null;
344 public void init(Properties props) {
345 this.integrityMonitorRestServerProperties = props;
350 public boolean start() {
352 ArrayList<HttpServletServer> servers = HttpServletServer.factory.build(integrityMonitorRestServerProperties);
354 if (!servers.isEmpty()) {
355 server = servers.get(0);
359 } catch (Exception e) {
360 logger.error("Exception building servers", e);
367 private void waitServerStart() {
369 server.waitedStart(5);
370 } catch (Exception e) {
371 logger.error("Exception waiting for servers to start: ", e);
376 public boolean stop() {
379 } catch (Exception e) {
380 logger.error("Exception during stop", e);
387 public void shutdown() {
392 public synchronized boolean isAlive() {
393 return this.integrityMonitorRestServerProperties != null;
397 public static DroolsPDPIntegrityMonitor getInstance() throws Exception{
398 if(logger.isDebugEnabled()){
399 logger.debug("getInstance() called");
402 String msg = "No DroolsPDPIntegrityMonitor instance exists."
403 + " Please use the method DroolsPDPIntegrityMonitor init(String configDir)";
404 throw new Exception(msg);