X-Git-Url: https://gerrit.onap.org/r/gitweb?p=policy%2Fengine.git;a=blobdiff_plain;f=ONAP-PDP-REST%2Fsrc%2Fmain%2Fjava%2Forg%2Fonap%2Fpolicy%2Fpdp%2Frest%2FXACMLPdpLoader.java;h=52dcd96f3f9aa9a02198726dd82be43c56951543;hp=9b2b61f76ef3f17f9d529eaaee504f25b2ea4ad8;hb=c2ca6ea5cb44103903e1409e8dd6db80167e61e8;hpb=827a2016429bc377e28d2a414b6bcbdf8b6dc924 diff --git a/ONAP-PDP-REST/src/main/java/org/onap/policy/pdp/rest/XACMLPdpLoader.java b/ONAP-PDP-REST/src/main/java/org/onap/policy/pdp/rest/XACMLPdpLoader.java index 9b2b61f76..52dcd96f3 100644 --- a/ONAP-PDP-REST/src/main/java/org/onap/policy/pdp/rest/XACMLPdpLoader.java +++ b/ONAP-PDP-REST/src/main/java/org/onap/policy/pdp/rest/XACMLPdpLoader.java @@ -2,14 +2,14 @@ * ============LICENSE_START======================================================= * ONAP-PDP-REST * ================================================================================ - * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at - * + * * http://www.apache.org/licenses/LICENSE-2.0 - * + * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. @@ -40,8 +40,8 @@ import java.util.Set; import org.apache.commons.io.IOUtils; import org.onap.policy.pdp.rest.notifications.NotificationController; -import org.onap.policy.rest.XACMLRest; -import org.onap.policy.rest.XACMLRestProperties; +import org.onap.policy.rest.XacmlRest; +import org.onap.policy.rest.XacmlRestProperties; import org.onap.policy.common.logging.flexlogger.FlexLogger; import org.onap.policy.common.logging.flexlogger.Logger; @@ -68,8 +68,8 @@ import com.google.common.base.Splitter; /** * Does the work for loading policy and PIP configurations sent from the PAP * servlet. - * - * + * + * * */ public class XACMLPdpLoader { @@ -107,10 +107,12 @@ public class XACMLPdpLoader { // - creating new ".file" properties for files existing // local // + LOGGER.info("XACMLPdpLoader: cache the policies."); XACMLPdpLoader.cachePolicies(policyProperties); // // Validate the policies // + LOGGER.info("XACMLPdpLoader: validating the policies."); XACMLPdpLoader.validatePolicies(policyProperties, status); if (LOGGER.isDebugEnabled()) { LOGGER.debug("Status: " + status); @@ -165,7 +167,7 @@ public class XACMLPdpLoader { // Reset our official properties the PDP factory // uses to configure the PDP engine. // - XACMLRest.loadXacmlProperties(policyProperties, pipProperties); + XacmlRest.loadXacmlProperties(policyProperties, pipProperties); // // Dump ALL our properties that we are trying to load // @@ -193,10 +195,11 @@ public class XACMLPdpLoader { } private static HashMap policyContainer = null; - + public static synchronized void sendNotification(){ Thread notify = new Thread(){ - public void run(){ + @Override + public void run(){ try{ Thread.sleep(notifyDelay); NotificationController.sendNotification(); @@ -207,7 +210,7 @@ public class XACMLPdpLoader { }; notify.start(); } - + public static synchronized void validatePolicies(Properties properties, StdPDPStatus status) throws PAPException { Set rootPolicies = XACMLProperties.getRootPolicyIDs(properties); @@ -215,12 +218,13 @@ public class XACMLPdpLoader { .getReferencedPolicyIDs(properties); policyContainer = new HashMap(); + LOGGER.info("XACMLPdpLoader: load rootPolicies"); for (String id : rootPolicies) { loadPolicy(properties, status, id, true); } // remember which policies were root policies status.addAllLoadedRootPolicies(status.getLoadedPolicies()); - + LOGGER.info("XACMLPdpLoader: load referencedPolicies"); for (String id : refPolicies) { loadPolicy(properties, status, id, false); } @@ -236,13 +240,13 @@ public class XACMLPdpLoader { policyContainer.clear(); } - + public static synchronized void loadPolicy(Properties properties, StdPDPStatus status, String id, boolean isRoot) throws PAPException { PolicyDef policy = null; String location = null; URI locationURI = null; - boolean isFile = false; + boolean isFile = false; boolean rougeFile = false; try { location = properties.getProperty(id + ".file"); @@ -252,7 +256,7 @@ public class XACMLPdpLoader { try (InputStream is = Files.newInputStream(Paths.get(location))) { policy = DOMPolicyDef.load(is); } catch (Exception e){ - // This Happens if a any issue with the error policyFile. Lets remove it. + // This Happens if a any issue with the error policyFile. Lets remove it. try { LOGGER.error("Corrupted policy file, deleting: " + location + e); Files.delete(Paths.get(location)); @@ -293,8 +297,8 @@ public class XACMLPdpLoader { papUrls.getNext(); break; } - urlConnection.setRequestProperty(XACMLRestProperties.PROP_PDP_HTTP_HEADER_ID, - XACMLProperties.getProperty(XACMLRestProperties.PROP_PDP_ID)); + urlConnection.setRequestProperty(XacmlRestProperties.PROP_PDP_HTTP_HEADER_ID, + XACMLProperties.getProperty(XacmlRestProperties.PROP_PDP_ID)); urlConnection.setRequestProperty("Authorization", "Basic " + encoding); // // Now construct the output file name @@ -340,7 +344,7 @@ public class XACMLPdpLoader { } }while(error && errorCount>2); } - } + } if (policy != null) { status.addLoadedPolicy(new StdPDPPolicy(id, isRoot, locationURI, properties)); @@ -370,7 +374,7 @@ public class XACMLPdpLoader { try { LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Corrupted policy file, deleting: " + location); Files.delete(Paths.get(location)); - + } catch (IOException e1) { LOGGER.error(XACMLErrorConstants.ERROR_SYSTEM_ERROR + e1); } @@ -421,10 +425,10 @@ public class XACMLPdpLoader { * the local directory; if so create a ".file" property for it. - if not, * get the "<PolicyID>.url" property and try to GET the policy from * that location (and set the ".file" property) - * + * * If the ".file" property is created, then true is returned to tell the * caller that the props object changed. - * + * * @param props * @return true/false if anything was changed in the props object * @throws PAPException @@ -521,8 +525,8 @@ public class XACMLPdpLoader { // Open the connection // URLConnection urlConnection = url.openConnection(); - urlConnection.setRequestProperty(XACMLRestProperties.PROP_PDP_HTTP_HEADER_ID, - XACMLProperties.getProperty(XACMLRestProperties.PROP_PDP_ID)); + urlConnection.setRequestProperty(XacmlRestProperties.PROP_PDP_HTTP_HEADER_ID, + XACMLProperties.getProperty(XacmlRestProperties.PROP_PDP_ID)); urlConnection.setRequestProperty("Authorization", "Basic " + encoding); // // Copy it to disk @@ -547,20 +551,14 @@ public class XACMLPdpLoader { // properties // changed = true; + } catch (MalformedURLException e) { + papUrls.failed(); + LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Policy '" + policy + + "' had bad URL in new configuration, URL='" + propLocation + "'"); } catch (Exception e) { papUrls.failed(); - if (e instanceof MalformedURLException) { - LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Policy '" - + policy - + "' had bad URL in new configuration, URL='" - + propLocation + "'"); - - } else { - LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error while retrieving policy " - + policy - + " from URL " - + url + ", e=" + e); - } + LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error while retrieving policy " + + policy + " from URL " + url + ", e=" + e); } papUrls.getNext(); } @@ -622,7 +620,7 @@ public class XACMLPdpLoader { public static synchronized Path getPDPConfig() throws PAPException { Path config = Paths.get(XACMLProperties - .getProperty(XACMLRestProperties.PROP_PDP_CONFIG)); + .getProperty(XacmlRestProperties.PROP_PDP_CONFIG)); if (Files.notExists(config)) { LOGGER.warn(XACMLErrorConstants.ERROR_PROCESS_FLOW + config.toAbsolutePath().toString() + " does NOT exist."); //