+ /**\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