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.net.InetSocketAddress;
24 import java.util.ArrayList;
25 import java.util.Properties;
27 import org.onap.policy.common.im.IntegrityMonitor;
28 import org.onap.policy.common.im.IntegrityMonitorException;
29 import org.onap.policy.drools.http.server.HttpServletServer;
30 import org.onap.policy.drools.properties.Startable;
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'
37 * virtual machine. The included audits are 'Database' and 'Repository'.
39 public class DroolsPDPIntegrityMonitor extends IntegrityMonitor
42 // get an instance of logger
43 private static final Logger logger = LoggerFactory.getLogger(DroolsPDPIntegrityMonitor.class);
45 // static global instance
46 static private DroolsPDPIntegrityMonitor im = null;
48 // list of audits to run
49 static private AuditBase[] audits =
50 new AuditBase[]{DbAudit.getInstance(), RepositoryAudit.getInstance()};
52 static private Properties subsystemTestProperties = null;
54 static private final String PROPERTIES_NAME = "feature-state-management.properties";
56 * Static initialization -- create Drools Integrity Monitor, and
57 * an HTTP server to handle REST 'test' requests
59 static public DroolsPDPIntegrityMonitor init(String configDir) throws Exception
62 logger.info("init: Entering and invoking PropertyUtil.getProperties() on '{}'", configDir);
65 Properties stateManagementProperties =
66 PropertyUtil.getProperties(configDir + "/" + PROPERTIES_NAME);
68 subsystemTestProperties = stateManagementProperties;
70 // fetch and verify definitions of some properties
71 // (the 'IntegrityMonitor' constructor does some additional verification)
73 String resourceName = stateManagementProperties.getProperty("resource.name");
74 String hostPort = stateManagementProperties.getProperty("hostPort");
75 String fpMonitorInterval = stateManagementProperties.getProperty("fp_monitor_interval");
76 String failedCounterThreshold = stateManagementProperties.getProperty("failed_counter_threshold");
77 String testTransInterval = stateManagementProperties.getProperty("test_trans_interval");
78 String writeFpcInterval = stateManagementProperties.getProperty("write_fpc_interval");
79 String siteName = stateManagementProperties.getProperty("site_name");
80 String nodeType = stateManagementProperties.getProperty("node_type");
81 String dependencyGroups = stateManagementProperties.getProperty("dependency_groups");
82 String javaxPersistenceJdbcDriver = stateManagementProperties.getProperty("javax.persistence.jdbc.driver");
83 String javaxPersistenceJdbcUrl = stateManagementProperties.getProperty("javax.persistence.jdbc.url");
84 String javaxPersistenceJdbcUser = stateManagementProperties.getProperty("javax.persistence.jdbc.user");
85 String javaxPersistenceJdbcPassword = stateManagementProperties.getProperty("javax.persistence.jdbc.password");
87 if (resourceName == null)
89 logger.error("init: Missing IntegrityMonitor property: 'resource.name'");
91 ("Missing IntegrityMonitor property: 'resource.name'");
95 logger.error("init: Missing IntegrityMonitor property: 'hostPort'");
97 ("Missing IntegrityMonitor property: 'hostPort'");
99 if (fpMonitorInterval == null)
101 logger.error("init: Missing IntegrityMonitor property: 'fp_monitor_interval'");
103 ("Missing IntegrityMonitor property: 'fp_monitor_interval'");
105 if (failedCounterThreshold == null)
107 logger.error("init: Missing IntegrityMonitor property: 'failed_counter_threshold'");
109 ("Missing IntegrityMonitor property: 'failed_counter_threshold'");
111 if (testTransInterval == null)
113 logger.error("init: Missing IntegrityMonitor property: 'test_trans_interval'");
115 ("Missing IntegrityMonitor property: 'test_trans_interval'");
117 if (writeFpcInterval == null)
119 logger.error("init: Missing IntegrityMonitor property: 'write_fpc_interval'");
121 ("Missing IntegrityMonitor property: 'write_fpc_interval'");
123 if (siteName == null)
125 logger.error("init: Missing IntegrityMonitor property: 'site_name'");
127 ("Missing IntegrityMonitor property: 'site_name'");
129 if (nodeType == null)
131 logger.error("init: Missing IntegrityMonitor property: 'node_type'");
133 ("Missing IntegrityMonitor property: 'node_type'");
135 if (dependencyGroups == null)
137 logger.error("init: Missing IntegrityMonitor property: 'dependency_groups'");
139 ("Missing IntegrityMonitor property: 'dependency_groups'");
141 if (javaxPersistenceJdbcDriver == null)
143 logger.error("init: Missing IntegrityMonitor property: 'javax.persistence.jbdc.driver for xacml DB'");
145 ("Missing IntegrityMonitor property: 'javax.persistence.jbdc.driver for xacml DB'");
147 if (javaxPersistenceJdbcUrl == null)
149 logger.error("init: Missing IntegrityMonitor property: 'javax.persistence.jbdc.url for xacml DB'");
151 ("Missing IntegrityMonitor property: 'javax.persistence.jbdc.url for xacml DB'"));
153 if (javaxPersistenceJdbcUser == null)
155 logger.error("init: Missing IntegrityMonitor property: 'javax.persistence.jbdc.user for xacml DB'");
157 ("Missing IntegrityMonitor property: 'javax.persistence.jbdc.user for xacml DB'");
159 if (javaxPersistenceJdbcPassword == null)
161 logger.error("init: Missing IntegrityMonitor property: 'javax.persistence.jbdc.password for xacml DB'");
163 ("Missing IntegrityMonitor property: 'javax.persistence.jbdc.password' for xacml DB'");
166 // Now that we've validated the properties, create Drools Integrity Monitor
167 // with these properties.
168 im = new DroolsPDPIntegrityMonitor(resourceName,
169 stateManagementProperties);
170 logger.info("init: New DroolsPDPIntegrityMonitor instantiated, hostPort= {}", hostPort);
172 // determine host and port for HTTP server
173 int index = hostPort.lastIndexOf(':');
174 InetSocketAddress addr;
178 addr = new InetSocketAddress(Integer.valueOf(hostPort));
182 addr = new InetSocketAddress
183 (hostPort.substring(0, index),
184 Integer.valueOf(hostPort.substring(index + 1)));
187 // create http server
189 logger.info("init: Starting HTTP server, addr= {}", addr);
190 IntegrityMonitorRestServer server = new IntegrityMonitorRestServer();
192 server.init(stateManagementProperties);
194 System.out.println("init: Started server on hostPort=" + hostPort);
195 } catch (Exception e) {
196 logger.error("init: Caught Exception attempting to start server on hostPort= {}, message = {}",
197 hostPort, e.getMessage());
202 logger.info("init: Exiting and returning DroolsPDPIntegrityMonitor");
207 * Constructor - pass arguments to superclass, but remember properties
208 * @param resourceName unique name of this Integrity Monitor
209 * @param url the JMX URL of the MBean server
210 * @param properties properties used locally, as well as by
212 * @throws Exception (passed from superclass)
214 private DroolsPDPIntegrityMonitor(String resourceName,
215 Properties consolidatedProperties
217 super(resourceName, consolidatedProperties);
221 * Run tests (audits) unique to Drools PDP VM (Database + Repository)
224 public void subsystemTest() throws IntegrityMonitorException
226 logger.info("DroolsPDPIntegrityMonitor.subsystemTest called");
228 // clear all responses (non-null values indicate an error)
229 for (AuditBase audit : audits)
231 audit.setResponse(null);
234 // invoke all of the audits
235 for (AuditBase audit : audits)
239 // invoke the audit (responses are stored within the audit object)
240 audit.invoke(subsystemTestProperties);
244 logger.error("{} audit error", audit.getName(), e);
245 if (audit.getResponse() == null)
247 // if there is no current response, use the exception message
248 audit.setResponse(e.getMessage());
253 // will contain list of subsystems where the audit failed
254 String responseMsg = "";
256 // Loop through all of the audits, and see which ones have failed.
257 // NOTE: response information is stored within the audit objects
258 // themselves -- only one can run at a time.
259 for (AuditBase audit : audits)
261 String response = audit.getResponse();
262 if (response != null)
264 // the audit has failed -- add subsystem and
265 // and 'responseValue' with the new information
266 responseMsg = responseMsg.concat("\n" + audit.getName() + ": " + response);
270 if(!responseMsg.isEmpty()){
271 throw new IntegrityMonitorException(responseMsg);
275 /* ============================================================ */
278 * This is the base class for audits invoked in 'subsystemTest'
280 static public abstract class AuditBase
283 protected String name;
285 // non-null indicates the error response
286 protected String response;
289 * Constructor - initialize the name, and clear the initial response
290 * @param name name of the audit
292 public AuditBase(String name)
295 this.response = null;
299 * @return the name of this audit
301 public String getName()
307 * @return the response String (non-null indicates the error message)
309 public String getResponse()
315 * Set the response string to the specified value
316 * @param value the new value of the response string (null = no errors)
318 public void setResponse(String value)
324 * Abstract method to invoke the audit
325 * @param persistenceProperties Used for DB access
326 * @throws Exception passed in by the audit
328 abstract void invoke(Properties persistenceProperties) throws Exception;
331 public static class IntegrityMonitorRestServer implements Startable {
332 protected volatile HttpServletServer server = null;
333 protected volatile Properties integrityMonitorRestServerProperties = null;
335 public void init(Properties props) {
336 this.integrityMonitorRestServerProperties = props;
341 public boolean start() throws IllegalStateException {
343 ArrayList<HttpServletServer> servers = HttpServletServer.factory.build(integrityMonitorRestServerProperties);
345 if (!servers.isEmpty()) {
346 server = servers.get(0);
349 server.waitedStart(5);
350 } catch (Exception e) {
351 logger.error("Exception waiting for servers to start: ", e);
354 } catch (Exception e) {
355 logger.error("Exception building servers", e);
363 public boolean stop() throws IllegalStateException {
366 } catch (Exception e) {
367 logger.error("Exception during stop", e);
374 public void shutdown() throws IllegalStateException {
379 public synchronized boolean isAlive() {
380 return this.integrityMonitorRestServerProperties != null;
384 public static DroolsPDPIntegrityMonitor getInstance() throws Exception{
385 if(logger.isDebugEnabled()){
386 logger.debug("getInstance() called");
389 String msg = "No DroolsPDPIntegrityMonitor instance exists."
390 + " Please use the method DroolsPDPIntegrityMonitor init(String configDir)";
391 throw new Exception(msg);