2 * ============LICENSE_START=======================================================
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.pap.xacml.rest;
24 import java.io.FileInputStream;
25 import java.io.IOException;
26 import java.io.InputStream;
27 import java.io.OutputStream;
28 import java.io.UnsupportedEncodingException;
29 import java.net.ConnectException;
30 import java.net.HttpURLConnection;
31 import java.net.InetAddress;
32 import java.net.MalformedURLException;
33 import java.net.SocketTimeoutException;
35 import java.net.URLDecoder;
36 import java.net.UnknownHostException;
37 import java.nio.file.Files;
38 import java.nio.file.Path;
39 import java.nio.file.Paths;
40 import java.util.ArrayList;
41 import java.util.HashMap;
42 import java.util.HashSet;
43 import java.util.Iterator;
44 import java.util.List;
45 import java.util.Properties;
47 import java.util.UUID;
48 import java.util.concurrent.CopyOnWriteArrayList;
50 import javax.persistence.EntityManagerFactory;
51 import javax.persistence.Persistence;
52 import javax.persistence.PersistenceException;
53 import javax.servlet.Servlet;
54 import javax.servlet.ServletConfig;
55 import javax.servlet.ServletException;
56 import javax.servlet.annotation.WebInitParam;
57 import javax.servlet.annotation.WebServlet;
58 import javax.servlet.http.HttpServlet;
59 import javax.servlet.http.HttpServletRequest;
60 import javax.servlet.http.HttpServletResponse;
62 import org.apache.commons.io.IOUtils;
63 import org.onap.policy.common.ia.IntegrityAudit;
64 import org.onap.policy.common.im.AdministrativeStateException;
65 import org.onap.policy.common.im.ForwardProgressException;
66 import org.onap.policy.common.im.IntegrityMonitor;
67 import org.onap.policy.common.im.IntegrityMonitorProperties;
68 import org.onap.policy.common.im.StandbyStatusException;
69 import org.onap.policy.common.logging.ONAPLoggingContext;
70 import org.onap.policy.common.logging.ONAPLoggingUtils;
71 import org.onap.policy.common.logging.eelf.MessageCodes;
72 import org.onap.policy.common.logging.eelf.PolicyLogger;
73 import org.onap.policy.common.logging.flexlogger.FlexLogger;
74 import org.onap.policy.common.logging.flexlogger.Logger;
75 import org.onap.policy.pap.xacml.rest.components.AutoPushPolicy;
76 import org.onap.policy.pap.xacml.rest.components.PolicyDBDao;
77 import org.onap.policy.pap.xacml.rest.components.PolicyDBDaoTransaction;
78 import org.onap.policy.pap.xacml.rest.handler.APIRequestHandler;
79 import org.onap.policy.pap.xacml.rest.handler.PushPolicyHandler;
80 import org.onap.policy.pap.xacml.rest.handler.SavePolicyHandler;
81 import org.onap.policy.pap.xacml.restAuth.CheckPDP;
82 import org.onap.policy.rest.XACMLRest;
83 import org.onap.policy.rest.XACMLRestProperties;
84 import org.onap.policy.utils.PolicyUtils;
85 import org.onap.policy.xacml.api.XACMLErrorConstants;
86 import org.onap.policy.xacml.api.pap.ONAPPapEngineFactory;
87 import org.onap.policy.xacml.api.pap.OnapPDP;
88 import org.onap.policy.xacml.api.pap.OnapPDPGroup;
89 import org.onap.policy.xacml.api.pap.PAPPolicyEngine;
90 import org.onap.policy.xacml.std.pap.StdPAPPolicy;
91 import org.onap.policy.xacml.std.pap.StdPDP;
92 import org.onap.policy.xacml.std.pap.StdPDPGroup;
93 import org.onap.policy.xacml.std.pap.StdPDPItemSetChangeNotifier.StdItemSetChangeListener;
94 import org.onap.policy.xacml.std.pap.StdPDPPolicy;
95 import org.onap.policy.xacml.std.pap.StdPDPStatus;
97 import com.att.research.xacml.api.pap.PAPException;
98 import com.att.research.xacml.api.pap.PDPPolicy;
99 import com.att.research.xacml.api.pap.PDPStatus;
100 import com.att.research.xacml.util.FactoryException;
101 import com.att.research.xacml.util.XACMLProperties;
102 import com.fasterxml.jackson.databind.ObjectMapper;
103 import com.google.common.base.Splitter;
106 * Servlet implementation class XacmlPapServlet
109 description = "Implements the XACML PAP RESTful API.",
110 urlPatterns = { "/" },
113 @WebInitParam(name = "XACML_PROPERTIES_NAME", value = "xacml.pap.properties", description = "The location of the properties file holding configuration information.")
115 public class XACMLPapServlet extends HttpServlet implements StdItemSetChangeListener, Runnable {
116 private static final long serialVersionUID = 1L;
117 private static final Logger LOGGER = FlexLogger.getLogger(XACMLPapServlet.class);
118 // audit (transaction) LOGGER
119 private static final Logger auditLogger = FlexLogger.getLogger("auditLogger");
120 //Persistence Unit for JPA
121 private static final String PERSISTENCE_UNIT = "XACML-PAP-REST";
122 private static final String AUDIT_PAP_PERSISTENCE_UNIT = "auditPapPU";
124 private static final String ENVIRONMENT_HEADER = "Environment";
126 * List of Admin Console URLs.
127 * Used to send notifications when configuration changes.
129 * The CopyOnWriteArrayList *should* protect from concurrency errors.
130 * This list is seldom changed but often read, so the costs of this approach make sense.
132 private static final CopyOnWriteArrayList<String> adminConsoleURLStringList = new CopyOnWriteArrayList<String>();
134 private static String CONFIG_HOME;
135 private static String ACTION_HOME;
137 * This PAP instance's own URL.
138 * Need this when creating URLs to send to the PDPs so they can GET the Policy files from this process.
140 private static String papURL = null;
141 // The heartbeat thread.
142 private static Heartbeat heartbeat = null;
143 private static Thread heartbeatThread = null;
144 //The entity manager factory for JPA access
145 private static EntityManagerFactory emf;
146 private static PolicyDBDao policyDBDao;
148 * papEngine - This is our engine workhorse that manages the PDP Groups and Nodes.
150 private static PAPPolicyEngine papEngine = null;
152 * These are the parameters needed for DB access from the PAP
154 private static int papIntegrityAuditPeriodSeconds = -1;
155 private static String papDbDriver = null;
156 private static String papDbUrl = null;
157 private static String papDbUser = null;
158 private static String papDbPassword = null;
159 private static Integer papTransWait = null;
160 private static Integer papTransTimeout = null;
161 private static Integer papAuditTimeout = null;
162 private static Boolean papAuditFlag = null;
163 private static Boolean papFileSystemAudit = null;
164 private static Boolean autoPushFlag = false;
165 private static String papResourceName = null;
166 private static Integer fpMonitorInterval = null;
167 private static Integer failedCounterThreshold = null;
168 private static Integer testTransInterval = null;
169 private static Integer writeFpcInterval = null;
170 private static String papSiteName=null;
171 private static String papNodeType=null;
172 private static String papDependencyGroups = null;
173 private static String[] papDependencyGroupsFlatArray = null;
174 private static String environment = null;
175 private static String pdpFile = null;
177 private String storedRequestId = null;
178 private IntegrityMonitor im;
179 private IntegrityAudit ia;
181 //MicroService Model Properties
182 private static String msOnapName;
183 private static String msPolicyName;
185 * This thread may be invoked upon startup to initiate sending PDP policy/pip configuration when
186 * this servlet starts. Its configurable by the admin.
188 private Thread initiateThread = null;
189 private ONAPLoggingContext baseLoggingContext = null;
190 private AutoPushPolicy autoPushPolicy;
193 * @see HttpServlet#HttpServlet()
195 public XACMLPapServlet() {
200 * @see Servlet#init(ServletConfig)
202 public void init(ServletConfig config) throws ServletException {
205 baseLoggingContext = new ONAPLoggingContext();
206 // fixed data that will be the same in all logging output goes here
208 String hostname = InetAddress.getLocalHost().getCanonicalHostName();
209 baseLoggingContext.setServer(hostname);
210 } catch (UnknownHostException e) {
211 LOGGER.warn(XACMLErrorConstants.ERROR_SYSTEM_ERROR + "Unable to get hostname for logging");
214 XACMLRest.xacmlInit(config);
215 // Load the properties
216 XACMLRest.loadXacmlProperties(null, null);
218 * Retrieve the property values
220 CONFIG_HOME = getConfigHome();
221 ACTION_HOME = getActionHome();
222 papDbDriver = XACMLProperties.getProperty(XACMLRestProperties.PROP_PAP_DB_DRIVER);
223 if(papDbDriver == null){
224 PolicyLogger.error(MessageCodes.ERROR_DATA_ISSUE,"XACMLPapServlet", " ERROR: Bad papDbDriver property entry");
225 throw new PAPException("papDbDriver is null");
227 setPapDbDriver(papDbDriver);
228 papDbUrl = XACMLProperties.getProperty(XACMLRestProperties.PROP_PAP_DB_URL);
229 if(papDbUrl == null){
230 PolicyLogger.error(MessageCodes.ERROR_DATA_ISSUE,"XACMLPapServlet", " ERROR: Bad papDbUrl property entry");
231 throw new PAPException("papDbUrl is null");
233 setPapDbUrl(papDbUrl);
234 papDbUser = XACMLProperties.getProperty(XACMLRestProperties.PROP_PAP_DB_USER);
235 if(papDbUser == null){
236 PolicyLogger.error(MessageCodes.ERROR_DATA_ISSUE,"XACMLPapServlet", " ERROR: Bad papDbUser property entry");
237 throw new PAPException("papDbUser is null");
239 setPapDbUser(papDbUser);
240 papDbPassword = XACMLProperties.getProperty(XACMLRestProperties.PROP_PAP_DB_PASSWORD);
241 if(papDbPassword == null){
242 PolicyLogger.error(MessageCodes.ERROR_DATA_ISSUE,"XACMLPapServlet", " ERROR: Bad papDbPassword property entry");
243 throw new PAPException("papDbPassword is null");
245 setPapDbPassword(papDbPassword);
246 papResourceName = XACMLProperties.getProperty(XACMLRestProperties.PAP_RESOURCE_NAME);
247 if(papResourceName == null){
248 PolicyLogger.error(MessageCodes.ERROR_DATA_ISSUE,"XACMLPapServlet", " ERROR: Bad papResourceName property entry");
249 throw new PAPException("papResourceName is null");
251 papSiteName = XACMLProperties.getProperty(XACMLRestProperties.PAP_SITE_NAME);
252 if(papSiteName == null){
253 PolicyLogger.error(MessageCodes.ERROR_DATA_ISSUE,"XACMLPapServlet", " ERROR: Bad papSiteName property entry");
254 throw new PAPException("papSiteName is null");
256 papNodeType = XACMLProperties.getProperty(XACMLRestProperties.PAP_NODE_TYPE);
257 if(papNodeType == null){
258 PolicyLogger.error(MessageCodes.ERROR_DATA_ISSUE,"XACMLPapServlet", " ERROR: Bad papNodeType property entry");
259 throw new PAPException("papNodeType is null");
261 environment = XACMLProperties.getProperty("ENVIRONMENT", "DEVL");
262 //Integer will throw an exception of anything is missing or unrecognized
263 papTransWait = Integer.parseInt(XACMLProperties.getProperty(XACMLRestProperties.PROP_PAP_TRANS_WAIT));
264 papTransTimeout = Integer.parseInt(XACMLProperties.getProperty(XACMLRestProperties.PROP_PAP_TRANS_TIMEOUT));
265 papAuditTimeout = Integer.parseInt(XACMLProperties.getProperty(XACMLRestProperties.PROP_PAP_AUDIT_TIMEOUT));
266 //Boolean will default to false if anything is missing or unrecognized
267 papAuditFlag = Boolean.parseBoolean(XACMLProperties.getProperty(XACMLRestProperties.PROP_PAP_RUN_AUDIT_FLAG));
268 papFileSystemAudit = Boolean.parseBoolean(XACMLProperties.getProperty(XACMLRestProperties.PROP_PAP_AUDIT_FLAG));
270 autoPushFlag = Boolean.parseBoolean(XACMLProperties.getProperty(XACMLRestProperties.PROP_PAP_PUSH_FLAG));
271 // if Auto push then Load with properties.
275 file = XACMLProperties.getProperty(XACMLRestProperties.PROP_PAP_PUSH_FILE);
276 if(file.endsWith(".properties")){
277 autoPushPolicy = new AutoPushPolicy(file);
279 throw new PAPException();
282 PolicyLogger.error(MessageCodes.ERROR_DATA_ISSUE + " Missing property or not a proper property file check for: " + XACMLRestProperties.PROP_PAP_PUSH_FILE );
283 LOGGER.info("Overriding the autoPushFlag to False...");
284 autoPushFlag = false;
287 papDependencyGroups = XACMLProperties.getProperty(XACMLRestProperties.PAP_DEPENDENCY_GROUPS);
288 if(papDependencyGroups == null){
289 throw new PAPException("papDependencyGroups is null");
292 //Now we have flattened the array into a simple comma-separated list
293 papDependencyGroupsFlatArray = papDependencyGroups.split("[;,]");
294 //clean up the entries
295 for (int i = 0 ; i < papDependencyGroupsFlatArray.length ; i ++){
296 papDependencyGroupsFlatArray[i] = papDependencyGroupsFlatArray[i].trim();
299 if(XACMLProperties.getProperty(XACMLRestProperties.PAP_INTEGRITY_AUDIT_PERIOD_SECONDS) != null){
300 papIntegrityAuditPeriodSeconds = Integer.parseInt(XACMLProperties.getProperty(XACMLRestProperties.PAP_INTEGRITY_AUDIT_PERIOD_SECONDS).trim());
303 String msg = "integrity_audit_period_seconds ";
304 LOGGER.error("\n\nERROR: " + msg + "Bad property entry: " + e.getMessage() + "\n");
305 PolicyLogger.error(MessageCodes.EXCEPTION_ERROR, e, "XACMLPapServlet", " ERROR: " + msg +"Bad property entry");
309 PolicyLogger.error(MessageCodes.EXCEPTION_ERROR, e, "XACMLPapServlet", " ERROR: Bad property entry");
312 //Integer will throw an exception of anything is missing or unrecognized
313 fpMonitorInterval = Integer.parseInt(XACMLProperties.getProperty(IntegrityMonitorProperties.FP_MONITOR_INTERVAL));
314 failedCounterThreshold = Integer.parseInt(XACMLProperties.getProperty(IntegrityMonitorProperties.FAILED_COUNTER_THRESHOLD));
315 testTransInterval = Integer.parseInt(XACMLProperties.getProperty(IntegrityMonitorProperties.TEST_TRANS_INTERVAL));
316 writeFpcInterval = Integer.parseInt(XACMLProperties.getProperty(IntegrityMonitorProperties.WRITE_FPC_INTERVAL));
317 LOGGER.debug("\n\n\n**************************************"
318 + "\n**************************************"
320 + "\n papDbDriver = " + papDbDriver
321 + "\n papDbUrl = " + papDbUrl
322 + "\n papDbUser = " + papDbUser
323 + "\n papDbPassword = " + papDbPassword
324 + "\n papTransWait = " + papTransWait
325 + "\n papTransTimeout = " + papTransTimeout
326 + "\n papAuditTimeout = " + papAuditTimeout
327 + "\n papAuditFlag = " + papAuditFlag
328 + "\n papFileSystemAudit = " + papFileSystemAudit
329 + "\n autoPushFlag = " + autoPushFlag
330 + "\n papResourceName = " + papResourceName
331 + "\n fpMonitorInterval = " + fpMonitorInterval
332 + "\n failedCounterThreshold = " + failedCounterThreshold
333 + "\n testTransInterval = " + testTransInterval
334 + "\n writeFpcInterval = " + writeFpcInterval
335 + "\n papSiteName = " + papSiteName
336 + "\n papNodeType = " + papNodeType
337 + "\n papDependencyGroupsList = " + papDependencyGroups
338 + "\n papIntegrityAuditPeriodSeconds = " + papIntegrityAuditPeriodSeconds
339 + "\n\n**************************************"
340 + "\n**************************************");
341 // Pull custom persistence settings
342 Properties properties;
344 properties = XACMLProperties.getProperties();
345 LOGGER.debug("\n\n\n**************************************"
346 + "\n**************************************"
348 + "properties = " + properties
349 + "\n\n**************************************");
350 } catch (IOException e) {
351 PolicyLogger.error(MessageCodes.ERROR_DATA_ISSUE, e, "XACMLPapServlet", " Error loading properties with: "
352 + "XACMLProperties.getProperties()");
353 throw new ServletException(e.getMessage(), e.getCause());
355 //Micro Service Properties
356 msOnapName=properties.getProperty("xacml.policy.msOnapName");
357 setMsOnapName(msOnapName);
358 msPolicyName=properties.getProperty("xacml.policy.msPolicyName");
359 setMsPolicyName(msPolicyName);
360 // PDPId File location
361 XACMLPapServlet.pdpFile = XACMLProperties.getProperty(XACMLRestProperties.PROP_PDP_IDFILE);
362 if (XACMLPapServlet.pdpFile == null) {
363 PolicyLogger.error(MessageCodes.ERROR_DATA_ISSUE + " The PDP Id Authentication File Property is not valid: "
364 + XACMLRestProperties.PROP_PDP_IDFILE);
365 throw new PAPException("The PDP Id Authentication File Property :"+ XACMLRestProperties.PROP_PDP_IDFILE+ " is not Valid. ");
367 // Create an IntegrityMonitor
368 im = IntegrityMonitor.getInstance(papResourceName,properties);
369 // Create an IntegrityAudit
370 ia = new IntegrityAudit(papResourceName, AUDIT_PAP_PERSISTENCE_UNIT, properties);
371 ia.startAuditThread();
372 // Create the entity manager factory
373 emf = Persistence.createEntityManagerFactory(PERSISTENCE_UNIT, properties);
375 PolicyLogger.error(MessageCodes.ERROR_DATA_ISSUE + " Error creating entity manager factory with persistence unit: "
377 throw new ServletException("Unable to create Entity Manager Factory");
379 // we are about to call the PDPs and give them their configuration.
380 // To do that we need to have the URL of this PAP so we can construct the Policy file URLs
381 XACMLPapServlet.papURL = XACMLProperties.getProperty(XACMLRestProperties.PROP_PAP_URL);
382 //Create the policyDBDao
383 policyDBDao = PolicyDBDao.getPolicyDBDaoInstance(getEmf());
384 // Load our PAP engine, first create a factory
385 ONAPPapEngineFactory factory = ONAPPapEngineFactory.newInstance(XACMLProperties.getProperty(XACMLProperties.PROP_PAP_PAPENGINEFACTORY));
386 // The factory knows how to go about creating a PAP Engine
387 XACMLPapServlet.papEngine = (PAPPolicyEngine) factory.newEngine();
388 PolicyDBDaoTransaction addNewGroup = null;
390 if(((org.onap.policy.xacml.std.pap.StdEngine)papEngine).wasDefaultGroupJustAdded){
391 addNewGroup = policyDBDao.getNewTransaction();
392 OnapPDPGroup group = papEngine.getDefaultGroup();
393 addNewGroup.createGroup(group.getId(), group.getName(), group.getDescription(), "automaticallyAdded");
394 addNewGroup.commitTransaction();
395 addNewGroup = policyDBDao.getNewTransaction();
396 addNewGroup.changeDefaultGroup(group, "automaticallyAdded");
397 addNewGroup.commitTransaction();
399 } catch(Exception e){
400 PolicyLogger.error(MessageCodes.EXCEPTION_ERROR, e, "XACMLPapServlet", " Error creating new default group in the database");
401 if(addNewGroup != null){
402 addNewGroup.rollbackTransaction();
405 policyDBDao.setPapEngine((PAPPolicyEngine) XACMLPapServlet.papEngine);
406 //boolean performFileToDatabaseAudit = false;
407 if (Boolean.parseBoolean(XACMLProperties.getProperty(XACMLRestProperties.PROP_PAP_RUN_AUDIT_FLAG))){
408 //get an AuditTransaction to lock out all other transactions
409 PolicyDBDaoTransaction auditTrans = policyDBDao.getNewAuditTransaction();
410 policyDBDao.auditLocalDatabase(XACMLPapServlet.papEngine);
411 //release the transaction lock
415 // Sanity check for URL.
416 if (XACMLPapServlet.papURL == null) {
417 throw new PAPException("The property " + XACMLRestProperties.PROP_PAP_URL + " is not valid: " + XACMLPapServlet.papURL);
419 // Configurable - have the PAP servlet initiate sending the latest PDP policy/pip configuration
420 // to all its known PDP nodes.
421 if (Boolean.parseBoolean(XACMLProperties.getProperty(XACMLRestProperties.PROP_PAP_INITIATE_PDP_CONFIG))) {
422 this.initiateThread = new Thread(this);
423 this.initiateThread.start();
425 // After startup, the PAP does Heartbeat's to each of the PDPs periodically
426 XACMLPapServlet.heartbeat = new Heartbeat((PAPPolicyEngine) XACMLPapServlet.papEngine);
427 XACMLPapServlet.heartbeatThread = new Thread(XACMLPapServlet.heartbeat);
428 XACMLPapServlet.heartbeatThread.start();
430 } catch (FactoryException | PAPException e) {
431 PolicyLogger.error(MessageCodes.ERROR_SYSTEM_ERROR, e, "XACMLPapServlet", " Failed to create engine");
432 throw new ServletException (XACMLErrorConstants.ERROR_SYSTEM_ERROR + "PAP not initialized; error: "+e);
433 } catch (Exception e) {
434 PolicyLogger.error(MessageCodes.ERROR_SYSTEM_ERROR, e, "XACMLPapServlet", " Failed to create engine - unexpected error");
435 throw new ServletException (XACMLErrorConstants.ERROR_SYSTEM_ERROR + "PAP not initialized; unexpected error: "+e);
440 * Thread used only during PAP startup to initiate change messages to all known PDPs.
441 * This must be on a separate thread so that any GET requests from the PDPs during this update can be serviced.
445 // send the current configuration to all the PDPs that we know about
450 * @see Servlet#destroy()
452 * Depending on how this servlet is run, we may or may not care about cleaning up the resources.
453 * For now we assume that we do care.
455 public void destroy() {
456 // Make sure our threads are destroyed
457 if (XACMLPapServlet.heartbeatThread != null) {
458 // stop the heartbeat
460 if (XACMLPapServlet.heartbeat != null) {
461 XACMLPapServlet.heartbeat.terminate();
463 XACMLPapServlet.heartbeatThread.interrupt();
464 XACMLPapServlet.heartbeatThread.join();
465 } catch (InterruptedException e) {
466 PolicyLogger.error(MessageCodes.ERROR_SYSTEM_ERROR, e, "XACMLPapServlet", " Error stopping heartbeat");
469 if (this.initiateThread != null) {
471 this.initiateThread.interrupt();
472 this.initiateThread.join();
473 } catch (InterruptedException e) {
474 PolicyLogger.error(MessageCodes.ERROR_SYSTEM_ERROR, e, "XACMLPapServlet", " Error stopping thread");
481 * - PDP nodes to register themselves with the PAP, and
482 * - Admin Console to make changes in the PDP Groups.
484 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
486 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
487 ONAPLoggingContext loggingContext = ONAPLoggingUtils.getLoggingContextForRequest(request, baseLoggingContext);
488 loggingContext.transactionStarted();
489 loggingContext.setServiceName("PAP.post");
490 if ((loggingContext.getRequestID() == null) || (loggingContext.getRequestID() == "")){
491 UUID requestID = UUID.randomUUID();
492 loggingContext.setRequestID(requestID.toString());
493 PolicyLogger.info("requestID not provided in call to XACMLPapSrvlet (doPost) so we generated one");
495 PolicyLogger.info("requestID was provided in call to XACMLPapSrvlet (doPost)");
497 PolicyDBDaoTransaction pdpTransaction = null;
499 im.startTransaction();
500 } catch (AdministrativeStateException ae){
501 String message = "POST interface called for PAP " + papResourceName + " but it has an Administrative"
502 + " state of " + im.getStateManager().getAdminState()
503 + "\n Exception Message: " + ae.getMessage();
504 LOGGER.info(message);
505 PolicyLogger.error(MessageCodes.ERROR_SYSTEM_ERROR + " " + message);
506 loggingContext.transactionEnded();
507 PolicyLogger.audit("Transaction Failed - See Error.log");
508 response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, message);
510 }catch (StandbyStatusException se) {
511 String message = "POST interface called for PAP " + papResourceName + " but it has a Standby Status"
512 + " of " + im.getStateManager().getStandbyStatus()
513 + "\n Exception Message: " + se.getMessage();
514 LOGGER.info(message);
515 PolicyLogger.error(MessageCodes.ERROR_SYSTEM_ERROR + " " + message);
516 loggingContext.transactionEnded();
517 PolicyLogger.audit("Transaction Failed - See Error.log");
518 response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, message);
522 XACMLRest.dumpRequest(request);
523 // since getParameter reads the content string, explicitly get the content before doing that.
524 // Simply getting the inputStream seems to protect it against being consumed by getParameter.
525 request.getInputStream();
526 String groupId = request.getParameter("groupId");
527 String apiflag = request.getParameter("apiflag");
528 if(groupId != null) {
529 // Is this from the Admin Console or API?
530 if(apiflag!=null && apiflag.equalsIgnoreCase("api")) {
531 // this is from the API so we need to check the client credentials before processing the request
532 if(!authorizeRequest(request)){
533 String message = "PEP not Authorized for making this Request!!";
534 PolicyLogger.error(MessageCodes.ERROR_PERMISSIONS + " " + message);
535 loggingContext.transactionEnded();
536 PolicyLogger.audit("Transaction Failed - See Error.log");
537 response.sendError(HttpServletResponse.SC_FORBIDDEN, message);
542 doACPost(request, response, groupId, loggingContext);
543 loggingContext.transactionEnded();
544 PolicyLogger.audit("Transaction Ended Successfully");
548 // Request is from a PDP asking for its config.
549 loggingContext.setServiceName("PDP:PAP.register");
551 String id = this.getPDPID(request);
552 String jmxport = this.getPDPJMX(request);
553 LOGGER.info("Request(doPost) from PDP coming up: " + id);
554 // Get the PDP Object
555 OnapPDP pdp = XACMLPapServlet.papEngine.getPDP(id);
558 LOGGER.info("Unknown PDP: " + id);
560 if(CheckPDP.validateID(id)){
561 pdpTransaction = policyDBDao.getNewTransaction();
563 pdpTransaction.addPdpToGroup(id, XACMLPapServlet.papEngine.getDefaultGroup().getId(), id, "Registered on first startup", Integer.parseInt(jmxport), "PDP autoregister");
564 XACMLPapServlet.papEngine.newPDP(id, XACMLPapServlet.papEngine.getDefaultGroup(), id, "Registered on first startup", Integer.parseInt(jmxport));
565 } catch (NullPointerException | PAPException | IllegalArgumentException | IllegalStateException | PersistenceException e) {
566 pdpTransaction.rollbackTransaction();
567 String message = "Failed to create new PDP for id: " + id;
568 PolicyLogger.error(MessageCodes.ERROR_PROCESS_FLOW, e, "XACMLPapServlet", " " + message);
569 loggingContext.transactionEnded();
570 PolicyLogger.audit("Transaction Failed - See Error.log");
571 response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e.getMessage());
575 // get the PDP we just created
576 pdp = XACMLPapServlet.papEngine.getPDP(id);
578 if(pdpTransaction != null){
579 pdpTransaction.rollbackTransaction();
581 String message = "Failed to create new PDP for id: " + id;
582 PolicyLogger.error(MessageCodes.ERROR_PROCESS_FLOW + " " + message);
583 loggingContext.transactionEnded();
584 PolicyLogger.audit("Transaction Failed - See Error.log");
585 response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, message);
590 String message = "PDP is Unauthorized to Connect to PAP: "+ id;
591 PolicyLogger.error(MessageCodes.ERROR_PERMISSIONS + " " + message);
592 loggingContext.transactionEnded();
593 response.sendError(HttpServletResponse.SC_UNAUTHORIZED, "PDP not Authorized to connect to this PAP. Please contact the PAP Admin for registration.");
594 PolicyLogger.audit("Transaction Failed - See Error.log");
599 pdpTransaction.commitTransaction();
600 } catch(Exception e){
601 PolicyLogger.error(MessageCodes.ERROR_PROCESS_FLOW, e, "XACMLPapServlet", "Could not commit transaction to put pdp in the database");
604 if (jmxport != null && jmxport != ""){
605 ((StdPDP) pdp).setJmxPort(Integer.valueOf(jmxport));
607 // Get the PDP's Group
608 OnapPDPGroup group = XACMLPapServlet.papEngine.getPDPGroup((OnapPDP) pdp);
610 PolicyLogger.error(MessageCodes.ERROR_PROCESS_FLOW + " PDP not associated with any group, even the default");
611 loggingContext.transactionEnded();
612 PolicyLogger.audit("Transaction Failed - See Error.log");
613 response.sendError(HttpServletResponse.SC_UNAUTHORIZED, "PDP not associated with any group, even the default");
617 // Determine what group the PDP node is in and get
618 // its policy/pip properties.
619 Properties policies = group.getPolicyProperties();
620 Properties pipconfig = group.getPipConfigProperties();
621 // Get the current policy/pip configuration that the PDP has
622 Properties pdpProperties = new Properties();
623 pdpProperties.load(request.getInputStream());
624 LOGGER.info("PDP Current Properties: " + pdpProperties.toString());
625 LOGGER.info("Policies: " + (policies != null ? policies.toString() : "null"));
626 LOGGER.info("Pip config: " + (pipconfig != null ? pipconfig.toString() : "null"));
627 // Validate the node's properties
628 boolean isCurrent = this.isPDPCurrent(policies, pipconfig, pdpProperties);
629 // Send back current configuration
630 if (isCurrent == false) {
631 // Tell the PDP we are sending back the current policies/pip config
632 LOGGER.info("PDP configuration NOT current.");
633 if (policies != null) {
634 // Put URL's into the properties in case the PDP needs to
636 this.populatePolicyURL(request.getRequestURL(), policies);
637 // Copy the properties to the output stream
638 policies.store(response.getOutputStream(), "");
640 if (pipconfig != null) {
641 // Copy the properties to the output stream
642 pipconfig.store(response.getOutputStream(), "");
644 // We are good - and we are sending them information
645 response.setStatus(HttpServletResponse.SC_OK);
646 setPDPSummaryStatus(pdp, PDPStatus.Status.OUT_OF_SYNCH);
648 // Tell them they are good
649 response.setStatus(HttpServletResponse.SC_NO_CONTENT);
650 setPDPSummaryStatus(pdp, PDPStatus.Status.UP_TO_DATE);
652 // tell the AC that something changed
654 loggingContext.transactionEnded();
655 auditLogger.info("Success");
656 PolicyLogger.audit("Transaction Ended Successfully");
657 } catch (PAPException e) {
658 if(pdpTransaction != null){
659 pdpTransaction.rollbackTransaction();
661 LOGGER.debug(XACMLErrorConstants.ERROR_PROCESS_FLOW + "POST exception: " + e, e);
662 loggingContext.transactionEnded();
663 PolicyLogger.audit("Transaction Failed - See Error.log");
664 response.sendError(500, e.getMessage());
668 //Catch anything that fell through
669 loggingContext.transactionEnded();
670 PolicyLogger.audit("Transaction Ended");
675 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
677 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
678 ONAPLoggingContext loggingContext = ONAPLoggingUtils.getLoggingContextForRequest(request, baseLoggingContext);
679 loggingContext.transactionStarted();
680 loggingContext.setServiceName("PAP.get");
681 if ((loggingContext.getRequestID() == null) || (loggingContext.getRequestID() == "")){
682 UUID requestID = UUID.randomUUID();
683 loggingContext.setRequestID(requestID.toString());
684 PolicyLogger.info("requestID not provided in call to XACMLPapSrvlet (doGet) so we generated one");
686 PolicyLogger.info("requestID was provided in call to XACMLPapSrvlet (doGet)");
689 XACMLRest.dumpRequest(request);
690 String pathInfo = request.getRequestURI();
691 LOGGER.info("path info: " + pathInfo);
692 if (pathInfo != null){
693 //DO NOT do a im.startTransaction for the test request
694 if (pathInfo.equals("/pap/test")) {
695 testService(loggingContext, response);
699 //This im.startTransaction() covers all other Get transactions
701 im.startTransaction();
702 } catch (AdministrativeStateException ae){
703 String message = "GET interface called for PAP " + papResourceName + " but it has an Administrative"
704 + " state of " + im.getStateManager().getAdminState()
705 + "\n Exception Message: " + ae.getMessage();
706 LOGGER.info(message);
707 PolicyLogger.error(MessageCodes.ERROR_SYSTEM_ERROR + " " + message);
708 loggingContext.transactionEnded();
709 PolicyLogger.audit("Transaction Failed - See Error.log");
710 response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, message);
712 }catch (StandbyStatusException se) {
713 String message = "GET interface called for PAP " + papResourceName + " but it has a Standby Status"
714 + " of " + im.getStateManager().getStandbyStatus()
715 + "\n Exception Message: " + se.getMessage();
716 LOGGER.info(message);
717 PolicyLogger.error(MessageCodes.ERROR_SYSTEM_ERROR + " " + message);
718 loggingContext.transactionEnded();
719 PolicyLogger.audit("Transaction Failed - See Error.log");
720 response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, message);
723 // Request from the API to get the gitPath
724 String apiflag = request.getParameter("apiflag");
726 if(authorizeRequest(request)){
727 APIRequestHandler apiRequestHandler = new APIRequestHandler();
728 apiRequestHandler.doGet(request,response, apiflag);
729 loggingContext.transactionEnded();
730 PolicyLogger.audit("Transaction Ended Successfully");
734 String message = "PEP not Authorized for making this Request!! \n Contact Administrator for this Scope. ";
735 PolicyLogger.error(MessageCodes.ERROR_PERMISSIONS + " " + message);
736 loggingContext.transactionEnded();
737 PolicyLogger.audit("Transaction Failed - See Error.log");
738 response.sendError(HttpServletResponse.SC_FORBIDDEN, message);
743 // Is this from the Admin Console?
744 String groupId = request.getParameter("groupId");
745 if (groupId != null) {
746 // this is from the Admin Console, so handle separately
747 doACGet(request, response, groupId, loggingContext);
748 loggingContext.transactionEnded();
749 PolicyLogger.audit("Transaction Ended Successfully");
754 String id = this.getPDPID(request);
755 LOGGER.info("doGet from: " + id);
756 // Get the PDP Object
757 OnapPDP pdp = XACMLPapServlet.papEngine.getPDP(id);
760 // Check if request came from localhost
761 if (request.getRemoteHost().equals("localhost") ||
762 request.getRemoteHost().equals("127.0.0.1") ||
763 request.getRemoteHost().equals(request.getLocalAddr())) {
764 // Return status information - basically all the groups
765 loggingContext.setServiceName("PAP.getGroups");
766 Set<OnapPDPGroup> groups = papEngine.getOnapPDPGroups();
767 // convert response object to JSON and include in the response
768 ObjectMapper mapper = new ObjectMapper();
769 mapper.writeValue(response.getOutputStream(), groups);
770 response.setHeader("content-type", "application/json");
771 response.setStatus(HttpServletResponse.SC_OK);
772 loggingContext.transactionEnded();
773 PolicyLogger.audit("Transaction Ended Successfully");
777 String message = "Unknown PDP: " + id + " from " + request.getRemoteHost() + " us: " + request.getLocalAddr();
778 PolicyLogger.error(MessageCodes.ERROR_PERMISSIONS + " " + message);
779 loggingContext.transactionEnded();
780 PolicyLogger.audit("Transaction Failed - See Error.log");
781 response.sendError(HttpServletResponse.SC_UNAUTHORIZED, message);
785 loggingContext.setServiceName("PAP.getPolicy");
786 // Get the PDP's Group
787 OnapPDPGroup group = XACMLPapServlet.papEngine.getPDPGroup((OnapPDP) pdp);
789 String message = "No group associated with pdp " + pdp.getId();
790 LOGGER.warn(XACMLErrorConstants.ERROR_PERMISSIONS + message);
791 loggingContext.transactionEnded();
792 PolicyLogger.audit("Transaction Failed - See Error.log");
793 response.sendError(HttpServletResponse.SC_UNAUTHORIZED, message);
797 // Which policy do they want?
798 String policyId = request.getParameter("id");
799 if (policyId == null) {
800 String message = "Did not specify an id for the policy";
801 LOGGER.warn(XACMLErrorConstants.ERROR_DATA_ISSUE + message);
802 loggingContext.transactionEnded();
803 PolicyLogger.audit("Transaction Failed - See Error.log");
804 response.sendError(HttpServletResponse.SC_NOT_FOUND, message);
808 PDPPolicy policy = group.getPolicy(policyId);
809 if (policy == null) {
810 String message = "Unknown policy: " + policyId;
811 LOGGER.warn(XACMLErrorConstants.ERROR_DATA_ISSUE + message);
812 loggingContext.transactionEnded();
813 PolicyLogger.audit("Transaction Failed - See Error.log");
814 response.sendError(HttpServletResponse.SC_NOT_FOUND, message);
818 LOGGER.warn("PolicyDebugging: Policy Validity: " + policy.isValid() + "\n "
819 + "Policy Name : " + policy.getName() + "\n Policy URI: " + policy.getLocation().toString());
820 try (InputStream is = new FileInputStream(((StdPDPGroup)group).getDirectory().toString()+File.separator+policyId); OutputStream os = response.getOutputStream()) {
821 // Send the policy back
822 IOUtils.copy(is, os);
823 response.setStatus(HttpServletResponse.SC_OK);
824 loggingContext.transactionEnded();
825 auditLogger.info("Success");
826 PolicyLogger.audit("Transaction Ended Successfully");
827 } catch (IOException e) {
828 String message = "Failed to open policy id " + policyId;
829 PolicyLogger.error(MessageCodes.ERROR_DATA_ISSUE + " " + message);
830 loggingContext.transactionEnded();
831 PolicyLogger.audit("Transaction Failed - See Error.log");
832 response.sendError(HttpServletResponse.SC_NOT_FOUND, message);
834 } catch (PAPException e) {
835 PolicyLogger.error(MessageCodes.ERROR_UNKNOWN, e, "XACMLPapServlet", " GET exception");
836 loggingContext.transactionEnded();
837 PolicyLogger.audit("Transaction Failed - See Error.log");
838 response.sendError(500, e.getMessage());
842 loggingContext.transactionEnded();
843 PolicyLogger.audit("Transaction Ended");
848 * @see HttpServlet#doPut(HttpServletRequest request, HttpServletResponse response)
850 protected void doPut(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
851 ONAPLoggingContext loggingContext = ONAPLoggingUtils.getLoggingContextForRequest(request, baseLoggingContext);
852 storedRequestId = loggingContext.getRequestID();
853 loggingContext.transactionStarted();
854 loggingContext.setServiceName("PAP.put");
855 if ((loggingContext.getRequestID() == null) || (loggingContext.getRequestID() == "")){
856 UUID requestID = UUID.randomUUID();
857 loggingContext.setRequestID(requestID.toString());
858 PolicyLogger.info("requestID not provided in call to XACMLPapSrvlet (doPut) so we generated one");
860 PolicyLogger.info("requestID was provided in call to XACMLPapSrvlet (doPut)");
863 im.startTransaction();
864 } catch (AdministrativeStateException ae){
865 String message = "PUT interface called for PAP " + papResourceName + " but it has an Administrative"
866 + " state of " + im.getStateManager().getAdminState()
867 + "\n Exception Message: " + ae.getMessage();
868 LOGGER.info(message +ae);
869 PolicyLogger.error(MessageCodes.ERROR_SYSTEM_ERROR + " " + message);
870 loggingContext.transactionEnded();
871 PolicyLogger.audit("Transaction Failed - See Error.log");
872 response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, message);
874 }catch (StandbyStatusException se) {
875 String message = "PUT interface called for PAP " + papResourceName + " but it has a Standby Status"
876 + " of " + im.getStateManager().getStandbyStatus()
877 + "\n Exception Message: " + se.getMessage();
878 LOGGER.info(message +se);
879 PolicyLogger.error(MessageCodes.ERROR_SYSTEM_ERROR + " " + message);
880 loggingContext.transactionEnded();
881 PolicyLogger.audit("Transaction Failed - See Error.log");
882 response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, message);
885 XACMLRest.dumpRequest(request);
886 //need to check if request is from the API or Admin console
887 String apiflag = request.getParameter("apiflag");
888 //This would occur if a PolicyDBDao notification was received
889 String policyDBDaoRequestUrl = request.getParameter("policydbdaourl");
890 if(policyDBDaoRequestUrl != null){
891 String policyDBDaoRequestEntityId = request.getParameter("entityid");
892 //String policyDBDaoRequestEntityType = request.getParameter("entitytype");
893 String policyDBDaoRequestEntityType = request.getParameter("entitytype");
894 String policyDBDaoRequestExtraData = request.getParameter("extradata");
895 if(policyDBDaoRequestEntityId == null || policyDBDaoRequestEntityType == null){
896 response.sendError(400, "entityid or entitytype not supplied");
897 loggingContext.transactionEnded();
898 PolicyLogger.audit("Transaction Ended Successfully");
902 policyDBDao.handleIncomingHttpNotification(policyDBDaoRequestUrl,policyDBDaoRequestEntityId,policyDBDaoRequestEntityType,policyDBDaoRequestExtraData,this);
903 response.setStatus(200);
904 loggingContext.transactionEnded();
905 PolicyLogger.audit("Transaction Ended Successfully");
910 * Request for ImportService
912 String importService = request.getParameter("importService");
913 if (importService != null) {
914 if(authorizeRequest(request)){
915 APIRequestHandler apiRequestHandler = new APIRequestHandler();
916 apiRequestHandler.doPut(request, response, importService);
920 String message = "PEP not Authorized for making this Request!! \n Contact Administrator for this Scope. ";
921 LOGGER.error(XACMLErrorConstants.ERROR_PERMISSIONS + message );
922 loggingContext.transactionEnded();
923 PolicyLogger.audit("Transaction Failed - See Error.log");
924 response.sendError(HttpServletResponse.SC_FORBIDDEN, message);
928 //This would occur if we received a notification of a policy rename from AC
929 String oldPolicyName = request.getParameter("oldPolicyName");
930 String newPolicyName = request.getParameter("newPolicyName");
931 if(oldPolicyName != null && newPolicyName != null){
932 if(LOGGER.isDebugEnabled()){
933 LOGGER.debug("\nXACMLPapServlet.doPut() - before decoding"
934 + "\npolicyToCreateUpdate = " + " ");
938 oldPolicyName = URLDecoder.decode(oldPolicyName, "UTF-8");
939 newPolicyName = URLDecoder.decode(newPolicyName, "UTF-8");
940 if(LOGGER.isDebugEnabled()){
941 LOGGER.debug("\nXACMLPapServlet.doPut() - after decoding"
942 + "\npolicyToCreateUpdate = " + " ");
944 } catch(UnsupportedEncodingException e){
945 PolicyLogger.error("\nXACMLPapServlet.doPut() - Unsupported URL encoding of policyToCreateUpdate (UTF-8)"
946 + "\npolicyToCreateUpdate = " + " ");
947 response.sendError(500,"policyToCreateUpdate encoding not supported"
948 + "\nfailure with the following exception: " + e);
949 loggingContext.transactionEnded();
950 PolicyLogger.audit("Transaction Failed - See error.log");
954 //send it to PolicyDBDao
955 PolicyDBDaoTransaction renameTransaction = policyDBDao.getNewTransaction();
957 renameTransaction.renamePolicy(oldPolicyName,newPolicyName, "XACMLPapServlet.doPut");
959 renameTransaction.rollbackTransaction();
960 response.sendError(500,"createUpdateTransaction.createPolicy(policyToCreateUpdate, XACMLPapServlet.doPut) "
961 + "\nfailure with the following exception: " + e);
962 loggingContext.transactionEnded();
963 PolicyLogger.audit("Transaction Failed - See error.log");
967 renameTransaction.commitTransaction();
968 response.setStatus(HttpServletResponse.SC_OK);
969 loggingContext.transactionEnded();
970 PolicyLogger.audit("Transaction Ended Successfully");
975 // See if this is Admin Console registering itself with us
977 String acURLString = request.getParameter("adminConsoleURL");
978 if (acURLString != null) {
979 loggingContext.setServiceName("AC:PAP.register");
980 // remember this Admin Console for future updates
981 if ( ! adminConsoleURLStringList.contains(acURLString)) {
982 adminConsoleURLStringList.add(acURLString);
984 if (LOGGER.isDebugEnabled()) {
985 LOGGER.debug("Admin Console registering with URL: " + acURLString);
987 response.setStatus(HttpServletResponse.SC_NO_CONTENT);
988 loggingContext.transactionEnded();
989 auditLogger.info("Success");
990 PolicyLogger.audit("Transaction Ended Successfully");
995 * This is to update the PDP Group with the policy/policies being pushed
996 * Part of a 2 step process to push policies to the PDP that can now be done
997 * From both the Admin Console and the PolicyEngine API
999 String groupId = request.getParameter("groupId");
1000 if (groupId != null) {
1002 if(!authorizeRequest(request)){
1003 String message = "PEP not Authorized for making this Request!! \n Contact Administrator for this Scope. ";
1004 PolicyLogger.error(MessageCodes.ERROR_PERMISSIONS + " " + message);
1005 loggingContext.transactionEnded();
1006 PolicyLogger.audit("Transaction Failed - See Error.log");
1007 response.sendError(HttpServletResponse.SC_FORBIDDEN, message);
1010 if(apiflag.equalsIgnoreCase("addPolicyToGroup")){
1011 updateGroupsFromAPI(request, response, groupId, loggingContext);
1012 loggingContext.transactionEnded();
1013 PolicyLogger.audit("Transaction Ended Successfully");
1014 im.endTransaction();
1018 // this is from the Admin Console, so handle separately
1019 doACPut(request, response, groupId, loggingContext);
1020 loggingContext.transactionEnded();
1021 PolicyLogger.audit("Transaction Ended Successfully");
1022 im.endTransaction();
1026 // Request is for policy validation and creation
1028 if (apiflag != null && apiflag.equalsIgnoreCase("admin")){
1029 // this request is from the Admin Console
1030 SavePolicyHandler savePolicyHandler = SavePolicyHandler.getInstance();
1031 savePolicyHandler.doPolicyAPIPut(request, response);
1032 loggingContext.transactionEnded();
1033 PolicyLogger.audit("Transaction Ended Successfully");
1034 im.endTransaction();
1036 } else if (apiflag != null && apiflag.equalsIgnoreCase("api")) {
1037 // this request is from the Policy Creation API
1038 if(authorizeRequest(request)){
1039 APIRequestHandler apiRequestHandler = new APIRequestHandler();
1040 apiRequestHandler.doPut(request, response, request.getHeader("ClientScope"));
1041 loggingContext.transactionEnded();
1042 PolicyLogger.audit("Transaction Ended Successfully");
1043 im.endTransaction();
1046 String message = "PEP not Authorized for making this Request!!";
1047 PolicyLogger.error(MessageCodes.ERROR_PERMISSIONS + " " + message);
1048 loggingContext.transactionEnded();
1049 PolicyLogger.audit("Transaction Failed - See Error.log");
1050 response.sendError(HttpServletResponse.SC_FORBIDDEN, message);
1051 im.endTransaction();
1055 // We do not expect anything from anywhere else.
1056 // This method is here in case we ever need to support other operations.
1057 LOGGER.error(XACMLErrorConstants.ERROR_DATA_ISSUE + "Request does not have groupId or apiflag");
1058 loggingContext.transactionEnded();
1059 PolicyLogger.audit("Transaction Failed - See Error.log");
1060 response.sendError(HttpServletResponse.SC_BAD_REQUEST, "Request does not have groupId or apiflag");
1061 loggingContext.transactionEnded();
1062 PolicyLogger.audit("Transaction Failed - See error.log");
1063 im.endTransaction();
1067 * @see HttpServlet#doDelete(HttpServletRequest request, HttpServletResponse response)
1069 protected void doDelete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
1070 ONAPLoggingContext loggingContext = ONAPLoggingUtils.getLoggingContextForRequest(request, baseLoggingContext);
1071 loggingContext.transactionStarted();
1072 loggingContext.setServiceName("PAP.delete");
1073 if ((loggingContext.getRequestID() == null) || (loggingContext.getRequestID() == "")){
1074 UUID requestID = UUID.randomUUID();
1075 loggingContext.setRequestID(requestID.toString());
1076 PolicyLogger.info("requestID not provided in call to XACMLPapSrvlet (doDelete) so we generated one");
1078 PolicyLogger.info("requestID was provided in call to XACMLPapSrvlet (doDelete)");
1081 im.startTransaction();
1082 } catch (AdministrativeStateException ae){
1083 String message = "DELETE interface called for PAP " + papResourceName + " but it has an Administrative"
1084 + " state of " + im.getStateManager().getAdminState()
1085 + "\n Exception Message: " + ae.getMessage();
1086 LOGGER.info(message);
1087 PolicyLogger.error(MessageCodes.ERROR_SYSTEM_ERROR + " " + message);
1088 loggingContext.transactionEnded();
1089 PolicyLogger.audit("Transaction Failed - See Error.log");
1090 response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, message);
1092 }catch (StandbyStatusException se) {
1093 String message = "PUT interface called for PAP " + papResourceName + " but it has a Standby Status"
1094 + " of " + im.getStateManager().getStandbyStatus()
1095 + "\n Exception Message: " + se.getMessage();
1096 LOGGER.info(message);
1097 PolicyLogger.error(MessageCodes.ERROR_SYSTEM_ERROR + " " + message);
1098 loggingContext.transactionEnded();
1099 PolicyLogger.audit("Transaction Failed - See Error.log");
1100 response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, message);
1103 XACMLRest.dumpRequest(request);
1104 String groupId = request.getParameter("groupId");
1105 String apiflag = request.getParameter("apiflag");
1106 if (groupId != null) {
1107 // Is this from the Admin Console or API?
1109 if(!authorizeRequest(request)){
1110 String message = "PEP not Authorized for making this Request!! \n Contact Administrator for this Scope. ";
1111 PolicyLogger.error(MessageCodes.ERROR_PERMISSIONS + " " + message);
1112 loggingContext.transactionEnded();
1113 PolicyLogger.audit("Transaction Failed - See Error.log");
1114 response.sendError(HttpServletResponse.SC_FORBIDDEN, message);
1117 APIRequestHandler apiRequestHandler = new APIRequestHandler();
1119 apiRequestHandler.doDelete(request, response, loggingContext, apiflag);
1120 } catch (Exception e) {
1121 LOGGER.error("Exception Occured"+e);
1123 if(apiRequestHandler.getNewGroup()!=null){
1124 groupChanged(apiRequestHandler.getNewGroup());
1128 // this is from the Admin Console, so handle separately
1129 doACDelete(request, response, groupId, loggingContext);
1130 loggingContext.transactionEnded();
1131 PolicyLogger.audit("Transaction Ended Successfully");
1132 im.endTransaction();
1135 //Catch anything that fell through
1136 PolicyLogger.error(MessageCodes.ERROR_DATA_ISSUE + " Request does not have groupId");
1137 loggingContext.transactionEnded();
1138 PolicyLogger.audit("Transaction Failed - See Error.log");
1139 response.sendError(HttpServletResponse.SC_BAD_REQUEST, "Request does not have groupId");
1140 im.endTransaction();
1143 private boolean isPDPCurrent(Properties policies, Properties pipconfig, Properties pdpProperties) {
1144 String localRootPolicies = policies.getProperty(XACMLProperties.PROP_ROOTPOLICIES);
1145 String localReferencedPolicies = policies.getProperty(XACMLProperties.PROP_REFERENCEDPOLICIES);
1146 if (localRootPolicies == null || localReferencedPolicies == null) {
1147 LOGGER.warn(XACMLErrorConstants.ERROR_DATA_ISSUE + "Missing property on PAP server: RootPolicies="+localRootPolicies+" ReferencedPolicies="+localReferencedPolicies);
1150 // Compare the policies and pipconfig properties to the pdpProperties
1152 // the policy properties includes only xacml.rootPolicies and
1153 // xacml.referencedPolicies without any .url entries
1154 Properties pdpPolicies = XACMLProperties.getPolicyProperties(pdpProperties, false);
1155 Properties pdpPipConfig = XACMLProperties.getPipProperties(pdpProperties);
1156 if (localRootPolicies.equals(pdpPolicies.getProperty(XACMLProperties.PROP_ROOTPOLICIES)) &&
1157 localReferencedPolicies.equals(pdpPolicies.getProperty(XACMLProperties.PROP_REFERENCEDPOLICIES)) &&
1158 pdpPipConfig.equals(pipconfig)) {
1159 // The PDP is current
1162 } catch (Exception e) {
1163 // we get here if the PDP did not include either xacml.rootPolicies or xacml.pip.engines,
1164 // or if there are policies that do not have a corresponding ".url" property.
1165 // Either of these cases means that the PDP is not up-to-date, so just drop-through to return false.
1166 PolicyLogger.error(MessageCodes.ERROR_SCHEMA_INVALID, e, "XACMLPapServlet", " PDP Error");
1171 private void populatePolicyURL(StringBuffer urlPath, Properties policies) {
1172 String lists[] = new String[2];
1173 lists[0] = policies.getProperty(XACMLProperties.PROP_ROOTPOLICIES);
1174 lists[1] = policies.getProperty(XACMLProperties.PROP_REFERENCEDPOLICIES);
1175 for (String list : lists) {
1176 if (list != null && list.isEmpty() == false) {
1177 for (String id : Splitter.on(',').trimResults().omitEmptyStrings().split(list)) {
1178 String url = urlPath + "?id=" + id;
1179 LOGGER.info("Policy URL for " + id + ": " + url);
1180 policies.setProperty(id + ".url", url);
1186 protected String getPDPID(HttpServletRequest request) {
1187 String pdpURL = request.getHeader(XACMLRestProperties.PROP_PDP_HTTP_HEADER_ID);
1188 if (pdpURL == null || pdpURL.isEmpty()) {
1189 // Should send back its port for identification
1190 LOGGER.warn(XACMLErrorConstants.ERROR_DATA_ISSUE + "PDP did not send custom header");
1196 protected String getPDPJMX(HttpServletRequest request) {
1197 String pdpJMMX = request.getHeader(XACMLRestProperties.PROP_PDP_HTTP_HEADER_JMX_PORT);
1198 if (pdpJMMX == null || pdpJMMX.isEmpty()) {
1199 // Should send back its port for identification
1200 LOGGER.warn(XACMLErrorConstants.ERROR_DATA_ISSUE + "PDP did not send custom header for JMX Port so the value of 0 is assigned");
1207 * Requests from the PolicyEngine API to update the PDP Group with pushed policy
1212 * @param loggingContext
1213 * @throws ServletException
1214 * @throws IOException
1216 public void updateGroupsFromAPI(HttpServletRequest request, HttpServletResponse response, String groupId, ONAPLoggingContext loggingContext) throws IOException {
1217 PolicyDBDaoTransaction acPutTransaction = policyDBDao.getNewTransaction();
1219 // for PUT operations the group may or may not need to exist before the operation can be done
1220 StdPDPGroup group = (StdPDPGroup) papEngine.getGroup(groupId);
1221 // get the request input stream content into a String
1223 java.util.Scanner scanner = new java.util.Scanner(request.getInputStream());
1224 scanner.useDelimiter("\\A");
1225 json = scanner.hasNext() ? scanner.next() : "";
1227 PolicyLogger.info("JSON request from PolicyEngine API: " + json);
1228 // convert Object sent as JSON into local object
1229 StdPDPPolicy policy = PolicyUtils.jsonStringToObject(json, StdPDPPolicy.class);
1230 Set<PDPPolicy> policies = new HashSet<>();
1232 policies.add(policy);
1234 //Get the current policies from the Group and Add the new one
1235 Set<PDPPolicy> currentPoliciesInGroup = new HashSet<>();
1236 currentPoliciesInGroup = group.getPolicies();
1237 //If the selected policy is in the group we must remove it because the name is default
1238 Iterator<PDPPolicy> policyIterator = policies.iterator();
1239 LOGGER.debug("policyIterator....." + policies);
1240 while (policyIterator.hasNext()) {
1241 PDPPolicy selPolicy = policyIterator.next();
1242 for (PDPPolicy existingPolicy : currentPoliciesInGroup) {
1243 if (existingPolicy.getId().equals(selPolicy.getId())) {
1244 group.removePolicyFromGroup(existingPolicy);
1245 LOGGER.debug("Removing policy: " + existingPolicy);
1250 //Update the PDP Group after removing old version of policy
1251 Set<PDPPolicy> updatedPoliciesInGroup = new HashSet<>();
1252 updatedPoliciesInGroup = group.getPolicies();
1253 //need to remove the policy with default name from group
1254 for (PDPPolicy updatedPolicy : currentPoliciesInGroup) {
1255 if (updatedPolicy.getName().equalsIgnoreCase("default")) {
1256 group.removePolicyFromGroup(updatedPolicy);
1260 if(updatedPoliciesInGroup!=null){
1261 policies.addAll(updatedPoliciesInGroup);
1263 group.setPolicies(policies);
1264 // Assume that this is an update of an existing PDP Group
1265 loggingContext.setServiceName("PolicyEngineAPI:PAP.updateGroup");
1267 acPutTransaction.updateGroup(group, "XACMLPapServlet.doACPut");
1268 } catch(Exception e){
1269 PolicyLogger.error(MessageCodes.ERROR_PROCESS_FLOW, e, "XACMLPapServlet", " Error while updating group in the database: "
1270 +"group="+group.getId());
1271 throw new PAPException(e.getMessage());
1273 papEngine.updateGroup(group);
1274 String policyId = "empty";
1276 policyId = policy.getId();
1278 response.setStatus(HttpServletResponse.SC_NO_CONTENT);
1279 response.addHeader("operation", "push");
1280 response.addHeader("policyId", policyId);
1281 response.addHeader("groupId", groupId);
1282 if (LOGGER.isDebugEnabled()) {
1283 LOGGER.debug("Group '" + group.getId() + "' updated");
1285 acPutTransaction.commitTransaction();
1287 // Group changed, which might include changing the policies
1288 groupChanged(group);
1289 loggingContext.transactionEnded();
1290 auditLogger.info("Success");
1292 if (policy != null && ((policy.getId().contains("Config_MS_")) || (policy.getId().contains("BRMS_Param")))) {
1293 PushPolicyHandler pushPolicyHandler = PushPolicyHandler.getInstance();
1294 if (pushPolicyHandler.preSafetyCheck(policy, CONFIG_HOME)) {
1295 LOGGER.debug("Precheck Successful.");
1299 PolicyLogger.audit("Transaction Ended Successfully");
1301 } catch (PAPException e) {
1302 acPutTransaction.rollbackTransaction();
1303 PolicyLogger.error(MessageCodes.ERROR_PROCESS_FLOW, e, "XACMLPapServlet", " API PUT exception");
1304 loggingContext.transactionEnded();
1305 PolicyLogger.audit("Transaction Failed - See Error.log");
1306 String message = XACMLErrorConstants.ERROR_PROCESS_FLOW + "Exception in request to update group from API - See Error.log on on the PAP.";
1307 response.sendError(500, e.getMessage());
1308 response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
1309 response.addHeader("error","addGroupError");
1310 response.addHeader("message", message);
1316 * Requests from the Admin Console for operations not on single specific objects
1321 * @param loggingContext
1322 * @throws ServletException
1323 * @throws IOException
1325 private void doACPost(HttpServletRequest request, HttpServletResponse response, String groupId, ONAPLoggingContext loggingContext) throws ServletException, IOException {
1326 PolicyDBDaoTransaction doACPostTransaction = null;
1328 String groupName = request.getParameter("groupName");
1329 String groupDescription = request.getParameter("groupDescription");
1330 String apiflag = request.getParameter("apiflag");
1331 if (groupName != null && groupDescription != null) {
1332 // Args: group=<groupId> groupName=<name> groupDescription=<description> <= create a new group
1333 loggingContext.setServiceName("AC:PAP.createGroup");
1334 String unescapedName = URLDecoder.decode(groupName, "UTF-8");
1335 String unescapedDescription = URLDecoder.decode(groupDescription, "UTF-8");
1336 PolicyDBDaoTransaction newGroupTransaction = policyDBDao.getNewTransaction();
1338 newGroupTransaction.createGroup(PolicyDBDao.createNewPDPGroupId(unescapedName), unescapedName, unescapedDescription,"XACMLPapServlet.doACPost");
1339 papEngine.newGroup(unescapedName, unescapedDescription);
1340 newGroupTransaction.commitTransaction();
1341 } catch (Exception e) {
1342 newGroupTransaction.rollbackTransaction();
1343 PolicyLogger.error(MessageCodes.ERROR_PROCESS_FLOW, e, "XACMLPapServlet", " Unable to create new group");
1344 loggingContext.transactionEnded();
1346 PolicyLogger.audit("Transaction Failed - See Error.log");
1347 response.sendError(500, "Unable to create new group '" + groupId + "'");
1350 response.setStatus(HttpServletResponse.SC_NO_CONTENT);
1351 if (LOGGER.isDebugEnabled()) {
1352 LOGGER.debug("New Group '" + groupId + "' created");
1354 // tell the Admin Consoles there is a change
1356 // new group by definition has no PDPs, so no need to notify them of changes
1357 loggingContext.transactionEnded();
1358 PolicyLogger.audit("Transaction Failed - See Error.log");
1359 auditLogger.info("Success");
1360 PolicyLogger.audit("Transaction Ended Successfully");
1363 // for all remaining POST operations the group must exist before the operation can be done
1364 OnapPDPGroup group = papEngine.getGroup(groupId);
1365 if (group == null) {
1366 String message = "Unknown groupId '" + groupId + "'";
1367 PolicyLogger.error(MessageCodes.ERROR_DATA_ISSUE + " " + message);
1368 loggingContext.transactionEnded();
1369 PolicyLogger.audit("Transaction Failed - See Error.log");
1371 response.addHeader("error", "unknownGroupId");
1372 response.addHeader("operation", "push");
1373 response.addHeader("message", message);
1374 response.setStatus(HttpServletResponse.SC_NOT_FOUND);
1376 response.sendError(HttpServletResponse.SC_NOT_FOUND, message);
1380 // determine the operation needed based on the parameters in the request
1381 if (request.getParameter("policyId") != null) {
1382 // Args: group=<groupId> policy=<policyId> <= copy file
1383 // copy a policy from the request contents into a file in the group's directory on this machine
1385 loggingContext.setServiceName("PolicyEngineAPI:PAP.postPolicy");
1387 loggingContext.setServiceName("AC:PAP.postPolicy");
1389 String policyId = request.getParameter("policyId");
1390 PolicyDBDaoTransaction addPolicyToGroupTransaction = policyDBDao.getNewTransaction();
1392 InputStream is = null;
1394 if (apiflag != null){
1395 // get the request content into a String if the request is from API
1397 // read the inputStream into a buffer (trick found online scans entire input looking for end-of-file)
1398 java.util.Scanner scanner = new java.util.Scanner(request.getInputStream());
1399 scanner.useDelimiter("\\A");
1400 json = scanner.hasNext() ? scanner.next() : "";
1402 LOGGER.info("JSON request from API: " + json);
1403 // convert Object sent as JSON into local object
1404 ObjectMapper mapper = new ObjectMapper();
1405 Object objectFromJSON = mapper.readValue(json, StdPAPPolicy.class);
1406 StdPAPPolicy policy = (StdPAPPolicy) objectFromJSON;
1407 temp = new File(policy.getLocation());
1408 is = new FileInputStream(temp);
1410 is = request.getInputStream();
1412 addPolicyToGroupTransaction.addPolicyToGroup(group.getId(), policyId,"XACMLPapServlet.doACPost");
1413 if (apiflag != null){
1414 ((StdPDPGroup) group).copyPolicyToFile(policyId,"API", is);
1417 if (policyId.endsWith(".xml")) {
1418 name = policyId.replace(".xml", "");
1419 name = name.substring(0, name.lastIndexOf("."));
1421 ((StdPDPGroup) group).copyPolicyToFile(policyId, name, is);
1423 if(is!=null && temp!=null){
1427 addPolicyToGroupTransaction.commitTransaction();
1428 } catch (Exception e) {
1429 addPolicyToGroupTransaction.rollbackTransaction();
1430 String message = "Policy '" + policyId + "' not copied to group '" + groupId +"': " + e;
1431 PolicyLogger.error(MessageCodes.ERROR_PROCESS_FLOW + " " + message);
1432 loggingContext.transactionEnded();
1433 PolicyLogger.audit("Transaction Failed - See Error.log");
1435 response.addHeader("error", "policyCopyError");
1436 response.addHeader("message", message);
1437 response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
1439 response.sendError(500, message);
1443 // policy file copied ok and the Group was updated on the PDP
1444 response.setStatus(HttpServletResponse.SC_NO_CONTENT);
1445 response.addHeader("operation", "push");
1446 response.addHeader("policyId", policyId);
1447 response.addHeader("groupId", groupId);
1448 if (LOGGER.isDebugEnabled()) {
1449 LOGGER.debug("policy '" + policyId + "' copied to directory for group '" + groupId + "'");
1451 loggingContext.transactionEnded();
1452 auditLogger.info("Success");
1453 PolicyLogger.audit("Transaction Ended Successfully");
1455 } else if (request.getParameter("default") != null) {
1456 // Args: group=<groupId> default=true <= make default
1457 // change the current default group to be the one identified in the request.
1458 loggingContext.setServiceName("AC:PAP.setDefaultGroup");
1459 // This is a POST operation rather than a PUT "update group" because of the side-effect that the current default group is also changed.
1460 // It should never be the case that multiple groups are currently marked as the default, but protect against that anyway.
1461 PolicyDBDaoTransaction setDefaultGroupTransaction = policyDBDao.getNewTransaction();
1463 setDefaultGroupTransaction.changeDefaultGroup(group, "XACMLPapServlet.doACPost");
1464 papEngine.SetDefaultGroup(group);
1465 setDefaultGroupTransaction.commitTransaction();
1466 } catch (Exception e) {
1467 setDefaultGroupTransaction.rollbackTransaction();
1468 PolicyLogger.error(MessageCodes.ERROR_PROCESS_FLOW, e, "XACMLPapServlet", " Unable to set group");
1469 loggingContext.transactionEnded();
1471 PolicyLogger.audit("Transaction Failed - See Error.log");
1472 response.sendError(500, "Unable to set group '" + groupId + "' to default");
1475 response.setStatus(HttpServletResponse.SC_NO_CONTENT);
1476 if (LOGGER.isDebugEnabled()) {
1477 LOGGER.debug("Group '" + groupId + "' set to be default");
1479 // Notify the Admin Consoles that something changed
1480 // For now the AC cannot handle anything more detailed than the whole set of PDPGroups, so just notify on that
1481 //TODO - Future: FIGURE OUT WHAT LEVEL TO NOTIFY: 2 groups or entire set - currently notify AC to update whole configuration of all groups
1483 // This does not affect any PDPs in the existing groups, so no need to notify them of this change
1484 loggingContext.transactionEnded();
1485 auditLogger.info("Success");
1486 PolicyLogger.audit("Transaction Ended Successfully");
1488 } else if (request.getParameter("pdpId") != null) {
1489 doACPostTransaction = policyDBDao.getNewTransaction();
1490 // Args: group=<groupId> pdpId=<pdpId> <= move PDP to group
1491 loggingContext.setServiceName("AC:PAP.movePDP");
1492 String pdpId = request.getParameter("pdpId");
1493 OnapPDP pdp = papEngine.getPDP(pdpId);
1494 OnapPDPGroup originalGroup = papEngine.getPDPGroup((OnapPDP) pdp);
1496 doACPostTransaction.movePdp(pdp, group, "XACMLPapServlet.doACPost");
1497 }catch(Exception e){
1498 doACPostTransaction.rollbackTransaction();
1499 PolicyLogger.error(MessageCodes.ERROR_PROCESS_FLOW, e, "XACMLPapServlet",
1500 " Error while moving pdp in the database: "
1501 +"pdp="+pdp.getId()+",to group="+group.getId());
1502 throw new PAPException(e.getMessage());
1504 papEngine.movePDP((OnapPDP) pdp, group);
1505 response.setStatus(HttpServletResponse.SC_NO_CONTENT);
1506 if (LOGGER.isDebugEnabled()) {
1507 LOGGER.debug("PDP '" + pdp.getId() +"' moved to group '" + group.getId() + "' set to be default");
1509 // update the status of both the original group and the new one
1510 ((StdPDPGroup)originalGroup).resetStatus();
1511 ((StdPDPGroup)group).resetStatus();
1512 // Notify the Admin Consoles that something changed
1513 // For now the AC cannot handle anything more detailed than the whole set of PDPGroups, so just notify on that
1515 // Need to notify the PDP that it's config may have changed
1517 doACPostTransaction.commitTransaction();
1518 loggingContext.transactionEnded();
1519 auditLogger.info("Success");
1520 PolicyLogger.audit("Transaction Ended Successfully");
1523 } catch (PAPException e) {
1524 PolicyLogger.error(MessageCodes.ERROR_PROCESS_FLOW, e, "XACMLPapServlet", " AC POST exception");
1525 loggingContext.transactionEnded();
1526 PolicyLogger.audit("Transaction Failed - See Error.log");
1527 response.sendError(500, e.getMessage());
1533 * Requests from the Admin Console to GET info about the Groups and PDPs
1538 * @param loggingContext
1539 * @throws ServletException
1540 * @throws IOException
1542 private void doACGet(HttpServletRequest request, HttpServletResponse response, String groupId, ONAPLoggingContext loggingContext) throws ServletException, IOException {
1544 String parameterDefault = request.getParameter("default");
1545 String pdpId = request.getParameter("pdpId");
1546 String pdpGroup = request.getParameter("getPDPGroup");
1547 if ("".equals(groupId)) {
1548 // request IS from AC but does not identify a group by name
1549 if (parameterDefault != null) {
1550 // Request is for the Default group (whatever its id)
1551 loggingContext.setServiceName("AC:PAP.getDefaultGroup");
1552 OnapPDPGroup group = papEngine.getDefaultGroup();
1553 // convert response object to JSON and include in the response
1554 ObjectMapper mapper = new ObjectMapper();
1555 mapper.writeValue(response.getOutputStream(), group);
1556 if (LOGGER.isDebugEnabled()) {
1557 LOGGER.debug("GET Default group req from '" + request.getRequestURL() + "'");
1559 response.setStatus(HttpServletResponse.SC_OK);
1560 response.setHeader("content-type", "application/json");
1561 response.getOutputStream().close();
1562 loggingContext.transactionEnded();
1563 auditLogger.info("Success");
1564 PolicyLogger.audit("Transaction Ended Successfully");
1566 } else if (pdpId != null) {
1567 // Request is related to a PDP
1568 if (pdpGroup == null) {
1569 // Request is for the (unspecified) group containing a given PDP
1570 loggingContext.setServiceName("AC:PAP.getPDP");
1571 OnapPDP pdp = papEngine.getPDP(pdpId);
1572 // convert response object to JSON and include in the response
1573 ObjectMapper mapper = new ObjectMapper();
1574 mapper.writeValue(response.getOutputStream(), pdp);
1575 if (LOGGER.isDebugEnabled()) {
1576 LOGGER.debug("GET pdp '" + pdpId + "' req from '" + request.getRequestURL() + "'");
1578 response.setStatus(HttpServletResponse.SC_OK);
1579 response.setHeader("content-type", "application/json");
1580 response.getOutputStream().close();
1581 loggingContext.transactionEnded();
1582 auditLogger.info("Success");
1583 PolicyLogger.audit("Transaction Ended Successfully");
1586 // Request is for the group containing a given PDP
1587 loggingContext.setServiceName("AC:PAP.getGroupForPDP");
1588 OnapPDP pdp = papEngine.getPDP(pdpId);
1589 OnapPDPGroup group = papEngine.getPDPGroup((OnapPDP) pdp);
1590 // convert response object to JSON and include in the response
1591 ObjectMapper mapper = new ObjectMapper();
1592 mapper.writeValue(response.getOutputStream(), group);
1593 if (LOGGER.isDebugEnabled()) {
1594 LOGGER.debug("GET PDP '" + pdpId + "' Group req from '" + request.getRequestURL() + "'");
1596 response.setStatus(HttpServletResponse.SC_OK);
1597 response.setHeader("content-type", "application/json");
1598 response.getOutputStream().close();
1599 loggingContext.transactionEnded();
1600 auditLogger.info("Success");
1601 PolicyLogger.audit("Transaction Ended Successfully");
1605 // request is for top-level properties about all groups
1606 loggingContext.setServiceName("AC:PAP.getAllGroups");
1607 Set<OnapPDPGroup> groups = papEngine.getOnapPDPGroups();
1608 // convert response object to JSON and include in the response
1609 ObjectMapper mapper = new ObjectMapper();
1610 mapper.writeValue(response.getOutputStream(), groups);
1611 if (LOGGER.isDebugEnabled()) {
1612 LOGGER.debug("GET All groups req");
1614 response.setStatus(HttpServletResponse.SC_OK);
1615 response.setHeader("content-type", "application/json");
1616 response.getOutputStream().close();
1617 loggingContext.transactionEnded();
1618 auditLogger.info("Success");
1619 PolicyLogger.audit("Transaction Ended Successfully");
1623 // for all other GET operations the group must exist before the operation can be done
1624 OnapPDPGroup group = papEngine.getGroup(groupId);
1625 if (group == null) {
1626 String message = "Unknown groupId '" + groupId + "'";
1627 PolicyLogger.error(MessageCodes.ERROR_DATA_ISSUE + " " + message);
1628 loggingContext.transactionEnded();
1630 PolicyLogger.audit("Transaction Failed - See Error.log");
1631 response.sendError(HttpServletResponse.SC_NOT_FOUND, message);
1634 // Figure out which request this is based on the parameters
1635 String policyId = request.getParameter("policyId");
1636 if (policyId != null) {
1637 // retrieve a policy
1638 loggingContext.setServiceName("AC:PAP.getPolicy");
1639 // convert response object to JSON and include in the response
1640 PolicyLogger.error(MessageCodes.ERROR_DATA_ISSUE + " GET Policy not implemented");
1641 loggingContext.transactionEnded();
1642 PolicyLogger.audit("Transaction Failed - See Error.log");
1643 response.sendError(HttpServletResponse.SC_BAD_REQUEST, "GET Policy not implemented");
1645 // No other parameters, so return the identified Group
1646 loggingContext.setServiceName("AC:PAP.getGroup");
1647 // convert response object to JSON and include in the response
1648 ObjectMapper mapper = new ObjectMapper();
1649 mapper.writeValue(response.getOutputStream(), group);
1650 if (LOGGER.isDebugEnabled()) {
1651 LOGGER.debug("GET group '" + group.getId() + "' req from '" + request.getRequestURL() + "'");
1653 response.setStatus(HttpServletResponse.SC_OK);
1654 response.setHeader("content-type", "application/json");
1655 response.getOutputStream().close();
1656 loggingContext.transactionEnded();
1657 auditLogger.info("Success");
1658 PolicyLogger.audit("Transaction Ended Successfully");
1661 // Currently there are no other GET calls from the AC.
1662 // The AC uses the "GET All Groups" operation to fill its local cache and uses that cache for all other GETs without calling the PAP.
1663 // Other GETs that could be called:
1664 // Specific Group (groupId=<groupId>)
1665 // A Policy (groupId=<groupId> policyId=<policyId>)
1666 // A PDP (groupId=<groupId> pdpId=<pdpId>)
1667 PolicyLogger.error(MessageCodes.ERROR_DATA_ISSUE + " UNIMPLEMENTED ");
1668 loggingContext.transactionEnded();
1669 PolicyLogger.audit("Transaction Failed - See Error.log");
1670 response.sendError(HttpServletResponse.SC_BAD_REQUEST, "UNIMPLEMENTED");
1671 } catch (PAPException e) {
1672 PolicyLogger.error(MessageCodes.ERROR_PROCESS_FLOW, e, "XACMLPapServlet", " AC Get exception");
1673 loggingContext.transactionEnded();
1674 PolicyLogger.audit("Transaction Failed - See Error.log");
1675 response.sendError(500, e.getMessage());
1681 * Requests from the Admin Console to create new items or update existing ones
1686 * @param loggingContext
1687 * @throws ServletException
1688 * @throws IOException
1690 private void doACPut(HttpServletRequest request, HttpServletResponse response, String groupId, ONAPLoggingContext loggingContext) throws ServletException, IOException {
1691 PolicyDBDaoTransaction acPutTransaction = policyDBDao.getNewTransaction();
1693 // for PUT operations the group may or may not need to exist before the operation can be done
1694 OnapPDPGroup group = papEngine.getGroup(groupId);
1695 // determine the operation needed based on the parameters in the request
1696 // for remaining operations the group must exist before the operation can be done
1697 if (group == null) {
1698 String message = "Unknown groupId '" + groupId + "'";
1699 PolicyLogger.error(MessageCodes.ERROR_DATA_ISSUE + " " + message);
1700 loggingContext.transactionEnded();
1701 PolicyLogger.audit("Transaction Failed - See Error.log");
1702 response.sendError(HttpServletResponse.SC_NOT_FOUND, message);
1705 if (request.getParameter("policy") != null) {
1706 // group=<groupId> policy=<policyId> contents=policy file <= Create new policy file in group dir, or replace it if it already exists (do not touch properties)
1707 loggingContext.setServiceName("AC:PAP.putPolicy");
1708 PolicyLogger.error(MessageCodes.ERROR_DATA_ISSUE + " PARTIALLY IMPLEMENTED!!! ACTUAL CHANGES SHOULD BE MADE BY PAP SERVLET!!! ");
1709 response.setStatus(HttpServletResponse.SC_NO_CONTENT);
1710 loggingContext.transactionEnded();
1711 PolicyLogger.audit("Transaction Failed - See Error.log");
1712 auditLogger.info("Success");
1713 PolicyLogger.audit("Transaction Ended Successfully");
1715 } else if (request.getParameter("pdpId") != null) {
1716 // ARGS: group=<groupId> pdpId=<pdpId/URL> <= create a new PDP or Update an Existing one
1717 String pdpId = request.getParameter("pdpId");
1718 if (papEngine.getPDP(pdpId) == null) {
1719 loggingContext.setServiceName("AC:PAP.createPDP");
1721 loggingContext.setServiceName("AC:PAP.updatePDP");
1723 // get the request content into a String
1725 // read the inputStream into a buffer (trick found online scans entire input looking for end-of-file)
1726 java.util.Scanner scanner = new java.util.Scanner(request.getInputStream());
1727 scanner.useDelimiter("\\A");
1728 json = scanner.hasNext() ? scanner.next() : "";
1730 LOGGER.info("JSON request from AC: " + json);
1731 // convert Object sent as JSON into local object
1732 ObjectMapper mapper = new ObjectMapper();
1733 Object objectFromJSON = mapper.readValue(json, StdPDP.class);
1734 if (pdpId == null ||
1735 objectFromJSON == null ||
1736 ! (objectFromJSON instanceof StdPDP) ||
1737 ((StdPDP)objectFromJSON).getId() == null ||
1738 ! ((StdPDP)objectFromJSON).getId().equals(pdpId)) {
1739 PolicyLogger.error(MessageCodes.ERROR_DATA_ISSUE + " PDP new/update had bad input. pdpId=" + pdpId + " objectFromJSON="+objectFromJSON);
1740 loggingContext.transactionEnded();
1741 PolicyLogger.audit("Transaction Failed - See Error.log");
1742 response.sendError(500, "Bad input, pdpid="+pdpId+" object="+objectFromJSON);
1744 StdPDP pdp = (StdPDP) objectFromJSON;
1746 if (papEngine.getPDP(pdpId) == null) {
1747 // this is a request to create a new PDP object
1749 acPutTransaction.addPdpToGroup(pdp.getId(), group.getId(), pdp.getName(),
1750 pdp.getDescription(), pdp.getJmxPort(),"XACMLPapServlet.doACPut");
1751 } catch(Exception e){
1752 PolicyLogger.error(MessageCodes.ERROR_PROCESS_FLOW, e, "XACMLPapServlet", " Error while adding pdp to group in the database: "
1753 +"pdp="+ (pdp.getId()) +",to group="+group.getId());
1754 throw new PAPException(e.getMessage());
1756 papEngine.newPDP(pdp.getId(), group, pdp.getName(), pdp.getDescription(), pdp.getJmxPort());
1759 acPutTransaction.updatePdp(pdp, "XACMLPapServlet.doACPut");
1760 } catch(Exception e){
1761 PolicyLogger.error(MessageCodes.ERROR_PROCESS_FLOW, e, "XACMLPapServlet", " Error while updating pdp in the database: "
1762 +"pdp="+ pdp.getId());
1763 throw new PAPException(e.getMessage());
1765 // this is a request to update the pdp
1766 papEngine.updatePDP(pdp);
1768 response.setStatus(HttpServletResponse.SC_NO_CONTENT);
1769 if (LOGGER.isDebugEnabled()) {
1770 LOGGER.debug("PDP '" + pdpId + "' created/updated");
1772 // adjust the group's state including the new PDP
1773 ((StdPDPGroup)group).resetStatus();
1774 // tell the Admin Consoles there is a change
1776 // this might affect the PDP, so notify it of the change
1778 acPutTransaction.commitTransaction();
1779 loggingContext.transactionEnded();
1780 auditLogger.info("Success");
1781 PolicyLogger.audit("Transaction Ended Successfully");
1785 PolicyLogger.error(MessageCodes.ERROR_PROCESS_FLOW, "XACMLPapServlet", " Error while adding pdp to group in the database: "
1786 +"pdp=null" + ",to group="+group.getId());
1787 throw new PAPException("PDP is null");
1788 } catch(Exception e){
1789 throw new PAPException("PDP is null" + e.getMessage() +e);
1792 } else if (request.getParameter("pipId") != null) {
1793 // group=<groupId> pipId=<pipEngineId> contents=pip properties <= add a PIP to pip config, or replace it if it already exists (lenient operation)
1794 loggingContext.setServiceName("AC:PAP.putPIP");
1795 PolicyLogger.error(MessageCodes.ERROR_SYSTEM_ERROR + " UNIMPLEMENTED");
1796 loggingContext.transactionEnded();
1797 PolicyLogger.audit("Transaction Failed - See Error.log");
1798 response.sendError(HttpServletResponse.SC_BAD_REQUEST, "UNIMPLEMENTED");
1801 // Assume that this is an update of an existing PDP Group
1802 // ARGS: group=<groupId> <= Update an Existing Group
1803 loggingContext.setServiceName("AC:PAP.updateGroup");
1804 // get the request content into a String
1806 // read the inputStream into a buffer (trick found online scans entire input looking for end-of-file)
1807 java.util.Scanner scanner = new java.util.Scanner(request.getInputStream());
1808 scanner.useDelimiter("\\A");
1809 json = scanner.hasNext() ? scanner.next() : "";
1811 LOGGER.info("JSON request from AC: " + json);
1812 // convert Object sent as JSON into local object
1813 ObjectMapper mapper = new ObjectMapper();
1814 Object objectFromJSON = mapper.readValue(json, StdPDPGroup.class);
1815 if (objectFromJSON == null || ! (objectFromJSON instanceof StdPDPGroup) ||
1816 ! ((StdPDPGroup)objectFromJSON).getId().equals(group.getId())) {
1817 PolicyLogger.error(MessageCodes.ERROR_DATA_ISSUE + " Group update had bad input. id=" + group.getId() + " objectFromJSON="+objectFromJSON);
1818 loggingContext.transactionEnded();
1819 PolicyLogger.audit("Transaction Failed - See Error.log");
1820 response.sendError(500, "Bad input, id="+group.getId() +" object="+objectFromJSON);
1822 // The Path on the PAP side is not carried on the RESTful interface with the AC
1823 // (because it is local to the PAP)
1824 // so we need to fill that in before submitting the group for update
1825 if(objectFromJSON != null){
1826 ((StdPDPGroup)objectFromJSON).setDirectory(((StdPDPGroup)group).getDirectory());
1829 acPutTransaction.updateGroup((StdPDPGroup)objectFromJSON, "XACMLPapServlet.doACPut");
1830 } catch(Exception e){
1831 PolicyLogger.error(MessageCodes.ERROR_PROCESS_FLOW + " Error while updating group in the database: "
1832 +"group="+group.getId());
1833 throw new PAPException(e.getMessage());
1836 PushPolicyHandler pushPolicyHandler = PushPolicyHandler.getInstance();
1837 OnapPDPGroup updatedGroup = (StdPDPGroup)objectFromJSON;
1838 if (pushPolicyHandler.preSafetyCheck(updatedGroup, CONFIG_HOME)) {
1839 LOGGER.debug("Precheck Successful.");
1842 papEngine.updateGroup((StdPDPGroup)objectFromJSON);
1844 response.setStatus(HttpServletResponse.SC_NO_CONTENT);
1845 if (LOGGER.isDebugEnabled()) {
1846 LOGGER.debug("Group '" + group.getId() + "' updated");
1848 acPutTransaction.commitTransaction();
1849 // tell the Admin Consoles there is a change
1851 // Group changed, which might include changing the policies
1852 groupChanged(group);
1853 loggingContext.transactionEnded();
1854 auditLogger.info("Success");
1855 PolicyLogger.audit("Transaction Ended Successfully");
1858 } catch (PAPException e) {
1859 acPutTransaction.rollbackTransaction();
1860 PolicyLogger.error(MessageCodes.ERROR_PROCESS_FLOW, e, "XACMLPapServlet", " AC PUT exception");
1861 loggingContext.transactionEnded();
1862 PolicyLogger.audit("Transaction Failed - See Error.log");
1863 response.sendError(500, e.getMessage());
1869 * Requests from the Admin Console to delete/remove items
1874 * @param loggingContext
1875 * @throws ServletException
1876 * @throws IOException
1878 private void doACDelete(HttpServletRequest request, HttpServletResponse response, String groupId, ONAPLoggingContext loggingContext) throws ServletException, IOException {
1879 //This code is to allow deletes to propagate to the database since delete is not implemented
1880 String isDeleteNotify = request.getParameter("isDeleteNotify");
1881 if(isDeleteNotify != null){
1882 String policyToDelete = request.getParameter("policyToDelete");
1884 policyToDelete = URLDecoder.decode(policyToDelete,"UTF-8");
1885 } catch(UnsupportedEncodingException e){
1886 PolicyLogger.error("Unsupported URL encoding of policyToDelete (UTF-8");
1887 response.sendError(500,"policyToDelete encoding not supported");
1890 PolicyDBDaoTransaction deleteTransaction = policyDBDao.getNewTransaction();
1892 deleteTransaction.deletePolicy(policyToDelete);
1893 } catch(Exception e){
1894 deleteTransaction.rollbackTransaction();
1895 response.sendError(500,"deleteTransaction.deleteTransaction(policyToDelete) "
1896 + "\nfailure with the following exception: " + e);
1899 deleteTransaction.commitTransaction();
1900 response.setStatus(HttpServletResponse.SC_OK);
1903 PolicyDBDaoTransaction removePdpOrGroupTransaction = policyDBDao.getNewTransaction();
1905 // for all DELETE operations the group must exist before the operation can be done
1906 loggingContext.setServiceName("AC:PAP.delete");
1907 OnapPDPGroup group = papEngine.getGroup(groupId);
1908 if (group == null) {
1909 String message = "Unknown groupId '" + groupId + "'";
1910 PolicyLogger.error(MessageCodes.ERROR_DATA_ISSUE + " " + message);
1911 loggingContext.transactionEnded();
1912 PolicyLogger.audit("Transaction Failed - See Error.log");
1913 response.sendError(HttpServletResponse.SC_NOT_FOUND, "Unknown groupId '" + groupId +"'");
1916 // determine the operation needed based on the parameters in the request
1917 if (request.getParameter("policy") != null) {
1918 // group=<groupId> policy=<policyId> [delete=<true|false>] <= delete policy file from group
1919 loggingContext.setServiceName("AC:PAP.deletePolicy");
1920 PolicyLogger.error(MessageCodes.ERROR_SYSTEM_ERROR + " UNIMPLEMENTED");
1921 loggingContext.transactionEnded();
1922 PolicyLogger.audit("Transaction Failed - See Error.log");
1923 response.sendError(HttpServletResponse.SC_BAD_REQUEST, "UNIMPLEMENTED");
1925 } else if (request.getParameter("pdpId") != null) {
1926 // ARGS: group=<groupId> pdpId=<pdpId> <= delete PDP
1927 String pdpId = request.getParameter("pdpId");
1928 OnapPDP pdp = papEngine.getPDP(pdpId);
1930 removePdpOrGroupTransaction.removePdpFromGroup(pdp.getId(),"XACMLPapServlet.doACDelete");
1931 } catch(Exception e){
1932 throw new PAPException();
1934 papEngine.removePDP((OnapPDP) pdp);
1935 // adjust the status of the group, which may have changed when we removed this PDP
1936 ((StdPDPGroup)group).resetStatus();
1937 response.setStatus(HttpServletResponse.SC_NO_CONTENT);
1939 // update the PDP and tell it that it has NO Policies (which prevents it from serving PEP Requests)
1941 removePdpOrGroupTransaction.commitTransaction();
1942 loggingContext.transactionEnded();
1943 auditLogger.info("Success");
1944 PolicyLogger.audit("Transaction Ended Successfully");
1946 } else if (request.getParameter("pipId") != null) {
1947 // group=<groupId> pipId=<pipEngineId> <= delete PIP config for given engine
1948 loggingContext.setServiceName("AC:PAP.deletePIPConfig");
1949 PolicyLogger.error(MessageCodes.ERROR_SYSTEM_ERROR + " UNIMPLEMENTED");
1950 loggingContext.transactionEnded();
1951 PolicyLogger.audit("Transaction Failed - See Error.log");
1952 response.sendError(HttpServletResponse.SC_BAD_REQUEST, "UNIMPLEMENTED");
1955 // ARGS: group=<groupId> movePDPsToGroupId=<movePDPsToGroupId> <= delete a group and move all its PDPs to the given group
1956 String moveToGroupId = request.getParameter("movePDPsToGroupId");
1957 OnapPDPGroup moveToGroup = null;
1958 if (moveToGroupId != null) {
1959 moveToGroup = papEngine.getGroup(moveToGroupId);
1961 // get list of PDPs in the group being deleted so we can notify them that they got changed
1962 Set<OnapPDP> movedPDPs = new HashSet<>();
1963 movedPDPs.addAll(group.getOnapPdps());
1964 // do the move/remove
1966 removePdpOrGroupTransaction.deleteGroup(group, moveToGroup,"XACMLPapServlet.doACDelete");
1967 } catch(Exception e){
1968 PolicyLogger.error(MessageCodes.ERROR_UNKNOWN, e, "XACMLPapServlet", " Failed to delete PDP Group. Exception");
1969 throw new PAPException(e.getMessage());
1971 papEngine.removeGroup(group, moveToGroup);
1972 response.setStatus(HttpServletResponse.SC_NO_CONTENT);
1974 // notify any PDPs in the removed set that their config may have changed
1975 for (OnapPDP pdp : movedPDPs) {
1978 removePdpOrGroupTransaction.commitTransaction();
1979 loggingContext.transactionEnded();
1980 auditLogger.info("Success");
1981 PolicyLogger.audit("Transaction Ended Successfully");
1984 } catch (PAPException e) {
1985 removePdpOrGroupTransaction.rollbackTransaction();
1986 PolicyLogger.error(MessageCodes.ERROR_PROCESS_FLOW, e, "XACMLPapServlet", " AC DELETE exception");
1987 loggingContext.transactionEnded();
1988 PolicyLogger.audit("Transaction Failed - See Error.log");
1989 response.sendError(500, e.getMessage());
1995 * Heartbeat thread - periodically check on PDPs' status
1997 * Heartbeat with all known PDPs.
1999 * Implementation note:
2001 * The PDPs are contacted Sequentially, not in Parallel.
2003 * If we did this in parallel using multiple threads we would simultaneously use
2007 * This could become a resource problem since we already use multiple threads and connections for updating the PDPs
2008 * when user changes occur.
2009 * Using separate threads can also make it tricky dealing with timeouts on PDPs that are non-responsive.
2011 * The Sequential operation does a heartbeat request to each PDP one at a time.
2012 * This has the flaw that any PDPs that do not respond will hold up the entire heartbeat sequence until they timeout.
2013 * If there are a lot of non-responsive PDPs and the timeout is large-ish (the default is 20 seconds)
2014 * it could take a long time to cycle through all of the PDPs.
2015 * That means that this may not notice a PDP being down in a predictable time.
2017 private class Heartbeat implements Runnable {
2018 private PAPPolicyEngine papEngine;
2019 private Set<OnapPDP> pdps = new HashSet<>();
2020 private int heartbeatInterval;
2021 private int heartbeatTimeout;
2023 public volatile boolean isRunning = false;
2025 public synchronized boolean isRunning() {
2026 return this.isRunning;
2029 public synchronized void terminate() {
2030 this.isRunning = false;
2033 public Heartbeat(PAPPolicyEngine papEngine2) {
2034 papEngine = papEngine2;
2035 this.heartbeatInterval = Integer.parseInt(XACMLProperties.getProperty(XACMLRestProperties.PROP_PAP_HEARTBEAT_INTERVAL, "10000"));
2036 this.heartbeatTimeout = Integer.parseInt(XACMLProperties.getProperty(XACMLRestProperties.PROP_PAP_HEARTBEAT_TIMEOUT, "10000"));
2041 // Set ourselves as running
2042 synchronized(this) {
2043 this.isRunning = true;
2045 HashMap<String, URL> idToURLMap = new HashMap<>();
2047 while (this.isRunning()) {
2048 // Wait the given time
2049 Thread.sleep(heartbeatInterval);
2050 // get the list of PDPs (may have changed since last time)
2052 synchronized(papEngine) {
2054 for (OnapPDPGroup g : papEngine.getOnapPDPGroups()) {
2055 for (OnapPDP p : g.getOnapPdps()) {
2059 } catch (PAPException e) {
2060 PolicyLogger.error(MessageCodes.ERROR_SYSTEM_ERROR, e, "XACMLPapServlet", "Heartbeat unable to read PDPs from PAPEngine");
2063 // Check for shutdown
2064 if (this.isRunning() == false) {
2065 LOGGER.info("isRunning is false, getting out of loop.");
2068 // try to get the summary status from each PDP
2069 boolean changeSeen = false;
2070 for (OnapPDP pdp : pdps) {
2071 // Check for shutdown
2072 if (this.isRunning() == false) {
2073 LOGGER.info("isRunning is false, getting out of loop.");
2076 // the id of the PDP is its url (though we add a query parameter)
2077 URL pdpURL = idToURLMap.get(pdp.getId());
2078 if (pdpURL == null) {
2079 // haven't seen this PDP before
2080 String fullURLString = null;
2083 if(CheckPDP.validateID(pdp.getId())){
2084 fullURLString = pdp.getId() + "?type=hb";
2085 pdpURL = new URL(fullURLString);
2086 idToURLMap.put(pdp.getId(), pdpURL);
2088 } catch (MalformedURLException e) {
2089 PolicyLogger.error(MessageCodes.ERROR_DATA_ISSUE, e, "XACMLPapServlet", " PDP id '" + fullURLString + "' is not a valid URL");
2093 // Do a GET with type HeartBeat
2094 String newStatus = "";
2095 HttpURLConnection connection = null;
2097 // Open up the connection
2099 connection = (HttpURLConnection)pdpURL.openConnection();
2100 // Setup our method and headers
2101 connection.setRequestMethod("GET");
2102 connection.setConnectTimeout(heartbeatTimeout);
2104 String encoding = CheckPDP.getEncoding(pdp.getId());
2105 if(encoding !=null){
2106 connection.setRequestProperty("Authorization", "Basic " + encoding);
2109 connection.connect();
2110 if (connection.getResponseCode() == 204) {
2111 newStatus = connection.getHeaderField(XACMLRestProperties.PROP_PDP_HTTP_HEADER_HB);
2112 if (LOGGER.isDebugEnabled()) {
2113 LOGGER.debug("Heartbeat '" + pdp.getId() + "' status='" + newStatus + "'");
2116 // anything else is an unexpected result
2117 newStatus = PDPStatus.Status.UNKNOWN.toString();
2118 PolicyLogger.error(MessageCodes.ERROR_SYSTEM_ERROR + " Heartbeat connect response code " + connection.getResponseCode() + ": " + pdp.getId());
2121 } catch (UnknownHostException e) {
2122 newStatus = PDPStatus.Status.NO_SUCH_HOST.toString();
2123 PolicyLogger.error(MessageCodes.ERROR_SYSTEM_ERROR, e, "XACMLPapServlet", " Heartbeat '" + pdp.getId() + "' NO_SUCH_HOST");
2124 } catch (SocketTimeoutException e) {
2125 newStatus = PDPStatus.Status.CANNOT_CONNECT.toString();
2126 PolicyLogger.error(MessageCodes.ERROR_SYSTEM_ERROR, e, "XACMLPapServlet", " Heartbeat '" + pdp.getId() + "' connection timeout");
2127 } catch (ConnectException e) {
2128 newStatus = PDPStatus.Status.CANNOT_CONNECT.toString();
2129 PolicyLogger.error(MessageCodes.ERROR_SYSTEM_ERROR, e, "XACMLPapServlet", " Heartbeat '" + pdp.getId() + "' cannot connect");
2130 } catch (Exception e) {
2131 newStatus = PDPStatus.Status.UNKNOWN.toString();
2132 PolicyLogger.error(MessageCodes.ERROR_SYSTEM_ERROR, e, "XACMLPapServlet", "Heartbeat '" + pdp.getId() + "' connect exception");
2134 // cleanup the connection
2135 if(connection != null)
2136 connection.disconnect();
2138 if ( ! pdp.getStatus().getStatus().toString().equals(newStatus)) {
2139 if (LOGGER.isDebugEnabled()) {
2140 LOGGER.debug("previous status='" + pdp.getStatus().getStatus()+"' new Status='" + newStatus + "'");
2143 setPDPSummaryStatus(pdp, newStatus);
2144 } catch (PAPException e) {
2145 PolicyLogger.error(MessageCodes.ERROR_PROCESS_FLOW, e, "XACMLPapServlet", "Unable to set state for PDP '" + pdp.getId());
2150 // Check for shutdown
2151 if (this.isRunning() == false) {
2152 LOGGER.info("isRunning is false, getting out of loop.");
2155 // if any of the PDPs changed state, tell the ACs to update
2160 } catch (InterruptedException e) {
2161 PolicyLogger.error(MessageCodes.ERROR_SYSTEM_ERROR + " Heartbeat interrupted. Shutting down");
2168 * HELPER to change Group status when PDP status is changed
2169 * (Must NOT be called from a method that is synchronized on the papEngine or it may deadlock)
2171 private void setPDPSummaryStatus(OnapPDP pdp, PDPStatus.Status newStatus) throws PAPException {
2172 setPDPSummaryStatus(pdp, newStatus.toString());
2175 private void setPDPSummaryStatus(OnapPDP pdp, String newStatus) throws PAPException {
2176 synchronized(papEngine) {
2177 StdPDPStatus status = new StdPDPStatus();
2178 status.setStatus(PDPStatus.Status.valueOf(newStatus));
2179 ((StdPDP)pdp).setStatus(status);
2180 // now adjust the group
2181 StdPDPGroup group = (StdPDPGroup)papEngine.getPDPGroup((OnapPDP) pdp);
2182 // if the PDP was just deleted it may transiently exist but not be in a group
2183 if (group != null) {
2184 group.resetStatus();
2190 * Callback methods telling this servlet to notify PDPs of changes made by the PAP StdEngine
2191 * in the PDP group directories
2194 public void changed() {
2195 // all PDPs in all groups need to be updated/sync'd
2196 Set<OnapPDPGroup> groups;
2198 groups = papEngine.getOnapPDPGroups();
2199 } catch (PAPException e) {
2200 PolicyLogger.error(MessageCodes.ERROR_SYSTEM_ERROR, e, "XACMLPapServlet", " getPDPGroups failed");
2201 throw new RuntimeException(XACMLErrorConstants.ERROR_SYSTEM_ERROR + "Unable to get Groups: " + e);
2203 for (OnapPDPGroup group : groups) {
2204 groupChanged(group);
2209 public void groupChanged(OnapPDPGroup group) {
2210 // all PDPs within one group need to be updated/sync'd
2211 for (OnapPDP pdp : group.getOnapPdps()) {
2217 public void pdpChanged(OnapPDP pdp) {
2218 // kick off a thread to do an event notification for each PDP.
2219 // This needs to be on a separate thread so that PDPs that do not respond (down, non-existent, etc)
2220 // do not block the PSP response to the AC, which would freeze the GUI until all PDPs sequentially respond or time-out.
2221 Thread t = new Thread(new UpdatePDPThread(pdp, storedRequestId));
2222 if(CheckPDP.validateID(pdp.getId())){
2227 private class UpdatePDPThread implements Runnable {
2228 private OnapPDP pdp;
2229 private String requestId;
2231 public UpdatePDPThread(OnapPDP pdp, String storedRequestId) {
2233 requestId = storedRequestId;
2237 // send the current configuration to one PDP
2238 HttpURLConnection connection = null;
2239 // get a new logging context for the thread
2240 ONAPLoggingContext loggingContext = new ONAPLoggingContext(baseLoggingContext);
2242 loggingContext.setServiceName("PAP:PDP.putConfig");
2243 // If a requestId was provided, use it, otherwise generate one; post to loggingContext to be used later when calling PDP
2244 if ((requestId == null) || (requestId == "")) {
2245 UUID requestID = UUID.randomUUID();
2246 loggingContext.setRequestID(requestID.toString());
2247 PolicyLogger.info("requestID not provided in call to XACMLPapSrvlet (UpdatePDPThread) so we generated one: " + loggingContext.getRequestID());
2249 loggingContext.setRequestID(requestId);
2250 PolicyLogger.info("requestID was provided in call to XACMLPapSrvlet (UpdatePDPThread): " + loggingContext.getRequestID());
2252 loggingContext.transactionStarted();
2253 // the Id of the PDP is its URL
2254 if (LOGGER.isDebugEnabled()) {
2255 LOGGER.debug("creating url for id '" + pdp.getId() + "'");
2257 //TODO - currently always send both policies and pips. Do we care enough to add code to allow sending just one or the other?
2258 //TODO (need to change "cache=", implying getting some input saying which to change)
2259 URL url = new URL(pdp.getId() + "?cache=all");
2260 // Open up the connection
2261 connection = (HttpURLConnection)url.openConnection();
2262 // Setup our method and headers
2263 connection.setRequestMethod("PUT");
2265 String encoding = CheckPDP.getEncoding(pdp.getId());
2266 if(encoding !=null){
2267 connection.setRequestProperty("Authorization", "Basic " + encoding);
2269 connection.setRequestProperty("Content-Type", "text/x-java-properties");
2270 connection.setRequestProperty("X-ECOMP-RequestID", loggingContext.getRequestID());
2271 storedRequestId = null;
2272 connection.setInstanceFollowRedirects(true);
2273 connection.setDoOutput(true);
2274 try (OutputStream os = connection.getOutputStream()) {
2275 OnapPDPGroup group = papEngine.getPDPGroup((OnapPDP) pdp);
2276 // if the PDP was just deleted, there is no group, but we want to send an update anyway
2277 if (group == null) {
2278 // create blank properties files
2279 Properties policyProperties = new Properties();
2280 policyProperties.put(XACMLProperties.PROP_ROOTPOLICIES, "");
2281 policyProperties.put(XACMLProperties.PROP_REFERENCEDPOLICIES, "");
2282 policyProperties.store(os, "");
2283 Properties pipProps = new Properties();
2284 pipProps.setProperty(XACMLProperties.PROP_PIP_ENGINES, "");
2285 pipProps.store(os, "");
2287 // send properties from the current group
2288 group.getPolicyProperties().store(os, "");
2289 Properties policyLocations = new Properties();
2290 for (PDPPolicy policy : group.getPolicies()) {
2291 policyLocations.put(policy.getId() + ".url", XACMLPapServlet.papURL + "?id=" + policy.getId());
2293 policyLocations.store(os, "");
2294 group.getPipConfigProperties().store(os, "");
2296 } catch (Exception e) {
2297 PolicyLogger.error(MessageCodes.ERROR_SYSTEM_ERROR, e, "XACMLPapServlet", " Failed to send property file to " + pdp.getId());
2298 // Since this is a server-side error, it probably does not reflect a problem on the client,
2299 // so do not change the PDP status.
2303 connection.connect();
2304 if (connection.getResponseCode() == 204) {
2305 LOGGER.info("Success. We are configured correctly.");
2306 loggingContext.transactionEnded();
2307 auditLogger.info("Success. PDP is configured correctly.");
2308 PolicyLogger.audit("Transaction Success. PDP is configured correctly.");
2309 setPDPSummaryStatus(pdp, PDPStatus.Status.UP_TO_DATE);
2310 } else if (connection.getResponseCode() == 200) {
2311 LOGGER.info("Success. PDP needs to update its configuration.");
2312 loggingContext.transactionEnded();
2313 auditLogger.info("Success. PDP needs to update its configuration.");
2314 PolicyLogger.audit("Transaction Success. PDP is configured correctly.");
2315 setPDPSummaryStatus(pdp, PDPStatus.Status.OUT_OF_SYNCH);
2317 LOGGER.warn("Failed: " + connection.getResponseCode() + " message: " + connection.getResponseMessage());
2318 loggingContext.transactionEnded();
2319 auditLogger.warn("Failed: " + connection.getResponseCode() + " message: " + connection.getResponseMessage());
2320 PolicyLogger.audit("Transaction Failed: " + connection.getResponseCode() + " message: " + connection.getResponseMessage());
2321 setPDPSummaryStatus(pdp, PDPStatus.Status.UNKNOWN);
2323 } catch (Exception e) {
2324 PolicyLogger.error(MessageCodes.ERROR_SYSTEM_ERROR, e, "XACMLPapServlet", " Unable to sync config with PDP '" + pdp.getId() + "'");
2325 loggingContext.transactionEnded();
2326 PolicyLogger.audit("Transaction Failed: Unable to sync config with PDP '" + pdp.getId() + "': " + e);
2328 setPDPSummaryStatus(pdp, PDPStatus.Status.UNKNOWN);
2329 } catch (PAPException e1) {
2330 PolicyLogger.audit("Transaction Failed: Unable to set status of PDP " + pdp.getId() + " to UNKNOWN: " + e);
2331 PolicyLogger.error(MessageCodes.ERROR_SYSTEM_ERROR, e, "XACMLPapServlet", " Unable to set status of PDP '" + pdp.getId() + "' to UNKNOWN");
2334 // cleanup the connection
2335 if(connection != null){
2336 connection.disconnect();
2338 // tell the AC to update it's status info
2345 * RESTful Interface from PAP to ACs notifying them of changes
2347 private void notifyAC() {
2348 // kick off a thread to do one event notification for all registered ACs
2349 // This needs to be on a separate thread so that ACs can make calls back to PAP to get the updated Group data
2350 // as part of processing this message on their end.
2351 Thread t = new Thread(new NotifyACThread());
2355 private class NotifyACThread implements Runnable {
2357 List<String> disconnectedACs = new ArrayList<>();
2358 // There should be no Concurrent exception here because the list is a CopyOnWriteArrayList.
2359 // The "for each" loop uses the collection's iterator under the covers, so it should be correct.
2360 for (String acURL : adminConsoleURLStringList) {
2361 HttpURLConnection connection = null;
2363 acURL += "?PAPNotification=true";
2364 //TODO - Currently we just tell AC that "Something changed" without being specific. Do we want to tell it which group/pdp changed?
2365 //TODO - If so, put correct parameters into the Query string here
2366 acURL += "&objectType=all" + "&action=update";
2367 if (LOGGER.isDebugEnabled()) {
2368 LOGGER.debug("creating url for id '" + acURL + "'");
2370 //TODO - currently always send both policies and pips. Do we care enough to add code to allow sending just one or the other?
2371 //TODO (need to change "cache=", implying getting some input saying which to change)
2372 URL url = new URL(acURL );
2373 // Open up the connection
2374 connection = (HttpURLConnection)url.openConnection();
2375 // Setup our method and headers
2376 connection.setRequestMethod("PUT");
2377 connection.setRequestProperty("Content-Type", "text/x-java-properties");
2378 // Adding this in. It seems the HttpUrlConnection class does NOT
2379 // properly forward our headers for POST re-direction. It does so
2380 // for a GET re-direction.
2381 // So we need to handle this ourselves.
2382 //TODO - is this needed for a PUT? seems better to leave in for now?
2383 connection.setInstanceFollowRedirects(false);
2384 // Do not include any data in the PUT because this is just a
2385 // notification to the AC.
2386 // The AC will use GETs back to the PAP to get what it needs
2387 // to fill in the screens.
2389 connection.connect();
2390 if (connection.getResponseCode() == 204) {
2391 LOGGER.info("Success. We updated correctly.");
2393 LOGGER.warn(XACMLErrorConstants.ERROR_SYSTEM_ERROR + "Failed: " + connection.getResponseCode() + " message: " + connection.getResponseMessage());
2396 } catch (Exception e) {
2397 PolicyLogger.error(MessageCodes.ERROR_SYSTEM_ERROR, e, "XACMLPapServlet", " Unable to sync config AC '" + acURL + "'");
2398 disconnectedACs.add(acURL);
2400 // cleanup the connection
2401 if(connection != null)
2402 connection.disconnect();
2405 // remove any ACs that are no longer connected
2406 if (disconnectedACs.size() > 0) {
2407 adminConsoleURLStringList.removeAll(disconnectedACs);
2412 private void testService(ONAPLoggingContext loggingContext, HttpServletResponse response) throws IOException{
2413 LOGGER.info("Test request received");
2415 im.evaluateSanity();
2416 //If we make it this far, all is well
2417 String message = "GET:/pap/test called and PAP " + papResourceName + " is OK";
2418 LOGGER.info(message);
2419 loggingContext.transactionEnded();
2420 PolicyLogger.audit("Transaction Failed - See Error.log");
2421 response.setStatus(HttpServletResponse.SC_OK);
2423 }catch (ForwardProgressException fpe){
2424 //No forward progress is being made
2425 String message = "GET:/pap/test called and PAP " + papResourceName + " is not making forward progress."
2426 + " Exception Message: " + fpe.getMessage();
2427 LOGGER.info(message);
2428 PolicyLogger.error(MessageCodes.ERROR_SYSTEM_ERROR + " " + message);
2429 loggingContext.transactionEnded();
2430 PolicyLogger.audit("Transaction Failed - See Error.log");
2431 response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, message);
2433 }catch (AdministrativeStateException ase){
2434 //Administrative State is locked
2435 String message = "GET:/pap/test called and PAP " + papResourceName + " Administrative State is LOCKED "
2436 + " Exception Message: " + ase.getMessage();
2437 LOGGER.info(message);
2438 PolicyLogger.error(MessageCodes.ERROR_SYSTEM_ERROR + " " + message);
2439 loggingContext.transactionEnded();
2440 PolicyLogger.audit("Transaction Failed - See Error.log");
2441 response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, message);
2443 }catch (StandbyStatusException sse){
2444 //Administrative State is locked
2445 String message = "GET:/pap/test called and PAP " + papResourceName + " Standby Status is NOT PROVIDING SERVICE "
2446 + " Exception Message: " + sse.getMessage();
2447 LOGGER.info(message);
2448 PolicyLogger.error(MessageCodes.ERROR_SYSTEM_ERROR + " " + message);
2449 loggingContext.transactionEnded();
2450 PolicyLogger.audit("Transaction Failed - See Error.log");
2451 response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, message);
2453 }catch (Exception e) {
2454 //A subsystem is not making progress, is locked, standby or is not responding
2455 String eMsg = e.getMessage();
2457 eMsg = "No Exception Message";
2459 String message = "GET:/pap/test called and PAP " + papResourceName + " has had a subsystem failure."
2460 + " Exception Message: " + eMsg;
2461 LOGGER.info(message);
2462 PolicyLogger.error(MessageCodes.ERROR_SYSTEM_ERROR + " " + message);
2463 loggingContext.transactionEnded();
2464 PolicyLogger.audit("Transaction Failed - See Error.log");
2465 //Get the specific list of subsystems that failed
2466 String ssFailureList = null;
2467 for(String failedSS : papDependencyGroupsFlatArray){
2468 if(eMsg.contains(failedSS)){
2469 if(ssFailureList == null){
2470 ssFailureList = failedSS;
2472 ssFailureList = ssFailureList.concat(","+failedSS);
2476 if(ssFailureList == null){
2477 ssFailureList = "UnknownSubSystem";
2479 response.addHeader("X-ONAP-SubsystemFailure", ssFailureList);
2480 response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, message);
2486 * Authorizing the PEP Requests.
2488 private boolean authorizeRequest(HttpServletRequest request) {
2489 String clientCredentials = request.getHeader(ENVIRONMENT_HEADER);
2490 // Check if the Client is Authorized.
2491 if(clientCredentials!=null && clientCredentials.equalsIgnoreCase(environment)){
2498 private static void loadWebapps() throws PAPException{
2499 if(ACTION_HOME == null || CONFIG_HOME == null){
2500 Path webappsPath = Paths.get(XACMLProperties.getProperty(XACMLRestProperties.PROP_PAP_WEBAPPS));
2502 if (webappsPath == null) {
2503 PolicyLogger.error("Invalid Webapps Path Location property : " + XACMLRestProperties.PROP_PAP_WEBAPPS);
2504 throw new PAPException("Invalid Webapps Path Location property : " + XACMLRestProperties.PROP_PAP_WEBAPPS);
2506 Path webappsPathConfig = Paths.get(webappsPath.toString()+File.separator+"Config");
2507 Path webappsPathAction = Paths.get(webappsPath.toString()+File.separator+"Action");
2508 if (Files.notExists(webappsPathConfig)) {
2510 Files.createDirectories(webappsPathConfig);
2511 } catch (IOException e) {
2512 PolicyLogger.error(MessageCodes.ERROR_PROCESS_FLOW, e, "XACMLPapServlet", " Failed to create config directory: "
2513 + webappsPathConfig.toAbsolutePath().toString());
2516 if (Files.notExists(webappsPathAction)) {
2518 Files.createDirectories(webappsPathAction);
2519 } catch (IOException e) {
2520 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Failed to create config directory: "
2521 + webappsPathAction.toAbsolutePath().toString(), e);
2524 ACTION_HOME = webappsPathAction.toString();
2525 CONFIG_HOME = webappsPathConfig.toString();
2529 public static String getConfigHome(){
2532 } catch (PAPException e) {
2538 public static String getActionHome(){
2541 } catch (PAPException e) {
2547 public static EntityManagerFactory getEmf() {
2551 public IntegrityAudit getIa() {
2555 public static String getPDPFile(){
2556 return XACMLPapServlet.pdpFile;
2559 public static String getPersistenceUnit(){
2560 return PERSISTENCE_UNIT;
2563 public static PAPPolicyEngine getPAPEngine(){
2567 public static PolicyDBDaoTransaction getDbDaoTransaction(){
2568 return policyDBDao.getNewTransaction();
2570 public static String getPapDbDriver() {
2574 public static void setPapDbDriver(String papDbDriver) {
2575 XACMLPapServlet.papDbDriver = papDbDriver;
2578 public static String getPapDbUrl() {
2582 public static void setPapDbUrl(String papDbUrl) {
2583 XACMLPapServlet.papDbUrl = papDbUrl;
2586 public static String getPapDbUser() {
2590 public static void setPapDbUser(String papDbUser) {
2591 XACMLPapServlet.papDbUser = papDbUser;
2594 public static String getPapDbPassword() {
2595 return papDbPassword;
2598 public static void setPapDbPassword(String papDbPassword) {
2599 XACMLPapServlet.papDbPassword = papDbPassword;
2602 public static String getMsOnapName() {
2606 public static void setMsOnapName(String msOnapName) {
2607 XACMLPapServlet.msOnapName = msOnapName;
2610 public static String getMsPolicyName() {
2611 return msPolicyName;
2614 public static void setMsPolicyName(String msPolicyName) {
2615 XACMLPapServlet.msPolicyName = msPolicyName;