[POLICY-73] replace openecomp for policy-engine
[policy/engine.git] / ONAP-PDP-REST / src / main / java / org / onap / policy / pdp / rest / XACMLPdpLoader.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP-PDP-REST
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20
21 package org.onap.policy.pdp.rest;
22
23 import java.io.FileOutputStream;
24 import java.io.IOException;
25 import java.io.InputStream;
26 import java.io.OutputStream;
27 import java.net.MalformedURLException;
28 import java.net.URI;
29 import java.net.URL;
30 import java.net.URLConnection;
31 import java.nio.charset.StandardCharsets;
32 import java.nio.file.Files;
33 import java.nio.file.Path;
34 import java.nio.file.Paths;
35 import java.util.Base64;
36 import java.util.ConcurrentModificationException;
37 import java.util.HashMap;
38 import java.util.Properties;
39 import java.util.Set;
40
41 import org.apache.commons.io.IOUtils;
42 import org.onap.policy.pdp.rest.notifications.NotificationController;
43 import org.onap.policy.rest.XACMLRest;
44 import org.onap.policy.rest.XACMLRestProperties;
45 import org.onap.policy.common.logging.flexlogger.FlexLogger;
46 import org.onap.policy.common.logging.flexlogger.Logger;
47
48 import org.onap.policy.xacml.api.XACMLErrorConstants;
49 import com.att.research.xacml.api.pap.PAPException;
50 import com.att.research.xacml.api.pap.PDPStatus;
51 import com.att.research.xacml.api.pap.PDPStatus.Status;
52 import com.att.research.xacml.api.pdp.PDPEngine;
53 import com.att.research.xacml.api.pdp.PDPEngineFactory;
54 import com.att.research.xacml.api.pip.PIPEngine;
55 import com.att.research.xacml.api.pip.PIPException;
56 import com.att.research.xacml.api.pip.PIPFinder;
57 import com.att.research.xacml.api.pip.PIPFinderFactory;
58 import org.onap.policy.xacml.std.pap.StdPDPPIPConfig;
59 import org.onap.policy.xacml.std.pap.StdPDPPolicy;
60 import org.onap.policy.xacml.std.pap.StdPDPStatus;
61 import com.att.research.xacml.util.FactoryException;
62 import com.att.research.xacml.util.XACMLProperties;
63 import com.att.research.xacmlatt.pdp.policy.PolicyDef;
64 import com.att.research.xacmlatt.pdp.policy.dom.DOMPolicyDef;
65 import com.att.research.xacmlatt.pdp.std.StdPolicyFinderFactory;
66 import com.google.common.base.Splitter;
67
68 /**
69  * Does the work for loading policy and PIP configurations sent from the PAP
70  * servlet.
71  * 
72  * 
73  *
74  */
75 public class XACMLPdpLoader {
76         private static final Logger LOGGER = FlexLogger.getLogger(XACMLPdpLoader.class);
77         private static NotificationController notificationController = new NotificationController();
78         private static final Long notifyDelay = (long) XACMLPdpServlet.getNotificationDelay();
79
80
81         public static synchronized PDPEngine loadEngine(StdPDPStatus status,
82                         Properties policyProperties, Properties pipProperties) {
83                 LOGGER.info("loadEngine: " + policyProperties + " " + pipProperties);
84                 //
85                 // First load our policies
86                 //
87                 try {
88                         //
89                         // Were we given some properties?
90                         //
91                         if (policyProperties == null) {
92                                 //
93                                 // On init we have no incoming configuration, so just
94                                 // Load our current saved configuration
95                                 //
96                                 policyProperties = new Properties();
97                                 try (InputStream is = Files.newInputStream(getPDPPolicyCache())) {
98                                         policyProperties.load(is);
99                                 }
100                         }
101
102                         //
103                         // Get our policy cache up-to-date
104                         //
105                         // Side effects of this include:
106                         // - downloading of policies from remote locations, and
107                         // - creating new "<PolicyId>.file" properties for files existing
108                         // local
109                         //
110                         XACMLPdpLoader.cachePolicies(policyProperties);
111                         //
112                         // Validate the policies
113                         //
114                         XACMLPdpLoader.validatePolicies(policyProperties, status);
115                         if (LOGGER.isDebugEnabled()) {
116                                 LOGGER.debug("Status: " + status);
117                         }
118                 } catch (ConcurrentModificationException e) {
119                         LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + e.getMessage() + e);
120                 } catch (Exception e) {
121                         String error = "Failed to load Policy Cache properties file: "
122                                         + e.getMessage();
123                         LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + error, e);
124                         status.addLoadError(error);
125                         status.setStatus(PDPStatus.Status.LOAD_ERRORS);
126                 }
127                 //
128                 // Load our PIP configuration
129                 //
130                 try {
131                         //
132                         // Were we given some properties to use?
133                         //
134                         if (pipProperties == null) {
135                                 //
136                                 // Load our current saved configuration
137                                 //
138                                 pipProperties = new Properties();
139                                 try (InputStream is = Files.newInputStream(getPIPConfig())) {
140                                         pipProperties.load(is);
141                                 }
142                         }
143                         //
144                         // Validate our PIP configurations
145                         //
146                         XACMLPdpLoader.validatePipConfiguration(pipProperties, status);
147                         if (LOGGER.isDebugEnabled()) {
148                                 LOGGER.debug("Status: " + status);
149                         }
150                 } catch (Exception e) {
151                         String error = "Failed to load/validate Pip Config properties file: "
152                                         + e.getMessage();
153                         LOGGER.error(XACMLErrorConstants.ERROR_DATA_ISSUE + error, e);
154                         status.addLoadError(XACMLErrorConstants.ERROR_PROCESS_FLOW + error);
155                         status.setStatus(PDPStatus.Status.LOAD_ERRORS);
156                 }
157                 //
158                 // Were they validated?
159                 //
160                 if (status.getStatus() == Status.LOAD_ERRORS) {
161                         LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW  +"there were load errors");
162                         return null;
163                 }
164                 //
165                 // Reset our official properties the PDP factory
166                 // uses to configure the PDP engine.
167                 //
168                 XACMLRest.loadXacmlProperties(policyProperties, pipProperties);
169                 //
170                 // Dump ALL our properties that we are trying to load
171                 //
172                 try {
173                         LOGGER.info(XACMLProperties.getProperties().toString());
174                 } catch (IOException e) {
175                         LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Failed to get XACML Properties", e);
176                 }
177                 //
178                 // Now load the PDP engine
179                 //
180                 PDPEngineFactory factory = null;
181                 PDPEngine engine = null;
182                 try {
183                         factory = PDPEngineFactory.newInstance();
184                         engine = factory.newEngine();
185                         LOGGER.info("Loaded new PDP engine.");
186                         status.setStatus(Status.UP_TO_DATE);
187                 } catch (FactoryException e) {
188                         String error = "Failed to create new PDP Engine";
189                         LOGGER.error(XACMLErrorConstants.ERROR_SYSTEM_ERROR +error, e);
190                         status.addLoadError(error);
191                 }
192                 // Notification will be Sent Here.
193                 sendNotification();
194                 return engine;
195         }
196
197         private static HashMap<String, PolicyDef> policyContainer = null;
198         
199         private static void sendNotification(){ 
200                 Thread notify = new Thread(){
201                         public void run(){
202                                 try{
203                                         Thread.sleep(notifyDelay);
204                                         NotificationController.sendNotification();
205                                 }catch(Exception e){
206                                         LOGGER.error(XACMLErrorConstants.ERROR_UNKNOWN + e);
207                                 }
208                         }
209                 };
210                 notify.start();
211         }
212         
213         public static synchronized void validatePolicies(Properties properties,
214                         StdPDPStatus status) throws PAPException {
215                 Set<String> rootPolicies = XACMLProperties.getRootPolicyIDs(properties);
216                 Set<String> refPolicies = XACMLProperties
217                                 .getReferencedPolicyIDs(properties);
218                 policyContainer = new HashMap<String, PolicyDef>();
219
220                 for (String id : rootPolicies) {
221                         loadPolicy(properties, status, id, true);
222                 }
223                 // remember which policies were root policies
224                 status.addAllLoadedRootPolicies(status.getLoadedPolicies());
225
226                 for (String id : refPolicies) {
227                         loadPolicy(properties, status, id, false);
228                 }
229                 LOGGER.info("Loaded " + status.getLoadedPolicies().size()
230                                 + " policies, failed to load "
231                                 + status.getFailedPolicies().size() + " policies, "
232                                 + status.getLoadedRootPolicies().size() + " root policies");
233                 notificationController.check(status, policyContainer);
234                 if (status.getLoadedRootPolicies().size() == 0) {
235                         LOGGER.warn(XACMLErrorConstants.ERROR_PROCESS_FLOW +"NO ROOT POLICIES LOADED!!!  Cannot serve PEP Requests.");
236                         status.addLoadWarning("NO ROOT POLICIES LOADED!!!  Cannot serve PEP Requests.");
237                 }
238                 policyContainer.clear();
239         }
240
241         
242         public static synchronized void loadPolicy(Properties properties,
243                         StdPDPStatus status, String id, boolean isRoot) throws PAPException {
244                 PolicyDef policy = null;
245                 String location = null;
246                 URI locationURI = null;
247                 boolean isFile = false;         
248                 boolean rougeFile = false;
249                 try {
250                         location = properties.getProperty(id + ".file");
251                         if(location != null){
252                                 isFile = true;
253                                 locationURI = Paths.get(location).toUri();
254                                 try (InputStream is = Files.newInputStream(Paths.get(location))) {
255                                         policy = DOMPolicyDef.load(is);
256                                 } catch (Exception e){
257                                         // This Happens if a any issue with the error policyFile. Lets remove it. 
258                                         try {
259                                                 LOGGER.error("Corrupted policy file, deleting: " + location + e);
260                                                 Files.delete(Paths.get(location));
261                                                 properties.remove(id + ".file");
262                                                 rougeFile = true;
263                                         } catch (IOException e1) {
264                                                 LOGGER.error(e1);
265                                         }
266                                 }
267                         }
268                         if(location==null || rougeFile){
269                                 if(rougeFile){
270                                         rougeFile = false;
271                                 }
272                                 location = properties.getProperty(id + ".url");
273                                 if (location != null) {
274                                         //
275                                         // Construct the URL
276                                         //
277                                         int errorCount=0;
278                                         boolean error= false;
279                                         do{
280                                                 error=false;
281                                                 PapUrlResolver papUrls = PapUrlResolver.getInstance();
282                                                 while(papUrls.hasMoreUrls()){
283                                                         String papID = papUrls.getUserId();
284                                                         String papPass = papUrls.getPass();
285                                                         Base64.Encoder encoder = Base64.getEncoder();
286                                                         String encoding = encoder.encodeToString((papID+":"+papPass).getBytes(StandardCharsets.UTF_8));
287                                                         locationURI = URI.create(papUrls.getUrl(PapUrlResolver.extractIdFromUrl(location)));
288                                                         URL url = locationURI.toURL();
289                                                         URLConnection urlConnection = null;
290                                                         try{
291                                                                 urlConnection = url.openConnection();
292                                                         } catch (IOException e){
293                                                                 LOGGER.error("Exception Occured while opening connection" +e);
294                                                                 papUrls.failed();
295                                                                 papUrls.getNext();
296                                                                 break;
297                                                         }
298                                                         urlConnection.setRequestProperty(XACMLRestProperties.PROP_PDP_HTTP_HEADER_ID,
299                                                                                         XACMLProperties.getProperty(XACMLRestProperties.PROP_PDP_ID));
300                                                         urlConnection.setRequestProperty("Authorization", "Basic " + encoding);
301                                                         //
302                                                         // Now construct the output file name
303                                                         //
304                                                         Path outFile = Paths.get(getPDPConfig().toAbsolutePath()
305                                                                         .toString(), id);
306                                                         //
307                                                         // Copy it to disk
308                                                         //
309                                                         try (FileOutputStream fos = new FileOutputStream(
310                                                                         outFile.toFile())) {
311                                                                 IOUtils.copy(urlConnection.getInputStream(), fos);
312                                                         } catch(IOException e){
313                                                                 LOGGER.error("Exception Occured while Copying  input stream" +e);
314                                                                 papUrls.failed();
315                                                                 papUrls.getNext();
316                                                                 break;
317                                                         }
318                                                         //
319                                                         // Now try to load
320                                                         //
321                                                         isFile = true;
322                                                         try (InputStream fis = Files.newInputStream(outFile)) {
323                                                                 policy = DOMPolicyDef.load(fis);
324                                                         }catch(Exception e){
325                                                                 try {
326                                                                         LOGGER.error("Corrupted policy file, deleting: " + location +e);
327                                                                         Files.delete(outFile);
328                                                                         error = true;
329                                                                         errorCount++;
330                                                                         break;
331                                                                 } catch (IOException e1) {
332                                                                         LOGGER.error(e1);
333                                                                 }
334                                                         }
335                                                         //
336                                                         // Save it
337                                                         //
338                                                         properties.setProperty(id + ".file", outFile
339                                                                         .toAbsolutePath().toString());
340                                                         error = false;
341                                                         break;
342                                                 }
343                                         }while(error && errorCount>2);
344                                 }
345                         } 
346                         if (policy != null) {
347                                 status.addLoadedPolicy(new StdPDPPolicy(id, isRoot,
348                                                 locationURI, properties));
349                                 LOGGER.info("Loaded policy: " + policy.getIdentifier()
350                                                 + " version: " + policy.getVersion().stringValue());
351                                 // Sending the policy objects to the Notification Controller.
352                                 policyContainer.put(id, policy);
353                         } else {
354                                 String error = "Failed to load policy " + location;
355                                 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + error);
356                                 status.setStatus(PDPStatus.Status.LOAD_ERRORS);
357                                 status.addLoadError(error);
358                                 status.addFailedPolicy(new StdPDPPolicy(id, isRoot));
359                         }
360                 } catch (Exception e) {
361                         LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW +"Failed to load policy '" + id + "' from location '"
362                                         + location + "'", e);
363                         status.setStatus(PDPStatus.Status.LOAD_ERRORS);
364                         status.addFailedPolicy(new StdPDPPolicy(id, isRoot));
365                         //
366                         // Is it a file?
367                         //
368                         if (isFile) {
369                                 //
370                                 // Let's remove it
371                                 //
372                                 try {
373                                         LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Corrupted policy file, deleting: " + location);
374                                         Files.delete(Paths.get(location));
375                                         
376                                 } catch (IOException e1) {
377                                         LOGGER.error(XACMLErrorConstants.ERROR_SYSTEM_ERROR + e1);
378                                 }
379                         }
380                 }
381         }
382
383         public static synchronized void validatePipConfiguration(
384                         Properties properties, StdPDPStatus status) throws PAPException {
385                 try {
386                         PIPFinderFactory factory = PIPFinderFactory.newInstance(properties);
387                         if (factory == null) {
388                                 throw new FactoryException(
389                                                 "Could not create PIP Finder Factory: "
390                                                                 + properties
391                                                                                 .getProperty(XACMLProperties.PROP_PIPFINDERFACTORY));
392                         }
393                         PIPFinder finder = factory.getFinder(properties);
394                         //
395                         // Check for this, although it should always return something
396                         //
397                         if (finder == null) {
398                                 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "pip finder factory returned a null engine.");
399                                 throw new PIPException("Could not create PIP Finder");
400                         } else {
401                                 LOGGER.info("Loaded PIP finder");
402                         }
403                         for (PIPEngine engine : finder.getPIPEngines()) {
404                                 LOGGER.info("Configured PIP Engine: " + engine.getName());
405                                 StdPDPPIPConfig config = new StdPDPPIPConfig();
406                                 config.setName(engine.getName());
407                                 status.addLoadedPipConfig(config);
408                         }
409                 } catch (FactoryException | PIPException e) {
410                         LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "validate PIP configuration failed: "
411                                         + e.getLocalizedMessage());
412                         status.addLoadError(e.getLocalizedMessage());
413                         status.setStatus(Status.LOAD_ERRORS);
414                         throw new PAPException(e);
415                 }
416         }
417
418         /**
419          * Iterates the policies defined in the props object to ensure they are
420          * loaded locally. Policies are searched for in the following order: - see
421          * if the current properties has a "&lt;PolicyID&gt;.file" entry and that
422          * file exists in the local directory - if not, see if the file exists in
423          * the local directory; if so create a ".file" property for it. - if not,
424          * get the "&lt;PolicyID&gt;.url" property and try to GET the policy from
425          * that location (and set the ".file" property)
426          * 
427          * If the ".file" property is created, then true is returned to tell the
428          * caller that the props object changed.
429          * 
430          * @param props
431          * @return true/false if anything was changed in the props object
432          * @throws PAPException
433          */
434         public static synchronized boolean cachePolicies(Properties props)
435                         throws PAPException {
436                 boolean changed = false;
437                 String[] lists = new String[2];
438                 lists[0] = props.getProperty(XACMLProperties.PROP_ROOTPOLICIES);
439                 lists[1] = props.getProperty(XACMLProperties.PROP_REFERENCEDPOLICIES);
440                 for (String list : lists) {
441                         //
442                         // Check for a null or empty parameter
443                         //
444                         if (list == null || list.length() == 0) {
445                                 continue;
446                         }
447                         Iterable<String> policies = Splitter.on(',').trimResults()
448                                         .omitEmptyStrings().split(list);
449                         for (String policy : policies) {
450                                 boolean policyExists = false;
451
452                                 // First look for ".file" property and verify the file exists
453                                 String propLocation = props.getProperty(policy
454                                                 + StdPolicyFinderFactory.PROP_FILE);
455                                 if (propLocation != null) {
456                                         //
457                                         // Does it exist?
458                                         //
459                                         policyExists = Files.exists(Paths.get(propLocation));
460                                         if (policyExists == false) {
461                                                 LOGGER.warn(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Policy file " + policy + " expected at "
462                                                                 + propLocation + " does NOT exist.");
463                                         }
464                                 }
465
466                                 // If ".file" property does not exist, try looking for the local
467                                 // file anyway
468                                 // (it might exist without having a ".file" property set for it)
469                                 if (policyExists == false) {
470                                         //
471                                         // Now construct the output file name
472                                         //
473                                         Path outFile = Paths.get(getPDPConfig().toAbsolutePath()
474                                                         .toString(), policy);
475                                         //
476                                         // Double check to see if we pulled it at some point
477                                         //
478                                         policyExists = Files.exists(outFile);
479                                         if (policyExists) {
480                                                 //
481                                                 // Set the property so the PDP engine doesn't have
482                                                 // to pull it from the URL but rather the FILE.
483                                                 //
484                                                 LOGGER.info("Policy does exist: "
485                                                                 + outFile.toAbsolutePath().toString());
486                                                 props.setProperty(policy
487                                                                 + StdPolicyFinderFactory.PROP_FILE, outFile
488                                                                 .toAbsolutePath().toString());
489                                                 //
490                                                 // Indicate that there were changes made to the
491                                                 // properties
492                                                 //
493                                                 changed = true;
494                                         } else {
495
496                                                 // File does not exist locally, so we need to get it
497                                                 // from the location given in the ".url" property (which
498                                                 // MUST exist)
499
500                                                 //
501                                                 // There better be a URL to retrieve it
502                                                 //
503                                                 propLocation = props.getProperty(policy
504                                                                 + StdPolicyFinderFactory.PROP_URL);
505                                                 if (propLocation != null) {
506                                                         //
507                                                         // Get it
508                                                         //
509                                                         PapUrlResolver papUrls = PapUrlResolver.getInstance();
510                                                         while(papUrls.hasMoreUrls()){
511                                                                 String papID = papUrls.getUserId();
512                                                                 String papPass = papUrls.getPass();
513                                                                 Base64.Encoder encoder = Base64.getEncoder();
514                                                                 String encoding = encoder.encodeToString((papID+":"+papPass).getBytes(StandardCharsets.UTF_8));
515                                                                 URL url = null;
516                                                                 try {
517                                                                         //
518                                                                         // Create the URL
519                                                                         //
520                                                                         url = new URL(papUrls.getUrl(PapUrlResolver.extractIdFromUrl(propLocation)));
521                                                                         LOGGER.info("Pulling " + url.toString());
522                                                                         //
523                                                                         // Open the connection
524                                                                         //
525                                                                         URLConnection urlConnection = url.openConnection();
526                                                                         urlConnection.setRequestProperty(XACMLRestProperties.PROP_PDP_HTTP_HEADER_ID,
527                                                                                                         XACMLProperties.getProperty(XACMLRestProperties.PROP_PDP_ID));
528                                                                         urlConnection.setRequestProperty("Authorization", "Basic " + encoding);
529                                                                         //
530                                                                         // Copy it to disk
531                                                                         //
532                                                                         try (InputStream is = urlConnection
533                                                                                         .getInputStream();
534                                                                                         OutputStream os = new FileOutputStream(
535                                                                                                         outFile.toFile())) {
536                                                                                 IOUtils.copy(is, os);
537                                                                         }
538                                                                         //
539                                                                         // Now save it in the properties as a .file
540                                                                         //
541                                                                         LOGGER.info("Pulled policy: "
542                                                                                         + outFile.toAbsolutePath().toString());
543                                                                         props.setProperty(policy
544                                                                                         + StdPolicyFinderFactory.PROP_FILE,
545                                                                                         outFile.toAbsolutePath().toString());
546                                                                         papUrls.succeeded();
547                                                                         //
548                                                                         // Indicate that there were changes made to the
549                                                                         // properties
550                                                                         //
551                                                                         changed = true;
552                                                                 } catch (Exception e) {
553                                                                         papUrls.failed();
554                                                                         if (e instanceof MalformedURLException) {
555                                                                                 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Policy '"
556                                                                                                 + policy
557                                                                                                 + "' had bad URL in new configuration, URL='"
558                                                                                                 + propLocation + "'");
559                                                                                 
560                                                                         } else {
561                                                                                 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error while retrieving policy "
562                                                                                                 + policy
563                                                                                                 + " from URL "
564                                                                                                 + url + ", e=" + e);
565                                                                         }
566                                                                 }
567                                                                 papUrls.getNext();
568                                                         }
569                                                 } else {
570                                                         LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW +  "Policy " + policy
571                                                                         + " does NOT exist and does NOT have a URL");
572                                                 }
573                                         }
574                                 }
575                         }
576                 }
577                 return changed;
578         }
579
580         public static synchronized Path getPDPPolicyCache() throws PAPException {
581                 Path config = getPDPConfig();
582                 Path policyProperties = Paths.get(config.toAbsolutePath().toString(),
583                                 "xacml.policy.properties");
584                 if (Files.notExists(policyProperties)) {
585                         LOGGER.warn(XACMLErrorConstants.ERROR_PROCESS_FLOW +  policyProperties.toAbsolutePath().toString()
586                                         + " does NOT exist.");
587                         //
588                         // Try to create the file
589                         //
590                         try {
591                                 Files.createFile(policyProperties);
592                         } catch (IOException e) {
593                                 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Failed to create policy properties file: "
594                                                 + policyProperties.toAbsolutePath().toString() +e);
595                                 throw new PAPException(
596                                                 "Failed to create policy properties file: "
597                                                                 + policyProperties.toAbsolutePath().toString());
598                         }
599                 }
600                 return policyProperties;
601         }
602
603         public static synchronized Path getPIPConfig() throws PAPException {
604                 Path config = getPDPConfig();
605                 Path pipConfigProperties = Paths.get(
606                                 config.toAbsolutePath().toString(), "xacml.pip.properties");
607                 if (Files.notExists(pipConfigProperties)) {
608                         LOGGER.warn(XACMLErrorConstants.ERROR_PROCESS_FLOW + pipConfigProperties.toAbsolutePath().toString()
609                                         + " does NOT exist.");
610                         //
611                         // Try to create the file
612                         //
613                         try {
614                                 Files.createFile(pipConfigProperties);
615                         } catch (IOException e) {
616                                 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Failed to create pip properties file: "
617                                                 + pipConfigProperties.toAbsolutePath().toString() +e);
618                                 throw new PAPException("Failed to create pip properties file: "
619                                                 + pipConfigProperties.toAbsolutePath().toString());
620                         }
621                 }
622                 return pipConfigProperties;
623         }
624
625         public static synchronized Path getPDPConfig() throws PAPException {
626                 Path config = Paths.get(XACMLProperties
627                                 .getProperty(XACMLRestProperties.PROP_PDP_CONFIG));
628                 if (Files.notExists(config)) {
629                         LOGGER.warn(XACMLErrorConstants.ERROR_PROCESS_FLOW + config.toAbsolutePath().toString() + " does NOT exist.");
630                         //
631                         // Try to create the directory
632                         //
633                         try {
634                                 Files.createDirectories(config);
635                         } catch (IOException e) {
636                                 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Failed to create config directory: "
637                                                 + config.toAbsolutePath().toString(), e);
638                                 throw new PAPException("Failed to create config directory: "
639                                                 + config.toAbsolutePath().toString());
640                         }
641                 }
642                 return config;
643         }
644
645 }