ConsoleAndApiService { public void doAcPost(HttpServletRequest request, HttpServletResponse response, String groupId, OnapLoggingContext loggingContext, PAPPolicyEngine papEngine) throws IOException { PolicyDbDaoTransaction doAcPostTransaction = null; try { String groupName = request.getParameter("groupName"); String groupDescription = request.getParameter("groupDescription"); String apiflag = request.getParameter("apiflag"); String userId = request.getParameter("userId"); if (groupName != null && groupDescription != null) { loggingContext.setServiceName("AC:PAP.createGroup"); String unescapedName = null; String unescapedDescription = null; try { unescapedName = URLDecoder.decode(groupName, "UTF-8"); unescapedDescription = URLDecoder.decode(groupDescription, "UTF-8"); } catch (UnsupportedEncodingException e) { LOGGER.error(e); } PolicyDbDaoTransaction newGroupTransaction = XACMLPapServlet.getPolicyDbDao().getNewTransaction(); try { newGroupTransaction.createGroup(PolicyDbDao.createNewPdpGroupId(unescapedName), unescapedName, unescapedDescription, PAPSERVLETDOACPOST); papEngine.newGroup(unescapedName, unescapedDescription); loggingContext.metricStarted(); newGroupTransaction.commitTransaction(); loggingContext.metricEnded(); PolicyLogger.metrics(ACPOSTCOMMITTRANS); } catch (Exception e) { newGroupTransaction.rollbackTransaction(); PolicyLogger.error(MessageCodes.ERROR_PROCESS_FLOW, e, XACMLPAPSERVLET, " Unable to create new group"); loggingContext.transactionEnded(); PolicyLogger.audit(TRANSACTIONFAILED); setResponseError(response, HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "Unable to create new group '" + groupId + "'"); return; } response.setStatus(HttpServletResponse.SC_NO_CONTENT); if (LOGGER.isDebugEnabled()) { LOGGER.debug("New Group '" + groupId + "' created"); } auditLogger.info(SUCCESS); PolicyLogger.audit(TRANSENDED); return; } OnapPDPGroup group = null; try { group = papEngine.getGroup(groupId); } catch (PAPException e) { LOGGER.error(e); } if (group == null) { String message = UNKNOWN_GROUP_ID + groupId + "'"; if (!message.matches(REGEX)) { response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); response.addHeader(ERROR, ADD_GROUP_ERROR); response.addHeader(MESSAGE, "GroupId Id is not valid"); return; } PolicyLogger.error(MessageCodes.ERROR_DATA_ISSUE + " " + message); loggingContext.transactionEnded(); PolicyLogger.audit(TRANSACTIONFAILED); if (apiflag != null) { response.addHeader(ERROR, "unknownGroupId"); response.addHeader("operation", "push"); response.addHeader(MESSAGE, message); response.setStatus(HttpServletResponse.SC_NOT_FOUND); } else { setResponseError(response, HttpServletResponse.SC_NOT_FOUND, message); } return; } if (request.getParameter(POLICYID) != null) { String policyName = request.getParameter(POLICYID); List<String> policyIdList = Arrays.asList(policyName.split(",")); loggingContext.setServiceName("PolicyEngineAPI:PAP.postPolicy"); LOGGER.info("PushPolicy Request - " + policyName + ", UserId - " + userId); StdPDPGroup updatedGroup = null; StdPDPPolicy policyForSafetyCheck = new StdPDPPolicy(); for (String policyId : policyIdList) { PolicyDbDaoTransaction addPolicyToGroupTransaction = XACMLPapServlet.getPolicyDbDao().getNewTransaction(); try { LOGGER.info("PapServlet: calling PolicyDBDao.addPolicyToGroup()"); updatedGroup = addPolicyToGroupTransaction.addPolicyToGroup(group.getId(), policyId, PAPSERVLETDOACPOST, userId); loggingContext.metricStarted(); addPolicyToGroupTransaction.commitTransaction(); loggingContext.metricEnded(); PolicyLogger.metrics(ACPOSTCOMMITTRANS); LOGGER.info("PapServlet: addPolicyToGroup() succeeded, transaction was committed"); if (policyId.contains("Config_MS_") || policyId.contains("BRMS_Param")) { PushPolicyHandler pushPolicyHandler = PushPolicyHandler.getInstance(); policyForSafetyCheck.setId(policyId); if (pushPolicyHandler.preSafetyCheck(policyForSafetyCheck, XACMLPapServlet.getConfigHome())) { LOGGER.debug("Precheck Successful."); } } Files.deleteIfExists(Paths.get(policyId)); } catch (Exception e) { addPolicyToGroupTransaction.rollbackTransaction(); String message = "Policy '" + policyName + "' not copied to group '" + groupId + "': " + e; if (!message.matches(REGEX)) { response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); response.addHeader(ERROR, ADD_GROUP_ERROR); response.addHeader(MESSAGE, "Policy Id is not valid"); return; } PolicyLogger.error(MessageCodes.ERROR_PROCESS_FLOW + " " + message); loggingContext.transactionEnded(); PolicyLogger.audit(TRANSACTIONFAILED); if (apiflag != null) { response.addHeader(ERROR, "policyCopyError"); response.addHeader(MESSAGE, message); response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); } else { setResponseError(response, HttpServletResponse.SC_INTERNAL_SERVER_ERROR, message); } return; } } PolicyDbDaoTransaction acPutTransaction = XACMLPapServlet.getPolicyDbDao().getNewTransaction(); try { loggingContext.setServiceName("PolicyEngineAPI:PAP.updateGroup"); try { acPutTransaction.updateGroup(updatedGroup, XACML_PAP_SERVLET_DO_AC_PUT, userId); } catch (Exception e) { PolicyLogger.error(MessageCodes.ERROR_PROCESS_FLOW, e, XACMLPAPSERVLET, " Error occurred when notifying PAPs of a group change: " + e); throw new PAPException(e.getMessage()); } LOGGER.info("Calling updatGroup() with new group"); papEngine.updateGroup(updatedGroup); LOGGER.info("Group - '" + updatedGroup.getId() + "' updated"); loggingContext.metricStarted(); acPutTransaction.commitTransaction(); loggingContext.metricEnded(); PolicyLogger.metrics("XACMLPapServlet updateGroupsFromAPI commitTransaction"); getPapInstance().groupChanged(updatedGroup, loggingContext); loggingContext.transactionEnded(); LOGGER.info(SUCCESS); } catch (Exception e) { acPutTransaction.rollbackTransaction(); PolicyLogger.error(MessageCodes.ERROR_PROCESS_FLOW, e, XACMLPAPSERVLET, " API PUT exception"); loggingContext.transactionEnded(); PolicyLogger.audit(TRANSACTIONFAILED); 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, ADD_GROUP_ERROR); response.addHeader(MESSAGE, message); return; } response.setStatus(HttpServletResponse.SC_NO_CONTENT); response.addHeader("operation", "push"); response.addHeader(POLICYID, policyName); response.addHeader("groupId", groupId); LOGGER.info("policy '" + policyName + "' copied to directory for group '" + groupId + "'"); loggingContext.transactionEnded(); auditLogger.info(SUCCESS); LOGGER.info(TRANSENDED); } else if (request.getParameter("default") != null) { loggingContext.setServiceName("AC:PAP.setDefaultGroup"); PolicyDbDaoTransaction setDefaultGroupTransaction = XACMLPapServlet.getPolicyDbDao().getNewTransaction(); try { setDefaultGroupTransaction.changeDefaultGroup(group, PAPSERVLETDOACPOST); papEngine.setDefaultGroup(group); loggingContext.metricStarted(); setDefaultGroupTransaction.commitTransaction(); loggingContext.metricEnded(); PolicyLogger.metrics(ACPOSTCOMMITTRANS); } catch (Exception e) { setDefaultGroupTransaction.rollbackTransaction(); PolicyLogger.error(MessageCodes.ERROR_PROCESS_FLOW, e, XACMLPAPSERVLET, " Unable to set group"); loggingContext.transactionEnded(); PolicyLogger.audit(TRANSACTIONFAILED); setResponseError(response, HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "Unable to set group '" + groupId + "' to default"); return; } response.setStatus(HttpServletResponse.SC_NO_CONTENT); if (LOGGER.isDebugEnabled()) { LOGGER.debug("Group- '" + groupId + "' set to be default"); } auditLogger.info(SUCCESS); LOGGER.info(TRANSENDED); } else if (request.getParameter(PDP_ID) != null) { doAcPostTransaction = XACMLPapServlet.getPolicyDbDao().getNewTransaction(); loggingContext.setServiceName("AC:PAP.movePDP"); String pdpId = request.getParameter(PDP_ID); OnapPDP pdp = papEngine.getPDP(pdpId); final OnapPDPGroup originalGroup = papEngine.getPDPGroup(pdp); try { doAcPostTransaction.movePdp(pdp, group, PAPSERVLETDOACPOST); } catch (Exception e) { doAcPostTransaction.rollbackTransaction(); PolicyLogger.error(MessageCodes.ERROR_PROCESS_FLOW, e, XACMLPAPSERVLET, " Error while moving pdp in the database: " + "pdp=" + pdp.getId() + TO_GROUP + group.getId()); throw new PAPException(e.getMessage()); } papEngine.movePDP(pdp, group); response.setStatus(HttpServletResponse.SC_NO_CONTENT); if (LOGGER.isDebugEnabled()) { LOGGER.debug( "PDP - '" + pdp.getId() + "' moved to group - '" + group.getId() + "' set to be default"); } ((StdPDPGroup) originalGroup).resetStatus(); ((StdPDPGroup) group).resetStatus(); getPapInstance().pdpChanged(pdp, loggingContext); loggingContext.metricStarted(); doAcPostTransaction.commitTransaction(); loggingContext.metricEnded(); PolicyLogger.metrics(ACPOSTCOMMITTRANS); loggingContext.transactionEnded(); auditLogger.info(SUCCESS); PolicyLogger.audit(TRANSENDED); } } catch (PAPException e) { PolicyLogger.error(MessageCodes.ERROR_PROCESS_FLOW, e, XACMLPAPSERVLET, " AC POST exception"); loggingContext.transactionEnded(); PolicyLogger.audit(TRANSACTIONFAILED); setResponseError(response, HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e.getMessage()); } }  void doAcPost(HttpServletRequest request, HttpServletResponse response, String groupId,
            OnapLoggingContext loggingContext, PAPPolicyEngine papEngine); void doAcGet(HttpServletRequest request, HttpServletResponse response, String groupId,
            OnapLoggingContext loggingContext, PAPPolicyEngine papEngine); void doAcPut(HttpServletRequest request, HttpServletResponse response, String groupId,
            OnapLoggingContext loggingContext, PAPPolicyEngine papEngine); void doAcDelete(HttpServletRequest request, HttpServletResponse response, String groupId,
            OnapLoggingContext loggingContext, PAPPolicyEngine papEngine);  }
@Test public void testGroupCreation() throws IOException { Mockito.when(httpServletRequest.getHeader(ENVIRONMENT_HEADER)).thenReturn(DEVL); Mockito.when(httpServletRequest.getMethod()).thenReturn(POST); Mockito.when(httpServletRequest.getParameter(APIFLAG)).thenReturn(API2); Mockito.when(httpServletRequest.getParameter(USER_ID)).thenReturn(API); Mockito.when(httpServletRequest.getParameter(GROUP_DESCRIPTION)).thenReturn("test"); Mockito.when(httpServletRequest.getParameter(GROUP_NAME)).thenReturn(TESTGRP1); consoleAndApi.doAcPost(httpServletRequest, httpServletResponse, TESTGRP1, logContext, stdEngine); assertTrue(HttpServletResponse.SC_NO_CONTENT == httpServletResponse.getStatus()); }
@Test public void testGroupNotExistInDb() throws IOException { Mockito.when(httpServletRequest.getHeader(ENVIRONMENT_HEADER)).thenReturn(DEVL); Mockito.when(httpServletRequest.getMethod()).thenReturn(POST); Mockito.when(httpServletRequest.getParameter(APIFLAG)).thenReturn(API2); Mockito.when(httpServletRequest.getParameter(USER_ID)).thenReturn(API); Mockito.when(httpServletRequest.getParameter(GROUP_NAME)).thenReturn("testgrpNotExist"); consoleAndApi.doAcPost(httpServletRequest, httpServletResponse, "testgrpNotExist", logContext, stdEngine); assertTrue(HttpServletResponse.SC_INTERNAL_SERVER_ERROR == httpServletResponse.getStatus()); }
@Test public void testGroupChange() throws IOException { Mockito.when(httpServletRequest.getHeader(ENVIRONMENT_HEADER)).thenReturn(DEVL); Mockito.when(httpServletRequest.getMethod()).thenReturn(POST); Mockito.when(httpServletRequest.getParameter(APIFLAG)).thenReturn(API2); Mockito.when(httpServletRequest.getParameter(USER_ID)).thenReturn(API); Mockito.when(httpServletRequest.getParameter(GROUP_DESCRIPTION)).thenReturn("test"); Mockito.when(httpServletRequest.getParameter(GROUP_NAME)).thenReturn(TESTGRP2); consoleAndApi.doAcPost(httpServletRequest, httpServletResponse, TESTGRP2, logContext, stdEngine); assertTrue(HttpServletResponse.SC_NO_CONTENT == httpServletResponse.getStatus()); Mockito.when(httpServletRequest.getParameter(GROUP_DESCRIPTION)).thenReturn(null); Mockito.when(httpServletRequest.getParameter(DEFAULT)).thenReturn(DEFAULT); consoleAndApi.doAcPost(httpServletRequest, httpServletResponse, TESTGRP2, logContext, stdEngine); assertTrue(HttpServletResponse.SC_NO_CONTENT == httpServletResponse.getStatus()); }
@Test public void testPushPolicy() throws Exception { Mockito.when(httpServletRequest.getHeader(ENVIRONMENT_HEADER)).thenReturn(DEVL); Mockito.when(httpServletRequest.getMethod()).thenReturn(POST); Mockito.when(httpServletRequest.getParameter(APIFLAG)).thenReturn(API2); Mockito.when(httpServletRequest.getParameter(USER_ID)).thenReturn(API); Mockito.when(httpServletRequest.getParameter(GROUP_DESCRIPTION)).thenReturn(null); Mockito.when(httpServletRequest.getParameter(POLICY_ID)).thenReturn(POLICY_NAME); stdEngine = new StdEngine(Paths.get(PDPS)); dbd.setPapEngine(stdEngine); populatePolicyInDb(); consoleAndApi.doAcPost(httpServletRequest, httpServletResponse, DEFAULT, logContext, stdEngine); assertTrue(HttpServletResponse.SC_NO_CONTENT == httpServletResponse.getStatus()); }