-/*-
- * ============LICENSE_START=======================================================
- * ONAP Policy API
- * ================================================================================
- * Copyright (C) 2019 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.
- *
- * SPDX-License-Identifier: Apache-2.0
- * ============LICENSE_END=========================================================
- */
-
-package org.onap.policy.api.main.rest.provider;
-
-import org.onap.policy.models.tosca.ToscaPolicyType;
-import org.onap.policy.models.tosca.ToscaPolicyTypeList;
-
-/**
- * Class to provide all kinds of policy type operations.
- */
-public class PolicyTypeProvider {
-
- private static final String POST_OK = "Successfully created";
- private static final String DELETE_OK = "Successfully deleted";
-
- /**
- * Retrieves a list of policy types matching specified policy type ID and version.
- *
- * @param policyTypeId the ID of policy type
- * @param policyTypeVersion the version of policy type
- *
- * @return the ToscaPolicyTypeList object containing a list of policy types matching specified fields
- */
- public ToscaPolicyTypeList fetchPolicyTypes(String policyTypeId, String policyTypeVersion) {
- // placeholder
- return new ToscaPolicyTypeList();
- }
-
- /**
- * Creates a new policy type.
- *
- * @param body the entity body of policy type
- *
- * @return a string message indicating the operation results
- */
- public String createPolicyType(ToscaPolicyType body) {
- // placeholder
- return POST_OK;
- }
-
- /**
- * Delete the policy types matching specified policy type ID and version.
- *
- * @param policyTypeId the ID of policy type
- * @param policyTypeVersion the version of policy type
- *
- * @return a string message indicating the operation results
- */
- public String deletePolicyTypes(String policyTypeId, String policyTypeVersion) {
- // placeholder
- return DELETE_OK;
- }
-}
+/*-\r
+ * ============LICENSE_START=======================================================\r
+ * ONAP Policy API\r
+ * ================================================================================\r
+ * Copyright (C) 2019-2020 AT&T Intellectual Property. All rights reserved.\r
+ * ================================================================================\r
+ * Licensed under the Apache License, Version 2.0 (the "License");\r
+ * you may not use this file except in compliance with the License.\r
+ * You may obtain a copy of the License at\r
+ *\r
+ * http://www.apache.org/licenses/LICENSE-2.0\r
+ *\r
+ * Unless required by applicable law or agreed to in writing, software\r
+ * distributed under the License is distributed on an "AS IS" BASIS,\r
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ * See the License for the specific language governing permissions and\r
+ * limitations under the License.\r
+ *\r
+ * SPDX-License-Identifier: Apache-2.0\r
+ * ============LICENSE_END=========================================================\r
+ */\r
+\r
+package org.onap.policy.api.main.rest.provider;\r
+\r
+import java.util.ArrayList;\r
+import java.util.HashMap;\r
+import java.util.List;\r
+import java.util.Map;\r
+import java.util.Map.Entry;\r
+import javax.ws.rs.core.Response;\r
+\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.ToscaPolicyType;\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 extends CommonModelProvider {\r
+\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
+ *\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 ToscaServiceTemplate fetchPolicyTypes(String policyTypeId, String policyTypeVersion)\r
+ throws PfModelException {\r
+\r
+ ToscaServiceTemplate serviceTemplate = getFilteredPolicyTypes(policyTypeId, policyTypeVersion);\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
+ ToscaServiceTemplate serviceTemplate =\r
+ getFilteredPolicyTypes(policyTypeId, ToscaPolicyTypeFilter.LATEST_VERSION);\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
+ * Creates a new policy type.\r
+ *\r
+ * @param body the entity body of policy type\r
+ *\r
+ * @return the ToscaServiceTemplate object\r
+ * @throws PfModelException the PfModel parsing exception\r
+ */\r
+ public ToscaServiceTemplate createPolicyType(ToscaServiceTemplate body) throws PfModelException {\r
+\r
+ if (!hasPolicyType(body)) {\r
+ throw new PfModelException(Response.Status.BAD_REQUEST,\r
+ "no policy types specified in the service template");\r
+ }\r
+ validatePolicyTypeVersion(body);\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 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
+ * 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
+ * @throws PfModelException the PfModel parsing exception\r
+ */\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
+ * Validates the provided policy type version in the payload.\r
+ *\r
+ * @param body the provided TOSCA service template which contains the policy types\r
+ *\r
+ * @throws PfModelException the PfModel parsing exception\r
+ */\r
+ private void validatePolicyTypeVersion(ToscaServiceTemplate body) throws PfModelException {\r
+\r
+ validatePolicyTypeVersionExist(body);\r
+ validateNoDuplicateVersionInDb(body);\r
+ }\r
+\r
+ /**\r
+ * Validates that each policy type has a version specified in the payload.\r
+ *\r
+ * @param body the TOSCA service template payload to check against\r
+ *\r
+ * @throws PfModelException the PfModel parsing exception\r
+ */\r
+ private void validatePolicyTypeVersionExist(ToscaServiceTemplate body) throws PfModelException {\r
+\r
+ List<String> invalidPolicyTypeNames = new ArrayList<>();\r
+ for (Entry<String, ToscaPolicyType> policyType: body.getPolicyTypes().entrySet()) {\r
+ if (!"tosca.policies.Root".equals(policyType.getValue().getDerivedFrom())\r
+ && policyType.getValue().getVersion() == null) {\r
+ invalidPolicyTypeNames.add(policyType.getKey());\r
+ }\r
+ }\r
+\r
+ if (!invalidPolicyTypeNames.isEmpty()) {\r
+ String errorMsg = "mandatory 'version' field is missing in policy types: "\r
+ + String.join(", ", invalidPolicyTypeNames);\r
+ throw new PfModelException(Response.Status.NOT_ACCEPTABLE, errorMsg);\r
+ }\r
+ }\r
+\r
+ /**\r
+ * Validates that there is no duplicate version of the policy type stored in the database.\r
+ *\r
+ * @param body the TOSCA service template payload\r
+ *\r
+ * @throws PfModelException the PfModel parsing exception\r
+ */\r
+ private void validateNoDuplicateVersionInDb(ToscaServiceTemplate body) throws PfModelException {\r
+\r
+ Map<String, String> invalidPolicyTypes = new HashMap<>();\r
+ for (Entry<String, ToscaPolicyType> policyType: body.getPolicyTypes().entrySet()) {\r
+ if ("tosca.policies.Root".equals(policyType.getValue().getDerivedFrom())) {\r
+ continue;\r
+ }\r
+ String policyTypeName = policyType.getKey();\r
+ String policyTypeVersion = policyType.getValue().getVersion();\r
+ ToscaServiceTemplate serviceTemplate = getFilteredPolicyTypes(policyTypeName, policyTypeVersion);\r
+ if (hasPolicyType(serviceTemplate)) {\r
+ String latestVersion = getFilteredPolicyTypes(policyTypeName, ToscaPolicyTypeFilter.LATEST_VERSION)\r
+ .getPolicyTypesAsMap().entrySet().iterator().next().getKey().getVersion();\r
+ invalidPolicyTypes.put(String.join(":", policyTypeName, policyTypeVersion), latestVersion);\r
+ }\r
+ }\r
+\r
+ if (!invalidPolicyTypes.isEmpty()) {\r
+ List<String> duplicateVersions = new ArrayList<>(invalidPolicyTypes.size());\r
+ for (Entry<String, String> invalidPolicyType : invalidPolicyTypes.entrySet()) {\r
+ String eachDuplicateVersion = "policy type " + invalidPolicyType.getKey()\r
+ + " already exists; its latest version is " + invalidPolicyType.getValue();\r
+ duplicateVersions.add(eachDuplicateVersion);\r
+ }\r
+ throw new PfModelException(Response.Status.NOT_ACCEPTABLE, String.join("\n", duplicateVersions));\r
+ }\r
+ }\r
+\r
+ /**\r
+ * Retrieves the specified version of the policy type.\r
+ *\r
+ * @param policyTypeName the name of the policy type\r
+ * @param policyTypeVersion the version of the policy type\r
+ *\r
+ * @return the TOSCA service template containing the specified version of the policy type\r
+ *\r
+ * @throws PfModelException the PfModel parsing exception\r
+ */\r
+ private ToscaServiceTemplate getFilteredPolicyTypes(String policyTypeName, String policyTypeVersion)\r
+ throws PfModelException {\r
+\r
+ ToscaPolicyTypeFilter policyTypeFilter = ToscaPolicyTypeFilter.builder()\r
+ .name(policyTypeName).version(policyTypeVersion).build();\r
+ return modelsProvider.getFilteredPolicyTypes(policyTypeFilter);\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