From 208cb6ae94922c0198648aea817085793ce8bc47 Mon Sep 17 00:00:00 2001 From: "Muthuramalingam, Brinda Santh(bs2796)" Date: Mon, 27 Aug 2018 17:29:51 +0000 Subject: Controller Blueprints Microservice Optimise model type repository search for DB and file in blueprint repo service. Change-Id: If5458e218b723d3fff451789a73a667dd75bc91c Issue-ID: CCSDK-487 Signed-off-by: Muthuramalingam, Brinda Santh(bs2796) --- .../service/BluePrintRepoDBService.java | 63 +++++++------- .../service/ResourceDictionaryService.java | 96 ++++++++++------------ 2 files changed, 72 insertions(+), 87 deletions(-) (limited to 'ms/controllerblueprints/modules/service/src/main') diff --git a/ms/controllerblueprints/modules/service/src/main/java/org/onap/ccsdk/apps/controllerblueprints/service/BluePrintRepoDBService.java b/ms/controllerblueprints/modules/service/src/main/java/org/onap/ccsdk/apps/controllerblueprints/service/BluePrintRepoDBService.java index 4a26119c..4c11d8c6 100644 --- a/ms/controllerblueprints/modules/service/src/main/java/org/onap/ccsdk/apps/controllerblueprints/service/BluePrintRepoDBService.java +++ b/ms/controllerblueprints/modules/service/src/main/java/org/onap/ccsdk/apps/controllerblueprints/service/BluePrintRepoDBService.java @@ -26,6 +26,7 @@ import org.onap.ccsdk.apps.controllerblueprints.core.utils.JacksonUtils; import org.onap.ccsdk.apps.controllerblueprints.service.domain.ModelType; import org.onap.ccsdk.apps.controllerblueprints.service.repository.ModelTypeRepository; import org.springframework.stereotype.Service; +import reactor.core.publisher.Mono; import java.util.Optional; @@ -43,59 +44,51 @@ public class BluePrintRepoDBService implements BluePrintRepoService { this.modelTypeRepository = modelTypeRepository; } - @Override - public NodeType getNodeType(String nodeTypeName) throws BluePrintException { - Preconditions.checkArgument(StringUtils.isNotBlank(nodeTypeName), "NodeType name is missing"); - String content = getModelDefinitions(nodeTypeName); - Preconditions.checkArgument(StringUtils.isNotBlank(content), "NodeType content is missing"); - return JacksonUtils.readValue(content, NodeType.class); + public Mono getNodeType(String nodeTypeName) throws BluePrintException { + return getModelType(nodeTypeName, NodeType.class); } - @Override - public DataType getDataType(String dataTypeName) throws BluePrintException { - Preconditions.checkArgument(StringUtils.isNotBlank(dataTypeName), "DataType name is missing"); - String content = getModelDefinitions(dataTypeName); - Preconditions.checkArgument(StringUtils.isNotBlank(content), "DataType content is missing"); - return JacksonUtils.readValue(content, DataType.class); + public Mono getDataType(String dataTypeName) throws BluePrintException { + return getModelType(dataTypeName, DataType.class); } - @Override - public ArtifactType getArtifactType(String artifactTypeName) throws BluePrintException { - Preconditions.checkArgument(StringUtils.isNotBlank(artifactTypeName), "ArtifactType name is missing"); - String content = getModelDefinitions(artifactTypeName); - Preconditions.checkArgument(StringUtils.isNotBlank(content), "ArtifactType content is missing"); - return JacksonUtils.readValue(content, ArtifactType.class); + public Mono getArtifactType(String artifactTypeName) throws BluePrintException { + return getModelType(artifactTypeName, ArtifactType.class); } - @Override - public RelationshipType getRelationshipType(String relationshipTypeName) throws BluePrintException { - Preconditions.checkArgument(StringUtils.isNotBlank(relationshipTypeName), "RelationshipType name is missing"); - String content = getModelDefinitions(relationshipTypeName); - Preconditions.checkArgument(StringUtils.isNotBlank(content), "RelationshipType content is missing"); - return JacksonUtils.readValue(content, RelationshipType.class); + public Mono getRelationshipType(String relationshipTypeName) throws BluePrintException { + return getModelType(relationshipTypeName, RelationshipType.class); } - @Override - public CapabilityDefinition getCapabilityDefinition(String capabilityDefinitionName) throws BluePrintException { - Preconditions.checkArgument(StringUtils.isNotBlank(capabilityDefinitionName), "CapabilityDefinition name is missing"); - String content = getModelDefinitions(capabilityDefinitionName); - Preconditions.checkArgument(StringUtils.isNotBlank(content), "CapabilityDefinition content is missing"); - return JacksonUtils.readValue(content, CapabilityDefinition.class); + public Mono getCapabilityDefinition(String capabilityDefinitionName) throws BluePrintException { + return getModelType(capabilityDefinitionName, CapabilityDefinition.class); } - private String getModelDefinitions(String modelName) throws BluePrintException { + private Mono getModelType(String modelName, Class valueClass) throws BluePrintException { + Preconditions.checkArgument(StringUtils.isNotBlank(modelName), + "Failed to get model from repo, model name is missing"); + + return getModelDefinitions(modelName).map(content -> { + Preconditions.checkArgument(StringUtils.isNotBlank(content), + String.format("Failed to get model content for model name (%s)", modelName)); + return JacksonUtils.readValue(content, valueClass); + } + ); + } + + private Mono getModelDefinitions(String modelName) throws BluePrintException { String modelDefinition = null; - Optional modelTypedb = modelTypeRepository.findByModelName(modelName); - if (modelTypedb.isPresent()) { - modelDefinition = modelTypedb.get().getDefinition(); + Optional modelTypeDb = modelTypeRepository.findByModelName(modelName); + if (modelTypeDb.isPresent()) { + modelDefinition = modelTypeDb.get().getDefinition(); } else { throw new BluePrintException(String.format("failed to get model definition (%s) from repo", modelName)); } - return modelDefinition; + return Mono.just(modelDefinition); } } diff --git a/ms/controllerblueprints/modules/service/src/main/java/org/onap/ccsdk/apps/controllerblueprints/service/ResourceDictionaryService.java b/ms/controllerblueprints/modules/service/src/main/java/org/onap/ccsdk/apps/controllerblueprints/service/ResourceDictionaryService.java index 5420dd39..629b94c0 100644 --- a/ms/controllerblueprints/modules/service/src/main/java/org/onap/ccsdk/apps/controllerblueprints/service/ResourceDictionaryService.java +++ b/ms/controllerblueprints/modules/service/src/main/java/org/onap/ccsdk/apps/controllerblueprints/service/ResourceDictionaryService.java @@ -17,6 +17,7 @@ package org.onap.ccsdk.apps.controllerblueprints.service; +import com.google.common.base.Preconditions; import org.apache.commons.lang3.StringUtils; import org.onap.ccsdk.apps.controllerblueprints.core.BluePrintException; import org.onap.ccsdk.apps.controllerblueprints.core.data.EntrySchema; @@ -112,60 +113,51 @@ public class ResourceDictionaryService { */ public ResourceDictionary saveResourceDictionary(ResourceDictionary resourceDictionary) throws BluePrintException { - if (resourceDictionary != null) { - ResourceDictionaryValidator.validateResourceDictionary(resourceDictionary); - - ResourceDefinition resourceDefinition = - JacksonUtils.readValue(resourceDictionary.getDefinition(), ResourceDefinition.class); - // Check the Source already Present - resourceDictionaryValidationService.validate(resourceDefinition); - - if (resourceDefinition == null) { - throw new BluePrintException( - "Resource dictionary definition is not valid content " + resourceDictionary.getDefinition()); - } - - resourceDefinition.setName(resourceDictionary.getName()); - resourceDefinition.setResourcePath(resourceDictionary.getResourcePath()); - resourceDefinition.setResourceType(resourceDictionary.getResourceType()); - - PropertyDefinition propertyDefinition = new PropertyDefinition(); - propertyDefinition.setType(resourceDictionary.getDataType()); - propertyDefinition.setDescription(resourceDictionary.getDescription()); - if (StringUtils.isNotBlank(resourceDictionary.getEntrySchema())) { - EntrySchema entrySchema = new EntrySchema(); - entrySchema.setType(resourceDictionary.getEntrySchema()); - propertyDefinition.setEntrySchema(entrySchema); - } else { - propertyDefinition.setEntrySchema(null); - } - resourceDefinition.setTags(resourceDictionary.getTags()); - resourceDefinition.setUpdatedBy(resourceDictionary.getUpdatedBy()); - - String definitionContent = JacksonUtils.getJson(resourceDefinition, true); - resourceDictionary.setDefinition(definitionContent); - - Optional dbResourceDictionaryData = - resourceDictionaryRepository.findByName(resourceDictionary.getName()); - if (dbResourceDictionaryData.isPresent()) { - ResourceDictionary dbResourceDictionary = dbResourceDictionaryData.get(); - - dbResourceDictionary.setName(resourceDictionary.getName()); - dbResourceDictionary.setDefinition(resourceDictionary.getDefinition()); - dbResourceDictionary.setDescription(resourceDictionary.getDescription()); - dbResourceDictionary.setResourceType(resourceDictionary.getResourceType()); - dbResourceDictionary.setResourcePath(resourceDictionary.getResourcePath()); - dbResourceDictionary.setDataType(resourceDictionary.getDataType()); - dbResourceDictionary.setEntrySchema(resourceDictionary.getEntrySchema()); - dbResourceDictionary.setTags(resourceDictionary.getTags()); - dbResourceDictionary.setValidValues(resourceDictionary.getValidValues()); - resourceDictionary = resourceDictionaryRepository.save(dbResourceDictionary); - } else { - resourceDictionary = resourceDictionaryRepository.save(resourceDictionary); - } + Preconditions.checkNotNull(resourceDictionary, "Resource Dictionary information is missing"); + Preconditions.checkArgument(StringUtils.isNotBlank(resourceDictionary.getDefinition()), + "Resource Dictionary definition information is missing"); + + ResourceDefinition resourceDefinition = + JacksonUtils.readValue(resourceDictionary.getDefinition(), ResourceDefinition.class); + // Validate the Resource Definitions + resourceDictionaryValidationService.validate(resourceDefinition); + + resourceDictionary.setResourceType(resourceDefinition.getResourceType()); + resourceDictionary.setResourcePath(resourceDefinition.getResourcePath()); + resourceDictionary.setTags(resourceDefinition.getTags()); + resourceDefinition.setUpdatedBy(resourceDictionary.getUpdatedBy()); + // Set the Property Definitions + PropertyDefinition propertyDefinition = resourceDefinition.getProperty(); + resourceDictionary.setDescription(propertyDefinition.getDescription()); + resourceDictionary.setDataType(propertyDefinition.getType()); + if(propertyDefinition.getEntrySchema() != null){ + resourceDictionary.setEntrySchema(propertyDefinition.getEntrySchema().getType()); + } + + String definitionContent = JacksonUtils.getJson(resourceDefinition, true); + resourceDictionary.setDefinition(definitionContent); + + ResourceDictionaryValidator.validateResourceDictionary(resourceDictionary); + + Optional dbResourceDictionaryData = + resourceDictionaryRepository.findByName(resourceDictionary.getName()); + if (dbResourceDictionaryData.isPresent()) { + ResourceDictionary dbResourceDictionary = dbResourceDictionaryData.get(); + + dbResourceDictionary.setName(resourceDictionary.getName()); + dbResourceDictionary.setDefinition(resourceDictionary.getDefinition()); + dbResourceDictionary.setDescription(resourceDictionary.getDescription()); + dbResourceDictionary.setResourceType(resourceDictionary.getResourceType()); + dbResourceDictionary.setResourcePath(resourceDictionary.getResourcePath()); + dbResourceDictionary.setTags(resourceDictionary.getTags()); + dbResourceDictionary.setUpdatedBy(resourceDictionary.getUpdatedBy()); + dbResourceDictionary.setDataType(resourceDictionary.getDataType()); + dbResourceDictionary.setEntrySchema(resourceDictionary.getEntrySchema()); + resourceDictionary = resourceDictionaryRepository.save(dbResourceDictionary); } else { - throw new BluePrintException("Resource Dictionary information is missing"); + resourceDictionary = resourceDictionaryRepository.save(resourceDictionary); } + return resourceDictionary; } -- cgit 1.2.3-korg