From 393bbb59b975747723c6801da2a5b151c542a0b5 Mon Sep 17 00:00:00 2001 From: Ashwin Sharma Date: Thu, 27 Feb 2020 17:24:52 +0000 Subject: Upload Tosca Model changes to remove policy model type parsing from UI. Dictionary API fix to allow bulk create or update of dictionary elements. Issue-ID: CLAMP-656 Change-Id: I6a0a36a6be40a098d917c45462872d31b7f47dd2 Signed-off-by: Ashwin Sharma --- .../clamp/loop/template/PolicyModelsService.java | 43 ++++++++++------------ src/main/java/org/onap/clamp/tosca/Dictionary.java | 13 +++++-- .../org/onap/clamp/tosca/DictionaryService.java | 26 ++++++------- 3 files changed, 43 insertions(+), 39 deletions(-) (limited to 'src/main/java/org/onap') diff --git a/src/main/java/org/onap/clamp/loop/template/PolicyModelsService.java b/src/main/java/org/onap/clamp/loop/template/PolicyModelsService.java index 7e21cc39..aeea55db 100644 --- a/src/main/java/org/onap/clamp/loop/template/PolicyModelsService.java +++ b/src/main/java/org/onap/clamp/loop/template/PolicyModelsService.java @@ -44,7 +44,7 @@ public class PolicyModelsService { @Autowired public PolicyModelsService(PolicyModelsRepository policyModelrepo, - ToscaYamlToJsonConvertor convertor) { + ToscaYamlToJsonConvertor convertor) { policyModelsRepository = policyModelrepo; toscaYamlToJsonConvertor = convertor; } @@ -72,40 +72,37 @@ public class PolicyModelsService { /** * Creates or updates the Tosca Policy Model. * - * @param policyModelType The policyModeltype in Tosca yaml * @param policyModelTosca The Policymodel object * @return The Policy Model created */ - public PolicyModel createNewPolicyModelFromTosca(String policyModelType, - String policyModelTosca) { + public PolicyModel createNewPolicyModelFromTosca(String policyModelTosca) { JsonObject jsonObject = toscaYamlToJsonConvertor.validateAndConvertToJson(policyModelTosca); String policyModelTypeFromTosca = toscaYamlToJsonConvertor.getValueFromMetadata(jsonObject, - ToscaSchemaConstants.METADATA_POLICY_MODEL_TYPE); - String policyModelTypeToUse = policyModelTypeFromTosca != null ? policyModelTypeFromTosca : policyModelType; - Iterable models = getAllPolicyModelsByType(policyModelTypeToUse); + ToscaSchemaConstants.METADATA_POLICY_MODEL_TYPE); + Iterable models = getAllPolicyModelsByType(policyModelTypeFromTosca); Collections.sort((List) models); - PolicyModel newPolicyModel = new PolicyModel(policyModelTypeToUse, policyModelTosca, - SemanticVersioning.incrementMajorVersion( - ((ArrayList) models).isEmpty() ? null : ((ArrayList) models).get(0).getVersion()), - toscaYamlToJsonConvertor.getValueFromMetadata(jsonObject, - ToscaSchemaConstants.METADATA_ACRONYM)); + PolicyModel newPolicyModel = new PolicyModel(policyModelTypeFromTosca, policyModelTosca, + SemanticVersioning.incrementMajorVersion(((ArrayList) models).isEmpty() ? null + : ((ArrayList) models).get(0).getVersion()), + toscaYamlToJsonConvertor.getValueFromMetadata(jsonObject, + ToscaSchemaConstants.METADATA_ACRONYM)); return saveOrUpdatePolicyModel(newPolicyModel); } /** * Update an existing Tosca Policy Model. * - * @param policyModelType The policy Model type in Tosca yaml + * @param policyModelType The policy Model type in Tosca yaml * @param policyModelVersion The policy Version to update - * @param policyModelTosca The Policy Model tosca + * @param policyModelTosca The Policy Model tosca * @return The Policy Model updated */ public PolicyModel updatePolicyModelTosca(String policyModelType, String policyModelVersion, - String policyModelTosca) { + String policyModelTosca) { JsonObject jsonObject = toscaYamlToJsonConvertor.validateAndConvertToJson(policyModelTosca); PolicyModel thePolicyModel = getPolicyModelByType(policyModelType, policyModelVersion); thePolicyModel.setPolicyAcronym(toscaYamlToJsonConvertor.getValueFromMetadata(jsonObject, - ToscaSchemaConstants.METADATA_ACRONYM)); + ToscaSchemaConstants.METADATA_ACRONYM)); thePolicyModel.setPolicyModelTosca(policyModelTosca); return saveOrUpdatePolicyModel(thePolicyModel); } @@ -133,13 +130,13 @@ public class PolicyModelsService { /** * Retrieves the Tosca model Yaml string. * - * @param type The Policy Model Type + * @param type The Policy Model Type * @param version The policy model version * @return The Tosca model Yaml string */ public String getPolicyModelTosca(String type, String version) { - return policyModelsRepository.findById(new PolicyModelId(type, version)).orElse(new PolicyModel()) - .getPolicyModelTosca(); + return policyModelsRepository.findById(new PolicyModelId(type, version)) + .orElse(new PolicyModel()).getPolicyModelTosca(); } /** @@ -149,8 +146,8 @@ public class PolicyModelsService { */ @Transactional(propagation = Propagation.REQUIRES_NEW) public void createPolicyInDbIfNeeded(PolicyModel policyModel) { - if (!policyModelsRepository - .existsById(new PolicyModelId(policyModel.getPolicyModelType(), policyModel.getVersion()))) { + if (!policyModelsRepository.existsById( + new PolicyModelId(policyModel.getPolicyModelType(), policyModel.getVersion()))) { policyModelsRepository.save(policyModel); } } @@ -165,8 +162,8 @@ public class PolicyModelsService { for (PolicyModel policyModel : policyModelList) { JsonArray supportedPdpGroups = new JsonArray(); for (PdpGroup pdpGroup : pdpGroupList) { - JsonObject supportedPdpGroup = pdpGroup.getSupportedSubgroups(policyModel.getPolicyModelType(), - policyModel.getVersion()); + JsonObject supportedPdpGroup = pdpGroup.getSupportedSubgroups( + policyModel.getPolicyModelType(), policyModel.getVersion()); if (supportedPdpGroup != null) { supportedPdpGroups.add(supportedPdpGroup); } diff --git a/src/main/java/org/onap/clamp/tosca/Dictionary.java b/src/main/java/org/onap/clamp/tosca/Dictionary.java index 44b5b6f6..cf514c4b 100644 --- a/src/main/java/org/onap/clamp/tosca/Dictionary.java +++ b/src/main/java/org/onap/clamp/tosca/Dictionary.java @@ -65,9 +65,7 @@ public class Dictionary extends AuditEntity implements Serializable { private String subDictionaryType; @Expose - @ManyToMany( - fetch = FetchType.EAGER, - cascade = {CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REFRESH}) + @ManyToMany(fetch = FetchType.EAGER, cascade = {CascadeType.ALL}) @JoinTable( name = "dictionary_to_dictionaryelements", joinColumns = @JoinColumn(name = "dictionary_name", referencedColumnName = "name"), @@ -149,6 +147,15 @@ public class Dictionary extends AuditEntity implements Serializable { dictionaryElement.getUsedByDictionaries().add(this); } + /** + * Method to set dictionaryElements. + * + * @param dictionaryElements The dictionary elements set + */ + public void setDictionaryElements(Set dictionaryElements) { + this.dictionaryElements = dictionaryElements; + } + /** * Method to delete a dictionaryElement from the list. * diff --git a/src/main/java/org/onap/clamp/tosca/DictionaryService.java b/src/main/java/org/onap/clamp/tosca/DictionaryService.java index 5b24def9..98e3516f 100644 --- a/src/main/java/org/onap/clamp/tosca/DictionaryService.java +++ b/src/main/java/org/onap/clamp/tosca/DictionaryService.java @@ -26,6 +26,7 @@ package org.onap.clamp.tosca; import com.google.common.collect.Sets; import java.util.List; import java.util.Set; +import java.util.stream.Collectors; import javax.persistence.EntityNotFoundException; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @@ -63,20 +64,19 @@ public class DictionaryService { Set newDictionaryElements = dictionary.getDictionaryElements(); - for (DictionaryElement dictionaryElement : newDictionaryElements) { - if (dict.getDictionaryElements().contains(dictionaryElement)) { - // Update the Dictionary Element - getAndUpdateDictionaryElement(dict, dictionaryElement); - } else { - // Create the Dictionary Element - dict.addDictionaryElements(getAndUpdateDictionaryElement(dict, dictionaryElement)); - dictionaryRepository.save(dict); - } + if (newDictionaryElements != null && !newDictionaryElements.isEmpty()) { + Set updatedDictionaryElements = newDictionaryElements.stream() + .map(dictionaryElement -> getAndUpdateDictionaryElement(dict, dictionaryElement)) + .collect(Collectors.toSet()); + + dict.getDictionaryElements().forEach(dictElement -> { + if (!updatedDictionaryElements.contains(dictElement)) { + updatedDictionaryElements.add(dictElement); + } + }); + dict.setDictionaryElements(updatedDictionaryElements); } - - // Fetch again to get Dictionary with most recent updates. - return dictionaryRepository.findById(dictionaryName).orElseThrow( - () -> new EntityNotFoundException("Couldn't find Dictionary named: " + dictionaryName)); + return dictionaryRepository.save(dict); } -- cgit 1.2.3-korg