diff options
author | Tej, Tarun <tt3868@att.com> | 2017-09-07 19:21:28 -0400 |
---|---|---|
committer | Tej, Tarun <tt3868@att.com> | 2017-09-08 00:51:01 -0400 |
commit | 24c6b1682bb59691f8d5e631b1074355b7d82a54 (patch) | |
tree | 50a5db354050faf79ccc7e7e9a08e488163651aa /ONAP-PAP-REST/src/main/java | |
parent | 461b2b7eeca1586423349bc0c4c65f80f027292d (diff) |
Sonar Fixes
Fixes to the sonar issues along with some cleanup code to remove warnings.
Issue-Id: POLICY-113
Change-Id: I826562f7509c346210ac865cc6b31bce26bee2cf
Signed-off-by: Tej, Tarun <tt3868@att.com>
Diffstat (limited to 'ONAP-PAP-REST/src/main/java')
22 files changed, 226 insertions, 157 deletions
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 5132fae48..121e4bc0b 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 @@ -80,6 +80,7 @@ import org.onap.policy.pap.xacml.rest.handler.SavePolicyHandler; import org.onap.policy.pap.xacml.restAuth.CheckPDP; import org.onap.policy.rest.XACMLRest; import org.onap.policy.rest.XACMLRestProperties; +import org.onap.policy.rest.dao.PolicyDBException; import org.onap.policy.utils.PolicyUtils; import org.onap.policy.xacml.api.XACMLErrorConstants; import org.onap.policy.xacml.api.pap.ONAPPapEngineFactory; @@ -113,7 +114,6 @@ import com.google.common.base.Splitter; }) public class XACMLPapServlet extends HttpServlet implements StdItemSetChangeListener, Runnable { private static final long serialVersionUID = 1L; - private static final String localIp = "127.0.0.1"; private static final Logger LOGGER = FlexLogger.getLogger(XACMLPapServlet.class); // audit (transaction) LOGGER private static final Logger auditLogger = FlexLogger.getLogger("auditLogger"); @@ -171,7 +171,7 @@ public class XACMLPapServlet extends HttpServlet implements StdItemSetChangeList * This thread may be invoked upon startup to initiate sending PDP policy/pip configuration when * this servlet starts. Its configurable by the admin. */ - private transient static Thread initiateThread = null; + private static transient Thread initiateThread = null; private transient ONAPLoggingContext baseLoggingContext = null; /** @@ -284,22 +284,24 @@ public class XACMLPapServlet extends HttpServlet implements StdItemSetChangeList // The factory knows how to go about creating a PAP Engine setPAPEngine((PAPPolicyEngine) factory.newEngine()); PolicyDBDaoTransaction addNewGroup = null; - try{ - if(((org.onap.policy.xacml.std.pap.StdEngine)papEngine).wasDefaultGroupJustAdded){ - addNewGroup = policyDBDao.getNewTransaction(); - OnapPDPGroup group = papEngine.getDefaultGroup(); - addNewGroup.createGroup(group.getId(), group.getName(), group.getDescription(), "automaticallyAdded"); - addNewGroup.commitTransaction(); - addNewGroup = policyDBDao.getNewTransaction(); - addNewGroup.changeDefaultGroup(group, "automaticallyAdded"); - addNewGroup.commitTransaction(); - } - } catch(Exception e){ - PolicyLogger.error(MessageCodes.EXCEPTION_ERROR, e, "XACMLPapServlet", " Error creating new default group in the database"); - if(addNewGroup != null){ - addNewGroup.rollbackTransaction(); - } - } + if (((org.onap.policy.xacml.std.pap.StdEngine) papEngine).wasDefaultGroupJustAdded) { + try { + addNewGroup = policyDBDao.getNewTransaction(); + OnapPDPGroup group = papEngine.getDefaultGroup(); + addNewGroup.createGroup(group.getId(), group.getName(), group.getDescription(), + "automaticallyAdded"); + addNewGroup.commitTransaction(); + addNewGroup = policyDBDao.getNewTransaction(); + addNewGroup.changeDefaultGroup(group, "automaticallyAdded"); + addNewGroup.commitTransaction(); + } catch (Exception e) { + PolicyLogger.error(MessageCodes.EXCEPTION_ERROR, e, "XACMLPapServlet", + " Error creating new default group in the database"); + if (addNewGroup != null) { + addNewGroup.rollbackTransaction(); + } + } + } policyDBDao.setPapEngine((PAPPolicyEngine) XACMLPapServlet.papEngine); if (Boolean.parseBoolean(XACMLProperties.getProperty(XACMLRestProperties.PROP_PAP_RUN_AUDIT_FLAG))){ //get an AuditTransaction to lock out all other transactions @@ -564,7 +566,7 @@ public class XACMLPapServlet extends HttpServlet implements StdItemSetChangeList try { pdpTransaction.addPdpToGroup(id, XACMLPapServlet.papEngine.getDefaultGroup().getId(), id, "Registered on first startup", Integer.parseInt(jmxport), "PDP autoregister"); XACMLPapServlet.papEngine.newPDP(id, XACMLPapServlet.papEngine.getDefaultGroup(), id, "Registered on first startup", Integer.parseInt(jmxport)); - } catch (NullPointerException | PAPException | IllegalArgumentException | IllegalStateException | PersistenceException e) { + } catch (NullPointerException | PAPException | IllegalArgumentException | IllegalStateException | PersistenceException | PolicyDBException e) { pdpTransaction.rollbackTransaction(); String message = "Failed to create new PDP for id: " + id; PolicyLogger.error(MessageCodes.ERROR_PROCESS_FLOW, e, "XACMLPapServlet", " " + message); @@ -575,7 +577,11 @@ public class XACMLPapServlet extends HttpServlet implements StdItemSetChangeList return; } // get the PDP we just created - pdp = XACMLPapServlet.papEngine.getPDP(id); + try{ + pdp = XACMLPapServlet.papEngine.getPDP(id); + }catch(PAPException e){ + LOGGER.error(e); + } if (pdp == null) { if(pdpTransaction != null){ pdpTransaction.rollbackTransaction(); @@ -604,10 +610,19 @@ public class XACMLPapServlet extends HttpServlet implements StdItemSetChangeList } } if (jmxport != null && jmxport != ""){ - ((StdPDP) pdp).setJmxPort(Integer.valueOf(jmxport)); + try{ + ((StdPDP) pdp).setJmxPort(Integer.valueOf(jmxport)); + }catch(NumberFormatException e){ + LOGGER.error(e); + } } // Get the PDP's Group - OnapPDPGroup group = XACMLPapServlet.papEngine.getPDPGroup((OnapPDP) pdp); + OnapPDPGroup group =null; + try{ + group= XACMLPapServlet.papEngine.getPDPGroup((OnapPDP) pdp); + }catch(PAPException e){ + LOGGER.error(e); + } if (group == null) { PolicyLogger.error(MessageCodes.ERROR_PROCESS_FLOW + " PDP not associated with any group, even the default"); loggingContext.transactionEnded(); @@ -622,7 +637,11 @@ public class XACMLPapServlet extends HttpServlet implements StdItemSetChangeList Properties pipconfig = group.getPipConfigProperties(); // Get the current policy/pip configuration that the PDP has Properties pdpProperties = new Properties(); - pdpProperties.load(request.getInputStream()); + try{ + pdpProperties.load(request.getInputStream()); + }catch(IOException e){ + LOGGER.error(e); + } LOGGER.info("PDP Current Properties: " + pdpProperties.toString()); LOGGER.info("Policies: " + (policies != null ? policies.toString() : "null")); LOGGER.info("Pip config: " + (pipconfig != null ? pipconfig.toString() : "null")); @@ -637,19 +656,35 @@ public class XACMLPapServlet extends HttpServlet implements StdItemSetChangeList // retrieve them. this.populatePolicyURL(request.getRequestURL(), policies); // Copy the properties to the output stream - policies.store(response.getOutputStream(), ""); + try{ + policies.store(response.getOutputStream(), ""); + }catch(IOException e){ + LOGGER.error(e); + } } if (pipconfig != null) { // Copy the properties to the output stream - pipconfig.store(response.getOutputStream(), ""); + try{ + pipconfig.store(response.getOutputStream(), ""); + }catch(IOException e){ + LOGGER.error(e); + } } // We are good - and we are sending them information response.setStatus(HttpServletResponse.SC_OK); - setPDPSummaryStatus(pdp, PDPStatus.Status.OUT_OF_SYNCH); + try{ + setPDPSummaryStatus(pdp, PDPStatus.Status.OUT_OF_SYNCH); + }catch(PAPException e){ + LOGGER.error(e); + } } else { // Tell them they are good response.setStatus(HttpServletResponse.SC_NO_CONTENT); - setPDPSummaryStatus(pdp, PDPStatus.Status.UP_TO_DATE); + try{ + setPDPSummaryStatus(pdp, PDPStatus.Status.UP_TO_DATE); + }catch(PAPException e){ + LOGGER.error(e); + } } // tell the AC that something changed notifyAC(); @@ -769,12 +804,16 @@ public class XACMLPapServlet extends HttpServlet implements StdItemSetChangeList String id = this.getPDPID(request); LOGGER.info("doGet from: " + id); // Get the PDP Object - OnapPDP pdp = XACMLPapServlet.papEngine.getPDP(id); + OnapPDP pdp = null; + try{ + pdp = XACMLPapServlet.papEngine.getPDP(id); + }catch(PAPException e){ + LOGGER.error(e); + } // Is it known? if (pdp == null) { // Check if request came from localhost if (request.getRemoteHost().equals("localhost") || - request.getRemoteHost().equals(localIp) || request.getRemoteHost().equals(request.getLocalAddr())) { // Return status information - basically all the groups loggingContext.setServiceName("PAP.getGroups"); @@ -1845,6 +1884,7 @@ public class XACMLPapServlet extends HttpServlet implements StdItemSetChangeList } catch(Exception e){ PolicyLogger.error(MessageCodes.ERROR_PROCESS_FLOW + " Error while updating group in the database: " +"group="+group.getId()); + LOGGER.error(e); throw new PAPException(e.getMessage()); } @@ -1899,7 +1939,7 @@ public class XACMLPapServlet extends HttpServlet implements StdItemSetChangeList try{ policyToDelete = URLDecoder.decode(policyToDelete,"UTF-8"); } catch(UnsupportedEncodingException e){ - PolicyLogger.error("Unsupported URL encoding of policyToDelete (UTF-8"); + LOGGER.error("Unsupported URL encoding of policyToDelete (UTF-8", e); setResponseError(response,HttpServletResponse.SC_INTERNAL_SERVER_ERROR,"policyToDelete encoding not supported"); return; } @@ -1945,7 +1985,7 @@ public class XACMLPapServlet extends HttpServlet implements StdItemSetChangeList try{ removePdpOrGroupTransaction.removePdpFromGroup(pdp.getId(),"XACMLPapServlet.doACDelete"); } catch(Exception e){ - throw new PAPException(); + throw new PAPException(e); } papEngine.removePDP((OnapPDP) pdp); // adjust the status of the group, which may have changed when we removed this PDP @@ -2215,7 +2255,7 @@ public class XACMLPapServlet extends HttpServlet implements StdItemSetChangeList groups = papEngine.getOnapPDPGroups(); } catch (PAPException e) { PolicyLogger.error(MessageCodes.ERROR_SYSTEM_ERROR, e, "XACMLPapServlet", " getPDPGroups failed"); - throw new RuntimeException(XACMLErrorConstants.ERROR_SYSTEM_ERROR + "Unable to get Groups: " + e); + throw new IllegalAccessError(XACMLErrorConstants.ERROR_SYSTEM_ERROR + "Unable to get Groups: " + e); } for (OnapPDPGroup group : groups) { groupChanged(group); @@ -2463,7 +2503,7 @@ public class XACMLPapServlet extends HttpServlet implements StdItemSetChangeList } String message = "GET:/pap/test called and PAP " + papResourceName + " has had a subsystem failure." + " Exception Message: " + eMsg; - LOGGER.info(message); + LOGGER.info(message, e); PolicyLogger.error(MessageCodes.ERROR_SYSTEM_ERROR + " " + message); loggingContext.transactionEnded(); PolicyLogger.audit("Transaction Failed - See Error.log"); @@ -2514,7 +2554,7 @@ public class XACMLPapServlet extends HttpServlet implements StdItemSetChangeList try { Files.createDirectories(webappsPathConfig); } catch (IOException e) { - PolicyLogger.error(MessageCodes.ERROR_PROCESS_FLOW, e, "XACMLPapServlet", " Failed to create config directory: " + PolicyLogger.error(MessageCodes.ERROR_PROCESS_FLOW, e, "XACMLPapServlet", "Failed to create config directory: " + webappsPathConfig.toAbsolutePath().toString()); } } @@ -2522,7 +2562,7 @@ public class XACMLPapServlet extends HttpServlet implements StdItemSetChangeList try { Files.createDirectories(webappsPathAction); } catch (IOException e) { - LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Failed to create config directory: " + LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Failed to create action directory: " + webappsPathAction.toAbsolutePath().toString(), e); } } diff --git a/ONAP-PAP-REST/src/main/java/org/onap/policy/pap/xacml/rest/components/ActionPolicy.java b/ONAP-PAP-REST/src/main/java/org/onap/policy/pap/xacml/rest/components/ActionPolicy.java index 7addf2f88..ae17ebf67 100644 --- a/ONAP-PAP-REST/src/main/java/org/onap/policy/pap/xacml/rest/components/ActionPolicy.java +++ b/ONAP-PAP-REST/src/main/java/org/onap/policy/pap/xacml/rest/components/ActionPolicy.java @@ -57,6 +57,9 @@ import org.onap.policy.rest.jpa.ActionPolicyDict; import org.onap.policy.rest.jpa.Datatype; import org.onap.policy.rest.jpa.FunctionDefinition; import org.onap.policy.xacml.api.XACMLErrorConstants; + +import com.att.research.xacml.api.pap.PAPException; + import org.onap.policy.common.logging.eelf.MessageCodes; import org.onap.policy.common.logging.eelf.PolicyLogger; import org.onap.policy.common.logging.flexlogger.FlexLogger; @@ -94,7 +97,7 @@ public class ActionPolicy extends Policy { protected Map<String, String> dropDownMap = new HashMap<>(); private static boolean isAttribute = false; - private static synchronized boolean getAttribute () { + private synchronized static boolean getAttribute () { return isAttribute; } @@ -111,7 +114,7 @@ public class ActionPolicy extends Policy { } @Override - public Map<String, String> savePolicies() throws Exception { + public Map<String, String> savePolicies() throws PAPException { Map<String, String> successMap = new HashMap<>(); if(isPolicyExists()){ @@ -139,7 +142,7 @@ public class ActionPolicy extends Policy { //This is the method for preparing the policy for saving. We have broken it out //separately because the fully configured policy is used for multiple things @Override - public boolean prepareToSave() throws Exception{ + public boolean prepareToSave() throws PAPException{ if(isPreparedToSave()){ //we have already done this diff --git a/ONAP-PAP-REST/src/main/java/org/onap/policy/pap/xacml/rest/components/ClosedLoopPolicy.java b/ONAP-PAP-REST/src/main/java/org/onap/policy/pap/xacml/rest/components/ClosedLoopPolicy.java index 2a5e9d3dd..8e9c4b659 100644 --- a/ONAP-PAP-REST/src/main/java/org/onap/policy/pap/xacml/rest/components/ClosedLoopPolicy.java +++ b/ONAP-PAP-REST/src/main/java/org/onap/policy/pap/xacml/rest/components/ClosedLoopPolicy.java @@ -39,6 +39,7 @@ import org.onap.policy.common.logging.flexlogger.FlexLogger; import org.onap.policy.common.logging.flexlogger.Logger; import org.onap.policy.rest.adapter.PolicyRestAdapter; +import com.att.research.xacml.api.pap.PAPException; import com.att.research.xacml.std.IdentifierImpl; import oasis.names.tc.xacml._3_0.core.schema.wd_17.AdviceExpressionType; @@ -114,7 +115,7 @@ public class ClosedLoopPolicy extends Policy { } @Override - public Map<String, String> savePolicies() throws Exception { + public Map<String, String> savePolicies() throws PAPException { Map<String, String> successMap = new HashMap<>(); if(isPolicyExists()){ @@ -137,7 +138,7 @@ public class ClosedLoopPolicy extends Policy { //This is the method for preparing the policy for saving. We have broken it out //separately because the fully configured policy is used for multiple things @Override - public boolean prepareToSave() throws Exception{ + public boolean prepareToSave() throws PAPException{ if(isPreparedToSave()){ //we have already done this diff --git a/ONAP-PAP-REST/src/main/java/org/onap/policy/pap/xacml/rest/components/ConfigPolicy.java b/ONAP-PAP-REST/src/main/java/org/onap/policy/pap/xacml/rest/components/ConfigPolicy.java index 02ba2ef14..63a62d23d 100644 --- a/ONAP-PAP-REST/src/main/java/org/onap/policy/pap/xacml/rest/components/ConfigPolicy.java +++ b/ONAP-PAP-REST/src/main/java/org/onap/policy/pap/xacml/rest/components/ConfigPolicy.java @@ -63,6 +63,7 @@ import org.xml.sax.SAXException; import org.xml.sax.SAXParseException; import org.xml.sax.XMLReader; +import com.att.research.xacml.api.pap.PAPException; import com.att.research.xacml.std.IdentifierImpl; public class ConfigPolicy extends Policy { @@ -231,7 +232,7 @@ public class ConfigPolicy extends Policy { } @Override - public Map<String, String> savePolicies() throws Exception { + public Map<String, String> savePolicies() throws PAPException { Map<String, String> successMap = new HashMap<>(); if(isPolicyExists()){ @@ -254,7 +255,7 @@ public class ConfigPolicy extends Policy { //This is the method for preparing the policy for saving. We have broken it out //separately because the fully configured policy is used for multiple things @Override - public boolean prepareToSave() throws Exception{ + public boolean prepareToSave() throws PAPException{ if(isPreparedToSave()){ return true; diff --git a/ONAP-PAP-REST/src/main/java/org/onap/policy/pap/xacml/rest/components/CreateBrmsParamPolicy.java b/ONAP-PAP-REST/src/main/java/org/onap/policy/pap/xacml/rest/components/CreateBrmsParamPolicy.java index 5abe7ce10..b27dd22ca 100644 --- a/ONAP-PAP-REST/src/main/java/org/onap/policy/pap/xacml/rest/components/CreateBrmsParamPolicy.java +++ b/ONAP-PAP-REST/src/main/java/org/onap/policy/pap/xacml/rest/components/CreateBrmsParamPolicy.java @@ -50,6 +50,7 @@ import org.onap.policy.pap.xacml.rest.daoimpl.CommonClassDaoImpl; import org.onap.policy.rest.adapter.PolicyRestAdapter; import org.onap.policy.rest.jpa.BRMSParamTemplate; +import com.att.research.xacml.api.pap.PAPException; import com.att.research.xacml.std.IdentifierImpl; import oasis.names.tc.xacml._3_0.core.schema.wd_17.AdviceExpressionType; @@ -164,7 +165,7 @@ public class CreateBrmsParamPolicy extends Policy { } @Override - public Map<String, String> savePolicies() throws Exception { + public Map<String, String> savePolicies() throws PAPException { Map<String, String> successMap = new HashMap<>(); if(isPolicyExists()){ @@ -303,7 +304,7 @@ public class CreateBrmsParamPolicy extends Policy { // separately because the fully configured policy is used for multiple // things @Override - public boolean prepareToSave() throws Exception { + public boolean prepareToSave() throws PAPException { if (isPreparedToSave()) { // we have already done this diff --git a/ONAP-PAP-REST/src/main/java/org/onap/policy/pap/xacml/rest/components/CreateBrmsRawPolicy.java b/ONAP-PAP-REST/src/main/java/org/onap/policy/pap/xacml/rest/components/CreateBrmsRawPolicy.java index 4bcb05788..a6240e0ee 100644 --- a/ONAP-PAP-REST/src/main/java/org/onap/policy/pap/xacml/rest/components/CreateBrmsRawPolicy.java +++ b/ONAP-PAP-REST/src/main/java/org/onap/policy/pap/xacml/rest/components/CreateBrmsRawPolicy.java @@ -40,6 +40,7 @@ import org.onap.policy.common.logging.eelf.PolicyLogger; import org.onap.policy.pap.xacml.rest.controller.BRMSDictionaryController; import org.onap.policy.rest.adapter.PolicyRestAdapter; +import com.att.research.xacml.api.pap.PAPException; import com.att.research.xacml.std.IdentifierImpl; import oasis.names.tc.xacml._3_0.core.schema.wd_17.AdviceExpressionType; @@ -115,7 +116,7 @@ public class CreateBrmsRawPolicy extends Policy { } @Override - public Map<String, String> savePolicies() throws Exception { + public Map<String, String> savePolicies() throws PAPException { Map<String, String> successMap = new HashMap<>(); if(isPolicyExists()){ @@ -146,7 +147,7 @@ public class CreateBrmsRawPolicy extends Policy { // separately because the fully configured policy is used for multiple // things @Override - public boolean prepareToSave() throws Exception { + public boolean prepareToSave() throws PAPException { if (isPreparedToSave()) { // we have already done this diff --git a/ONAP-PAP-REST/src/main/java/org/onap/policy/pap/xacml/rest/components/CreateClosedLoopPerformanceMetrics.java b/ONAP-PAP-REST/src/main/java/org/onap/policy/pap/xacml/rest/components/CreateClosedLoopPerformanceMetrics.java index ada704465..68cc9eff7 100644 --- a/ONAP-PAP-REST/src/main/java/org/onap/policy/pap/xacml/rest/components/CreateClosedLoopPerformanceMetrics.java +++ b/ONAP-PAP-REST/src/main/java/org/onap/policy/pap/xacml/rest/components/CreateClosedLoopPerformanceMetrics.java @@ -37,6 +37,7 @@ import org.onap.policy.common.logging.flexlogger.FlexLogger; import org.onap.policy.common.logging.flexlogger.Logger; import org.onap.policy.rest.adapter.PolicyRestAdapter; +import com.att.research.xacml.api.pap.PAPException; import com.att.research.xacml.std.IdentifierImpl; import oasis.names.tc.xacml._3_0.core.schema.wd_17.AdviceExpressionType; @@ -99,7 +100,7 @@ public class CreateClosedLoopPerformanceMetrics extends Policy { } @Override - public Map<String, String> savePolicies() throws Exception { + public Map<String, String> savePolicies() throws PAPException { Map<String, String> successMap = new HashMap<>(); if(isPolicyExists()){ @@ -124,7 +125,7 @@ public class CreateClosedLoopPerformanceMetrics extends Policy { //This is the method for preparing the policy for saving. We have broken it out //separately because the fully configured policy is used for multiple things @Override - public boolean prepareToSave() throws Exception{ + public boolean prepareToSave() throws PAPException{ if(isPreparedToSave()){ //we have already done this diff --git a/ONAP-PAP-REST/src/main/java/org/onap/policy/pap/xacml/rest/components/DecisionPolicy.java b/ONAP-PAP-REST/src/main/java/org/onap/policy/pap/xacml/rest/components/DecisionPolicy.java index fe9c581e9..6e43c967c 100644 --- a/ONAP-PAP-REST/src/main/java/org/onap/policy/pap/xacml/rest/components/DecisionPolicy.java +++ b/ONAP-PAP-REST/src/main/java/org/onap/policy/pap/xacml/rest/components/DecisionPolicy.java @@ -63,6 +63,7 @@ import org.onap.policy.xacml.api.XACMLErrorConstants; import org.onap.policy.xacml.std.pip.engines.aaf.AAFEngine; import org.onap.policy.xacml.util.XACMLPolicyScanner; +import com.att.research.xacml.api.pap.PAPException; import com.att.research.xacml.std.IdentifierImpl; import oasis.names.tc.xacml._3_0.core.schema.wd_17.AdviceExpressionType; @@ -118,7 +119,7 @@ public class DecisionPolicy extends Policy { } @Override - public Map<String, String> savePolicies() throws Exception { + public Map<String, String> savePolicies() throws PAPException { Map<String, String> successMap = new HashMap<>(); if(isPolicyExists()){ @@ -142,7 +143,7 @@ public class DecisionPolicy extends Policy { //This is the method for preparing the policy for saving. We have broken it out //separately because the fully configured policy is used for multiple things @Override - public boolean prepareToSave() throws Exception{ + public boolean prepareToSave() throws PAPException{ if(isPreparedToSave()){ //we have already done this @@ -176,12 +177,17 @@ public class DecisionPolicy extends Policy { yamlParams.put(ONAPNAME, policyAdapter.getOnapName()); Map<String, String> params = policyAdapter.getDynamicFieldConfigAttributes(); yamlParams.putAll(params); - // Call YAML to XACML - PolicyType decisionPolicy = getGuardPolicy(yamlParams, policyAdapter.getRuleProvider()); - decisionPolicy.setRuleCombiningAlgId(policyAdapter.getRuleCombiningAlgId()); - decisionPolicy.setVersion(Integer.toString(version)); - policyAdapter.setPolicyData(decisionPolicy); - policyAdapter.setData(decisionPolicy); + // Call YAML to XACML + try { + PolicyType decisionPolicy = getGuardPolicy(yamlParams, policyAdapter.getRuleProvider()); + decisionPolicy.setRuleCombiningAlgId(policyAdapter.getRuleCombiningAlgId()); + decisionPolicy.setVersion(Integer.toString(version)); + policyAdapter.setPolicyData(decisionPolicy); + policyAdapter.setData(decisionPolicy); + } catch (BuilderException e) { + LOGGER.error(e); + throw new PAPException(e); + } }else if (policyAdapter.getData() != null) { PolicyType decisionPolicy = (PolicyType) policyAdapter.getData(); diff --git a/ONAP-PAP-REST/src/main/java/org/onap/policy/pap/xacml/rest/components/FirewallConfigPolicy.java b/ONAP-PAP-REST/src/main/java/org/onap/policy/pap/xacml/rest/components/FirewallConfigPolicy.java index 0c8784c81..d95b36709 100644 --- a/ONAP-PAP-REST/src/main/java/org/onap/policy/pap/xacml/rest/components/FirewallConfigPolicy.java +++ b/ONAP-PAP-REST/src/main/java/org/onap/policy/pap/xacml/rest/components/FirewallConfigPolicy.java @@ -62,6 +62,7 @@ import org.onap.policy.rest.jpa.ServiceList; import org.onap.policy.rest.jpa.TermList; import org.onap.policy.rest.jpa.UserInfo; +import com.att.research.xacml.api.pap.PAPException; import com.att.research.xacml.std.IdentifierImpl; import com.fasterxml.jackson.databind.JsonNode; import com.github.fge.jackson.JsonLoader; @@ -129,7 +130,7 @@ public class FirewallConfigPolicy extends Policy { } @Override - public Map<String, String> savePolicies() throws Exception { + public Map<String, String> savePolicies() throws PAPException { Map<String, String> successMap = new HashMap<>(); if(isPolicyExists()){ successMap.put("EXISTS", "This Policy already exist on the PAP"); @@ -147,7 +148,11 @@ public class FirewallConfigPolicy extends Policy { if (policyAdapter.isEditPolicy()) { dbIsUpdated = updateFirewallDictionaryData(policyAdapter.getJsonBody(), policyAdapter.getPrevJsonBody()); } else { - dbIsUpdated = insertFirewallDicionaryData(policyAdapter.getJsonBody()); + try { + dbIsUpdated = insertFirewallDicionaryData(policyAdapter.getJsonBody()); + } catch (SQLException e) { + throw new PAPException(e); + } } } else { dbIsUpdated = true; @@ -174,7 +179,7 @@ public class FirewallConfigPolicy extends Policy { //This is the method for preparing the policy for saving. We have broken it out //separately because the fully configured policy is used for multiple things @Override - public boolean prepareToSave() throws Exception{ + public boolean prepareToSave() throws PAPException{ if(isPreparedToSave()){ //we have already done this diff --git a/ONAP-PAP-REST/src/main/java/org/onap/policy/pap/xacml/rest/components/MicroServiceConfigPolicy.java b/ONAP-PAP-REST/src/main/java/org/onap/policy/pap/xacml/rest/components/MicroServiceConfigPolicy.java index b45d2941a..b0e48a7f4 100644 --- a/ONAP-PAP-REST/src/main/java/org/onap/policy/pap/xacml/rest/components/MicroServiceConfigPolicy.java +++ b/ONAP-PAP-REST/src/main/java/org/onap/policy/pap/xacml/rest/components/MicroServiceConfigPolicy.java @@ -21,6 +21,7 @@ package org.onap.policy.pap.xacml.rest.components; import java.io.File; +import java.io.IOException; import java.io.PrintWriter; import java.net.URI; import java.net.URISyntaxException; @@ -42,6 +43,7 @@ import org.onap.policy.pap.xacml.rest.daoimpl.CommonClassDaoImpl; import org.onap.policy.rest.adapter.PolicyRestAdapter; import org.onap.policy.rest.jpa.MicroServiceModels; +import com.att.research.xacml.api.pap.PAPException; import com.att.research.xacml.std.IdentifierImpl; import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; @@ -100,7 +102,7 @@ public class MicroServiceConfigPolicy extends Policy { @Override - public Map<String, String> savePolicies() throws Exception { + public Map<String, String> savePolicies() throws PAPException { Map<String, String> successMap = new HashMap<>(); if(isPolicyExists()){ @@ -125,7 +127,7 @@ public class MicroServiceConfigPolicy extends Policy { //This is the method for preparing the policy for saving. We have broken it out //separately because the fully configured policy is used for multiple things @Override - public boolean prepareToSave() throws Exception{ + public boolean prepareToSave() throws PAPException{ if(isPreparedToSave()){ //we have already done this @@ -172,26 +174,28 @@ public class MicroServiceConfigPolicy extends Policy { //setup values for pulling out matching attributes ObjectMapper mapper = new ObjectMapper(); - JsonNode rootNode = mapper.readTree(policyAdapter.getJsonBody()); String matching = null; - - if (policyAdapter.getTtlDate()==null){ - policyAdapter.setTtlDate("NA"); - } - if (policyAdapter.getServiceType().contains("-v")){ - matching = getValueFromDictionary(policyAdapter.getServiceType()); - } else { - String jsonVersion = StringUtils.replaceEach(rootNode.get("version").toString(), new String[]{"\""}, new String[]{""}); - matching = getValueFromDictionary(policyAdapter.getServiceType() + "-v" + jsonVersion); - } - Map<String, String> matchMap = null; - if (matching != null && !matching.isEmpty()){ - matchMap = Splitter.on(",").withKeyValueSeparator("=").split(matching); - setMatchMap(matchMap); - if(policyAdapter.getJsonBody() != null){ - pullMatchValue(rootNode); + try { + JsonNode rootNode = mapper.readTree(policyAdapter.getJsonBody()); + if (policyAdapter.getTtlDate()==null){ + policyAdapter.setTtlDate("NA"); + } + if (policyAdapter.getServiceType().contains("-v")){ + matching = getValueFromDictionary(policyAdapter.getServiceType()); + } else { + String jsonVersion = StringUtils.replaceEach(rootNode.get("version").toString(), new String[]{"\""}, new String[]{""}); + matching = getValueFromDictionary(policyAdapter.getServiceType() + "-v" + jsonVersion); + } + if (matching != null && !matching.isEmpty()){ + matchMap = Splitter.on(",").withKeyValueSeparator("=").split(matching); + setMatchMap(matchMap); + if(policyAdapter.getJsonBody() != null){ + pullMatchValue(rootNode); + } } + } catch (IOException e1) { + throw new PAPException(e1); } // Match for policyName diff --git a/ONAP-PAP-REST/src/main/java/org/onap/policy/pap/xacml/rest/components/Policy.java b/ONAP-PAP-REST/src/main/java/org/onap/policy/pap/xacml/rest/components/Policy.java index 130b2e55a..169e6e14e 100644 --- a/ONAP-PAP-REST/src/main/java/org/onap/policy/pap/xacml/rest/components/Policy.java +++ b/ONAP-PAP-REST/src/main/java/org/onap/policy/pap/xacml/rest/components/Policy.java @@ -45,6 +45,7 @@ import org.onap.policy.rest.XACMLRestProperties; import org.onap.policy.rest.adapter.PolicyRestAdapter; import org.onap.policy.xacml.util.XACMLPolicyWriter; +import com.att.research.xacml.api.pap.PAPException; import com.att.research.xacml.std.IdentifierImpl; import com.att.research.xacml.util.XACMLProperties; import com.att.research.xacmlatt.pdp.policy.PolicyDef; @@ -144,11 +145,11 @@ public abstract class Policy { * @return Either the PolicyAdapter.getData() or PolicyAdapter.getPolicyData() */ public abstract Object getCorrectPolicyDataObject(); - public abstract Map<String, String> savePolicies() throws Exception; + public abstract Map<String, String> savePolicies() throws PAPException; //This is the method for preparing the policy for saving. We have broken it out //separately because the fully configured policy is used for multiple things - public abstract boolean prepareToSave() throws Exception; + public abstract boolean prepareToSave() throws PAPException; // create match for onap and config name @@ -213,7 +214,7 @@ public abstract class Policy { protected static boolean isJSONValid(String data) { JsonReader jsonReader = null; try { - JSONObject j = new JSONObject(data); + new JSONObject(data); InputStream stream = new ByteArrayInputStream(data.getBytes(StandardCharsets.UTF_8)); jsonReader = Json.createReader(stream); LOGGER.info("Json Value is: " + jsonReader.read().toString() ); @@ -350,13 +351,13 @@ public abstract class Policy { return actionHome; } - private static void loadWebapps() throws Exception{ + private static void loadWebapps() throws PAPException{ if(actionHome == null || configHome == null){ Path webappsPath = Paths.get(XACMLProperties.getProperty(XACMLRestProperties.PROP_PAP_WEBAPPS)); //Sanity Check if (webappsPath == null) { PolicyLogger.error("Invalid Webapps Path Location property : " + XACMLRestProperties.PROP_PAP_WEBAPPS); - throw new Exception("Invalid Webapps Path Location property : " + XACMLRestProperties.PROP_PAP_WEBAPPS); + throw new PAPException("Invalid Webapps Path Location property : " + XACMLRestProperties.PROP_PAP_WEBAPPS); } Path webappsPathConfig; Path webappsPathAction; @@ -367,14 +368,14 @@ public abstract class Policy { webappsPathConfig = Paths.get(webappsPath.toString()+"/Config"); webappsPathAction = Paths.get(webappsPath.toString()+"/Action"); } - if(Files.notExists(webappsPathConfig)){ + if(!webappsPathConfig.toFile().exists()){ try { Files.createDirectories(webappsPathConfig); } catch (IOException e) { PolicyLogger.error(MessageCodes.ERROR_PROCESS_FLOW, e, "Policy", "Failed to create config directory"); } } - if(Files.notExists(webappsPathAction)){ + if(!webappsPathAction.toFile().exists()){ try { Files.createDirectories(webappsPathAction); } catch (IOException e) { 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 876e87580..bf632f1f1 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 @@ -28,6 +28,7 @@ import java.io.FileWriter; import java.io.IOException; import java.io.InputStream; import java.io.StringReader; +import java.io.UnsupportedEncodingException; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.ProtocolException; @@ -37,7 +38,9 @@ import java.nio.file.Files; import java.nio.file.InvalidPathException; import java.nio.file.Path; import java.nio.file.Paths; +import java.security.InvalidKeyException; import java.security.Key; +import java.security.NoSuchAlgorithmException; import java.util.Base64; import java.util.Date; import java.util.HashMap; @@ -49,7 +52,10 @@ import java.util.Map; import java.util.Set; import java.util.UUID; +import javax.crypto.BadPaddingException; import javax.crypto.Cipher; +import javax.crypto.IllegalBlockSizeException; +import javax.crypto.NoSuchPaddingException; import javax.crypto.spec.SecretKeySpec; import javax.persistence.EntityManager; import javax.persistence.EntityManagerFactory; @@ -71,6 +77,7 @@ import org.onap.policy.common.logging.flexlogger.Logger; import org.onap.policy.pap.xacml.rest.XACMLPapServlet; import org.onap.policy.rest.XACMLRestProperties; import org.onap.policy.rest.adapter.PolicyRestAdapter; +import org.onap.policy.rest.dao.PolicyDBException; import org.onap.policy.rest.jpa.ActionBodyEntity; import org.onap.policy.rest.jpa.ConfigurationDataEntity; import org.onap.policy.rest.jpa.DatabaseLockEntity; @@ -115,7 +122,7 @@ public class PolicyDBDao { * @return The new instance of PolicyDBDao or throw exception if the given emf is null. * @throws IllegalStateException if a PolicyDBDao has already been constructed. Call getPolicyDBDaoInstance() to get this. */ - public static PolicyDBDao getPolicyDBDaoInstance(EntityManagerFactory emf) throws Exception{ + public static PolicyDBDao getPolicyDBDaoInstance(EntityManagerFactory emf){ logger.debug("getPolicyDBDaoInstance(EntityManagerFactory emf) as getPolicyDBDaoInstance("+emf+") called"); if(currentInstance == null){ if(emf != null){ @@ -132,7 +139,7 @@ public class PolicyDBDao { * @return The instance of PolicyDBDao or throws exception if the given instance is null. * @throws IllegalStateException if a PolicyDBDao instance is null. Call createPolicyDBDaoInstance(EntityManagerFactory emf) to get this. */ - public static PolicyDBDao getPolicyDBDaoInstance() throws Exception{ + public static PolicyDBDao getPolicyDBDaoInstance(){ logger.debug("getPolicyDBDaoInstance() as getPolicyDBDaoInstance() called"); if(currentInstance != null){ return currentInstance; @@ -324,15 +331,15 @@ public class PolicyDBDao { return urlUserPass; } - private static String encryptPassword(String password) throws Exception{ + private static String encryptPassword(String password) throws UnsupportedEncodingException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException{ Cipher cipher = Cipher.getInstance("AES"); cipher.init(Cipher.ENCRYPT_MODE, aesKey()); byte[] encryption = cipher.doFinal(password.getBytes("UTF-8")); - System.out.println(encryption); + logger.debug("Encryption value is " + encryption); return new String(Base64.getMimeEncoder().encode(encryption),"UTF-8"); } - private static String decryptPassword(String encryptedPassword) throws Exception{ + private static String decryptPassword(String encryptedPassword) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException, UnsupportedEncodingException{ Cipher cipher = Cipher.getInstance("AES"); cipher.init(Cipher.DECRYPT_MODE, aesKey()); byte[] password = cipher.doFinal(Base64.getDecoder().decode(encryptedPassword.getBytes("UTF-8"))); @@ -700,7 +707,7 @@ public class PolicyDBDao { //no changes should be being made in this function, we still need to close transaction.rollbackTransaction(); } - private void handleIncomingGroupChange(String url, String groupId, String extraData,PolicyDBDaoTransaction transaction,XACMLPapServlet xacmlPapServlet) throws PAPException{ + private void handleIncomingGroupChange(String url, String groupId, String extraData,PolicyDBDaoTransaction transaction,XACMLPapServlet xacmlPapServlet) throws PAPException, PolicyDBException{ GroupEntity groupRecord = null; long groupIdLong = -1; try{ @@ -824,7 +831,7 @@ public class PolicyDBDao { //call command that corresponds to the change that was made } //this will also handle removes, since incoming pdpGroup has no policies internally, we are just going to add them all in from the db - private boolean updateGroupPoliciesInFileSystem(OnapPDPGroup pdpGroup,OnapPDPGroup oldPdpGroup, GroupEntity groupRecord, PolicyDBDaoTransaction transaction) throws PAPException{ + private boolean updateGroupPoliciesInFileSystem(OnapPDPGroup pdpGroup,OnapPDPGroup oldPdpGroup, GroupEntity groupRecord, PolicyDBDaoTransaction transaction) throws PAPException, PolicyDBException{ if(!(pdpGroup instanceof StdPDPGroup)){ throw new PAPException("group is not a StdPDPGroup"); } @@ -862,7 +869,7 @@ public class PolicyDBDao { return didUpdate; } - private String removeExtensionAndVersionFromPolicyName(String originalPolicyName){ + private String removeExtensionAndVersionFromPolicyName(String originalPolicyName) throws PolicyDBException{ return getPolicyNameAndVersionFromPolicyFileName(originalPolicyName)[0]; } @@ -871,14 +878,14 @@ public class PolicyDBDao { * @param originalPolicyName: a policy file name ex: Config_policy.2.xml * @return An array [0]: The policy name, [1]: the policy version, as a string */ - private String[] getPolicyNameAndVersionFromPolicyFileName(String originalPolicyName){ + private String[] getPolicyNameAndVersionFromPolicyFileName(String originalPolicyName) throws PolicyDBException{ String policyName = originalPolicyName; String[] nameAndVersion = new String[2]; try{ policyName = removeFileExtension(policyName); nameAndVersion[0] = policyName.substring(0,policyName.lastIndexOf('.')); if(isNullOrEmpty(nameAndVersion[0])){ - throw new Exception(); + throw new PolicyDBException(); } } catch(Exception e){ nameAndVersion[0] = originalPolicyName; @@ -887,7 +894,7 @@ public class PolicyDBDao { try{ nameAndVersion[1] = policyName.substring(policyName.lastIndexOf('.')+1); if(isNullOrEmpty(nameAndVersion[1])){ - throw new Exception(); + throw new PolicyDBException(); } } catch(Exception e){ nameAndVersion[1] = "1"; @@ -2584,7 +2591,7 @@ public class PolicyDBDao { @Override - public void deleteGroup(OnapPDPGroup group, OnapPDPGroup moveToGroup, String username) throws PAPException { + public void deleteGroup(OnapPDPGroup group, OnapPDPGroup moveToGroup, String username) throws PolicyDBException { logger.debug("deleteGroup(PDPGroup group, PDPGroup moveToGroup, String username) as deleteGroup("+group+", "+moveToGroup+","+username+") called"); if(group == null){ throw new IllegalArgumentException("PDPGroup group cannot be null"); @@ -2595,7 +2602,7 @@ public class PolicyDBDao { if(group.isDefaultGroup()){ PolicyLogger.error("The default group "+group.getId()+" was attempted to be deleted. It cannot be."); - throw new PAPException("You cannot delete the default group."); + throw new PolicyDBException("You cannot delete the default group."); } synchronized(emLock){ checkBeforeOperationRun(); @@ -2665,7 +2672,7 @@ public class PolicyDBDao { } } else { PolicyLogger.error("Group "+group.getId()+" is trying to be delted with PDPs. No group was provided to move them to"); - throw new PAPException("Group has PDPs. Must provide a group for them to move to"); + throw new PolicyDBException("Group has PDPs. Must provide a group for them to move to"); } } @@ -2681,7 +2688,7 @@ public class PolicyDBDao { } @Override - public void addPolicyToGroup(String groupID, String policyID, String username) { + public void addPolicyToGroup(String groupID, String policyID, String username) throws PolicyDBException { logger.debug("addPolicyToGroup(String groupID, String policyID, String username) as addPolicyToGroup("+groupID+", "+policyID+","+username+") called"); if(isNullOrEmpty(groupID, policyID, username)){ throw new IllegalArgumentException("groupID, policyID, and username must not be null or empty"); @@ -2802,16 +2809,16 @@ public class PolicyDBDao { String computeScope(String fullPath, String pathToExclude){ return PolicyDBDao.computeScope(fullPath, pathToExclude); } - String encryptPassword(String password) throws Exception{ + String encryptPassword(String password) throws InvalidKeyException, UnsupportedEncodingException, NoSuchAlgorithmException, NoSuchPaddingException, IllegalBlockSizeException, BadPaddingException{ return PolicyDBDao.encryptPassword(password); } - String decryptPassword(String password) throws Exception{ + String decryptPassword(String password) throws InvalidKeyException, NoSuchAlgorithmException, NoSuchPaddingException, IllegalBlockSizeException, BadPaddingException, UnsupportedEncodingException{ return PolicyDBDao.decryptPassword(password); } String getDescriptionFromXacml(String xacmlData){ return PolicyDBDao.getDescriptionFromXacml(xacmlData); } - String[] getPolicyNameAndVersionFromPolicyFileName(String originalPolicyName){ + String[] getPolicyNameAndVersionFromPolicyFileName(String originalPolicyName) throws PolicyDBException{ return PolicyDBDao.this.getPolicyNameAndVersionFromPolicyFileName(originalPolicyName); } } diff --git a/ONAP-PAP-REST/src/main/java/org/onap/policy/pap/xacml/rest/components/PolicyDBDaoTransaction.java b/ONAP-PAP-REST/src/main/java/org/onap/policy/pap/xacml/rest/components/PolicyDBDaoTransaction.java index f5a22bcd4..dfe5c70f3 100644 --- a/ONAP-PAP-REST/src/main/java/org/onap/policy/pap/xacml/rest/components/PolicyDBDaoTransaction.java +++ b/ONAP-PAP-REST/src/main/java/org/onap/policy/pap/xacml/rest/components/PolicyDBDaoTransaction.java @@ -24,6 +24,7 @@ import java.util.List; import javax.persistence.PersistenceException; +import org.onap.policy.rest.dao.PolicyDBException; import org.onap.policy.rest.jpa.GroupEntity; import org.onap.policy.rest.jpa.PdpEntity; import org.onap.policy.xacml.api.pap.OnapPDP; @@ -48,7 +49,7 @@ public interface PolicyDBDaoTransaction { * @throws PersistenceException If a database error occurs * @throws IllegalArgumentException If the Policy's PolicyRestAdapter contains incorrect data. */ - public void createPolicy(Policy policy, String username) throws IllegalStateException, PersistenceException, IllegalArgumentException; + public void createPolicy(Policy policy, String username) throws PolicyDBException; /** * Check if the PolicyDBDaoTransaction is currently open @@ -63,9 +64,9 @@ public interface PolicyDBDaoTransaction { * @param policyToDelete The file path of the policy to delete * @throws IllegalArgumentException If the file path given can not be parsed * @throws IllegalStateException If a transaction is open that has not yet been committed - * @throws PersistenceException If a database error occurs + * @throws PolicyDBException If a database error occurs */ - public void deletePolicy(String policyToDelete) throws IllegalStateException, PersistenceException, IllegalArgumentException; + public void deletePolicy(String policyToDelete) throws PolicyDBException; /** * Rollback (undo) the current transaction. @@ -88,7 +89,7 @@ public interface PolicyDBDaoTransaction { * @throws IllegalStateException If a transaction is already open * @throws PersistenceException If a database error occurs */ - public void createGroup(String groupID, String groupName, String groupDescription, String username) throws IllegalArgumentException, IllegalStateException, PersistenceException; + public void createGroup(String groupID, String groupName, String groupDescription, String username) throws PolicyDBException; /** * Updates a group in the database with a new name of description @@ -98,7 +99,7 @@ public interface PolicyDBDaoTransaction { * @throws IllegalStateException If a transaction is already open * @throws PersistenceException If a database error occurs or if the group can not be found */ - public void updateGroup(OnapPDPGroup group, String username) throws IllegalArgumentException, IllegalStateException, PersistenceException; + public void updateGroup(OnapPDPGroup group, String username) throws PolicyDBException; /** * Updates a PDP in the database with new information @@ -108,7 +109,7 @@ public interface PolicyDBDaoTransaction { * @throws IllegalStateException If a transaction is already open * @throws PersistenceException If a database error occurs or if the pdp can not be found */ - public void updatePdp(OnapPDP pdp, String username) throws IllegalArgumentException, IllegalStateException, PersistenceException; + public void updatePdp(OnapPDP pdp, String username) throws PolicyDBException; /** * Change the default group in the database to the group provided. @@ -118,7 +119,7 @@ public interface PolicyDBDaoTransaction { * @throws IllegalStateException If a transaction is already open * @throws PersistenceException If a database error occurs */ - public void changeDefaultGroup(OnapPDPGroup group, String username) throws IllegalArgumentException, IllegalStateException, PersistenceException; + public void changeDefaultGroup(OnapPDPGroup group, String username) throws PolicyDBException; /** * Moves a PDP to a new group. @@ -129,7 +130,7 @@ public interface PolicyDBDaoTransaction { * @throws IllegalStateException If a transaction is already open * @throws PersistenceException If a database error occurs */ - public void movePdp(OnapPDP pdp, OnapPDPGroup group, String username) throws IllegalArgumentException, IllegalStateException, PersistenceException; + public void movePdp(OnapPDP pdp, OnapPDPGroup group, String username) throws PolicyDBException; /** * Add a new PDP to an existing group @@ -143,7 +144,7 @@ public interface PolicyDBDaoTransaction { * @throws IllegalStateException If a transaction is already open * @throws PersistenceException If a database error occurs */ - public void addPdpToGroup(String pdpID, String groupID, String pdpName, String pdpDescription, int pdpJmxPort, String username) throws IllegalArgumentException, IllegalStateException, PersistenceException; + public void addPdpToGroup(String pdpID, String groupID, String pdpName, String pdpDescription, int pdpJmxPort, String username) throws PolicyDBException; /** * Add an existing policy to an existing group @@ -152,12 +153,13 @@ public interface PolicyDBDaoTransaction { * @throws IllegalArgumentException If non-optional parameters are null or empty strings * @throws IllegalStateException If a transaction is already open * @throws PersistenceException If a database error occurs + * @throws PolicyDBException */ - public void addPolicyToGroup(String group, String policyID, String username) throws IllegalArgumentException, IllegalStateException, PersistenceException; + public void addPolicyToGroup(String group, String policyID, String username) throws PolicyDBException; /** - * Delete an existing PDP group + * Delete an existing PDP groupPolicyDBException * @param group A PDPGroup object representing the group to delete * @param moveToGroup A PDPGroup object representing another existing group which PDPs in the group being deleted should be moved to * @throws IllegalArgumentException If non-optional parameters are null or empty strings @@ -165,7 +167,7 @@ public interface PolicyDBDaoTransaction { * @throws PersistenceException If a database error occurs * @throws PAPException If an error relating to how groups are handled occurs */ - public void deleteGroup(OnapPDPGroup group, OnapPDPGroup moveToGroup, String username)throws IllegalArgumentException, IllegalStateException, PersistenceException, PAPException; + public void deleteGroup(OnapPDPGroup group, OnapPDPGroup moveToGroup, String username)throws PolicyDBException; /** * Removes an existing PDP from its group and deletes it. @@ -174,7 +176,7 @@ public interface PolicyDBDaoTransaction { * @throws IllegalStateException If a transaction is already open * @throws PersistenceException If a database error occurs */ - public void removePdpFromGroup(String pdpID, String username) throws IllegalArgumentException, IllegalStateException, PersistenceException; + public void removePdpFromGroup(String pdpID, String username) throws PolicyDBException; public GroupEntity getGroup(long groupKey); public GroupEntity getGroup(String groupId); diff --git a/ONAP-PAP-REST/src/main/java/org/onap/policy/pap/xacml/rest/controller/ActionPolicyDictionaryController.java b/ONAP-PAP-REST/src/main/java/org/onap/policy/pap/xacml/rest/controller/ActionPolicyDictionaryController.java index 75b02ab58..b5a3b45e8 100644 --- a/ONAP-PAP-REST/src/main/java/org/onap/policy/pap/xacml/rest/controller/ActionPolicyDictionaryController.java +++ b/ONAP-PAP-REST/src/main/java/org/onap/policy/pap/xacml/rest/controller/ActionPolicyDictionaryController.java @@ -22,7 +22,6 @@ package org.onap.policy.pap.xacml.rest.controller; import java.io.IOException; import java.io.PrintWriter; -import java.util.ArrayList; import java.util.Date; import java.util.HashMap; import java.util.LinkedHashMap; diff --git a/ONAP-PAP-REST/src/main/java/org/onap/policy/pap/xacml/rest/controller/DictionaryController.java b/ONAP-PAP-REST/src/main/java/org/onap/policy/pap/xacml/rest/controller/DictionaryController.java index 1fef35459..85e3944fd 100644 --- a/ONAP-PAP-REST/src/main/java/org/onap/policy/pap/xacml/rest/controller/DictionaryController.java +++ b/ONAP-PAP-REST/src/main/java/org/onap/policy/pap/xacml/rest/controller/DictionaryController.java @@ -22,7 +22,6 @@ package org.onap.policy.pap.xacml.rest.controller; import java.io.IOException; import java.io.PrintWriter; -import java.util.ArrayList; import java.util.Date; import java.util.HashMap; import java.util.LinkedHashMap; diff --git a/ONAP-PAP-REST/src/main/java/org/onap/policy/pap/xacml/rest/controller/DictionaryImportController.java b/ONAP-PAP-REST/src/main/java/org/onap/policy/pap/xacml/rest/controller/DictionaryImportController.java index 62b830d6d..aee05da45 100644 --- a/ONAP-PAP-REST/src/main/java/org/onap/policy/pap/xacml/rest/controller/DictionaryImportController.java +++ b/ONAP-PAP-REST/src/main/java/org/onap/policy/pap/xacml/rest/controller/DictionaryImportController.java @@ -28,11 +28,9 @@ import java.io.FileOutputStream; import java.io.FileReader; import java.io.IOException; import java.io.OutputStream; -import java.io.UnsupportedEncodingException; import java.util.List; import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; import org.apache.commons.compress.utils.IOUtils; import org.onap.policy.rest.dao.CommonClassDao; @@ -45,8 +43,8 @@ import org.onap.policy.rest.jpa.Category; import org.onap.policy.rest.jpa.Datatype; import org.onap.policy.rest.jpa.DecisionSettings; import org.onap.policy.rest.jpa.DescriptiveScope; -import org.onap.policy.rest.jpa.OnapName; import org.onap.policy.rest.jpa.GroupServiceList; +import org.onap.policy.rest.jpa.OnapName; import org.onap.policy.rest.jpa.PEPOptions; import org.onap.policy.rest.jpa.PrefixList; import org.onap.policy.rest.jpa.ProtocolList; diff --git a/ONAP-PAP-REST/src/main/java/org/onap/policy/pap/xacml/rest/elk/client/PolicyElasticSearchController.java b/ONAP-PAP-REST/src/main/java/org/onap/policy/pap/xacml/rest/elk/client/PolicyElasticSearchController.java index e9c4af27e..97da76708 100644 --- a/ONAP-PAP-REST/src/main/java/org/onap/policy/pap/xacml/rest/elk/client/PolicyElasticSearchController.java +++ b/ONAP-PAP-REST/src/main/java/org/onap/policy/pap/xacml/rest/elk/client/PolicyElasticSearchController.java @@ -97,7 +97,7 @@ public class PolicyElasticSearchController{ private static final long serialVersionUID = 1L; }; - public static CommonClassDao commonClassDao; + private static CommonClassDao commonClassDao; @Autowired public PolicyElasticSearchController(CommonClassDao commonClassDao) { diff --git a/ONAP-PAP-REST/src/main/java/org/onap/policy/pap/xacml/rest/model/PDPPolicyContainer.java b/ONAP-PAP-REST/src/main/java/org/onap/policy/pap/xacml/rest/model/PDPPolicyContainer.java index 18f0636a8..23aa761be 100644 --- a/ONAP-PAP-REST/src/main/java/org/onap/policy/pap/xacml/rest/model/PDPPolicyContainer.java +++ b/ONAP-PAP-REST/src/main/java/org/onap/policy/pap/xacml/rest/model/PDPPolicyContainer.java @@ -71,23 +71,23 @@ public class PDPPolicyContainer extends PolicyItemSetChangeNotifier implements P /** * List of the string identifiers for the available properties. */ - public static Collection<String> PDPPOLICY_PROPERTIES; + private static Collection<String> pdpPolicyProperties; - private final Object data; - private List<PDPPolicy> policies; + private final transient Object data; + private transient List<PDPPolicy> policies; @SuppressWarnings("unchecked") public PDPPolicyContainer(Object data) { super(); this.data = data; if (this.data instanceof PDPGroup) { - policies = new ArrayList<PDPPolicy> (((PDPGroup) this.data).getPolicies()); + policies = new ArrayList<> (((PDPGroup) this.data).getPolicies()); } if (this.data instanceof PDP) { - policies = new ArrayList<PDPPolicy> (((PDP) this.data).getPolicies()); + policies = new ArrayList<> (((PDP) this.data).getPolicies()); } if (this.data instanceof Set) { - policies = new ArrayList<PDPPolicy> ((Set<PDPPolicy>)data); + policies = new ArrayList<> ((Set<PDPPolicy>)data); } if (this.policies == null) { logger.info("NULL policies"); @@ -165,14 +165,13 @@ public class PDPPolicyContainer extends PolicyItemSetChangeNotifier implements P } @Override - public Object addItemAfter(Object previousItemId) - throws UnsupportedOperationException { + public Object addItemAfter(Object previousItemId){ return null; } @Override public Collection<?> getContainerPropertyIds() { - return PDPPOLICY_PROPERTIES; + return pdpPolicyProperties; } @Override @@ -220,13 +219,12 @@ public class PDPPolicyContainer extends PolicyItemSetChangeNotifier implements P } @Override - public Object addItem() throws UnsupportedOperationException { + public Object addItem(){ throw new UnsupportedOperationException("Cannot add an empty policy."); } @Override - public boolean removeItem(Object itemId) - throws UnsupportedOperationException { + public boolean removeItem(Object itemId){ if (logger.isTraceEnabled()) { logger.trace("removeItem: " + itemId); } @@ -248,18 +246,17 @@ public class PDPPolicyContainer extends PolicyItemSetChangeNotifier implements P @Override public boolean addContainerProperty(Object propertyId, Class<?> type, - Object defaultValue) throws UnsupportedOperationException { + Object defaultValue){ return false; } @Override - public boolean removeContainerProperty(Object propertyId) - throws UnsupportedOperationException { + public boolean removeContainerProperty(Object propertyId){ return false; } @Override - public boolean removeAllItems() throws UnsupportedOperationException { + public boolean removeAllItems(){ return false; } @@ -291,7 +288,7 @@ public class PDPPolicyContainer extends PolicyItemSetChangeNotifier implements P } @Override - public Object addItemAt(int index) throws UnsupportedOperationException { + public Object addItemAt(int index) { if (logger.isTraceEnabled()) { logger.trace("addItemAt: " + index); } diff --git a/ONAP-PAP-REST/src/main/java/org/onap/policy/pap/xacml/rest/model/RemoveGroupPolicy.java b/ONAP-PAP-REST/src/main/java/org/onap/policy/pap/xacml/rest/model/RemoveGroupPolicy.java index 7645535b0..d05a1ab6e 100644 --- a/ONAP-PAP-REST/src/main/java/org/onap/policy/pap/xacml/rest/model/RemoveGroupPolicy.java +++ b/ONAP-PAP-REST/src/main/java/org/onap/policy/pap/xacml/rest/model/RemoveGroupPolicy.java @@ -35,7 +35,7 @@ public class RemoveGroupPolicy { //Container from where we are fetching the policies private static PDPPolicyContainer policyContainer; - private final RemoveGroupPolicy self = this; + private RemoveGroupPolicy self = this; private StdPDPGroup updatedObject; private final StdPDPGroup group; private boolean isSaved = false; @@ -51,8 +51,7 @@ public class RemoveGroupPolicy { if (this.group == null) { return; } - - RemoveGroupPolicy.policyContainer = new PDPPolicyContainer(group); + setRemoveGroupPolicy(new PDPPolicyContainer(group)); RemoveGroupPolicy.policyContainer.removeItem(policy); @@ -62,13 +61,16 @@ public class RemoveGroupPolicy { } - @SuppressWarnings("unchecked") + private static void setRemoveGroupPolicy(PDPPolicyContainer pdpPolicyContainer) { + RemoveGroupPolicy.policyContainer = pdpPolicyContainer; + } + + @SuppressWarnings("unchecked") protected void doSave() { if (this.group == null) { return; } - //StdPDPGroup pdpGroup = (StdPDPGroup) group; StdPDPGroup updatedGroupObject = new StdPDPGroup( group.getId(), group.isDefaultGroup(), diff --git a/ONAP-PAP-REST/src/main/java/org/onap/policy/pap/xacml/rest/policycontroller/PolicyCreation.java b/ONAP-PAP-REST/src/main/java/org/onap/policy/pap/xacml/rest/policycontroller/PolicyCreation.java index eff4e7f2f..d989470a2 100644 --- a/ONAP-PAP-REST/src/main/java/org/onap/policy/pap/xacml/rest/policycontroller/PolicyCreation.java +++ b/ONAP-PAP-REST/src/main/java/org/onap/policy/pap/xacml/rest/policycontroller/PolicyCreation.java @@ -99,7 +99,7 @@ public class PolicyCreation extends AbstractPolicyCreation{ @RequestMapping(value="/policycreation/save_policy", method = RequestMethod.POST) @ResponseBody - public ResponseEntity<String> savePolicy(@RequestBody PolicyRestAdapter policyData, HttpServletResponse response) throws Exception{ + public ResponseEntity<String> savePolicy(@RequestBody PolicyRestAdapter policyData, HttpServletResponse response){ String body = null; HttpStatus status = HttpStatus.BAD_REQUEST; Map<String, String> successMap = new HashMap<>(); diff --git a/ONAP-PAP-REST/src/main/java/org/onap/policy/pap/xacml/rest/service/MetricService.java b/ONAP-PAP-REST/src/main/java/org/onap/policy/pap/xacml/rest/service/MetricService.java index e33f8d5ba..3fff9f266 100644 --- a/ONAP-PAP-REST/src/main/java/org/onap/policy/pap/xacml/rest/service/MetricService.java +++ b/ONAP-PAP-REST/src/main/java/org/onap/policy/pap/xacml/rest/service/MetricService.java @@ -23,7 +23,6 @@ import java.util.HashSet; import java.util.Set; import javax.persistence.EntityManager; -import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.json.JSONObject; diff --git a/ONAP-PAP-REST/src/main/java/org/onap/policy/pap/xacml/restAuth/CheckPDP.java b/ONAP-PAP-REST/src/main/java/org/onap/policy/pap/xacml/restAuth/CheckPDP.java index 02ec467ed..335b94f51 100644 --- a/ONAP-PAP-REST/src/main/java/org/onap/policy/pap/xacml/restAuth/CheckPDP.java +++ b/ONAP-PAP-REST/src/main/java/org/onap/policy/pap/xacml/restAuth/CheckPDP.java @@ -43,6 +43,8 @@ import org.onap.policy.common.logging.flexlogger.Logger; import org.onap.policy.pap.xacml.rest.XACMLPapServlet; import org.onap.policy.xacml.api.XACMLErrorConstants; +import com.att.research.xacml.api.pap.PAPException; + public class CheckPDP { private static Path pdpPath = null; @@ -67,23 +69,23 @@ public class CheckPDP { return false; } - private static void readFile() throws Exception { + private static void readFile() throws PAPException { String pdpFile = XACMLPapServlet.getPDPFile(); if (pdpFile == null) { PolicyLogger.error(MessageCodes.ERROR_SYSTEM_ERROR + "PDP File name is undefined"); - throw new Exception(XACMLErrorConstants.ERROR_SYSTEM_ERROR +"PDP File name not Valid : " + pdpFile); + throw new PAPException(XACMLErrorConstants.ERROR_SYSTEM_ERROR +"PDP File name not Valid : " + pdpFile); } if (pdpPath == null) { pdpPath = Paths.get(pdpFile); if (Files.notExists(pdpPath)) { PolicyLogger.error(MessageCodes.ERROR_SYSTEM_ERROR + "File doesn't exist in the specified Path"); - throw new Exception(XACMLErrorConstants.ERROR_SYSTEM_ERROR +"File doesn't exist in the specified Path : "+ pdpPath.toString()); + throw new PAPException(XACMLErrorConstants.ERROR_SYSTEM_ERROR +"File doesn't exist in the specified Path : "+ pdpPath.toString()); } if (pdpPath.toString().endsWith(".properties")) { readProps(); } else { PolicyLogger.error(MessageCodes.ERROR_SYSTEM_ERROR + "Not a .properties file"); - throw new Exception(XACMLErrorConstants.ERROR_SYSTEM_ERROR +"Not a .properties file"); + throw new PAPException(XACMLErrorConstants.ERROR_SYSTEM_ERROR +"Not a .properties file"); } } // Check if File is updated recently @@ -97,7 +99,7 @@ public class CheckPDP { } @SuppressWarnings({ "rawtypes", "unchecked" }) - private static void readProps() throws Exception { + private static void readProps() throws PAPException { InputStream in; pdpProp = new Properties(); try { @@ -106,7 +108,7 @@ public class CheckPDP { pdpProp.load(in); } catch (IOException e) { PolicyLogger.error(MessageCodes.ERROR_SYSTEM_ERROR, e, "CheckPDP", "Cannot load the Properties file"); - throw new Exception("Cannot Load the Properties file", e); + throw new PAPException("Cannot Load the Properties file", e); } // Read the Properties and Load the PDPs and encoding. pdpMap = new HashMap<>(); @@ -118,7 +120,7 @@ public class CheckPDP { if (propKey.startsWith("PDP_URL")) { String check_val = pdpProp.getProperty(propKey); if (check_val == null) { - throw new Exception("Properties file doesn't have the PDP_URL parameter"); + throw new PAPException("Properties file doesn't have the PDP_URL parameter"); } if (check_val.contains(";")) { List<String> pdp_default = new ArrayList<>(Arrays.asList(check_val.split("\\s*;\\s*"))); @@ -135,11 +137,11 @@ public class CheckPDP { } if (pdpMap == null || pdpMap.isEmpty()) { logger.debug(XACMLErrorConstants.ERROR_SYSTEM_ERROR + "Cannot Proceed without PDP_URLs"); - throw new Exception(XACMLErrorConstants.ERROR_SYSTEM_ERROR +"Cannot Proceed without PDP_URLs"); + throw new PAPException(XACMLErrorConstants.ERROR_SYSTEM_ERROR +"Cannot Proceed without PDP_URLs"); } } - private static void readPDPParam(String pdpVal) throws Exception{ + private static void readPDPParam(String pdpVal) throws PAPException{ if(pdpVal.contains(",")){ List<String> pdpValues = new ArrayList<>(Arrays.asList(pdpVal.split("\\s*,\\s*"))); if(pdpValues.size()==3){ @@ -151,11 +153,11 @@ public class CheckPDP { pdpMap.put(pdpValues.get(0), encoder.encodeToString((userID+":"+pass).getBytes(StandardCharsets.UTF_8))); }else{ PolicyLogger.error(MessageCodes.ERROR_PERMISSIONS + "No Credentials to send Request"); - throw new Exception(XACMLErrorConstants.ERROR_PERMISSIONS + "No enough Credentials to send Request. " + pdpValues); + throw new PAPException(XACMLErrorConstants.ERROR_PERMISSIONS + "No enough Credentials to send Request. " + pdpValues); } }else{ PolicyLogger.error(MessageCodes.ERROR_PERMISSIONS + "No Credentials to send Request: " + pdpVal); - throw new Exception(XACMLErrorConstants.ERROR_PERMISSIONS +"No enough Credentials to send Request."); + throw new PAPException(XACMLErrorConstants.ERROR_PERMISSIONS +"No enough Credentials to send Request."); } } |