CHeckstyle and JUnit for base package in ONAP-REST
[policy/engine.git] / ONAP-PDP-REST / src / main / java / org / onap / policy / pdp / rest / XACMLPdpLoader.java
index 7526af6..52dcd96 100644 (file)
@@ -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 "<PolicyId>.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
                //
@@ -189,16 +191,15 @@ public class XACMLPdpLoader {
                        LOGGER.error(XACMLErrorConstants.ERROR_SYSTEM_ERROR +error, e);
                        status.addLoadError(error);
                }
-               // Notification will be Sent Here.
-               sendNotification();
                return engine;
        }
 
        private static HashMap<String, PolicyDef> policyContainer = null;
-       
-       private static void sendNotification(){ 
+
+       public static synchronized void sendNotification(){
                Thread notify = new Thread(){
-                       public void run(){
+                       @Override
+            public void run(){
                                try{
                                        Thread.sleep(notifyDelay);
                                        NotificationController.sendNotification();
@@ -209,7 +210,7 @@ public class XACMLPdpLoader {
                };
                notify.start();
        }
-       
+
        public static synchronized void validatePolicies(Properties properties,
                        StdPDPStatus status) throws PAPException {
                Set<String> rootPolicies = XACMLProperties.getRootPolicyIDs(properties);
@@ -217,12 +218,13 @@ public class XACMLPdpLoader {
                                .getReferencedPolicyIDs(properties);
                policyContainer = new HashMap<String, PolicyDef>();
 
+               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);
                }
@@ -238,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");
@@ -254,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));
@@ -295,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
@@ -342,7 +344,7 @@ public class XACMLPdpLoader {
                                                }
                                        }while(error && errorCount>2);
                                }
-                       } 
+                       }
                        if (policy != null) {
                                status.addLoadedPolicy(new StdPDPPolicy(id, isRoot,
                                                locationURI, properties));
@@ -372,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);
                                }
@@ -423,10 +425,10 @@ public class XACMLPdpLoader {
         * the local directory; if so create a ".file" property for it. - if not,
         * get the "&lt;PolicyID&gt;.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
@@ -523,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
@@ -549,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();
                                                        }
@@ -624,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.");
                        //