From c5d97e8a9a6bea71f3be329a2e44bdbe5fe50882 Mon Sep 17 00:00:00 2001 From: Michael Mokry Date: Mon, 5 Feb 2018 09:48:59 -0600 Subject: [PATCH] MS Model Input Validation - Provides validation for MS policy input content body that matches the GUI validations when create/update MS policy from API - Added changes to satisfy review comments and updated copywright headers for modified and new files Change-Id: I02bfa639bffb48520badd0e4fa34eb36418547ae Issue-ID: POLICY-377 Signed-off-by: Michael Mokry --- .../policy/pap/xacml/rest/XACMLPapServlet.java | 61 +++++++------- .../pap/xacml/rest/components/PolicyDBDao.java | 73 +++++++++++------ .../MicroServiceDictionaryController.java | 6 +- .../xacml/rest/elk/client/ElkConnectorImpl.java | 6 +- .../pap/xacml/rest/handler/APIRequestHandler.java | 4 +- .../pap/xacml/rest/handler/DeleteHandler.java | 95 +++++++++++----------- .../pap/xacml/rest/handler/SavePolicyHandler.java | 11 ++- .../org/onap/policy/pdp/rest/XACMLPdpLoader.java | 7 +- .../org/onap/policy/pdp/rest/XACMLPdpServlet.java | 26 ++++-- .../services/CreateUpdatePolicyServiceImpl.java | 16 ++-- .../pdp/rest/api/services/DeletePolicyService.java | 20 +++-- .../pdp/rest/api/services/NotificationService.java | 5 +- .../policy/pdp/rest/api/services/PAPServices.java | 27 +++--- .../rest/notifications/NotificationController.java | 36 ++++---- .../pdp/rest/notifications/NotificationServer.java | 6 +- .../rest/api/test/PolicyEngineServicesTest.java | 18 ++-- ONAP-PDP-REST/xacml.pdp.properties | 4 +- .../onap/policy/rest/util/PolicyValidation.java | 95 ++++++++++++---------- .../rest/util/PolicyValidationRequestWrapper.java | 32 ++++---- .../policy/rest/util/PolicyValidationTest.java | 80 ++++++++++++++++++ .../onap/policy/controller/AutoPushController.java | 20 +++-- .../install/servers/pdp/bin/xacml.pdp.properties | 4 +- 22 files changed, 416 insertions(+), 236 deletions(-) create mode 100644 ONAP-REST/src/test/java/org/onap/policy/rest/util/PolicyValidationTest.java diff --git a/ONAP-PAP-REST/src/main/java/org/onap/policy/pap/xacml/rest/XACMLPapServlet.java b/ONAP-PAP-REST/src/main/java/org/onap/policy/pap/xacml/rest/XACMLPapServlet.java index d6c298731..b43254e51 100644 --- a/ONAP-PAP-REST/src/main/java/org/onap/policy/pap/xacml/rest/XACMLPapServlet.java +++ b/ONAP-PAP-REST/src/main/java/org/onap/policy/pap/xacml/rest/XACMLPapServlet.java @@ -2,7 +2,7 @@ * ============LICENSE_START======================================================= * ONAP-PAP-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. @@ -1016,6 +1016,7 @@ public class XACMLPapServlet extends HttpServlet implements StdItemSetChangeList //This would occur if a PolicyDBDao notification was received String policyDBDaoRequestUrl = request.getParameter("policydbdaourl"); if(policyDBDaoRequestUrl != null){ + LOGGER.info("XACMLPapServlet: PolicyDBDao Notification received." ); String policyDBDaoRequestEntityId = request.getParameter("entityid"); String policyDBDaoRequestEntityType = request.getParameter("entitytype"); String policyDBDaoRequestExtraData = request.getParameter("extradata"); @@ -1026,7 +1027,8 @@ public class XACMLPapServlet extends HttpServlet implements StdItemSetChangeList im.endTransaction(); return; } - loggingContext.metricStarted(); + loggingContext.metricStarted(); + LOGGER.info("XACMLPapServlet: Calling PolicyDBDao to handlIncomingHttpNotification"); policyDBDao.handleIncomingHttpNotification(policyDBDaoRequestUrl,policyDBDaoRequestEntityId,policyDBDaoRequestEntityType,policyDBDaoRequestExtraData,this); loggingContext.metricEnded(); PolicyLogger.metrics("XACMLPapServlet doPut handle incoming http notification"); @@ -1574,8 +1576,10 @@ public class XACMLPapServlet extends HttpServlet implements StdItemSetChangeList if(apiflag!=null){ loggingContext.setServiceName("PolicyEngineAPI:PAP.postPolicy"); + LOGGER.info("PushPolicy Request From The API"); } else { loggingContext.setServiceName("AC:PAP.postPolicy"); + LOGGER.info("PushPolicy Request From The AC"); } String policyId = request.getParameter("policyId"); @@ -1607,27 +1611,28 @@ public class XACMLPapServlet extends HttpServlet implements StdItemSetChangeList return; } - // Get new transaction to perform updateGroup() - PolicyDBDaoTransaction acPutTransaction = policyDBDao.getNewTransaction(); - try { + if(apiflag != null){ /* * If request comes from the API we need to run the PolicyDBDao updateGroup() to notify other paps of the change. * The GUI does this from the POLICY-SDK-APP code. */ - if(apiflag != null){ - - // read the inputStream into a buffer + + // Get new transaction to perform updateGroup() + PolicyDBDaoTransaction acPutTransaction = policyDBDao.getNewTransaction(); + try { + // get the request content into a String and read the inputStream into a buffer java.util.Scanner scanner = new java.util.Scanner(request.getInputStream()); scanner.useDelimiter("\\A"); String json = scanner.hasNext() ? scanner.next() : ""; scanner.close(); - LOGGER.info("PushPolicy API request: " + json); // convert Object sent as JSON into local object ObjectMapper mapper = new ObjectMapper(); Object objectFromJSON = mapper.readValue(json, StdPDPPolicy.class); StdPDPPolicy policy = (StdPDPPolicy) objectFromJSON; + LOGGER.info("Request JSON Payload: " + json); + // Assume that this is an update of an existing PDP Group loggingContext.setServiceName("PolicyEngineAPI:PAP.updateGroup"); try{ @@ -1667,27 +1672,23 @@ public class XACMLPapServlet extends HttpServlet implements StdItemSetChangeList } //delete temporary policy file from the bin directory - if(policy != null) { - Files.deleteIfExists(Paths.get(policy.getId())); - } - + Files.deleteIfExists(Paths.get(policy.getId())); + + } catch (Exception e) { + acPutTransaction.rollbackTransaction(); + PolicyLogger.error(MessageCodes.ERROR_PROCESS_FLOW, e, "XACMLPapServlet", " API PUT exception"); + loggingContext.transactionEnded(); + PolicyLogger.audit("Transaction Failed - See Error.log"); + String message = XACMLErrorConstants.ERROR_PROCESS_FLOW + "Exception occurred when updating the group from API."; + LOGGER.error(message); + setResponseError(response, HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e.getMessage()); + response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); + response.addHeader("error","addGroupError"); + response.addHeader("message", message); + return; } - } catch (Exception e) { - acPutTransaction.rollbackTransaction(); - PolicyLogger.error(MessageCodes.ERROR_PROCESS_FLOW, e, "XACMLPapServlet", " API PUT exception"); - loggingContext.transactionEnded(); - PolicyLogger.audit("Transaction Failed - See Error.log"); - String message = XACMLErrorConstants.ERROR_PROCESS_FLOW + "Exception occurred when updating the group from API."; - LOGGER.error(message); - setResponseError(response, HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e.getMessage()); - response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); - response.addHeader("error","addGroupError"); - response.addHeader("message", message); - return; } - - // policy file copied ok and the Group was updated on the PDP response.setStatus(HttpServletResponse.SC_NO_CONTENT); response.addHeader("operation", "push"); @@ -2142,7 +2143,11 @@ public class XACMLPapServlet extends HttpServlet implements StdItemSetChangeList ((StdPDPGroup)objectFromJSON).setDirectory(((StdPDPGroup)group).getDirectory()); } try{ - acPutTransaction.updateGroup((StdPDPGroup)objectFromJSON, "XACMLPapServlet.doACPut"); + if("delete".equals(((StdPDPGroup)objectFromJSON).getOperation())){ + acPutTransaction.updateGroup((StdPDPGroup)objectFromJSON, "XACMLPapServlet.doDelete"); + } else { + acPutTransaction.updateGroup((StdPDPGroup)objectFromJSON, "XACMLPapServlet.doACPut"); + } } catch(Exception e){ PolicyLogger.error(MessageCodes.ERROR_PROCESS_FLOW + " Error while updating group in the database: " +"group="+group.getId()); diff --git a/ONAP-PAP-REST/src/main/java/org/onap/policy/pap/xacml/rest/components/PolicyDBDao.java b/ONAP-PAP-REST/src/main/java/org/onap/policy/pap/xacml/rest/components/PolicyDBDao.java index b2fb3e5a8..885e5e888 100644 --- a/ONAP-PAP-REST/src/main/java/org/onap/policy/pap/xacml/rest/components/PolicyDBDao.java +++ b/ONAP-PAP-REST/src/main/java/org/onap/policy/pap/xacml/rest/components/PolicyDBDao.java @@ -2,7 +2,7 @@ * ============LICENSE_START======================================================= * ONAP-PAP-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. @@ -71,6 +71,7 @@ import javax.xml.xpath.XPathFactory; import org.apache.commons.io.FilenameUtils; import org.apache.commons.io.IOUtils; +import org.elasticsearch.common.Strings; import org.onap.policy.common.logging.eelf.MessageCodes; import org.onap.policy.common.logging.eelf.PolicyLogger; import org.onap.policy.common.logging.flexlogger.FlexLogger; @@ -537,7 +538,8 @@ public class PolicyDBDao { // // Open up the connection // - logger.debug("Connecting with url: "+url); + logger.info("PolicyDBDao: NotifyOtherThread: notifying other PAPs of an update"); + logger.info("Connecting with url: "+url); try { connection = (HttpURLConnection)url.openConnection(); } catch (Exception e) { @@ -688,7 +690,7 @@ public class PolicyDBDao { case GROUP_NOTIFICATION: for(int i=0; i newPolicySet = new HashSet<>(group.getPolicies().size()); //a multiple of n runtime is faster than n^2, so I am using a hashset to do the comparison for(PDPPolicy pol: group.getPolicies()){ @@ -2424,19 +2425,45 @@ public class PolicyDBDao { //should be fast since getPolicies uses a HashSet in StdPDPGroup if(!newPolicySet.contains(pol.getId())){ String[] scopeAndName = getNameScopeAndVersionFromPdpPolicy(pol.getId()); - PolicyEntity policyToDelete; + PolicyEntity policyToDelete = null; try{ - policyToDelete = getPolicy(scopeAndName[0],scopeAndName[1]); + if(scopeAndName!=null){ + policyToDelete = getPolicy(scopeAndName[0],scopeAndName[1]); + + if ("XACMLPapServlet.doDelete".equals(username)) { + + Iterator dbPolicyIt = groupToUpdateInDB.getPolicies().iterator(); + String policyName = getPolicyNameAndVersionFromPolicyFileName(policyToDelete.getPolicyName())[0]; + + logger.info("PolicyDBDao: delete policy from GroupEntity"); + try{ + while(dbPolicyIt.hasNext()){ + PolicyEntity dbpolicy = dbPolicyIt.next(); + if(policyToDelete.getScope().equals(dbpolicy.getScope()) && + getPolicyNameAndVersionFromPolicyFileName(dbpolicy.getPolicyName())[0].equals(policyName)) { + dbPolicyIt.remove(); + + logger.info("PolicyDBDao: deleting policy from the existing group:\n " + + "policyName is " + policyToDelete.getScope()+"."+policyToDelete.getPolicyName() + "\n" + + "group is " + groupToUpdateInDB.getGroupId()); + } + } + }catch(Exception e){ + logger.debug(e); + PolicyLogger.error("Could not delete policy with name: "+ policyToDelete.getScope()+"."+policyToDelete.getPolicyName()+"\n ID: "+ policyToDelete.getPolicyId()); + } + } + } + }catch(Exception e){ PolicyLogger.error(MessageCodes.EXCEPTION_ERROR, e, "PolicyDBDao", "Could not get policy to remove: "+pol.getId()); throw new PersistenceException("Could not get policy to remove: "+pol.getId()); } - groupToUpdate.getPolicies().remove(policyToDelete); - } } } - if(group.getName() != null && !stringEquals(group.getName(),groupToUpdate.getgroupName())){ + + if(group.getName() != null && !stringEquals(group.getName(),groupToUpdateInDB.getgroupName())){ //we need to check if the new id exists in the database String newGroupId = createNewPDPGroupId(group.getName()); Query checkGroupQuery = em.createQuery("SELECT g FROM GroupEntity g WHERE g.groupId=:groupId AND g.deleted=:deleted"); @@ -2453,13 +2480,13 @@ public class PolicyDBDao { PolicyLogger.error("The new group name already exists, group id "+newGroupId); throw new PersistenceException("The new group name already exists, group id "+newGroupId); } - groupToUpdate.setGroupId(newGroupId); - groupToUpdate.setGroupName(group.getName()); + groupToUpdateInDB.setGroupId(newGroupId); + groupToUpdateInDB.setGroupName(group.getName()); this.newGroupId = group.getId(); } em.flush(); - this.groupId = groupToUpdate.getGroupKey(); + this.groupId = groupToUpdateInDB.getGroupKey(); } } diff --git a/ONAP-PAP-REST/src/main/java/org/onap/policy/pap/xacml/rest/controller/MicroServiceDictionaryController.java b/ONAP-PAP-REST/src/main/java/org/onap/policy/pap/xacml/rest/controller/MicroServiceDictionaryController.java index 2eca73ea5..ff62080e2 100644 --- a/ONAP-PAP-REST/src/main/java/org/onap/policy/pap/xacml/rest/controller/MicroServiceDictionaryController.java +++ b/ONAP-PAP-REST/src/main/java/org/onap/policy/pap/xacml/rest/controller/MicroServiceDictionaryController.java @@ -2,7 +2,7 @@ * ============LICENSE_START======================================================= * ONAP-PAP-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. @@ -856,11 +856,11 @@ public class MicroServiceDictionaryController { this.newModel.setSub_attributes(value); String attributes= mainClass.getAttribute().toString().replace("{", "").replace("}", ""); int equalsIndexForAttributes= attributes.indexOf("="); - String atttributesAfterFirstEquals= attributes.substring(equalsIndexForAttributes+1); + String atttributesAfterFirstEquals= attributes.substring(equalsIndexForAttributes+1, attributes.length()-1); this.newModel.setAttributes(atttributesAfterFirstEquals); String refAttributes= mainClass.getRefAttribute().toString().replace("{", "").replace("}", ""); int equalsIndex= refAttributes.indexOf("="); - String refAttributesAfterFirstEquals= refAttributes.substring(equalsIndex+1); + String refAttributesAfterFirstEquals= refAttributes.substring(equalsIndex+1, refAttributes.length()-1); this.newModel.setRef_attributes(refAttributesAfterFirstEquals); this.newModel.setEnumValues(mainClass.getEnumType().toString().replace("{", "").replace("}", "")); this.newModel.setAnnotation(mainClass.getMatchingSet().toString().replace("{", "").replace("}", "")); diff --git a/ONAP-PAP-REST/src/main/java/org/onap/policy/pap/xacml/rest/elk/client/ElkConnectorImpl.java b/ONAP-PAP-REST/src/main/java/org/onap/policy/pap/xacml/rest/elk/client/ElkConnectorImpl.java index 9091d79ed..59bf50071 100644 --- a/ONAP-PAP-REST/src/main/java/org/onap/policy/pap/xacml/rest/elk/client/ElkConnectorImpl.java +++ b/ONAP-PAP-REST/src/main/java/org/onap/policy/pap/xacml/rest/elk/client/ElkConnectorImpl.java @@ -2,7 +2,7 @@ * ============LICENSE_START======================================================= * ONAP-PAP-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. @@ -349,13 +349,13 @@ public class ElkConnectorImpl implements ElkConnector{ if (result.isSucceeded()) { if (LOGGER.isInfoEnabled()) - LOGGER.info("OK: PUT operation of " + "->" + ": " + + LOGGER.info("ElkConnector: OK: PUT operation of " + "->" + ": " + "success=" + result.isSucceeded() + "[" + result.getResponseCode() + ":" + result.getPathToResult() + "]" + System.lineSeparator() + result.getJsonString()); } else { if (LOGGER.isWarnEnabled()) - LOGGER.warn("FAILURE: PUT operation of "+ "->" + ": " + + LOGGER.warn("ElkConnector: FAILURE: PUT operation of "+ "->" + ": " + "success=" + result.isSucceeded() + "[" + result.getResponseCode() + ":" + result.getPathToResult() + "]" + System.lineSeparator() + result.getJsonString()); diff --git a/ONAP-PAP-REST/src/main/java/org/onap/policy/pap/xacml/rest/handler/APIRequestHandler.java b/ONAP-PAP-REST/src/main/java/org/onap/policy/pap/xacml/rest/handler/APIRequestHandler.java index 3b254b005..a031ac647 100644 --- a/ONAP-PAP-REST/src/main/java/org/onap/policy/pap/xacml/rest/handler/APIRequestHandler.java +++ b/ONAP-PAP-REST/src/main/java/org/onap/policy/pap/xacml/rest/handler/APIRequestHandler.java @@ -2,7 +2,7 @@ * ============LICENSE_START======================================================= * ONAP-PAP-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. @@ -78,7 +78,7 @@ public class APIRequestHandler { public void doDelete(HttpServletRequest request, HttpServletResponse response, ONAPLoggingContext loggingContext, String apiflag) throws IOException, SQLException{ DeleteHandler deleteHandler = DeleteHandler.getInstance(); if ("deletePapApi".equalsIgnoreCase(apiflag)) { - deleteHandler.doAPIDeleteFromPAP(request, response, loggingContext); + deleteHandler.doAPIDeleteFromPAP(request, response); return; } else if ("deletePdpApi".equalsIgnoreCase(apiflag)) { deleteHandler.doAPIDeleteFromPDP(request, response, loggingContext); diff --git a/ONAP-PAP-REST/src/main/java/org/onap/policy/pap/xacml/rest/handler/DeleteHandler.java b/ONAP-PAP-REST/src/main/java/org/onap/policy/pap/xacml/rest/handler/DeleteHandler.java index 7f612a57e..85b6e24a1 100644 --- a/ONAP-PAP-REST/src/main/java/org/onap/policy/pap/xacml/rest/handler/DeleteHandler.java +++ b/ONAP-PAP-REST/src/main/java/org/onap/policy/pap/xacml/rest/handler/DeleteHandler.java @@ -2,7 +2,7 @@ * ============LICENSE_START======================================================= * ONAP-PAP-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. @@ -61,15 +61,19 @@ public class DeleteHandler { private OnapPDPGroup newgroup; private static Logger logger = FlexLogger.getLogger(DeleteHandler.class); + public static final String POLICY_IN_PDP = "PolicyInPDP"; + public static final String ERROR = "error"; + public static final String UNKNOWN = "unknown"; - public void doAPIDeleteFromPAP(HttpServletRequest request, HttpServletResponse response, ONAPLoggingContext loggingContext) throws IOException, SQLException { + + public void doAPIDeleteFromPAP(HttpServletRequest request, HttpServletResponse response) throws IOException, SQLException { // get the request content into a String String json = null; java.util.Scanner scanner = new java.util.Scanner(request.getInputStream()); scanner.useDelimiter("\\A"); json = scanner.hasNext() ? scanner.next() : ""; scanner.close(); - PolicyLogger.info("JSON request from API: " + json); + PolicyLogger.info("JSON request from API to Delete Policy from the PAP: " + json); // convert Object sent as JSON into local object StdPAPPolicy policy = PolicyUtils.jsonStringToObject(json, StdPAPPolicy.class); String policyName = policy.getPolicyName(); @@ -79,7 +83,7 @@ public class DeleteHandler { String removeVersionExtension; String splitPolicyName = null; String[] split = null; - String status = "error"; + String status = ERROR; PolicyEntity policyEntity = null; JPAUtils jpaUtils = null; @@ -93,7 +97,7 @@ public class DeleteHandler { jpaUtils = JPAUtils.getJPAUtilsInstance(XACMLPapServlet.getEmf()); } catch (Exception e) { PolicyLogger.error(MessageCodes.EXCEPTION_ERROR, e, "XACMLPapServlet", " Could not create JPAUtils instance on the PAP"); - response.addHeader("error", "jpautils"); + response.addHeader(ERROR, "jpautils"); response.addHeader("operation", "delete"); response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); return; @@ -110,8 +114,8 @@ public class DeleteHandler { try{ if(policyName.endsWith(".xml")){ removeXMLExtension = policyName.replace(".xml", ""); - currentVersion = Integer.parseInt(removeXMLExtension.substring(removeXMLExtension.lastIndexOf(".")+1)); - removeVersionExtension = removeXMLExtension.substring(0, removeXMLExtension.lastIndexOf(".")); + currentVersion = Integer.parseInt(removeXMLExtension.substring(removeXMLExtension.lastIndexOf('.')+1)); + removeVersionExtension = removeXMLExtension.substring(0, removeXMLExtension.lastIndexOf('.')); boolean queryCheck = true; if(policy.getDeleteCondition().equalsIgnoreCase("All Versions")){ if(policyName.contains("Config_")){ @@ -125,7 +129,7 @@ public class DeleteHandler { split = splitPolicyName.split(":"); }else{ PolicyLogger.error(MessageCodes.ERROR_UNKNOWN + "Failed to delete the policy. Please, provide the valid policyname."); - response.addHeader("error", "unknown"); + response.addHeader(ERROR, UNKNOWN); response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR ); return; } @@ -162,23 +166,23 @@ public class DeleteHandler { con = DriverManager.getConnection(papDbUrl,papDbUser,papDbPassword); if(policy.getDeleteCondition().equalsIgnoreCase("All Versions")){ - boolean groupCheck = checkPolicyGroupEntity(em, con, peResult); + boolean groupCheck = checkPolicyGroupEntity(con, peResult); if(!groupCheck){ for(Object peData : peResult){ policyEntity = (PolicyEntity) peData; status = deletePolicyEntityData(em, policyEntity); } }else{ - status = "PolicyInPDP"; + status = POLICY_IN_PDP; } - if(status.equals("error")){ + if(status.equals(ERROR)){ PolicyLogger.error(MessageCodes.ERROR_DATA_ISSUE + "Exception Occured while deleting the Entity from Database."); - response.addHeader("error", "unknown"); + response.addHeader(ERROR, UNKNOWN); response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR ); return; - }else if(status.equals("PolicyInPDP")){ + }else if(status.equals(POLICY_IN_PDP)){ PolicyLogger.error(MessageCodes.GENERAL_WARNING + "Policy can't be deleted, it is active in PDP Groups."); - response.addHeader("error", "PolicyInPDP"); + response.addHeader(ERROR, POLICY_IN_PDP); response.setStatus(HttpServletResponse.SC_CONFLICT); return; }else{ @@ -191,22 +195,22 @@ public class DeleteHandler { } } }else if(policy.getDeleteCondition().equalsIgnoreCase("Current Version")){ - boolean groupCheck = checkPolicyGroupEntity(em, con, peResult); + boolean groupCheck = checkPolicyGroupEntity(con, peResult); if(!groupCheck){ policyEntity = (PolicyEntity) peResult.get(0); status = deletePolicyEntityData(em, policyEntity); }else{ - status = "PolicyInPDP"; + status = POLICY_IN_PDP; } - if(status.equals("error")){ + if(ERROR.equals(status)){ PolicyLogger.error(MessageCodes.ERROR_DATA_ISSUE + "Exception Occured while deleting the Entity from Database."); - response.addHeader("error", "unknown"); + response.addHeader(ERROR, UNKNOWN); response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR ); return; - }else if(status.equals("PolicyInPDP")){ + }else if(POLICY_IN_PDP.equals(status)){ PolicyLogger.error(MessageCodes.GENERAL_WARNING + "Policy can't be deleted, it is active in PDP Groups."); - response.addHeader("error", "unknown"); + response.addHeader(ERROR, POLICY_IN_PDP); response.setStatus(HttpServletResponse.SC_CONFLICT); return; }else{ @@ -215,7 +219,7 @@ public class DeleteHandler { for(Object object : peResult){ policyEntity = (PolicyEntity) object; String policyEntityName = policyEntity.getPolicyName().replace(".xml", ""); - int policyEntityVersion = Integer.parseInt(policyEntityName.substring(policyEntityName.lastIndexOf(".")+1)); + int policyEntityVersion = Integer.parseInt(policyEntityName.substring(policyEntityName.lastIndexOf('.')+1)); if(policyEntityVersion > newVersion){ newVersion = policyEntityVersion-1; } @@ -243,7 +247,7 @@ public class DeleteHandler { } }else{ PolicyLogger.error(MessageCodes.ERROR_UNKNOWN + "Failed to delete the policy for an unknown reason. Check the file system and other logs for further information."); - response.addHeader("error", "unknown"); + response.addHeader(ERROR, UNKNOWN); response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR ); return; } @@ -252,7 +256,7 @@ public class DeleteHandler { }catch(Exception e){ em.getTransaction().rollback(); PolicyLogger.error(MessageCodes.EXCEPTION_ERROR, e, "XACMLPapServlet", " ERROR"); - response.addHeader("error", "deleteDB"); + response.addHeader(ERROR, "deleteDB"); response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); return; } finally { @@ -269,7 +273,7 @@ public class DeleteHandler { return; } else { PolicyLogger.error(MessageCodes.ERROR_UNKNOWN + "Failed to delete the policy for an unknown reason. Check the file system and other logs for further information."); - response.addHeader("error", "unknown"); + response.addHeader(ERROR, UNKNOWN); response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR ); return; } @@ -291,37 +295,33 @@ public class DeleteHandler { em.remove(policyEntity); }catch(Exception e){ logger.error(e.getMessage(),e); - return "error"; + return ERROR; } return "success"; } - public static boolean checkPolicyGroupEntity(EntityManager em, Connection con, List peResult) throws SQLException{ + public static boolean checkPolicyGroupEntity(Connection con, List peResult) throws SQLException{ for(Object peData : peResult){ PolicyEntity policyEntity = (PolicyEntity) peData; - Statement st = null; - ResultSet rs = null; - try{ - st = con.createStatement(); - rs = st.executeQuery("Select * from PolicyGroupEntity where policyid = '"+policyEntity.getPolicyId()+"'"); + try(Statement st = con.createStatement(); + ResultSet rs = st.executeQuery("Select * from PolicyGroupEntity where policyid = '"+policyEntity.getPolicyId()+"'")){ boolean gEntityList = rs.next(); - rs.close(); if(gEntityList){ return true; } - }finally{ - if(st != null){ - st.close(); - } } } return false; } public void doAPIDeleteFromPDP(HttpServletRequest request, HttpServletResponse response, ONAPLoggingContext loggingContext) throws IOException { + String policyName = request.getParameter("policyName"); String groupId = request.getParameter("groupId"); String responseString = null; + + PolicyLogger.info("JSON request from API to Delete Policy from the PDP: " + policyName); + // for PUT operations the group may or may not need to exist before the operation can be done OnapPDPGroup group = null; try { @@ -330,12 +330,13 @@ public class DeleteHandler { PolicyLogger.error("Exception occured While PUT operation is performing for PDP Group"+e); } if (group == null) { - String message = "Unknown groupId '" + groupId + "'"; + String message = "Unknown groupId '" + groupId + "'."; PolicyLogger.error(MessageCodes.ERROR_DATA_ISSUE + " " + message); loggingContext.transactionEnded(); PolicyLogger.audit("Transaction Failed - See Error.log"); - response.addHeader("error", "UnknownGroup"); - response.sendError(HttpServletResponse.SC_NOT_FOUND, message); + response.addHeader(ERROR, "UnknownGroup"); + response.addHeader("message", message); + response.setStatus(HttpServletResponse.SC_NOT_FOUND); return; } else { loggingContext.setServiceName("API:PAP.deletPolicyFromPDPGroup"); @@ -346,7 +347,7 @@ public class DeleteHandler { + "policyName must be the full name of the file to be deleted including version and extension"; PolicyLogger.error(MessageCodes.ERROR_DATA_ISSUE + " Invalid policyName... " + "policyName must be the full name of the file to be deleted including version and extension"); - response.addHeader("error", message); + response.addHeader(ERROR, message); response.setStatus(HttpServletResponse.SC_BAD_REQUEST); return; } @@ -359,7 +360,7 @@ public class DeleteHandler { PolicyLogger.debug("Precheck Successful."); } } - + PolicyLogger.info("Preparing to remove policy from group: " + group.getId()); removePolicy.prepareToRemove(policy); OnapPDPGroup updatedGroup = removePolicy.getUpdatedObject(); responseString = deletePolicyFromPDPGroup(updatedGroup, loggingContext); @@ -367,7 +368,7 @@ public class DeleteHandler { String message = XACMLErrorConstants.ERROR_DATA_ISSUE + "Policy does not exist on the PDP."; PolicyLogger.error(message); PolicyLogger.error(MessageCodes.ERROR_DATA_ISSUE + " Policy does not exist on the PDP."); - response.addHeader("error", message); + response.addHeader(ERROR, message); response.setStatus(HttpServletResponse.SC_BAD_REQUEST); return; } @@ -384,17 +385,17 @@ public class DeleteHandler { String message = XACMLErrorConstants.ERROR_DATA_ISSUE + "Group update had bad input."; PolicyLogger.error(MessageCodes.ERROR_DATA_ISSUE + " Group update had bad input."); response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); - response.addHeader("error", "groupUpdate"); + response.addHeader(ERROR, "groupUpdate"); response.addHeader("message", message); return; } else if (responseString.equals("DB Error")) { PolicyLogger.error(MessageCodes.ERROR_PROCESS_FLOW + " Error while updating group in the database"); response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); - response.addHeader("error", "deleteDB"); + response.addHeader(ERROR, "deleteDB"); return; } else { PolicyLogger.error(MessageCodes.ERROR_UNKNOWN + " Failed to delete the policy for an unknown reason. Check the file system and other logs for further information."); - response.addHeader("error", "unknown"); + response.addHeader(ERROR, UNKNOWN); response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR ); return; } @@ -410,7 +411,7 @@ public class DeleteHandler { } catch (PAPException e1) { PolicyLogger.error("Exception occured While Deleting Policy From PDP Group"+e1); } - if (group == null || ! (group instanceof StdPDPGroup) || existingGroup == null || ! (group.getId().equals(existingGroup.getId()))) { + if (!(group instanceof StdPDPGroup) || existingGroup == null || !(group.getId().equals(existingGroup.getId()))) { String existingID = null; if(existingGroup != null){ existingID = existingGroup.getId(); @@ -426,7 +427,7 @@ public class DeleteHandler { // so we need to fill that in before submitting the group for update ((StdPDPGroup)group).setDirectory(((StdPDPGroup)existingGroup).getDirectory()); try{ - acPutTransaction.updateGroup(group, "XACMLPapServlet.doAPIDelete"); + acPutTransaction.updateGroup(group, "XACMLPapServlet.doDelete"); } catch(Exception e){ PolicyLogger.error(MessageCodes.ERROR_PROCESS_FLOW, e, "XACMLPapServlet", " Error while updating group in the database: " +"group="+existingGroup.getId()); diff --git a/ONAP-PAP-REST/src/main/java/org/onap/policy/pap/xacml/rest/handler/SavePolicyHandler.java b/ONAP-PAP-REST/src/main/java/org/onap/policy/pap/xacml/rest/handler/SavePolicyHandler.java index 9be4b0342..6606d383b 100644 --- a/ONAP-PAP-REST/src/main/java/org/onap/policy/pap/xacml/rest/handler/SavePolicyHandler.java +++ b/ONAP-PAP-REST/src/main/java/org/onap/policy/pap/xacml/rest/handler/SavePolicyHandler.java @@ -2,7 +2,7 @@ * ============LICENSE_START======================================================= * ONAP-PAP-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. @@ -63,11 +63,18 @@ public class SavePolicyHandler { scanner.useDelimiter("\\A"); json = scanner.hasNext() ? scanner.next() : ""; scanner.close(); - PolicyLogger.info("JSON request from API: " + json); + + if(policyAdapter.isEditPolicy()){ + PolicyLogger.info("SavePolicyHandler: JSON request from API to update a policy: " + json); + } else { + PolicyLogger.info("SavePolicyHandler: JSON request from API to create a policy: " + json); + } + // convert Object sent as JSON into local object StdPAPPolicy policy = PolicyUtils.jsonStringToObject(json, StdPAPPolicy.class); //Set policyAdapter values including parentPath (Common to all policy types) try { + PolicyLogger.info("SavePolicyHandler: Setting parameter values to PolicyAdapter"); policyAdapter = setDataToPolicyAdapter(policy, policyType, apiflag); if(!extendedPolicyOptions(policyAdapter, response)){ 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..5c9f8602f 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,7 +2,7 @@ * ============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. @@ -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); @@ -215,12 +217,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); } diff --git a/ONAP-PDP-REST/src/main/java/org/onap/policy/pdp/rest/XACMLPdpServlet.java b/ONAP-PDP-REST/src/main/java/org/onap/policy/pdp/rest/XACMLPdpServlet.java index 04572ccbc..608295325 100644 --- a/ONAP-PDP-REST/src/main/java/org/onap/policy/pdp/rest/XACMLPdpServlet.java +++ b/ONAP-PDP-REST/src/main/java/org/onap/policy/pdp/rest/XACMLPdpServlet.java @@ -2,7 +2,7 @@ * ============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. @@ -208,8 +208,8 @@ public class XACMLPdpServlet extends HttpServlet implements Runnable { XACMLRest.xacmlInit(config); // Load the Notification Delay. setNotificationDelay(); - // Load Queue size. - int queueSize = 5; // Set default Queue Size here. + // Load Queue size. Not sure if we really need to have the queue bounded, we should look further into this + int queueSize = 50; // Set default Queue Size here. queueSize = Integer.parseInt(XACMLProperties.getProperty("REQUEST_BUFFER_SIZE",String.valueOf(queueSize))); initQueue(queueSize); // Load our engine - this will use the latest configuration @@ -477,6 +477,7 @@ public class XACMLPdpServlet extends HttpServlet implements Runnable { im.endTransaction(); return; } + logger.info("XACMLPdpServlet: calling doPutConfig to add properties to the queue"); this.doPutConfig(cache, request, response, loggingContext); loggingContext.transactionEnded(); PolicyLogger.audit("Transaction ended"); @@ -504,6 +505,7 @@ public class XACMLPdpServlet extends HttpServlet implements Runnable { protected void doPutConfig(String config, HttpServletRequest request, HttpServletResponse response, ONAPLoggingContext loggingContext) throws ServletException, IOException { try { // prevent multiple configuration changes from stacking up + logger.info("XACMLPdpServlet: checking remainingCapacity of Queue."); if (XACMLPdpServlet.queue.remainingCapacity() <= 0) { logger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Queue capacity reached"); PolicyLogger.error(MessageCodes.ERROR_PROCESS_FLOW, "Queue capacity reached"); @@ -542,6 +544,7 @@ public class XACMLPdpServlet extends HttpServlet implements Runnable { response.sendError(HttpServletResponse.SC_BAD_REQUEST, "PUT with cache=policies must contain at least one policy property"); return; } + logger.info("XACMLPdpServlet: offer policies to queue. No pip properties added."); XACMLPdpServlet.queue.offer(new PutRequest(newProperties, null)); loggingContext.transactionEnded(); auditLogger.info("Success"); @@ -556,6 +559,7 @@ public class XACMLPdpServlet extends HttpServlet implements Runnable { response.sendError(HttpServletResponse.SC_BAD_REQUEST, "PUT with cache=pips must contain at least one pip property"); return; } + logger.info("XACMLPdpServlet: offer pips to queue. No policy properties added."); XACMLPdpServlet.queue.offer(new PutRequest(null, newProperties)); loggingContext.transactionEnded(); auditLogger.info("Success"); @@ -579,10 +583,12 @@ public class XACMLPdpServlet extends HttpServlet implements Runnable { response.sendError(HttpServletResponse.SC_BAD_REQUEST, "PUT with cache=all must contain at least one pip property"); return; } + logger.info("XACMLPdpServlet: offer policies and pips to queue."); XACMLPdpServlet.queue.offer(new PutRequest(newPolicyProperties, newPipProperties)); loggingContext.transactionEnded(); auditLogger.info("Success"); PolicyLogger.audit("Success"); + } else { // // Invalid value @@ -1238,25 +1244,32 @@ public class XACMLPdpServlet extends HttpServlet implements Runnable { try { // variable not used, but constructor has needed side-effects so don't remove: while (! XACMLPdpServlet.configThreadTerminate) { + logger.info("XACMLPdpServlet: Taking requests from the queue"); PutRequest request = XACMLPdpServlet.queue.take(); + logger.info("XACMLPdpServlet: Taking requests from the queue COMPLETED"); StdPDPStatus newStatus = new StdPDPStatus(); PDPEngine newEngine = null; synchronized(pdpStatusLock) { XACMLPdpServlet.status.setStatus(Status.UPDATING_CONFIGURATION); + + logger.info("created new PDPEngine"); newEngine = XACMLPdpLoader.loadEngine(newStatus, request.policyProperties, request.pipConfigProperties); } if (newEngine != null) { + logger.info("XACMLPdpServlet: newEngine created, assigning newEngine to the pdpEngine."); synchronized(XACMLPdpServlet.pdpEngineLock) { XACMLPdpServlet.pdpEngine = newEngine; try { logger.info("Saving configuration."); if (request.policyProperties != null) { + logger.info("Saving configuration: Policy Properties: " + request.policyProperties); try (OutputStream os = Files.newOutputStream(XACMLPdpLoader.getPDPPolicyCache())) { request.policyProperties.store(os, ""); } } if (request.pipConfigProperties != null) { + logger.info("Saving configuration: PIP Properties: " + request.pipConfigProperties); try (OutputStream os = Files.newOutputStream(XACMLPdpLoader.getPIPConfig())) { request.pipConfigProperties.store(os, ""); } @@ -1269,14 +1282,17 @@ public class XACMLPdpServlet extends HttpServlet implements Runnable { newStatus.addLoadWarning("Unable to save configuration: " + e.getMessage()); } } - // Notification will be Sent Here. - XACMLPdpLoader.sendNotification(); } else { newStatus.setStatus(Status.LAST_UPDATE_FAILED); } synchronized(pdpStatusLock) { XACMLPdpServlet.status.set(newStatus); } + logger.info("New PDP Servlet Status: " + newStatus.getStatus()); + if (Status.UP_TO_DATE.equals(newStatus.getStatus())) { + // Notification will be Sent Here. + XACMLPdpLoader.sendNotification(); + } } } catch (InterruptedException e) { logger.error(XACMLErrorConstants.ERROR_SYSTEM_ERROR + "interrupted"+e); diff --git a/ONAP-PDP-REST/src/main/java/org/onap/policy/pdp/rest/api/services/CreateUpdatePolicyServiceImpl.java b/ONAP-PDP-REST/src/main/java/org/onap/policy/pdp/rest/api/services/CreateUpdatePolicyServiceImpl.java index f91f6e2f2..62b850370 100644 --- a/ONAP-PDP-REST/src/main/java/org/onap/policy/pdp/rest/api/services/CreateUpdatePolicyServiceImpl.java +++ b/ONAP-PDP-REST/src/main/java/org/onap/policy/pdp/rest/api/services/CreateUpdatePolicyServiceImpl.java @@ -2,7 +2,7 @@ * ============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. @@ -177,7 +177,7 @@ public class CreateUpdatePolicyServiceImpl implements CreateUpdatePolicyService response = microServicesPolicyService.getResult(updateFlag); break; default: - String message = XACMLErrorConstants.ERROR_DATA_ISSUE+ " Invalid Config Type Present"; + message = XACMLErrorConstants.ERROR_DATA_ISSUE+ " Invalid Config Type Present"; LOGGER.error(message); status = HttpStatus.BAD_REQUEST; return message; @@ -208,7 +208,7 @@ public class CreateUpdatePolicyServiceImpl implements CreateUpdatePolicyService break; } }else { - String message = XACMLErrorConstants.ERROR_DATA_ISSUE + "No Policy Class found."; + message = XACMLErrorConstants.ERROR_DATA_ISSUE + "No Policy Class found."; LOGGER.error(message); status = HttpStatus.BAD_REQUEST; response = message; @@ -254,7 +254,12 @@ public class CreateUpdatePolicyServiceImpl implements CreateUpdatePolicyService try { PolicyValidationRequestWrapper wrapper = new PolicyValidationRequestWrapper(); PolicyRestAdapter policyData = wrapper.populateRequestParameters(policyParameters); - responseString = validation.validatePolicy(policyData); + if(policyData!=null) { + responseString = validation.validatePolicy(policyData); + } else { + message = XACMLErrorConstants.ERROR_DATA_ISSUE+ " improper JSON object : " + policyParameters.getConfigBody(); + return false; + } } catch (Exception e) { LOGGER.error("Exception Occured during Policy Validation" +e); if(e.getMessage()!=null){ @@ -277,7 +282,8 @@ public class CreateUpdatePolicyServiceImpl implements CreateUpdatePolicyService } if (responseString!=null){ - if("success".equals(responseString.toString())||"success@#".equals(responseString.toString())){ + String response = responseString.toString().substring(0, 7); + if("success".equals(response)) { return true; } else { message = XACMLErrorConstants.ERROR_DATA_ISSUE + PolicyApiUtils.formatResponse(responseString); diff --git a/ONAP-PDP-REST/src/main/java/org/onap/policy/pdp/rest/api/services/DeletePolicyService.java b/ONAP-PDP-REST/src/main/java/org/onap/policy/pdp/rest/api/services/DeletePolicyService.java index 01670a5fd..144d3a48d 100644 --- a/ONAP-PDP-REST/src/main/java/org/onap/policy/pdp/rest/api/services/DeletePolicyService.java +++ b/ONAP-PDP-REST/src/main/java/org/onap/policy/pdp/rest/api/services/DeletePolicyService.java @@ -2,7 +2,7 @@ * ============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. @@ -75,13 +75,15 @@ public class DeletePolicyService { return; } if (deleteResult.contains("BAD REQUEST")||deleteResult.contains("PE300")||deleteResult.contains("PE200")||deleteResult.contains("not exist")||deleteResult.contains("Invalid policyName")) { - status = HttpStatus.BAD_REQUEST; + if(deleteResult.contains("groupId")) { + status = HttpStatus.NOT_FOUND; + } else { + status = HttpStatus.BAD_REQUEST; + } } else if (deleteResult.contains("locked down")){ status = HttpStatus.ACCEPTED; } else if (deleteResult.contains("not Authorized")) { status = HttpStatus.FORBIDDEN; - } else if (deleteResult.contains("groupId")) { - status = HttpStatus.NOT_FOUND; } else if (deleteResult.contains("JPAUtils")||deleteResult.contains("database")||deleteResult.contains("policy file")|| deleteResult.contains("unknown")||deleteResult.contains("configuration")) { status = HttpStatus.INTERNAL_SERVER_ERROR; @@ -148,7 +150,7 @@ public class DeletePolicyService { if ("PAP".equalsIgnoreCase(deletePolicyParameters.getPolicyComponent())) { if (deletePolicyParameters.getDeleteCondition()==null||deletePolicyParameters.getDeleteCondition().toString().trim().isEmpty()){ - String message = XACMLErrorConstants.ERROR_DATA_ISSUE + "No Delete Condition given."; + message = XACMLErrorConstants.ERROR_DATA_ISSUE + "No Delete Condition given."; LOGGER.error(message); return message; } @@ -158,14 +160,14 @@ public class DeletePolicyService { response = (String) papServices.callPAP(deletePapPolicy, new String[] {"groupId="+pdpGroup, "apiflag=deletePapApi", "operation=delete" }, deletePolicyParameters.getRequestID(), clientScope); } else if ("PDP".equalsIgnoreCase(deletePolicyParameters.getPolicyComponent())) { if (deletePolicyParameters.getPdpGroup()==null||deletePolicyParameters.getPdpGroup().trim().isEmpty()){ - String message = XACMLErrorConstants.ERROR_DATA_ISSUE + "No PDP Group given."; + message = XACMLErrorConstants.ERROR_DATA_ISSUE + "No PDP Group given."; LOGGER.error(message); return message; } //send JSON object to PAP response = (String) papServices.callPAP(null, new String[] {"policyName="+fullPolicyName, "groupId="+pdpGroup, "apiflag=deletePdpApi", "operation=delete" }, deletePolicyParameters.getRequestID(), clientScope); } else { - String message = XACMLErrorConstants.ERROR_DATA_ISSUE + "Policy Component does not exist. Please enter either PAP or PDP to delete the policy from a specified Policy Component."; + message = XACMLErrorConstants.ERROR_DATA_ISSUE + "Policy Component does not exist. Please enter either PAP or PDP to delete the policy from a specified Policy Component."; LOGGER.error(message); response = message; } @@ -181,9 +183,9 @@ public class DeletePolicyService { if (!deletePolicyParameters.getPolicyName().contains("xml")) { if (deletePolicyParameters.getPolicyName() != null && deletePolicyParameters.getPolicyName().contains(".")) { - policyName = deletePolicyParameters.getPolicyName().substring(deletePolicyParameters.getPolicyName().lastIndexOf(".") + 1, + policyName = deletePolicyParameters.getPolicyName().substring(deletePolicyParameters.getPolicyName().lastIndexOf('.') + 1, deletePolicyParameters.getPolicyName().length()); - policyScope = deletePolicyParameters.getPolicyName().substring(0,deletePolicyParameters.getPolicyName().lastIndexOf(".")); + policyScope = deletePolicyParameters.getPolicyName().substring(0,deletePolicyParameters.getPolicyName().lastIndexOf('.')); LOGGER.info("Name is " + policyName + " scope is " + policyScope); } else { message = XACMLErrorConstants.ERROR_DATA_ISSUE + "No Policy Scope given."; diff --git a/ONAP-PDP-REST/src/main/java/org/onap/policy/pdp/rest/api/services/NotificationService.java b/ONAP-PDP-REST/src/main/java/org/onap/policy/pdp/rest/api/services/NotificationService.java index bba51965b..3806d26d9 100644 --- a/ONAP-PDP-REST/src/main/java/org/onap/policy/pdp/rest/api/services/NotificationService.java +++ b/ONAP-PDP-REST/src/main/java/org/onap/policy/pdp/rest/api/services/NotificationService.java @@ -2,7 +2,7 @@ * ============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. @@ -304,8 +304,9 @@ public class NotificationService { aafLogin, aafPassword); // Sending notification through DMaaP Message Router + logger.info("NotificationService: send DMaaP Message. "); publisher.send( "MyPartitionKey", notification); - logger.debug("Message Published on DMaaP :" + dmaapList.get(0) + "for Topic: " + topic); + logger.info("Message Published on DMaaP :" + dmaapList.get(0) + "for Topic: " + topic); publisher.close(); } diff --git a/ONAP-PDP-REST/src/main/java/org/onap/policy/pdp/rest/api/services/PAPServices.java b/ONAP-PDP-REST/src/main/java/org/onap/policy/pdp/rest/api/services/PAPServices.java index 72d8fdeec..57a80b5d1 100644 --- a/ONAP-PDP-REST/src/main/java/org/onap/policy/pdp/rest/api/services/PAPServices.java +++ b/ONAP-PDP-REST/src/main/java/org/onap/policy/pdp/rest/api/services/PAPServices.java @@ -2,7 +2,7 @@ * ============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. @@ -111,7 +111,7 @@ public class PAPServices { // This makes it Real-Time to change the list depending on their // availability. if (paps == null || paps.isEmpty()) { - String message = XACMLErrorConstants.ERROR_DATA_ISSUE + "PAPs List is Empty."; + String message = XACMLErrorConstants.ERROR_SYSTEM_ERROR + "PAPs List is Empty."; LOGGER.error(message); throw new PolicyException(message); } @@ -215,7 +215,7 @@ public class PAPServices { } return response; } else { - response = XACMLErrorConstants.ERROR_SYSTEM_ERROR + response = XACMLErrorConstants.ERROR_DATA_ISSUE + "Unable to get valid response from PAP(s) " + paps; return response; } @@ -415,10 +415,10 @@ public class PAPServices { + ". PEP is not Authorized for making this Request!! \n Contact Administrator for this Scope. "; LOGGER.error(response); } else if (connection.getResponseCode() == 404 && connection.getHeaderField("error") != null) { - if ("unknownGroupId".equals(connection.getHeaderField("error"))) { + if ("UnknownGroup".equals(connection.getHeaderField("error"))) { response = XACMLErrorConstants.ERROR_DATA_ISSUE + connection.getHeaderField("message") - + " Please check the pdpGroup you are requesting to move the policy to."; + + " Please check the pdpGroup you are requesting to push the policy to."; LOGGER.error(response); } else if ("policyNotAvailableForEdit".equals(connection.getHeaderField("error"))) { response = XACMLErrorConstants.ERROR_DATA_ISSUE @@ -490,13 +490,16 @@ public class PAPServices { response = XACMLErrorConstants.ERROR_UNKNOWN + "Could not create or update the policy for and unknown reason"; }else{ - response = XACMLErrorConstants.ERROR_DATA_ISSUE - + "BAD REQUEST: Error occured while attempting perform this operation.. the request may be incorrect. " + connection.getHeaderField("error"); + response = XACMLErrorConstants.ERROR_SYSTEM_ERROR + + "Error occured while attempting perform this operation.. " + + "the request may be incorrect or the PAP is unreachable. " + + connection.getHeaderField("error"); } LOGGER.error(response); } else { - response = XACMLErrorConstants.ERROR_DATA_ISSUE - + "BAD REQUEST: Error occured while attempting perform this operation.. the request may be incorrect."; + response = XACMLErrorConstants.ERROR_SYSTEM_ERROR + + "Error occured while attempting perform this operation.. " + + "the request may be incorrect or the PAP is unreachable."; LOGGER.error(response); } return response; @@ -544,11 +547,11 @@ public class PAPServices { + "\"policyName\": \""+policyName+"\"," + "\"clientScope\": \""+clientScope+"\"," + "\"pdpGroup\": \""+pdpGroup+"\"}"; - //String response = null; - HttpURLConnection connection = null; + + HttpURLConnection connection = null; responseCode = 0; if (paps == null || paps.isEmpty()) { - String message = XACMLErrorConstants.ERROR_DATA_ISSUE + "PAPs List is Empty."; + String message = XACMLErrorConstants.ERROR_SYSTEM_ERROR + "PAPs List is Empty."; LOGGER.error(message); throw new PolicyException(message); } diff --git a/ONAP-PDP-REST/src/main/java/org/onap/policy/pdp/rest/notifications/NotificationController.java b/ONAP-PDP-REST/src/main/java/org/onap/policy/pdp/rest/notifications/NotificationController.java index 8121bdc8f..c62872fbd 100644 --- a/ONAP-PDP-REST/src/main/java/org/onap/policy/pdp/rest/notifications/NotificationController.java +++ b/ONAP-PDP-REST/src/main/java/org/onap/policy/pdp/rest/notifications/NotificationController.java @@ -2,7 +2,7 @@ * ============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. @@ -81,8 +81,10 @@ public class NotificationController { private static Boolean notificationFlag = false; public void check(PDPStatus newStatus, Map policyContainer) { - boolean updated = false; - boolean removed = false; + + LOGGER.info("NotificationController: checking for updated and removed policies."); + boolean isUpdated = false; + boolean isRemoved = false; Notification notification = new Notification(); HashSet removedPolicies = new HashSet<>(); HashSet updatedPolicies = new HashSet<>(); @@ -90,16 +92,18 @@ public class NotificationController { if (oldStatus == null) { oldStatus = newStatus; } - // Debugging purpose only. - LOGGER.debug("old config Status :" + oldStatus.getStatus()); - LOGGER.debug("new config Status :" + newStatus.getStatus()); + + LOGGER.info("old config Status :" + oldStatus.getStatus()); + LOGGER.info("new config Status :" + newStatus.getStatus()); // Depending on the above condition taking the Change as an Update. if (oldStatus.getStatus().toString() != newStatus.getStatus().toString()) { LOGGER.info("There is an Update to the PDP"); LOGGER.debug(oldStatus.getLoadedPolicies()); LOGGER.debug(newStatus.getLoadedPolicies()); + // Check if there is an Update/additions in the policy. + LOGGER.info("NotificationController: check for updated or new policies"); for (PDPPolicy newPolicy : newStatus.getLoadedPolicies()) { boolean change = true; for (PDPPolicy oldPolicy : oldStatus.getLoadedPolicies()) { @@ -113,12 +117,14 @@ public class NotificationController { } // if there is a change Send the notifications to the Client. if (change) { + LOGGER.info("NotificationController: sending update/new policy notification"); sendUpdate(newPolicy, policyContainer); - updated = true; + isUpdated = true; updatedPolicies.add(this.updated); } } // Check if there is any removal of policy. + LOGGER.info("NotificationController: check for removed policies"); for (PDPPolicy oldPolicy : oldStatus.getLoadedPolicies()) { boolean change = true; for (PDPPolicy newPolicy : newStatus.getLoadedPolicies()) { @@ -132,8 +138,9 @@ public class NotificationController { } // if there is a change Send the notifications to the Client. if (change) { + LOGGER.info("NotificationController: sending removal notification"); sendremove(oldPolicy); - removed = true; + isRemoved = true; removedPolicies.add(this.removed); } } @@ -141,15 +148,15 @@ public class NotificationController { // At the end the oldStatus must be updated with the newStatus. oldStatus = newStatus; // Sending Notification to the Server to pass over to the clients - if (updated || removed) { + if (isUpdated || isRemoved) { // Call the Notification Server.. notification.setRemovedPolicies(removedPolicies); notification.setLoadedPolicies(updatedPolicies); - notification = setUpdateTypes(updated, removed, notification); + notification = setUpdateTypes(isUpdated, isRemoved, notification); ObjectWriter om = new ObjectMapper().writer(); try { setNotificationJSON(om.writeValueAsString(notification)); - LOGGER.info(notificationJSON); + LOGGER.info("NotificationController notificationJson: " + notificationJSON); // NotificationServer Method here. setPropNotification(); if (("ueb".equals(propNotificationType) || "dmaap".equals(propNotificationType)) @@ -191,6 +198,7 @@ public class NotificationController { public static void sendNotification() { if (notificationFlag) { try { + LOGGER.info("NotificationController: calling NotificationServer to sendNotification"); NotificationServer.sendNotification(notificationJSON, propNotificationType, pdpURL); } catch (Exception e) { LOGGER.info(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error in sending the Event Notification: " @@ -255,7 +263,7 @@ public class NotificationController { } else if (newPolicy.getName().contains(".Action_")) { // Take Configuration copy to PDP Webapps. // Action policies have .json as extension. - String urlString = "$URL/Action/" + newPolicy.getId().substring(0, newPolicy.getId().lastIndexOf(".")) + String urlString = "$URL/Action/" + newPolicy.getId().substring(0, newPolicy.getId().lastIndexOf('.')) + ".json"; callPap(urlString, "Action"); } @@ -390,7 +398,7 @@ public class NotificationController { } if (delete) { FileFilter fileFilter = new WildcardFileFilter( - oldPolicy.getId().substring(0, oldPolicy.getId().lastIndexOf(".")) + ".*"); + oldPolicy.getId().substring(0, oldPolicy.getId().lastIndexOf('.')) + ".*"); File[] configFile = dir.listFiles(fileFilter); if (configFile.length == 1) { Files.deleteIfExists(configFile[0].toPath()); @@ -418,7 +426,7 @@ public class NotificationController { String papPath = papUrls.getUrl(); papPath = papPath.substring(0, papPath.lastIndexOf("/pap")); String papAddress = urlString.replace("$URL", papPath); - String fileName = papAddress.substring(papAddress.lastIndexOf("/") + 1); + String fileName = papAddress.substring(papAddress.lastIndexOf('/') + 1); String fileLocation = configLocation.toString() + File.separator + fileName; try { URL papURL = new URL(papAddress); diff --git a/ONAP-PDP-REST/src/main/java/org/onap/policy/pdp/rest/notifications/NotificationServer.java b/ONAP-PDP-REST/src/main/java/org/onap/policy/pdp/rest/notifications/NotificationServer.java index 2f3d58203..4c2d1ef57 100644 --- a/ONAP-PDP-REST/src/main/java/org/onap/policy/pdp/rest/notifications/NotificationServer.java +++ b/ONAP-PDP-REST/src/main/java/org/onap/policy/pdp/rest/notifications/NotificationServer.java @@ -2,7 +2,7 @@ * ============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. @@ -208,11 +208,15 @@ public class NotificationServer { for(Session session: queue) { try { + LOGGER.info("\n Sending Notification: " + notification + " for client session id: " + session.getId() + "\n " + + "PDPUrl is " + pdpURL); + LOGGER.info("NotificationServer: sending text message"); session.getBasicRemote().sendText(notification); } catch (IOException e) { LOGGER.info(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error in sending the Event Notification: "+ e.getMessage() + e); } } + NotificationService.sendNotification(notification); } diff --git a/ONAP-PDP-REST/src/test/java/org/onap/policy/pdp/rest/api/test/PolicyEngineServicesTest.java b/ONAP-PDP-REST/src/test/java/org/onap/policy/pdp/rest/api/test/PolicyEngineServicesTest.java index 8fea627c9..471f1a9a2 100644 --- a/ONAP-PDP-REST/src/test/java/org/onap/policy/pdp/rest/api/test/PolicyEngineServicesTest.java +++ b/ONAP-PDP-REST/src/test/java/org/onap/policy/pdp/rest/api/test/PolicyEngineServicesTest.java @@ -2,7 +2,7 @@ * ============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. @@ -168,9 +168,9 @@ public class PolicyEngineServicesTest { mockMvc.perform(get("/getMetrics")).andExpect(status().isBadRequest()); mockMvc.perform(get("/getMetrics").header(CLIENTAUTHHEADER, "Basic 123")).andExpect(status().isUnauthorized()); //Service Tests. - mockMvc.perform(get("/getMetrics").headers(headers).header(UUIDHEADER, "123")).andExpect(status().isOk()); - mockMvc.perform(get("/getMetrics").headers(headers).header(UUIDHEADER, UUID.randomUUID())).andExpect(status().isOk()); - mockMvc.perform(get("/getMetrics").headers(headers)).andExpect(status().isOk()); + mockMvc.perform(get("/getMetrics").headers(headers).header(UUIDHEADER, "123")).andExpect(status().isBadRequest()); + mockMvc.perform(get("/getMetrics").headers(headers).header(UUIDHEADER, UUID.randomUUID())).andExpect(status().isBadRequest()); + mockMvc.perform(get("/getMetrics").headers(headers)).andExpect(status().isBadRequest()); } @Test @@ -699,9 +699,9 @@ public class PolicyEngineServicesTest { .headers(headers)).andExpect(status().isBadRequest()); pep.setActionPerformer("PEP"); mockMvc.perform(put("/createPolicy").content(PolicyUtils.objectToJsonString(pep)).contentType(MediaType.APPLICATION_JSON) - .headers(headers)).andExpect(status().isInternalServerError()); + .headers(headers)).andExpect(status().isBadRequest()); mockMvc.perform(put("/updatePolicy").content(PolicyUtils.objectToJsonString(pep)).contentType(MediaType.APPLICATION_JSON) - .headers(headers)).andExpect(status().isInternalServerError()); + .headers(headers)).andExpect(status().isBadRequest()); // Checks for Decision Policy. pep.setPolicyClass(PolicyClass.Decision); mockMvc.perform(put("/createPolicy").content(PolicyUtils.objectToJsonString(pep)).contentType(MediaType.APPLICATION_JSON) @@ -743,7 +743,7 @@ public class PolicyEngineServicesTest { .headers(headers)).andExpect(status().isBadRequest()); pep.setDictionaryJson("{\"test\":\"value\"}"); mockMvc.perform(put("/createDictionaryItem").content(PolicyUtils.objectToJsonString(pep)).contentType(MediaType.APPLICATION_JSON) - .headers(headers).header(UUIDHEADER, UUID.randomUUID())).andExpect(status().isInternalServerError()); + .headers(headers).header(UUIDHEADER, UUID.randomUUID())).andExpect(status().isBadRequest()); pep.setDictionaryJson("test123"); mockMvc.perform(put("/updateDictionaryItem").content(PolicyUtils.objectToJsonString(pep)).contentType(MediaType.APPLICATION_JSON) .headers(headers).header(UUIDHEADER, "123")).andExpect(status().isBadRequest()); @@ -766,10 +766,10 @@ public class PolicyEngineServicesTest { .headers(headers).header(UUIDHEADER, UUID.randomUUID().toString())).andExpect(status().isBadRequest()); pep.setDictionary("OnapName"); mockMvc.perform(post("/getDictionaryItems").content(PolicyUtils.objectToJsonString(pep)).contentType(MediaType.APPLICATION_JSON) - .headers(headers)).andExpect(status().isInternalServerError()); + .headers(headers)).andExpect(status().isBadRequest()); pep.setRequestID(UUID.randomUUID()); mockMvc.perform(post("/getDictionaryItems").content(PolicyUtils.objectToJsonString(pep)).contentType(MediaType.APPLICATION_JSON) - .headers(headers)).andExpect(status().isInternalServerError()); + .headers(headers)).andExpect(status().isBadRequest()); } @Test diff --git a/ONAP-PDP-REST/xacml.pdp.properties b/ONAP-PDP-REST/xacml.pdp.properties index 4d120042a..57e1d708b 100644 --- a/ONAP-PDP-REST/xacml.pdp.properties +++ b/ONAP-PDP-REST/xacml.pdp.properties @@ -2,7 +2,7 @@ # ============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. @@ -123,7 +123,7 @@ xacml.rest.notification.delay=30 CLIENT_INTERVAL=15000 # Buffer Size. -REQUEST_BUFFER_SIZE=15 +REQUEST_BUFFER_SIZE=50 #properties for MySql xacml database: PLEASE DO NOT REMOVE... NEEDED FOR APIs javax.persistence.jdbc.driver=org.mariadb.jdbc.Driver diff --git a/ONAP-REST/src/main/java/org/onap/policy/rest/util/PolicyValidation.java b/ONAP-REST/src/main/java/org/onap/policy/rest/util/PolicyValidation.java index 446073d40..47291cf7f 100644 --- a/ONAP-REST/src/main/java/org/onap/policy/rest/util/PolicyValidation.java +++ b/ONAP-REST/src/main/java/org/onap/policy/rest/util/PolicyValidation.java @@ -515,7 +515,7 @@ public class PolicyValidation { if (MICROSERVICES.equals(policyData.getConfigPolicyType())){ if(!Strings.isNullOrEmpty(policyData.getServiceType())){ - modelRequiredFieldsList = new ArrayList<>(); + modelRequiredFieldsList.clear(); pullJsonKeyPairs((JsonNode) policyData.getPolicyJSON()); String service; @@ -570,7 +570,7 @@ public class PolicyValidation { } } else { // Validate for configName, location, uuid, and policyScope if no annotations exist for this model - if(Strings.isNullOrEmpty(policyData.getMsLocation())){ + if(Strings.isNullOrEmpty(policyData.getLocation())){ responseString.append("Micro Service Model: location is required for this model" + HTML_ITALICS_LNBREAK); valid = false; } @@ -591,51 +591,64 @@ public class PolicyValidation { } } - // get list of required fields from the sub_Attributes of the Model - if(!Strings.isNullOrEmpty(subAttributes)) { - JsonObject subAttributesJson = stringToJsonObject(subAttributes); - findRequiredFields(subAttributesJson); - } - - // get list of required fields from the attributes of the Model - if (!Strings.isNullOrEmpty(modelAttributes)) { - Map modelAttributesMap = Splitter.on(",").withKeyValueSeparator("=").split(modelAttributes); - String json = new ObjectMapper().writeValueAsString(modelAttributesMap); - findRequiredFields(stringToJsonObject(json)); - } - - // get list of required fields from the ref_Attributes of the Model - if (!Strings.isNullOrEmpty(refAttributes)) { - Map refAttributesMap = Splitter.on(",").withKeyValueSeparator("=").split(refAttributes); - String json = new ObjectMapper().writeValueAsString(refAttributesMap); - findRequiredFields(stringToJsonObject(json)); - } - - // Validate Required Fields in the Micro Service Model - if (modelRequiredFieldsList!=null || !modelRequiredFieldsList.isEmpty()) { - // create jsonRequestMap with all json keys and values from request - JsonNode rootNode = (JsonNode) policyData.getPolicyJSON(); - pullModelJsonKeyPairs(rootNode); + // If request comes from the API we need to validate required fields in the Micro Service Model + // GUI request are already validated from the SDK-APP + if("API".equals(policyData.getApiflag())){ + // get list of required fields from the sub_Attributes of the Model + if(!Strings.isNullOrEmpty(subAttributes)) { + JsonObject subAttributesJson = stringToJsonObject(subAttributes); + findRequiredFields(subAttributesJson); + } + + // get list of required fields from the attributes of the Model + if (!Strings.isNullOrEmpty(modelAttributes)) { + Map modelAttributesMap = null; + if (",".equals(modelAttributes.substring(modelAttributes.length()-1))) { + String attributeString = modelAttributes.substring(0, modelAttributes.length()-1); + modelAttributesMap = Splitter.on(",").withKeyValueSeparator("=").split(attributeString); + } else { + modelAttributesMap = Splitter.on(",").withKeyValueSeparator("=").split(modelAttributes); + } + String json = new ObjectMapper().writeValueAsString(modelAttributesMap); + findRequiredFields(stringToJsonObject(json)); + } - // validate if the requiredFields are in the request - for(String requiredField : modelRequiredFieldsList) { - if (jsonRequestMap.containsKey(requiredField)) { - String value = jsonRequestMap.get(requiredField); - if(Strings.isNullOrEmpty(jsonRequestMap.get(requiredField)) || - "\"\"".equals(value) || - "".equals(jsonRequestMap.get(requiredField))){ + // get list of required fields from the ref_Attributes of the Model + if (!Strings.isNullOrEmpty(refAttributes)) { + Map refAttributesMap = null; + if (",".equals(refAttributes.substring(refAttributes.length()-1))) { + String attributesString = refAttributes.substring(0, refAttributes.length()-1); + refAttributesMap = Splitter.on(",").withKeyValueSeparator("=").split(attributesString); + } else { + refAttributesMap = Splitter.on(",").withKeyValueSeparator("=").split(modelAttributes); + } + String json = new ObjectMapper().writeValueAsString(refAttributesMap); + findRequiredFields(stringToJsonObject(json)); + } + + if (modelRequiredFieldsList!=null || !modelRequiredFieldsList.isEmpty()) { + // create jsonRequestMap with all json keys and values from request + JsonNode rootNode = (JsonNode) policyData.getPolicyJSON(); + jsonRequestMap.clear(); + pullModelJsonKeyPairs(rootNode); + + // validate if the requiredFields are in the request + for(String requiredField : modelRequiredFieldsList) { + if (jsonRequestMap.containsKey(requiredField)) { + String value = jsonRequestMap.get(requiredField); + if(Strings.isNullOrEmpty(jsonRequestMap.get(requiredField)) || + "\"\"".equals(value) || + "".equals(jsonRequestMap.get(requiredField))){ + responseString.append("Micro Service Model: " + requiredField + " is required" + HTML_ITALICS_LNBREAK); + valid = false; + } + } else { responseString.append("Micro Service Model: " + requiredField + " is required" + HTML_ITALICS_LNBREAK); valid = false; } - } else { - responseString.append("Micro Service Model: " + requiredField + " is required" + HTML_ITALICS_LNBREAK); - valid = false; } } - } - - - + } } else { responseString.append("Micro Service Model: Invalid Model. The model name, " + service + " of version, " + version + " was not found in the dictionary" + HTML_ITALICS_LNBREAK); diff --git a/ONAP-REST/src/main/java/org/onap/policy/rest/util/PolicyValidationRequestWrapper.java b/ONAP-REST/src/main/java/org/onap/policy/rest/util/PolicyValidationRequestWrapper.java index f19773964..76584e7c7 100644 --- a/ONAP-REST/src/main/java/org/onap/policy/rest/util/PolicyValidationRequestWrapper.java +++ b/ONAP-REST/src/main/java/org/onap/policy/rest/util/PolicyValidationRequestWrapper.java @@ -52,6 +52,9 @@ public class PolicyValidationRequestWrapper { private static final Logger LOGGER = FlexLogger.getLogger(PolicyValidationRequestWrapper.class); public static final String CONFIG_NAME="configName"; + public static final String INVALIDJSON = " improper JSON format: "; + public static final String ONAPNAME = "onapname"; + public static final String SERVICETYPE_POLICY_NAME = "serviceTypePolicyName"; public PolicyRestAdapter populateRequestParameters(HttpServletRequest request) { @@ -107,6 +110,7 @@ public class PolicyValidationRequestWrapper { policyData.setRiskLevel(parameters.getRiskLevel());//Safe parameters Attributes policyData.setGuard(String.valueOf(parameters.getGuard()));//Safe parameters Attributes policyData.setTtlDate(convertDate(parameters.getTtlDate()));//Safe parameters Attributes + policyData.setApiflag("API"); //Some policies require jsonObject conversion from String for configBody (i.e. MicroService and Firewall) JsonObject json = null; @@ -115,7 +119,7 @@ public class PolicyValidationRequestWrapper { json = stringToJsonObject(parameters.getConfigBody()); } } catch(JsonException| IllegalStateException e){ - String message = XACMLErrorConstants.ERROR_DATA_ISSUE+ " improper JSON object : " + parameters.getConfigBody(); + String message = XACMLErrorConstants.ERROR_DATA_ISSUE+ INVALIDJSON + parameters.getConfigBody(); LOGGER.error(message, e); return null; } @@ -284,7 +288,7 @@ public class PolicyValidationRequestWrapper { try { policyJSON = mapper.readTree(content); } catch (IOException e) { - String message = XACMLErrorConstants.ERROR_DATA_ISSUE+ " improper JSON object : " + parameters.getConfigBody(); + String message = XACMLErrorConstants.ERROR_DATA_ISSUE+ INVALIDJSON + parameters.getConfigBody(); LOGGER.error(message, e); return null; } @@ -300,7 +304,7 @@ public class PolicyValidationRequestWrapper { } if (json.containsKey("location")){ String msLocation = json.get("location").toString().replace("\"", ""); - policyData.setMsLocation(msLocation); + policyData.setLocation(msLocation); } if (json.containsKey(CONFIG_NAME)){ String configName = json.get(CONFIG_NAME).toString().replace("\"", ""); @@ -331,7 +335,7 @@ public class PolicyValidationRequestWrapper { policyData.setGuard(guard); } } else { - String message = XACMLErrorConstants.ERROR_DATA_ISSUE+ " improper JSON object : " + parameters.getConfigBody(); + String message = XACMLErrorConstants.ERROR_DATA_ISSUE+ INVALIDJSON + parameters.getConfigBody(); LOGGER.error(message); return null; } @@ -339,12 +343,11 @@ public class PolicyValidationRequestWrapper { } else if("Fault".equals(parameters.getPolicyConfigType().toString())){ policyData.setConfigPolicyType("ClosedLoop_Fault"); - policyData.setApiflag("API"); if(json != null){ policyData.setJsonBody(json.toString()); - if (json.get("onapname")!=null){ - String onapName = json.get("onapname").toString().replace("\"", ""); + if (json.get(ONAPNAME)!=null){ + String onapName = json.get(ONAPNAME).toString().replace("\"", ""); policyData.setOnapName(onapName); } } @@ -355,14 +358,14 @@ public class PolicyValidationRequestWrapper { if(json != null){ policyData.setJsonBody(json.toString()); - if (json.get("onapname")!=null){ - String onapName = json.get("onapname").toString().replace("\"", ""); + if (json.get(ONAPNAME)!=null){ + String onapName = json.get(ONAPNAME).toString().replace("\"", ""); policyData.setOnapName(onapName); } - if (json.get("serviceTypePolicyName")!=null){ - String serviceType = json.get("serviceTypePolicyName").toString().replace("\"", ""); + if (json.get(SERVICETYPE_POLICY_NAME)!=null){ + String serviceType = json.get(SERVICETYPE_POLICY_NAME).toString().replace("\"", ""); LinkedHashMap serviceTypePolicyName = new LinkedHashMap<>(); - serviceTypePolicyName.put("serviceTypePolicyName", serviceType); + serviceTypePolicyName.put(SERVICETYPE_POLICY_NAME, serviceType); policyData.setServiceTypePolicyName(serviceTypePolicyName); } } @@ -377,11 +380,10 @@ public class PolicyValidationRequestWrapper { return policyData; } - + private JsonObject stringToJsonObject(String value) { - try(JsonReader jsonReader = Json.createReader(new StringReader(value))){ - return jsonReader.readObject(); + return jsonReader.readObject(); } catch(JsonException| IllegalStateException e){ LOGGER.info(XACMLErrorConstants.ERROR_DATA_ISSUE+ "Improper JSON format... may or may not cause issues in validating the policy: " + value, e); return null; diff --git a/ONAP-REST/src/test/java/org/onap/policy/rest/util/PolicyValidationTest.java b/ONAP-REST/src/test/java/org/onap/policy/rest/util/PolicyValidationTest.java new file mode 100644 index 000000000..fb51c2871 --- /dev/null +++ b/ONAP-REST/src/test/java/org/onap/policy/rest/util/PolicyValidationTest.java @@ -0,0 +1,80 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP-REST + * ================================================================================ + * Copyright (C) 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. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ +package org.onap.policy.rest.util; + +import static org.junit.Assert.*; +import java.text.SimpleDateFormat; +import java.util.Date; +import java.util.UUID; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.onap.policy.api.PolicyConfigType; +import org.onap.policy.api.PolicyParameters; +import org.onap.policy.rest.adapter.PolicyRestAdapter; + +public class PolicyValidationTest { + + @Before + public void setUp() throws Exception { + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void microServicePolicyTests() throws Exception{ + PolicyValidation validation = new PolicyValidation(); + PolicyValidationRequestWrapper wrapper = new PolicyValidationRequestWrapper(); + PolicyParameters policyParameters = new PolicyParameters(); + + policyParameters.setPolicyConfigType(PolicyConfigType.MicroService); + policyParameters.setPolicyName("Test.junitPolicy"); + policyParameters.setPolicyDescription("This is a sample Micro Service policy Create example"); + policyParameters.setOnapName("DCAE"); + policyParameters.setPriority("1"); + String MSjsonString = "{\"service\":\"TOSCA_namingJenny\",\"location\":\"Test DictMSLoc\",\"uuid\":\"testDict DCAEUIID\",\"policyName\":\"testModelValidation\",\"description\":\"test\",\"configName\":\"testDict MSConfName\",\"templateVersion\":\"1607\",\"version\":\"gw12181031\",\"priority\":\"5\",\"policyScope\":\"resource=ResourcetypeVenktest1,service=ServiceName1707,type=Name1707,closedLoopControlName=Retest_retest1\",\"riskType\":\"Test\",\"riskLevel\":\"3\",\"guard\":\"True\",\"content\":{\"police-instance-name\":\"testing\",\"naming-models\":[{\"naming-properties\":[{\"property-value\":\"test\",\"source-endpoint\":\"test\",\"property-name\":\"testPropertyname\",\"increment-sequence\":{\"scope\":\"VNF\",\"start-value\":\"1\",\"length\":\"3\",\"increment\":\"2\"},\"source-system\":\"TOSCA\"}],\"naming-type\":\"testNamingType\",\"naming-recipe\":\"testNamingRecipe\"}]}}";; + policyParameters.setConfigBody(MSjsonString); + policyParameters.setRequestID(UUID.randomUUID()); + SimpleDateFormat dateformat3 = new SimpleDateFormat("dd/MM/yyyy"); + Date date = dateformat3.parse("15/10/2016"); + policyParameters.setTtlDate(date); + policyParameters.setGuard(true); + policyParameters.setRiskLevel("5"); + policyParameters.setRiskType("TEST"); + policyParameters.setRequestID(UUID.randomUUID()); + + + PolicyRestAdapter policyData = wrapper.populateRequestParameters(policyParameters); + StringBuilder responseString = validation.validatePolicy(policyData); + + assertNotSame("success", responseString.toString()); + + } + + @Test + public final void testEmailValidation() { + PolicyValidation validation = new PolicyValidation(); + String result = validation.emailValidation("testemail@test.com", "SUCCESS"); + assertEquals("success", result); + } + +} diff --git a/POLICY-SDK-APP/src/main/java/org/onap/policy/controller/AutoPushController.java b/POLICY-SDK-APP/src/main/java/org/onap/policy/controller/AutoPushController.java index 96e104656..64928f3f4 100644 --- a/POLICY-SDK-APP/src/main/java/org/onap/policy/controller/AutoPushController.java +++ b/POLICY-SDK-APP/src/main/java/org/onap/policy/controller/AutoPushController.java @@ -2,7 +2,7 @@ * ============LICENSE_START======================================================= * ONAP Policy Engine * ================================================================================ - * 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. @@ -78,6 +78,8 @@ import com.fasterxml.jackson.databind.ObjectMapper; public class AutoPushController extends RestrictedBaseController{ private static final Logger logger = FlexLogger.getLogger(AutoPushController.class); + private static final String UTF8 = "UTF-8"; + @Autowired CommonClassDao commonClassDao; @@ -319,9 +321,9 @@ public class AutoPushController extends RestrictedBaseController{ updatedGroupObject.setPolicies(currentPoliciesInGroup); this.container.updateGroup(updatedGroupObject); - response.setCharacterEncoding("UTF-8"); + response.setCharacterEncoding(UTF8); response.setContentType("application / json"); - request.setCharacterEncoding("UTF-8"); + request.setCharacterEncoding(UTF8); PrintWriter out = response.getWriter(); refreshGroups(); @@ -336,8 +338,8 @@ public class AutoPushController extends RestrictedBaseController{ } } catch (Exception e){ - response.setCharacterEncoding("UTF-8"); - request.setCharacterEncoding("UTF-8"); + response.setCharacterEncoding(UTF8); + request.setCharacterEncoding(UTF8); PrintWriter out = response.getWriter(); logger.error(e); out.write(PolicyUtils.CATCH_EXCEPTION); @@ -379,9 +381,9 @@ public class AutoPushController extends RestrictedBaseController{ this.container.updateGroup(updatedGroupObject); } - response.setCharacterEncoding("UTF-8"); + response.setCharacterEncoding(UTF8); response.setContentType("application / json"); - request.setCharacterEncoding("UTF-8"); + request.setCharacterEncoding(UTF8); PrintWriter out = response.getWriter(); refreshGroups(); @@ -393,8 +395,8 @@ public class AutoPushController extends RestrictedBaseController{ return null; } catch (Exception e){ - response.setCharacterEncoding("UTF-8"); - request.setCharacterEncoding("UTF-8"); + response.setCharacterEncoding(UTF8); + request.setCharacterEncoding(UTF8); PrintWriter out = response.getWriter(); logger.error(e); out.write(PolicyUtils.CATCH_EXCEPTION); diff --git a/packages/base/src/files/install/servers/pdp/bin/xacml.pdp.properties b/packages/base/src/files/install/servers/pdp/bin/xacml.pdp.properties index fe5277d98..f05f9e951 100644 --- a/packages/base/src/files/install/servers/pdp/bin/xacml.pdp.properties +++ b/packages/base/src/files/install/servers/pdp/bin/xacml.pdp.properties @@ -2,7 +2,7 @@ # ============LICENSE_START======================================================= # ONAP Policy Engine # ================================================================================ -# 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. @@ -111,7 +111,7 @@ xacml.rest.notification.delay=30 # Client interval to ping notification service. CLIENT_INTERVAL=15000 # Request Buffer Size. -REQUEST_BUFFER_SIZE=5 +REQUEST_BUFFER_SIZE=50 #***Properties for IntegrityMonitor integration defined in XACMLRestProperties.java*** #The name of the PDP. Must be unique across the system xacml.rest.pdp.resource.name=${{resource_name}} -- 2.16.6