\r
package org.onap.policy.api.main.rest.provider;\r
\r
-import org.onap.policy.models.tosca.concepts.ToscaServiceTemplate;\r
+import java.util.List;\r
+import javax.ws.rs.core.Response;\r
+import org.onap.policy.models.base.PfModelException;\r
+import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;\r
+import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyFilter;\r
+import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyTypeFilter;\r
+import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;\r
\r
/**\r
* Class to provide all kinds of policy type operations.\r
+ *\r
+ * @author Chenfei Gao (cgao@research.att.com)\r
*/\r
-public class PolicyTypeProvider {\r
+public class PolicyTypeProvider extends CommonModelProvider {\r
\r
- private static final String POST_OK = "Successfully created";\r
- private static final String DELETE_OK = "Successfully deleted";\r
+ /**\r
+ * Default constructor.\r
+ */\r
+ public PolicyTypeProvider() throws PfModelException {\r
+ super();\r
+ }\r
\r
/**\r
* Retrieves a list of policy types matching specified policy type ID and version.\r
* @param policyTypeId the ID of policy type\r
* @param policyTypeVersion the version of policy type\r
*\r
- * @return the ToscaPolicyTypeList object containing a list of policy types matching specified fields\r
+ * @return the ToscaServiceTemplate object\r
+ *\r
+ * @throws PfModelException the PfModel parsing exception\r
*/\r
- public ToscaServiceTemplate fetchPolicyTypes(String policyTypeId, String policyTypeVersion) {\r
- // placeholder\r
- // something like return new PolicyModelProvider().getPolicyTypes(<blah>);\r
- return new ToscaServiceTemplate();\r
+ public ToscaServiceTemplate fetchPolicyTypes(String policyTypeId, String policyTypeVersion)\r
+ throws PfModelException {\r
+\r
+ ToscaPolicyTypeFilter policyTypeFilter = ToscaPolicyTypeFilter.builder()\r
+ .name(policyTypeId).version(policyTypeVersion).build();\r
+ ToscaServiceTemplate serviceTemplate = modelsProvider.getFilteredPolicyTypes(policyTypeFilter);\r
+\r
+ if (policyTypeId != null && !hasPolicyType(serviceTemplate)) {\r
+ throw new PfModelException(Response.Status.NOT_FOUND,\r
+ constructResourceNotFoundMessage(policyTypeId, policyTypeVersion));\r
+ }\r
+\r
+ return serviceTemplate;\r
+ }\r
+\r
+ /**\r
+ * Retrieves a list of policy types with the latest versions.\r
+ *\r
+ * @param policyTypeId the ID of policy type\r
+ *\r
+ * @return the ToscaServiceTemplate object\r
+ *\r
+ * @throws PfModelException the PfModel parsing exception\r
+ */\r
+ public ToscaServiceTemplate fetchLatestPolicyTypes(String policyTypeId) throws PfModelException {\r
+\r
+ ToscaPolicyTypeFilter policyTypeFilter = ToscaPolicyTypeFilter.builder()\r
+ .name(policyTypeId).version(ToscaPolicyTypeFilter.LATEST_VERSION).build();\r
+ ToscaServiceTemplate serviceTemplate = modelsProvider.getFilteredPolicyTypes(policyTypeFilter);\r
+ if (!hasPolicyType(serviceTemplate)) {\r
+ throw new PfModelException(Response.Status.NOT_FOUND,\r
+ constructResourceNotFoundMessage(policyTypeId, null));\r
+ }\r
+\r
+ return serviceTemplate;\r
}\r
\r
/**\r
*\r
* @param body the entity body of policy type\r
*\r
- * @return a string message indicating the operation results\r
+ * @return the ToscaServiceTemplate object\r
+ * @throws PfModelException the PfModel parsing exception\r
+ */\r
+ public ToscaServiceTemplate createPolicyType(ToscaServiceTemplate body) throws PfModelException {\r
+\r
+ return modelsProvider.createPolicyTypes(body);\r
+ }\r
+\r
+ /**\r
+ * Delete the policy type matching specified policy type ID and version.\r
+ *\r
+ * @param policyTypeId the ID of policy type\r
+ * @param policyTypeVersion the version of policy type\r
+ *\r
+ * @return the ToscaServiceTemplate object\r
+ *\r
+ * @throws PfModelException the PfModel parsing exception\r
*/\r
- public String createPolicyType(ToscaServiceTemplate body) {\r
- // placeholder\r
- // something like return new PolicyModelProvider().createPolicyTypes(<blah>);\r
- return POST_OK;\r
+ public ToscaServiceTemplate deletePolicyType(String policyTypeId, String policyTypeVersion)\r
+ throws PfModelException {\r
+\r
+ validateDeleteEligibility(policyTypeId, policyTypeVersion);\r
+\r
+ ToscaServiceTemplate serviceTemplate = modelsProvider.deletePolicyType(policyTypeId, policyTypeVersion);\r
+\r
+ if (!hasPolicyType(serviceTemplate)) {\r
+ throw new PfModelException(Response.Status.NOT_FOUND,\r
+ constructResourceNotFoundMessage(policyTypeId, policyTypeVersion));\r
+ }\r
+\r
+ return serviceTemplate;\r
}\r
\r
/**\r
- * Delete the policy types matching specified policy type ID and version.\r
+ * Validates whether specified policy type can be deleted based on the rule that\r
+ * policy type parameterized by at least one policies cannot be deleted.\r
*\r
* @param policyTypeId the ID of policy type\r
* @param policyTypeVersion the version of policy type\r
*\r
- * @return a string message indicating the operation results\r
+ * @throws PfModelException the PfModel parsing exception\r
*/\r
- public String deletePolicyTypes(String policyTypeId, String policyTypeVersion) {\r
- // placeholder\r
- // something like return new PolicyModelProvider().deletePolicyTypes(<blah>);\r
- return DELETE_OK;\r
+ private void validateDeleteEligibility(String policyTypeId, String policyTypeVersion) throws PfModelException {\r
+\r
+ ToscaPolicyFilter policyFilter = ToscaPolicyFilter.builder()\r
+ .type(policyTypeId).typeVersion(policyTypeVersion).build();\r
+ List<ToscaPolicy> policies = modelsProvider.getFilteredPolicyList(policyFilter);\r
+ if (!policies.isEmpty()) {\r
+ throw new PfModelException(Response.Status.CONFLICT,\r
+ constructDeletePolicyTypeViolationMessage(policyTypeId, policyTypeVersion, policies));\r
+ }\r
+ }\r
+\r
+ /**\r
+ * Constructs returned message for not found resource.\r
+ *\r
+ * @param policyTypeId the ID of policy type\r
+ * @param policyTypeVersion the version of policy type\r
+ *\r
+ * @return constructed message\r
+ */\r
+ private String constructResourceNotFoundMessage(String policyTypeId, String policyTypeVersion) {\r
+\r
+ return "policy type with ID " + policyTypeId + ":" + policyTypeVersion + " does not exist";\r
}\r
}\r