Fixed the Policy API issues and Bugfixes
[policy/engine.git] / ECOMP-PAP-REST / src / main / java / org / openecomp / policy / pap / xacml / rest / XACMLPapServlet.java
index 1b5f854..edbc2ac 100644 (file)
@@ -152,10 +152,10 @@ public class XACMLPapServlet extends HttpServlet implements StdItemSetChangeList
         * These are the parameters needed for DB access from the PAP
         */
        private static int papIntegrityAuditPeriodSeconds = -1;
-       public static String papDbDriver = null;
-       public static String papDbUrl = null;
-       public static String papDbUser = null;
-       public static String papDbPassword = null;
+       private static String papDbDriver = null;
+       private static String papDbUrl = null;
+       private static String papDbUser = null;
+       private static String papDbPassword = null;
        private static Integer papTransWait = null;
        private static Integer papTransTimeout = null;
        private static Integer papAuditTimeout = null;
@@ -179,8 +179,8 @@ public class XACMLPapServlet extends HttpServlet implements StdItemSetChangeList
        private IntegrityAudit ia;
        
        //MicroService Model Properties
-       public static String msEcompName;
-       public static String msPolicyName;
+       private static String msEcompName;
+       private static String msPolicyName;
        /*
         * This thread may be invoked upon startup to initiate sending PDP policy/pip configuration when
         * this servlet starts. Its configurable by the admin.
@@ -224,21 +224,25 @@ public class XACMLPapServlet extends HttpServlet implements StdItemSetChangeList
                                PolicyLogger.error(MessageCodes.ERROR_DATA_ISSUE,"XACMLPapServlet", " ERROR: Bad papDbDriver property entry");
                                throw new PAPException("papDbDriver is null");
                        }
+                       setPapDbDriver(papDbDriver);
                        papDbUrl = XACMLProperties.getProperty(XACMLRestProperties.PROP_PAP_DB_URL);
                        if(papDbUrl == null){
                                PolicyLogger.error(MessageCodes.ERROR_DATA_ISSUE,"XACMLPapServlet", " ERROR: Bad papDbUrl property entry");
                                throw new PAPException("papDbUrl is null");
                        }
+                       setPapDbUrl(papDbUrl);
                        papDbUser = XACMLProperties.getProperty(XACMLRestProperties.PROP_PAP_DB_USER);
                        if(papDbUser == null){
                                PolicyLogger.error(MessageCodes.ERROR_DATA_ISSUE,"XACMLPapServlet", " ERROR: Bad papDbUser property entry");
                                throw new PAPException("papDbUser is null");
                        }
+                       setPapDbUser(papDbUser);
                        papDbPassword = XACMLProperties.getProperty(XACMLRestProperties.PROP_PAP_DB_PASSWORD);
                        if(papDbPassword == null){
                                PolicyLogger.error(MessageCodes.ERROR_DATA_ISSUE,"XACMLPapServlet", " ERROR: Bad papDbPassword property entry");
                                throw new PAPException("papDbPassword is null");
                        }
+                       setPapDbPassword(papDbPassword);
                        papResourceName = XACMLProperties.getProperty(XACMLRestProperties.PAP_RESOURCE_NAME);
                        if(papResourceName == null){
                                PolicyLogger.error(MessageCodes.ERROR_DATA_ISSUE,"XACMLPapServlet", " ERROR: Bad papResourceName property entry");
@@ -350,7 +354,9 @@ public class XACMLPapServlet extends HttpServlet implements StdItemSetChangeList
                        }
                        //Micro Service Properties
                        msEcompName=properties.getProperty("xacml.policy.msEcompName");
+                       setMsEcompName(msEcompName);
                        msPolicyName=properties.getProperty("xacml.policy.msPolicyName");
+                       setMsPolicyName(msPolicyName);
                        // PDPId File location 
                        XACMLPapServlet.pdpFile = XACMLProperties.getProperty(XACMLRestProperties.PROP_PDP_IDFILE);
                        if (XACMLPapServlet.pdpFile == null) {
@@ -1275,12 +1281,12 @@ public class XACMLPapServlet extends HttpServlet implements StdItemSetChangeList
                        PolicyLogger.info("JSON request from PolicyEngine API: " + json);
                        // convert Object sent as JSON into local object
                        StdPDPPolicy policy = PolicyUtils.jsonStringToObject(json, StdPDPPolicy.class);
-                       Set<PDPPolicy> policies = new HashSet<PDPPolicy>();
+                       Set<PDPPolicy> policies = new HashSet<>();
                        if(policy!=null){
                                policies.add(policy);
                        }
                        //Get the current policies from the Group and Add the new one
-                       Set<PDPPolicy> currentPoliciesInGroup = new HashSet<PDPPolicy>();
+                       Set<PDPPolicy> currentPoliciesInGroup = new HashSet<>();
                        currentPoliciesInGroup = group.getPolicies();
                        //If the selected policy is in the group we must remove it because the name is default
                        Iterator<PDPPolicy> policyIterator = policies.iterator();
@@ -1296,7 +1302,7 @@ public class XACMLPapServlet extends HttpServlet implements StdItemSetChangeList
                                }
                        }
                        //Update the PDP Group after removing old version of policy
-                       Set<PDPPolicy> updatedPoliciesInGroup = new HashSet<PDPPolicy>();
+                       Set<PDPPolicy> updatedPoliciesInGroup = new HashSet<>();
                        updatedPoliciesInGroup = group.getPolicies();
                        //need to remove the policy with default name from group
                        for (PDPPolicy updatedPolicy : currentPoliciesInGroup) {
@@ -1319,9 +1325,13 @@ public class XACMLPapServlet extends HttpServlet implements StdItemSetChangeList
                                throw new PAPException(e.getMessage()); 
                        }
                        papEngine.updateGroup(group);
+                       String policyId = "empty";
+                       if(policy!=null){
+                               policyId = policy.getId();
+                       }
                        response.setStatus(HttpServletResponse.SC_NO_CONTENT);
                        response.addHeader("operation", "push");
-                       response.addHeader("policyId", policy.getId());
+                       response.addHeader("policyId", policyId);
                        response.addHeader("groupId", groupId);
                        if (LOGGER.isDebugEnabled()) {          
                                LOGGER.debug("Group '" + group.getId() + "' updated");
@@ -1333,7 +1343,7 @@ public class XACMLPapServlet extends HttpServlet implements StdItemSetChangeList
                        loggingContext.transactionEnded();
                        auditLogger.info("Success");
 
-                       if ((policy.getId().contains("Config_MS_")) || (policy.getId().contains("BRMS_Param"))) {
+                       if (policy != null && (policy.getId().contains("Config_MS_")) || (policy.getId().contains("BRMS_Param"))) {
                                PushPolicyHandler pushPolicyHandler = PushPolicyHandler.getInstance();
                                if (pushPolicyHandler.preSafetyCheck(policy, CONFIG_HOME)) {
                                        LOGGER.debug("Precheck Successful.");
@@ -1791,10 +1801,12 @@ public class XACMLPapServlet extends HttpServlet implements StdItemSetChangeList
                                if (papEngine.getPDP(pdpId) == null) {
                                        // this is a request to create a new PDP object
                                        try{
-                                               acPutTransaction.addPdpToGroup(pdp.getId(), group.getId(), pdp.getName(), pdp.getDescription(), pdp.getJmxPort(),"XACMLPapServlet.doACPut");
+                                               acPutTransaction.addPdpToGroup(pdp == null ? "PDP is null" : pdp.getId(), group.getId(), pdp == null ? "PDP is null" : pdp.getName(), 
+                                                               pdp == null ? "PDP is null" : pdp.getDescription(), 
+                                                               pdp == null ? 0 : pdp.getJmxPort(),"XACMLPapServlet.doACPut");
                                        } catch(Exception e){
                                                PolicyLogger.error(MessageCodes.ERROR_PROCESS_FLOW, e, "XACMLPapServlet", " Error while adding pdp to group in the database: "
-                                                               +"pdp="+pdp.getId()+",to group="+group.getId());
+                                                               +"pdp="+ (pdp == null ? "PDP is null" : pdp.getId()) +",to group="+group.getId());
                                                throw new PAPException(e.getMessage());
                                        }
                                        papEngine.newPDP(pdp.getId(), group, pdp.getName(), pdp.getDescription(), pdp.getJmxPort());
@@ -1803,7 +1815,7 @@ public class XACMLPapServlet extends HttpServlet implements StdItemSetChangeList
                                                acPutTransaction.updatePdp(pdp, "XACMLPapServlet.doACPut");
                                        } catch(Exception e){
                                                PolicyLogger.error(MessageCodes.ERROR_PROCESS_FLOW, e, "XACMLPapServlet", " Error while updating pdp in the database: "
-                                                               +"pdp="+pdp.getId());
+                                                               +"pdp="+(pdp == null ? "PDP is null" : pdp.getId()));
                                                throw new PAPException(e.getMessage());
                                        }
                                        // this is a request to update the pdp
@@ -1857,7 +1869,9 @@ public class XACMLPapServlet extends HttpServlet implements StdItemSetChangeList
                                // The Path on the PAP side is not carried on the RESTful interface with the AC
                                // (because it is local to the PAP)
                                // so we need to fill that in before submitting the group for update
-                               ((StdPDPGroup)objectFromJSON).setDirectory(((StdPDPGroup)group).getDirectory());
+                               if(objectFromJSON != null){
+                                       ((StdPDPGroup)objectFromJSON).setDirectory(((StdPDPGroup)group).getDirectory());
+                               }
                                try{
                                        acPutTransaction.updateGroup((StdPDPGroup)objectFromJSON, "XACMLPapServlet.doACPut");
                                } catch(Exception e){
@@ -1992,7 +2006,7 @@ public class XACMLPapServlet extends HttpServlet implements StdItemSetChangeList
                                        moveToGroup = papEngine.getGroup(moveToGroupId);
                                }
                                // get list of PDPs in the group being deleted so we can notify them that they got changed
-                               Set<EcompPDP> movedPDPs = new HashSet<EcompPDP>();
+                               Set<EcompPDP> movedPDPs = new HashSet<>();
                                movedPDPs.addAll(group.getEcompPdps());
                                // do the move/remove
                                try{
@@ -2049,7 +2063,7 @@ public class XACMLPapServlet extends HttpServlet implements StdItemSetChangeList
         */
        private class Heartbeat implements Runnable {
                private PAPPolicyEngine papEngine;
-               private Set<EcompPDP> pdps = new HashSet<EcompPDP>();
+               private Set<EcompPDP> pdps = new HashSet<>();
                private int heartbeatInterval;
                private int heartbeatTimeout;
 
@@ -2075,7 +2089,7 @@ public class XACMLPapServlet extends HttpServlet implements StdItemSetChangeList
                        synchronized(this) {
                                this.isRunning = true;
                        }
-                       HashMap<String, URL> idToURLMap = new HashMap<String, URL>();
+                       HashMap<String, URL> idToURLMap = new HashMap<>();
                        try {
                                while (this.isRunning()) {
                                        // Wait the given time
@@ -2128,26 +2142,28 @@ public class XACMLPapServlet extends HttpServlet implements StdItemSetChangeList
                                                HttpURLConnection connection = null;
                                                try {
                                                        // Open up the connection
-                                                       connection = (HttpURLConnection)pdpURL.openConnection();
-                                                       // Setup our method and headers
-                                                       connection.setRequestMethod("GET");
-                                                       connection.setConnectTimeout(heartbeatTimeout);
-                                                       // Authentication
-                                                       String encoding = CheckPDP.getEncoding(pdp.getId());
-                                                       if(encoding !=null){
-                                                               connection.setRequestProperty("Authorization", "Basic " + encoding);
-                                                       }
-                                                       // Do the connect
-                                                       connection.connect();
-                                                       if (connection.getResponseCode() == 204) {
-                                                               newStatus = connection.getHeaderField(XACMLRestProperties.PROP_PDP_HTTP_HEADER_HB);
-                                                               if (LOGGER.isDebugEnabled()) {
-                                                                       LOGGER.debug("Heartbeat '" + pdp.getId() + "' status='" + newStatus + "'");
+                                                       if(pdpURL != null){
+                                                               connection = (HttpURLConnection)pdpURL.openConnection();
+                                                               // Setup our method and headers
+                                                               connection.setRequestMethod("GET");
+                                                               connection.setConnectTimeout(heartbeatTimeout);
+                                                               // Authentication
+                                                               String encoding = CheckPDP.getEncoding(pdp.getId());
+                                                               if(encoding !=null){
+                                                                       connection.setRequestProperty("Authorization", "Basic " + encoding);
                                                                }
-                                                       } else {
-                                                               // anything else is an unexpected result
-                                                               newStatus = PDPStatus.Status.UNKNOWN.toString();
-                                                               PolicyLogger.error(MessageCodes.ERROR_SYSTEM_ERROR + " Heartbeat connect response code " + connection.getResponseCode() + ": " + pdp.getId());
+                                                               // Do the connect
+                                                               connection.connect();
+                                                               if (connection.getResponseCode() == 204) {
+                                                                       newStatus = connection.getHeaderField(XACMLRestProperties.PROP_PDP_HTTP_HEADER_HB);
+                                                                       if (LOGGER.isDebugEnabled()) {
+                                                                               LOGGER.debug("Heartbeat '" + pdp.getId() + "' status='" + newStatus + "'");
+                                                                       }
+                                                               } else {
+                                                                       // anything else is an unexpected result
+                                                                       newStatus = PDPStatus.Status.UNKNOWN.toString();
+                                                                       PolicyLogger.error(MessageCodes.ERROR_SYSTEM_ERROR + " Heartbeat connect response code " + connection.getResponseCode() + ": " + pdp.getId());
+                                                               }       
                                                        }
                                                } catch (UnknownHostException e) {
                                                        newStatus = PDPStatus.Status.NO_SUCH_HOST.toString();
@@ -2163,7 +2179,8 @@ public class XACMLPapServlet extends HttpServlet implements StdItemSetChangeList
                                                        PolicyLogger.error(MessageCodes.ERROR_SYSTEM_ERROR, e, "XACMLPapServlet", "Heartbeat '" + pdp.getId() + "' connect exception");
                                                } finally {
                                                        // cleanup the connection
-                                                       connection.disconnect();
+                                                       if(connection != null)
+                                                               connection.disconnect();
                                                }
                                                if ( ! pdp.getStatus().getStatus().toString().equals(newStatus)) {
                                                        if (LOGGER.isDebugEnabled()) {
@@ -2362,7 +2379,9 @@ public class XACMLPapServlet extends HttpServlet implements StdItemSetChangeList
                                }
                        } finally {
                                // cleanup the connection
-                               connection.disconnect();
+                               if(connection != null){
+                                       connection.disconnect();        
+                               }
                                // tell the AC to update it's status info
                                notifyAC();
                        }
@@ -2382,7 +2401,7 @@ public class XACMLPapServlet extends HttpServlet implements StdItemSetChangeList
 
        private class NotifyACThread implements Runnable {
                public void run() {
-                       List<String> disconnectedACs = new ArrayList<String>();
+                       List<String> disconnectedACs = new ArrayList<>();
                        // There should be no Concurrent exception here because the list is a CopyOnWriteArrayList.
                        // The "for each" loop uses the collection's iterator under the covers, so it should be correct.
                        for (String acURL : adminConsoleURLStringList) {
@@ -2426,7 +2445,8 @@ public class XACMLPapServlet extends HttpServlet implements StdItemSetChangeList
                                        disconnectedACs.add(acURL);
                                } finally {
                                        // cleanup the connection
-                                       connection.disconnect();
+                                       if(connection != null)
+                                               connection.disconnect();
                                }
                        }
                        // remove any ACs that are no longer connected
@@ -2590,4 +2610,51 @@ public class XACMLPapServlet extends HttpServlet implements StdItemSetChangeList
        public static PolicyDBDaoTransaction getDbDaoTransaction(){
                return policyDBDao.getNewTransaction();
        }
+       public static String getPapDbDriver() {
+               return papDbDriver;
+       }
+
+       public static void setPapDbDriver(String papDbDriver) {
+               XACMLPapServlet.papDbDriver = papDbDriver;
+       }
+
+       public static String getPapDbUrl() {
+               return papDbUrl;
+       }
+
+       public static void setPapDbUrl(String papDbUrl) {
+               XACMLPapServlet.papDbUrl = papDbUrl;
+       }
+
+       public static String getPapDbUser() {
+               return papDbUser;
+       }
+
+       public static void setPapDbUser(String papDbUser) {
+               XACMLPapServlet.papDbUser = papDbUser;
+       }
+
+       public static String getPapDbPassword() {
+               return papDbPassword;
+       }
+
+       public static void setPapDbPassword(String papDbPassword) {
+               XACMLPapServlet.papDbPassword = papDbPassword;
+       }
+
+       public static String getMsEcompName() {
+               return msEcompName;
+       }
+
+       public static void setMsEcompName(String msEcompName) {
+               XACMLPapServlet.msEcompName = msEcompName;
+       }
+
+       public static String getMsPolicyName() {
+               return msPolicyName;
+       }
+
+       public static void setMsPolicyName(String msPolicyName) {
+               XACMLPapServlet.msPolicyName = msPolicyName;
+       }
 }