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 java.util.ArrayList;
24 import java.util.Properties;
25 import org.onap.policy.common.im.IntegrityMonitor;
26 import org.onap.policy.common.im.IntegrityMonitorException;
27 import org.onap.policy.drools.http.server.HttpServletServer;
28 import org.onap.policy.drools.properties.Startable;
29 import org.onap.policy.drools.utils.PropertyUtil;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
34 * This class extends 'IntegrityMonitor' for use in the 'Drools PDP'
35 * virtual machine. The included audits are 'Database' and 'Repository'.
37 public class DroolsPDPIntegrityMonitor extends IntegrityMonitor
40 private static final String INVALID_PROPERTY_VALUE = "init: property {} does not have the expected value of {}";
42 // get an instance of logger
43 private static final Logger logger = LoggerFactory.getLogger(DroolsPDPIntegrityMonitor.class);
45 // static global instance
46 private static DroolsPDPIntegrityMonitor im = null;
48 // list of audits to run
49 private static AuditBase[] audits =
50 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
58 * @param resourceName unique name of this Integrity Monitor
59 * @param url the JMX URL of the MBean server
60 * @param properties properties used locally, as well as by
62 * @throws Exception (passed from superclass)
64 private DroolsPDPIntegrityMonitor(String resourceName,
65 Properties consolidatedProperties
67 super(resourceName, consolidatedProperties);
70 private static void missingProperty(String prop) throws StateManagementPropertiesException{
71 String msg = "init: missing IntegrityMonitor property: ".concat(prop);
73 throw new StateManagementPropertiesException(msg);
76 private static void logPropertyValue(String prop, String val){
77 if(logger.isInfoEnabled()){
78 String msg = "\n\n init: property: " + prop + " = " + val + "\n";
84 * Static initialization -- create Drools Integrity Monitor, and
85 * an HTTP server to handle REST 'test' requests
87 public static DroolsPDPIntegrityMonitor init(String configDir) throws Exception
90 logger.info("init: Entering and invoking PropertyUtil.getProperties() on '{}'", configDir);
93 Properties stateManagementProperties =
94 PropertyUtil.getProperties(configDir + "/" + PROPERTIES_NAME);
95 // fetch and verify definitions of some properties
96 // (the 'IntegrityMonitor' constructor does some additional verification)
97 String testHost = stateManagementProperties.getProperty(StateManagementProperties.TEST_HOST);
98 String testPort = stateManagementProperties.getProperty(StateManagementProperties.TEST_PORT);
99 String testServices = stateManagementProperties.getProperty(StateManagementProperties.TEST_SERVICES);
100 String testRestClasses = stateManagementProperties.getProperty(StateManagementProperties.TEST_REST_CLASSES);
101 String testManaged = stateManagementProperties.getProperty(StateManagementProperties.TEST_MANAGED);
102 String testSwagger = stateManagementProperties.getProperty(StateManagementProperties.TEST_SWAGGER);
103 String resourceName = stateManagementProperties.getProperty(StateManagementProperties.RESOURCE_NAME);
104 String fpMonitorInterval = stateManagementProperties.getProperty(StateManagementProperties.FP_MONITOR_INTERVAL);
105 String failedCounterThreshold = stateManagementProperties.getProperty(StateManagementProperties.FAILED_COUNTER_THRESHOLD);
106 String testTransInterval = stateManagementProperties.getProperty(StateManagementProperties.TEST_TRANS_INTERVAL);
107 String writeFpcInterval = stateManagementProperties.getProperty(StateManagementProperties.WRITE_FPC_INTERVAL);
108 String siteName = stateManagementProperties.getProperty(StateManagementProperties.SITE_NAME);
109 String nodeType = stateManagementProperties.getProperty(StateManagementProperties.NODE_TYPE);
110 String dependencyGroups = stateManagementProperties.getProperty(StateManagementProperties.DEPENDENCY_GROUPS);
111 String javaxPersistenceJdbcDriver = stateManagementProperties.getProperty(StateManagementProperties.DB_DRIVER);
112 String javaxPersistenceJdbcUrl = stateManagementProperties.getProperty(StateManagementProperties.DB_URL);
113 String javaxPersistenceJdbcUser = stateManagementProperties.getProperty(StateManagementProperties.DB_USER);
114 String javaxPersistenceJdbcPassword = stateManagementProperties.getProperty(StateManagementProperties.DB_PWD);
116 if (testHost == null){
117 missingProperty(StateManagementProperties.TEST_HOST);
119 if (testPort == null){
120 missingProperty(StateManagementProperties.TEST_PORT);
122 if (testServices == null) {
123 testServices = StateManagementProperties.TEST_SERVICES_DEFAULT;
124 stateManagementProperties.put(StateManagementProperties.TEST_SERVICES, testServices);
126 if (testRestClasses == null) {
127 testRestClasses = StateManagementProperties.TEST_REST_CLASSES_DEFAULT;
128 stateManagementProperties.put(StateManagementProperties.TEST_REST_CLASSES, testRestClasses);
130 if (testManaged == null) {
131 testManaged = StateManagementProperties.TEST_MANAGED_DEFAULT;
132 stateManagementProperties.put(StateManagementProperties.TEST_MANAGED, testManaged);
134 if (testSwagger == null) {
135 testSwagger = StateManagementProperties.TEST_SWAGGER_DEFAULT;
136 stateManagementProperties.put(StateManagementProperties.TEST_SWAGGER, testSwagger);
138 if (!testServices.equals(StateManagementProperties.TEST_SERVICES_DEFAULT)){
139 logger.error(INVALID_PROPERTY_VALUE,
140 StateManagementProperties.TEST_SERVICES,
141 StateManagementProperties.TEST_SERVICES_DEFAULT);
143 if (!testRestClasses.equals(StateManagementProperties.TEST_REST_CLASSES_DEFAULT)){
144 logger.error(INVALID_PROPERTY_VALUE,
145 StateManagementProperties.TEST_REST_CLASSES,
146 StateManagementProperties.TEST_REST_CLASSES_DEFAULT);
148 if (!testManaged.equals(StateManagementProperties.TEST_MANAGED_DEFAULT)){
149 logger.warn(INVALID_PROPERTY_VALUE,
150 StateManagementProperties.TEST_MANAGED,
151 StateManagementProperties.TEST_MANAGED_DEFAULT);
153 if (!testSwagger.equals(StateManagementProperties.TEST_SWAGGER_DEFAULT)){
154 logger.warn(INVALID_PROPERTY_VALUE,
155 StateManagementProperties.TEST_SWAGGER,
156 StateManagementProperties.TEST_SWAGGER_DEFAULT);
158 if (resourceName == null){
159 missingProperty(StateManagementProperties.RESOURCE_NAME);
161 if (fpMonitorInterval == null){
162 missingProperty(StateManagementProperties.FP_MONITOR_INTERVAL);
164 if (failedCounterThreshold == null){
165 missingProperty(StateManagementProperties.FAILED_COUNTER_THRESHOLD);
167 if (testTransInterval == null){
168 missingProperty(StateManagementProperties.TEST_TRANS_INTERVAL);
170 if (writeFpcInterval == null){
171 missingProperty(StateManagementProperties.WRITE_FPC_INTERVAL);
173 if (siteName == null){
174 missingProperty(StateManagementProperties.SITE_NAME);
176 if (nodeType == null){
177 missingProperty(StateManagementProperties.NODE_TYPE);
179 if (dependencyGroups == null){
180 missingProperty(StateManagementProperties.DEPENDENCY_GROUPS);
182 if (javaxPersistenceJdbcDriver == null){
183 missingProperty(StateManagementProperties.DB_DRIVER);
185 if (javaxPersistenceJdbcUrl == null){
186 missingProperty(StateManagementProperties.DB_URL);
188 if (javaxPersistenceJdbcUser == null){
189 missingProperty(StateManagementProperties.DB_USER);
191 if (javaxPersistenceJdbcPassword == null){
192 missingProperty(StateManagementProperties.DB_PWD);
195 //Log the values so we can diagnose any issues
196 logPropertyValue(StateManagementProperties.TEST_HOST,testHost);
197 logPropertyValue(StateManagementProperties.TEST_PORT,testPort);
198 logPropertyValue(StateManagementProperties.TEST_SERVICES,testServices);
199 logPropertyValue(StateManagementProperties.TEST_REST_CLASSES,testRestClasses);
200 logPropertyValue(StateManagementProperties.TEST_MANAGED,testManaged);
201 logPropertyValue(StateManagementProperties.TEST_SWAGGER,testSwagger);
202 logPropertyValue(StateManagementProperties.RESOURCE_NAME,resourceName);
203 logPropertyValue(StateManagementProperties.FP_MONITOR_INTERVAL,fpMonitorInterval);
204 logPropertyValue(StateManagementProperties.FAILED_COUNTER_THRESHOLD,failedCounterThreshold);
205 logPropertyValue(StateManagementProperties.TEST_TRANS_INTERVAL,testTransInterval);
206 logPropertyValue(StateManagementProperties.WRITE_FPC_INTERVAL,writeFpcInterval);
207 logPropertyValue(StateManagementProperties.SITE_NAME,siteName);
208 logPropertyValue(StateManagementProperties.NODE_TYPE,nodeType);
209 logPropertyValue(StateManagementProperties.DEPENDENCY_GROUPS,dependencyGroups);
210 logPropertyValue(StateManagementProperties.DB_DRIVER,javaxPersistenceJdbcDriver);
211 logPropertyValue(StateManagementProperties.DB_URL,javaxPersistenceJdbcUrl);
212 logPropertyValue(StateManagementProperties.DB_USER,javaxPersistenceJdbcUser);
213 logPropertyValue(StateManagementProperties.DB_PWD,javaxPersistenceJdbcPassword);
215 subsystemTestProperties = stateManagementProperties;
217 // Now that we've validated the properties, create Drools Integrity Monitor
218 // with these properties.
219 im = new DroolsPDPIntegrityMonitor(resourceName,
220 stateManagementProperties);
221 logger.info("init: New DroolsPDPIntegrityMonitor instantiated, resourceName = ", resourceName);
223 // create http server
225 logger.info("init: Starting HTTP server, addr= {}", testHost+":"+testPort);
226 IntegrityMonitorRestServer server = new IntegrityMonitorRestServer();
228 server.init(stateManagementProperties);
229 } catch (Exception e) {
230 logger.error("init: Caught Exception attempting to start server on testPort= {} message:",
234 logger.info("init: Exiting and returning DroolsPDPIntegrityMonitor");
239 * Run tests (audits) unique to Drools PDP VM (Database + Repository)
242 public void subsystemTest() throws IntegrityMonitorException
244 logger.info("DroolsPDPIntegrityMonitor.subsystemTest called");
246 // clear all responses (non-null values indicate an error)
247 for (AuditBase audit : audits)
249 audit.setResponse(null);
252 // invoke all of the audits
253 for (AuditBase audit : audits)
257 // invoke the audit (responses are stored within the audit object)
258 audit.invoke(subsystemTestProperties);
262 logger.error("{} audit error", audit.getName(), e);
263 if (audit.getResponse() == null)
265 // if there is no current response, use the exception message
266 audit.setResponse(e.getMessage());
271 // will contain list of subsystems where the audit failed
272 String responseMsg = "";
274 // Loop through all of the audits, and see which ones have failed.
275 // NOTE: response information is stored within the audit objects
276 // themselves -- only one can run at a time.
277 for (AuditBase audit : audits)
279 String response = audit.getResponse();
280 if (response != null)
282 // the audit has failed -- add subsystem and
283 // and 'responseValue' with the new information
284 responseMsg = responseMsg.concat("\n" + audit.getName() + ": " + response);
288 if(!responseMsg.isEmpty()){
289 throw new IntegrityMonitorException(responseMsg);
293 /* ============================================================ */
296 * This is the base class for audits invoked in 'subsystemTest'
298 public abstract static class AuditBase
301 protected String name;
303 // non-null indicates the error response
304 protected String response;
307 * Constructor - initialize the name, and clear the initial response
308 * @param name name of the audit
310 public AuditBase(String name)
313 this.response = null;
317 * @return the name of this audit
319 public String getName()
325 * @return the response String (non-null indicates the error message)
327 public String getResponse()
333 * Set the response string to the specified value
334 * @param value the new value of the response string (null = no errors)
336 public void setResponse(String value)
342 * Abstract method to invoke the audit
343 * @param persistenceProperties Used for DB access
344 * @throws Exception passed in by the audit
346 abstract void invoke(Properties persistenceProperties) throws Exception;
349 public static class IntegrityMonitorRestServer implements Startable {
350 protected volatile HttpServletServer server = null;
351 protected volatile Properties integrityMonitorRestServerProperties = null;
353 public void init(Properties props) {
354 this.integrityMonitorRestServerProperties = props;
359 public boolean start() {
361 ArrayList<HttpServletServer> servers = HttpServletServer.factory.build(integrityMonitorRestServerProperties);
363 if (!servers.isEmpty()) {
364 server = servers.get(0);
368 } catch (Exception e) {
369 logger.error("Exception building servers", e);
376 private void waitServerStart() {
378 server.waitedStart(5);
379 } catch (Exception e) {
380 logger.error("Exception waiting for servers to start: ", e);
385 public boolean stop() {
388 } catch (Exception e) {
389 logger.error("Exception during stop", e);
396 public void shutdown() {
401 public synchronized boolean isAlive() {
402 return this.integrityMonitorRestServerProperties != null;
406 public static DroolsPDPIntegrityMonitor getInstance() throws Exception{
407 if(logger.isDebugEnabled()){
408 logger.debug("getInstance() called");
411 String msg = "No DroolsPDPIntegrityMonitor instance exists."
412 + " Please use the method DroolsPDPIntegrityMonitor init(String configDir)";
413 throw new Exception(msg);