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;
24 import java.io.FileInputStream;
25 import java.net.InetSocketAddress;
26 import java.util.ArrayList;
27 import java.util.Properties;
29 import org.onap.policy.common.im.IntegrityMonitor;
30 import org.onap.policy.common.im.IntegrityMonitorException;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33 import org.onap.policy.drools.core.PolicyContainer;
34 import org.onap.policy.drools.http.server.HttpServletServer;
35 import org.onap.policy.drools.properties.Startable;
36 import org.onap.policy.drools.utils.PropertyUtil;
39 * This class extends 'IntegrityMonitor' for use in the 'Drools PDP'
40 * virtual machine. The included audits are 'Database' and 'Repository'.
42 public class DroolsPDPIntegrityMonitor extends IntegrityMonitor
45 // get an instance of logger
46 private static final Logger logger = LoggerFactory.getLogger(DroolsPDPIntegrityMonitor.class);
48 // static global instance
49 static private DroolsPDPIntegrityMonitor im = null;
51 // list of audits to run
52 static private AuditBase[] audits =
53 new AuditBase[]{DbAudit.getInstance(), RepositoryAudit.getInstance()};
55 static private Properties subsystemTestProperties = null;
57 static private final String PROPERTIES_NAME = "feature-state-management.properties";
59 * Static initialization -- create Drools Integrity Monitor, and
60 * an HTTP server to handle REST 'test' requests
62 static public DroolsPDPIntegrityMonitor init(String configDir) throws Exception
65 logger.info("init: Entering and invoking PropertyUtil.getProperties() on '{}'", configDir);
68 Properties stateManagementProperties =
69 PropertyUtil.getProperties(configDir + "/" + PROPERTIES_NAME);
71 subsystemTestProperties = stateManagementProperties;
73 // fetch and verify definitions of some properties
74 // (the 'IntegrityMonitor' constructor does some additional verification)
76 String resourceName = stateManagementProperties.getProperty("resource.name");
77 String hostPort = stateManagementProperties.getProperty("hostPort");
78 String fpMonitorInterval = stateManagementProperties.getProperty("fp_monitor_interval");
79 String failedCounterThreshold = stateManagementProperties.getProperty("failed_counter_threshold");
80 String testTransInterval = stateManagementProperties.getProperty("test_trans_interval");
81 String writeFpcInterval = stateManagementProperties.getProperty("write_fpc_interval");
82 String siteName = stateManagementProperties.getProperty("site_name");
83 String nodeType = stateManagementProperties.getProperty("node_type");
84 String dependencyGroups = stateManagementProperties.getProperty("dependency_groups");
85 String javaxPersistenceJdbcDriver = stateManagementProperties.getProperty("javax.persistence.jdbc.driver");
86 String javaxPersistenceJdbcUrl = stateManagementProperties.getProperty("javax.persistence.jdbc.url");
87 String javaxPersistenceJdbcUser = stateManagementProperties.getProperty("javax.persistence.jdbc.user");
88 String javaxPersistenceJdbcPassword = stateManagementProperties.getProperty("javax.persistence.jdbc.password");
90 if (resourceName == null)
92 logger.error("init: Missing IntegrityMonitor property: 'resource.name'");
94 ("Missing IntegrityMonitor property: 'resource.name'"));
98 logger.error("init: Missing IntegrityMonitor property: 'hostPort'");
100 ("Missing IntegrityMonitor property: 'hostPort'"));
102 if (fpMonitorInterval == null)
104 logger.error("init: Missing IntegrityMonitor property: 'fp_monitor_interval'");
106 ("Missing IntegrityMonitor property: 'fp_monitor_interval'"));
108 if (failedCounterThreshold == null)
110 logger.error("init: Missing IntegrityMonitor property: 'failed_counter_threshold'");
112 ("Missing IntegrityMonitor property: 'failed_counter_threshold'"));
114 if (testTransInterval == null)
116 logger.error("init: Missing IntegrityMonitor property: 'test_trans_interval'");
118 ("Missing IntegrityMonitor property: 'test_trans_interval'"));
120 if (writeFpcInterval == null)
122 logger.error("init: Missing IntegrityMonitor property: 'write_fpc_interval'");
124 ("Missing IntegrityMonitor property: 'write_fpc_interval'"));
126 if (siteName == null)
128 logger.error("init: Missing IntegrityMonitor property: 'site_name'");
130 ("Missing IntegrityMonitor property: 'site_name'"));
132 if (nodeType == null)
134 logger.error("init: Missing IntegrityMonitor property: 'node_type'");
136 ("Missing IntegrityMonitor property: 'node_type'"));
138 if (dependencyGroups == null)
140 logger.error("init: Missing IntegrityMonitor property: 'dependency_groups'");
142 ("Missing IntegrityMonitor property: 'dependency_groups'"));
144 if (javaxPersistenceJdbcDriver == null)
146 logger.error("init: Missing IntegrityMonitor property: 'javax.persistence.jbdc.driver for xacml DB'");
148 ("Missing IntegrityMonitor property: 'javax.persistence.jbdc.driver for xacml DB'"));
150 if (javaxPersistenceJdbcUrl == null)
152 logger.error("init: Missing IntegrityMonitor property: 'javax.persistence.jbdc.url for xacml DB'");
154 ("Missing IntegrityMonitor property: 'javax.persistence.jbdc.url for xacml DB'"));
156 if (javaxPersistenceJdbcUser == null)
158 logger.error("init: Missing IntegrityMonitor property: 'javax.persistence.jbdc.user for xacml DB'");
160 ("Missing IntegrityMonitor property: 'javax.persistence.jbdc.user for xacml DB'"));
162 if (javaxPersistenceJdbcPassword == null)
164 logger.error("init: Missing IntegrityMonitor property: 'javax.persistence.jbdc.password for xacml DB'");
166 ("Missing IntegrityMonitor property: 'javax.persistence.jbdc.password' for xacml DB'"));
169 // Now that we've validated the properties, create Drools Integrity Monitor
170 // with these properties.
171 im = new DroolsPDPIntegrityMonitor(resourceName,
172 stateManagementProperties);
173 logger.info("init: New DroolsPDPIntegrityMonitor instantiated, hostPort= {}", hostPort);
175 // determine host and port for HTTP server
176 int index = hostPort.lastIndexOf(':');
177 InetSocketAddress addr;
181 addr = new InetSocketAddress(Integer.valueOf(hostPort));
185 addr = new InetSocketAddress
186 (hostPort.substring(0, index),
187 Integer.valueOf(hostPort.substring(index + 1)));
190 // create http server
192 logger.info("init: Starting HTTP server, addr= {}", addr);
193 IntegrityMonitorRestServer server = new IntegrityMonitorRestServer();
195 server.init(stateManagementProperties);
197 System.out.println("init: Started server on hostPort=" + hostPort);
198 } catch (Exception e) {
199 logger.error("init: Caught Exception attempting to start server on hostPort= {}, message = {}",
200 hostPort, e.getMessage());
205 logger.info("init: Exiting and returning DroolsPDPIntegrityMonitor");
210 * Constructor - pass arguments to superclass, but remember properties
211 * @param resourceName unique name of this Integrity Monitor
212 * @param url the JMX URL of the MBean server
213 * @param properties properties used locally, as well as by
215 * @throws Exception (passed from superclass)
217 private DroolsPDPIntegrityMonitor(String resourceName,
218 Properties consolidatedProperties
220 super(resourceName, consolidatedProperties);
224 * Run tests (audits) unique to Drools PDP VM (Database + Repository)
227 public void subsystemTest() throws IntegrityMonitorException
229 logger.info("DroolsPDPIntegrityMonitor.subsystemTest called");
231 // clear all responses (non-null values indicate an error)
232 for (AuditBase audit : audits)
234 audit.setResponse(null);
237 // invoke all of the audits
238 for (AuditBase audit : audits)
242 // invoke the audit (responses are stored within the audit object)
243 audit.invoke(subsystemTestProperties);
247 logger.error("{} audit error", audit.getName(), e);
248 if (audit.getResponse() == null)
250 // if there is no current response, use the exception message
251 audit.setResponse(e.getMessage());
256 // will contain list of subsystems where the audit failed
257 String responseMsg = "";
259 // Loop through all of the audits, and see which ones have failed.
260 // NOTE: response information is stored within the audit objects
261 // themselves -- only one can run at a time.
262 for (AuditBase audit : audits)
264 String response = audit.getResponse();
265 if (response != null)
267 // the audit has failed -- add subsystem and
268 // and 'responseValue' with the new information
269 responseMsg = responseMsg.concat("\n" + audit.getName() + ": " + response);
273 if(!responseMsg.isEmpty()){
274 throw new IntegrityMonitorException(responseMsg);
278 /* ============================================================ */
281 * This is the base class for audits invoked in 'subsystemTest'
283 static public abstract class AuditBase
286 protected String name;
288 // non-null indicates the error response
289 protected String response;
292 * Constructor - initialize the name, and clear the initial response
293 * @param name name of the audit
295 public AuditBase(String name)
298 this.response = null;
302 * @return the name of this audit
304 public String getName()
310 * @return the response String (non-null indicates the error message)
312 public String getResponse()
318 * Set the response string to the specified value
319 * @param value the new value of the response string (null = no errors)
321 public void setResponse(String value)
327 * Abstract method to invoke the audit
328 * @param persistenceProperties Used for DB access
329 * @throws Exception passed in by the audit
331 abstract void invoke(Properties persistenceProperties) throws Exception;
334 public static class IntegrityMonitorRestServer implements Startable {
335 protected volatile HttpServletServer server = null;
336 protected volatile Properties integrityMonitorRestServerProperties = null;
338 public void init(Properties props) {
339 this.integrityMonitorRestServerProperties = props;
344 public boolean start() throws IllegalStateException {
346 ArrayList<HttpServletServer> servers = HttpServletServer.factory.build(integrityMonitorRestServerProperties);
348 if (!servers.isEmpty()) {
349 server = servers.get(0);
352 server.waitedStart(5);
353 } catch (Exception e) {
357 } catch (Exception e) {
365 public boolean stop() throws IllegalStateException {
368 } catch (Exception e) {
376 public void shutdown() throws IllegalStateException {
381 public synchronized boolean isAlive() {
382 return this.integrityMonitorRestServerProperties != null;
386 public static DroolsPDPIntegrityMonitor getInstance() throws Exception{
387 if(logger.isDebugEnabled()){
388 logger.debug("getInstance() called");
391 String msg = "No DroolsPDPIntegrityMonitor instance exists."
392 + " Please use the method DroolsPDPIntegrityMonitor init(String configDir)";
393 throw new Exception(msg);