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