* ============LICENSE_START=======================================================
* ONAP-PDP-REST
* ================================================================================
- * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
+ * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
* ================================================================================
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
import org.onap.policy.common.im.AdministrativeStateException;
import org.onap.policy.common.im.ForwardProgressException;
import org.onap.policy.common.im.IntegrityMonitor;
+import org.onap.policy.common.im.IntegrityMonitorException;
import org.onap.policy.common.im.IntegrityMonitorProperties;
import org.onap.policy.common.im.StandbyStatusException;
import org.onap.policy.common.logging.ONAPLoggingContext;
// This thread may getting invoked on startup, to let the PAP know
// that we are up and running.
//
- private Thread registerThread = null;
- private XACMLPdpRegisterThread registerRunnable = null;
+ private static transient Thread registerThread = null;
+ private static transient XACMLPdpRegisterThread registerRunnable = null;
//
// This is our PDP engine pointer. There is a synchronized lock used
// for access to the pointer. In case we are servicing PEP requests while
// This is our configuration thread that attempts to load
// a new configuration request.
//
- private Thread configThread = null;
- private volatile boolean configThreadTerminate = false;
- private ONAPLoggingContext baseLoggingContext = null;
- private IntegrityMonitor im;
+ private static transient Thread configThread = null;
+ private static volatile boolean configThreadTerminate = false;
+ private transient ONAPLoggingContext baseLoggingContext = null;
+ private transient IntegrityMonitor im;
+ public IntegrityMonitor getIm() {
+ return im;
+ }
+
+ public void setIm(IntegrityMonitor im) {
+ this.im = im;
+ }
+
/**
* Default constructor.
*/
// Initialize
//
XACMLRest.xacmlInit(config);
- // Load the Notification Delay.
- try{
- XACMLPdpServlet.notificationDelay = Integer.parseInt(XACMLProperties.getProperty(XACMLRestProperties.PROP_NOTIFICATION_DELAY));
- }catch(Exception e){
- logger.info("Notification Delay Not set. Keeping it 0 as default."+e);
- }
- // Load Queue size.
- int queueSize = 5; // Set default Queue Size here.
+ // Load the Notification Delay.
+ setNotificationDelay();
+ // Load Queue size. Not sure if we really need to have the queue bounded, we should look further into this
+ int queueSize = 50; // Set default Queue Size here.
queueSize = Integer.parseInt(XACMLProperties.getProperty("REQUEST_BUFFER_SIZE",String.valueOf(queueSize)));
- queue = new LinkedBlockingQueue<PutRequest>(queueSize);
+ initQueue(queueSize);
// Load our engine - this will use the latest configuration
// that was saved to disk and set our initial status object.
//
synchronized(pdpEngineLock) {
pdpEngine = engine;
}
+ // Notification will be Sent Here.
+ XACMLPdpLoader.sendNotification();
}
//
// Logging stuff....
baseLoggingContext = new ONAPLoggingContext();
// fixed data that will be the same in all logging output goes here
try {
- String ipaddress = InetAddress.getLocalHost().getHostAddress();
- baseLoggingContext.setServer(ipaddress);
+ String hostname = InetAddress.getLocalHost().getCanonicalHostName();
+ baseLoggingContext.setServer(hostname);
} catch (UnknownHostException e) {
logger.warn(XACMLErrorConstants.ERROR_SYSTEM_ERROR + "Unable to get hostname for logging"+e);
}
}
PolicyLogger.info("\n Properties Given : \n" + properties.toString());
}
- pdpResourceName = properties.getProperty(XACMLRestProperties.PDP_RESOURCE_NAME);
- if(pdpResourceName == null){
- PolicyLogger.error(MessageCodes.MISS_PROPERTY_ERROR, XACMLRestProperties.PDP_RESOURCE_NAME, "xacml.pdp");
- throw new ServletException("pdpResourceName is null");
- }
-
+ setPDPResourceName(properties);
dependencyGroups = properties.getProperty(IntegrityMonitorProperties.DEPENDENCY_GROUPS);
if(dependencyGroups == null){
PolicyLogger.error(MessageCodes.MISS_PROPERTY_ERROR, IntegrityMonitorProperties.DEPENDENCY_GROUPS, "xacml.pdp");
throw new ServletException("dependency_groups is null");
}
- // dependency_groups is a semicolon-delimited list of groups, and
- // each group is a comma-separated list of nodes. For our purposes
- // we just need a list of dependencies without regard to grouping,
- // so split the list into nodes separated by either comma or semicolon.
- dependencyNodes = dependencyGroups.split("[;,]");
- for (int i = 0 ; i < dependencyNodes.length ; i++){
- dependencyNodes[i] = dependencyNodes[i].trim();
- }
+ setDependencyNodes(dependencyGroups);
+
// CreateUpdatePolicy ResourceName
createUpdateResourceName = properties.getProperty("createUpdatePolicy.impl.className", CREATE_UPDATE_POLICY_SERVICE);
PolicyLogger.error(MessageCodes.ERROR_SYSTEM_ERROR, e, "Failed to create IntegrityMonitor" +e);
throw new ServletException(e);
}
-
- environment = XACMLProperties.getProperty("ENVIRONMENT", "DEVL");
- //
- // Kick off our thread to register with the PAP servlet.
- //
- if (Boolean.parseBoolean(XACMLProperties.getProperty(XACMLRestProperties.PROP_PDP_REGISTER))) {
- this.registerRunnable = new XACMLPdpRegisterThread(baseLoggingContext);
- this.registerThread = new Thread(this.registerRunnable);
- this.registerThread.start();
- }
- //
- // This is our thread that manages incoming configuration
- // changes.
- //
- this.configThread = new Thread(this);
- this.configThread.start();
+ startThreads(baseLoggingContext, new Thread(this));
}
- /**
+ private static void startThreads(ONAPLoggingContext baseLoggingContext, Thread thread) {
+ environment = XACMLProperties.getProperty("ENVIRONMENT", "DEVL");
+ //
+ // Kick off our thread to register with the PAP servlet.
+ //
+ if (Boolean.parseBoolean(XACMLProperties.getProperty(XACMLRestProperties.PROP_PDP_REGISTER))) {
+ XACMLPdpServlet.registerRunnable = new XACMLPdpRegisterThread(baseLoggingContext);
+ XACMLPdpServlet.registerThread = new Thread(XACMLPdpServlet.registerRunnable);
+ XACMLPdpServlet.registerThread.start();
+ }
+ //
+ // This is our thread that manages incoming configuration
+ // changes.
+ //
+ XACMLPdpServlet.configThread = thread;
+ XACMLPdpServlet.configThread.start();
+ }
+
+ private static void setDependencyNodes(String dependencyGroups) {
+ // dependency_groups is a semicolon-delimited list of groups, and
+ // each group is a comma-separated list of nodes. For our purposes
+ // we just need a list of dependencies without regard to grouping,
+ // so split the list into nodes separated by either comma or semicolon.
+ dependencyNodes = dependencyGroups.split("[;,]");
+ for (int i = 0 ; i < dependencyNodes.length ; i++){
+ dependencyNodes[i] = dependencyNodes[i].trim();
+ }
+ }
+
+ private static void setPDPResourceName(Properties properties) throws ServletException {
+ pdpResourceName = properties.getProperty(XACMLRestProperties.PDP_RESOURCE_NAME);
+ if(pdpResourceName == null){
+ PolicyLogger.error(MessageCodes.MISS_PROPERTY_ERROR, XACMLRestProperties.PDP_RESOURCE_NAME, "xacml.pdp");
+ throw new ServletException("pdpResourceName is null");
+ }
+ }
+
+ private static void initQueue(int queueSize) {
+ queue = new LinkedBlockingQueue<>(queueSize);
+ }
+
+ private static void setNotificationDelay() {
+ try{
+ XACMLPdpServlet.notificationDelay = Integer.parseInt(XACMLProperties.getProperty(XACMLRestProperties.PROP_NOTIFICATION_DELAY));
+ }catch(NumberFormatException e){
+ logger.error("Error in notification delay format, Taking the default value.", e);
+ }
+ }
+
+ /**
* @see Servlet#destroy()
*/
@Override
//
// Make sure the register thread is not running
//
- if (this.registerRunnable != null) {
+ if (XACMLPdpServlet.registerRunnable != null) {
try {
- this.registerRunnable.terminate();
- if (this.registerThread != null) {
- this.registerThread.interrupt();
- this.registerThread.join();
+ XACMLPdpServlet.registerRunnable.terminate();
+ if (XACMLPdpServlet.registerThread != null) {
+ XACMLPdpServlet.registerThread.interrupt();
+ XACMLPdpServlet.registerThread.join();
}
} catch (InterruptedException e) {
logger.error(XACMLErrorConstants.ERROR_SYSTEM_ERROR + e);
PolicyLogger.error(MessageCodes.ERROR_SYSTEM_ERROR, e, "");
+ XACMLPdpServlet.registerThread.interrupt();
}
}
//
// Make sure the configure thread is not running
//
- this.configThreadTerminate = true;
+ setConfigThreadTerminate(true);
try {
- this.configThread.interrupt();
- this.configThread.join();
+ XACMLPdpServlet.configThread.interrupt();
+ XACMLPdpServlet.configThread.join();
} catch (InterruptedException e) {
logger.error(XACMLErrorConstants.ERROR_SYSTEM_ERROR + e);
PolicyLogger.error(MessageCodes.ERROR_SYSTEM_ERROR, e, "");
+ XACMLPdpServlet.configThread.interrupt();
}
logger.info("Destroyed.");
}
- /**
+ private static void setConfigThreadTerminate(boolean value) {
+ XACMLPdpServlet.configThreadTerminate = value;
+ }
+
+ /**
* PUT - The PAP engine sends configuration information using HTTP PUT request.
*
* One parameter is expected:
try {
im.startTransaction();
}
- catch (AdministrativeStateException | StandbyStatusException e) {
+ catch (IntegrityMonitorException e) {
String message = e.toString();
PolicyLogger.error(MessageCodes.ERROR_SYSTEM_ERROR, message + e);
loggingContext.transactionEnded();
im.endTransaction();
return;
}
+ logger.info("XACMLPdpServlet: calling doPutConfig to add properties to the queue");
this.doPutConfig(cache, request, response, loggingContext);
loggingContext.transactionEnded();
PolicyLogger.audit("Transaction ended");
protected void doPutConfig(String config, HttpServletRequest request, HttpServletResponse response, ONAPLoggingContext loggingContext) throws ServletException, IOException {
try {
// prevent multiple configuration changes from stacking up
+ logger.info("XACMLPdpServlet: checking remainingCapacity of Queue.");
if (XACMLPdpServlet.queue.remainingCapacity() <= 0) {
logger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Queue capacity reached");
PolicyLogger.error(MessageCodes.ERROR_PROCESS_FLOW, "Queue capacity reached");
response.sendError(HttpServletResponse.SC_BAD_REQUEST, "PUT with cache=policies must contain at least one policy property");
return;
}
+ logger.info("XACMLPdpServlet: offer policies to queue. No pip properties added.");
XACMLPdpServlet.queue.offer(new PutRequest(newProperties, null));
loggingContext.transactionEnded();
auditLogger.info("Success");
response.sendError(HttpServletResponse.SC_BAD_REQUEST, "PUT with cache=pips must contain at least one pip property");
return;
}
+ logger.info("XACMLPdpServlet: offer pips to queue. No policy properties added.");
XACMLPdpServlet.queue.offer(new PutRequest(null, newProperties));
loggingContext.transactionEnded();
auditLogger.info("Success");
response.sendError(HttpServletResponse.SC_BAD_REQUEST, "PUT with cache=all must contain at least one pip property");
return;
}
+ logger.info("XACMLPdpServlet: offer policies and pips to queue.");
XACMLPdpServlet.queue.offer(new PutRequest(newPolicyProperties, newPipProperties));
loggingContext.transactionEnded();
auditLogger.info("Success");
PolicyLogger.audit("Success");
+
} else {
//
// Invalid value
try {
im.startTransaction();
}
- catch (AdministrativeStateException | StandbyStatusException e) {
+ catch (IntegrityMonitorException e) {
String message = e.toString();
PolicyLogger.error(MessageCodes.ERROR_SYSTEM_ERROR, message);
loggingContext.transactionEnded();
try {
im.startTransaction();
}
- catch (AdministrativeStateException | StandbyStatusException e) {
+ catch (IntegrityMonitorException e) {
String message = e.toString();
PolicyLogger.error(MessageCodes.ERROR_SYSTEM_ERROR, message + e);
loggingContext.transactionEnded();
// Read in the string
//
StringBuilder buffer = new StringBuilder();
- BufferedReader reader = new BufferedReader(new InputStreamReader(request.getInputStream()));
+ BufferedReader reader = null;
+ try{
+ reader = new BufferedReader(new InputStreamReader(request.getInputStream()));
+ }catch(IOException e){
+ logger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error during reading input stream",e);
+ return;
+ }
String line;
try{
while((line = reader.readLine()) != null){
//
try {
// variable not used, but constructor has needed side-effects so don't remove:
- while (! this.configThreadTerminate) {
+ while (! XACMLPdpServlet.configThreadTerminate) {
+ logger.info("XACMLPdpServlet: Taking requests from the queue");
PutRequest request = XACMLPdpServlet.queue.take();
+ logger.info("XACMLPdpServlet: Taking requests from the queue COMPLETED");
StdPDPStatus newStatus = new StdPDPStatus();
PDPEngine newEngine = null;
synchronized(pdpStatusLock) {
XACMLPdpServlet.status.setStatus(Status.UPDATING_CONFIGURATION);
+
+ logger.info("created new PDPEngine");
newEngine = XACMLPdpLoader.loadEngine(newStatus, request.policyProperties, request.pipConfigProperties);
}
if (newEngine != null) {
+ logger.info("XACMLPdpServlet: newEngine created, assigning newEngine to the pdpEngine.");
synchronized(XACMLPdpServlet.pdpEngineLock) {
XACMLPdpServlet.pdpEngine = newEngine;
try {
logger.info("Saving configuration.");
if (request.policyProperties != null) {
+ logger.info("Saving configuration: Policy Properties: " + request.policyProperties);
try (OutputStream os = Files.newOutputStream(XACMLPdpLoader.getPDPPolicyCache())) {
request.policyProperties.store(os, "");
}
}
if (request.pipConfigProperties != null) {
+ logger.info("Saving configuration: PIP Properties: " + request.pipConfigProperties);
try (OutputStream os = Files.newOutputStream(XACMLPdpLoader.getPIPConfig())) {
request.pipConfigProperties.store(os, "");
}
synchronized(pdpStatusLock) {
XACMLPdpServlet.status.set(newStatus);
}
+ logger.info("New PDP Servlet Status: " + newStatus.getStatus());
+ if (Status.UP_TO_DATE.equals(newStatus.getStatus())) {
+ // Notification will be Sent Here.
+ XACMLPdpLoader.sendNotification();
+ }
}
} catch (InterruptedException e) {
logger.error(XACMLErrorConstants.ERROR_SYSTEM_ERROR + "interrupted"+e);