From e0bf80ea0f864a4da8bc2d9bf6e5b19bde75af41 Mon Sep 17 00:00:00 2001 From: "Muthuramalingam, Brinda Santh(bs2796)" Date: Thu, 30 Aug 2018 14:17:06 +0000 Subject: Controller Blueprints Microservice Add Resource Seuencing validation and Optimise resource assignment validation logics Change-Id: I6f31ca5dbeb6f6aa89959b7d96fbfad25468b3a4 Issue-ID: CCSDK-416 Signed-off-by: Muthuramalingam, Brinda Santh(bs2796) --- .../service/ConfigModelCreateService.java | 39 +++++++++--------- .../ResourceAssignmentValidationService.java | 29 +++++++++++++ .../service/ServiceTemplateService.java | 22 +++++----- .../validator/ServiceTemplateValidator.java | 47 +++++++++++++++++++--- .../validator/ServiceTemplateValidationTest.java | 19 ++++++++- .../test/resources/enhance/enhance-template.json | 5 +-- .../test/resources/enhance/enhanced-template.json | 2 +- 7 files changed, 117 insertions(+), 46 deletions(-) create mode 100644 ms/controllerblueprints/modules/service/src/main/java/org/onap/ccsdk/apps/controllerblueprints/service/ResourceAssignmentValidationService.java (limited to 'ms/controllerblueprints/modules/service') diff --git a/ms/controllerblueprints/modules/service/src/main/java/org/onap/ccsdk/apps/controllerblueprints/service/ConfigModelCreateService.java b/ms/controllerblueprints/modules/service/src/main/java/org/onap/ccsdk/apps/controllerblueprints/service/ConfigModelCreateService.java index f5213719..9c1a045c 100644 --- a/ms/controllerblueprints/modules/service/src/main/java/org/onap/ccsdk/apps/controllerblueprints/service/ConfigModelCreateService.java +++ b/ms/controllerblueprints/modules/service/src/main/java/org/onap/ccsdk/apps/controllerblueprints/service/ConfigModelCreateService.java @@ -21,6 +21,7 @@ import com.google.common.base.Preconditions; import org.apache.commons.collections.CollectionUtils; import org.apache.commons.io.IOUtils; import org.apache.commons.lang3.StringUtils; +import org.jetbrains.annotations.NotNull; import org.onap.ccsdk.apps.controllerblueprints.core.BluePrintConstants; import org.onap.ccsdk.apps.controllerblueprints.core.BluePrintException; import org.onap.ccsdk.apps.controllerblueprints.core.ConfigModelConstant; @@ -290,31 +291,29 @@ public class ConfigModelCreateService { * @return ConfigModel * @throws BluePrintException BluePrintException */ - public ConfigModel publishConfigModel(Long id) throws BluePrintException { + public ConfigModel publishConfigModel(@NotNull Long id) throws BluePrintException { ConfigModel dbConfigModel = null; - if (id != null) { - Optional dbConfigModelOptional = configModelRepository.findById(id); - if (dbConfigModelOptional.isPresent()) { - dbConfigModel = dbConfigModelOptional.get(); - List configModelContents = dbConfigModel.getConfigModelContents(); - if (configModelContents != null && !configModelContents.isEmpty()) { - for (ConfigModelContent configModelContent : configModelContents) { - if (configModelContent.getContentType() - .equals(ConfigModelConstant.MODEL_CONTENT_TYPE_TOSCA_JSON)) { - ServiceTemplate serviceTemplate = JacksonUtils - .readValue(configModelContent.getContent(), ServiceTemplate.class); - if (serviceTemplate != null) { - validateServiceTemplate(serviceTemplate); - } + Optional dbConfigModelOptional = configModelRepository.findById(id); + if (dbConfigModelOptional.isPresent()) { + dbConfigModel = dbConfigModelOptional.get(); + List configModelContents = dbConfigModel.getConfigModelContents(); + if (configModelContents != null && !configModelContents.isEmpty()) { + for (ConfigModelContent configModelContent : configModelContents) { + if (configModelContent.getContentType() + .equals(ConfigModelConstant.MODEL_CONTENT_TYPE_TOSCA_JSON)) { + ServiceTemplate serviceTemplate = JacksonUtils + .readValue(configModelContent.getContent(), ServiceTemplate.class); + if (serviceTemplate != null) { + validateServiceTemplate(serviceTemplate); } } } - dbConfigModel.setPublished(ApplicationConstants.ACTIVE_Y); - configModelRepository.save(dbConfigModel); - log.info("Config model ({}) published successfully.", id); - } - + dbConfigModel.setPublished(ApplicationConstants.ACTIVE_Y); + configModelRepository.save(dbConfigModel); + log.info("Config model ({}) published successfully.", id); + } else { + throw new BluePrintException(String.format("Couldn't get Config model for id :(%s)", id)); } return dbConfigModel; } diff --git a/ms/controllerblueprints/modules/service/src/main/java/org/onap/ccsdk/apps/controllerblueprints/service/ResourceAssignmentValidationService.java b/ms/controllerblueprints/modules/service/src/main/java/org/onap/ccsdk/apps/controllerblueprints/service/ResourceAssignmentValidationService.java new file mode 100644 index 00000000..1228e2ee --- /dev/null +++ b/ms/controllerblueprints/modules/service/src/main/java/org/onap/ccsdk/apps/controllerblueprints/service/ResourceAssignmentValidationService.java @@ -0,0 +1,29 @@ +/* + * Copyright © 2017-2018 AT&T Intellectual Property. + * + * 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. + */ + +package org.onap.ccsdk.apps.controllerblueprints.service; + +import org.onap.ccsdk.apps.controllerblueprints.resource.dict.service.ResourceAssignmentValidationDefaultService; +import org.springframework.stereotype.Service; +/** + * ResourceAssignmentValidationService. + * + * @author Brinda Santh + */ +@Service +public class ResourceAssignmentValidationService extends ResourceAssignmentValidationDefaultService { + +} \ No newline at end of file diff --git a/ms/controllerblueprints/modules/service/src/main/java/org/onap/ccsdk/apps/controllerblueprints/service/ServiceTemplateService.java b/ms/controllerblueprints/modules/service/src/main/java/org/onap/ccsdk/apps/controllerblueprints/service/ServiceTemplateService.java index 70cee3c9..3e3c8e28 100644 --- a/ms/controllerblueprints/modules/service/src/main/java/org/onap/ccsdk/apps/controllerblueprints/service/ServiceTemplateService.java +++ b/ms/controllerblueprints/modules/service/src/main/java/org/onap/ccsdk/apps/controllerblueprints/service/ServiceTemplateService.java @@ -20,7 +20,6 @@ import org.apache.commons.lang3.StringUtils; import org.onap.ccsdk.apps.controllerblueprints.core.BluePrintException; import org.onap.ccsdk.apps.controllerblueprints.core.data.ServiceTemplate; import org.onap.ccsdk.apps.controllerblueprints.resource.dict.ResourceAssignment; -import org.onap.ccsdk.apps.controllerblueprints.resource.dict.validator.ResourceAssignmentValidator; import org.onap.ccsdk.apps.controllerblueprints.service.domain.ConfigModelContent; import org.onap.ccsdk.apps.controllerblueprints.service.model.AutoMapResponse; import org.onap.ccsdk.apps.controllerblueprints.service.repository.ResourceDictionaryRepository; @@ -45,21 +44,24 @@ public class ServiceTemplateService { private ConfigModelCreateService configModelCreateService; private BluePrintEnhancerService bluePrintEnhancerService; + private ResourceAssignmentValidationService resourceAssignmentValidationService; /** * This is a SchemaGeneratorService constructor * - * @param dataDictionaryRepository dataDictionaryRepository - * @param configModelCreateService configModelCreateService - * @param bluePrintEnhancerService bluePrintEnhancerService + * @param dataDictionaryRepository dataDictionaryRepository + * @param configModelCreateService configModelCreateService + * @param bluePrintEnhancerService bluePrintEnhancerService + * @param resourceAssignmentValidationService resourceAssignmentValidationService */ public ServiceTemplateService(ResourceDictionaryRepository dataDictionaryRepository, ConfigModelCreateService configModelCreateService, - BluePrintEnhancerService bluePrintEnhancerService) { + BluePrintEnhancerService bluePrintEnhancerService, + ResourceAssignmentValidationService resourceAssignmentValidationService) { this.dataDictionaryRepository = dataDictionaryRepository; this.configModelCreateService = configModelCreateService; this.bluePrintEnhancerService = bluePrintEnhancerService; - + this.resourceAssignmentValidationService = resourceAssignmentValidationService; } /** @@ -105,13 +107,7 @@ public class ServiceTemplateService { */ public List validateResourceAssignments(List resourceAssignments) throws BluePrintException { - try { - ResourceAssignmentValidator resourceAssignmentValidator = - new ResourceAssignmentValidator(resourceAssignments); - resourceAssignmentValidator.validateResourceAssignment(); - } catch (BluePrintException e) { - throw new BluePrintException(e.getMessage(), e); - } + resourceAssignmentValidationService.validate(resourceAssignments); return resourceAssignments; } diff --git a/ms/controllerblueprints/modules/service/src/main/java/org/onap/ccsdk/apps/controllerblueprints/service/validator/ServiceTemplateValidator.java b/ms/controllerblueprints/modules/service/src/main/java/org/onap/ccsdk/apps/controllerblueprints/service/validator/ServiceTemplateValidator.java index 848a32f5..42adf1a3 100644 --- a/ms/controllerblueprints/modules/service/src/main/java/org/onap/ccsdk/apps/controllerblueprints/service/validator/ServiceTemplateValidator.java +++ b/ms/controllerblueprints/modules/service/src/main/java/org/onap/ccsdk/apps/controllerblueprints/service/validator/ServiceTemplateValidator.java @@ -17,16 +17,23 @@ package org.onap.ccsdk.apps.controllerblueprints.service.validator; import com.google.common.base.Preconditions; +import org.apache.commons.collections.MapUtils; import org.apache.commons.lang3.StringUtils; import org.jetbrains.annotations.NotNull; +import org.onap.ccsdk.apps.controllerblueprints.core.BluePrintConstants; import org.onap.ccsdk.apps.controllerblueprints.core.BluePrintException; +import org.onap.ccsdk.apps.controllerblueprints.core.ConfigModelConstant; +import org.onap.ccsdk.apps.controllerblueprints.core.data.CapabilityAssignment; import org.onap.ccsdk.apps.controllerblueprints.core.data.NodeTemplate; import org.onap.ccsdk.apps.controllerblueprints.core.data.ServiceTemplate; import org.onap.ccsdk.apps.controllerblueprints.core.service.BluePrintValidatorDefaultService; import org.onap.ccsdk.apps.controllerblueprints.core.utils.JacksonUtils; -import org.onap.ccsdk.apps.controllerblueprints.resource.dict.validator.ResourceAssignmentValidator; +import org.onap.ccsdk.apps.controllerblueprints.resource.dict.ResourceAssignment; +import org.onap.ccsdk.apps.controllerblueprints.resource.dict.service.ResourceAssignmentValidationDefaultService; +import org.onap.ccsdk.apps.controllerblueprints.resource.dict.service.ResourceAssignmentValidationService; import java.util.HashMap; +import java.util.List; import java.util.Map; /** @@ -62,7 +69,7 @@ public class ServiceTemplateValidator extends BluePrintValidatorDefaultService { /** * This is a validateServiceTemplate * - * @param serviceTemplate + * @param serviceTemplate serviceTemplate * @return boolean * @throws BluePrintException BluePrintException */ @@ -76,7 +83,7 @@ public class ServiceTemplateValidator extends BluePrintValidatorDefaultService { /** * This is a getMetaData to get the key information during the * - * @return Map + * @return Map */ public Map getMetaData() { return metaData; @@ -104,9 +111,37 @@ public class ServiceTemplateValidator extends BluePrintValidatorDefaultService { private void validateNodeTemplateCustom(@NotNull String nodeTemplateName, @NotNull NodeTemplate nodeTemplate) throws BluePrintException { String derivedFrom = getBluePrintContext().nodeTemplateNodeType(nodeTemplateName).getDerivedFrom(); - if ("tosca.nodes.Artifact".equals(derivedFrom)) { - ResourceAssignmentValidator resourceAssignmentValidator = new ResourceAssignmentValidator(nodeTemplate); - resourceAssignmentValidator.validateResourceAssignment(); + + if (BluePrintConstants.MODEL_TYPE_NODE_ARTIFACT.equals(derivedFrom)) { + List resourceAssignment = getResourceAssignments(nodeTemplate); + ResourceAssignmentValidationService resourceAssignmentValidationService = new ResourceAssignmentValidationDefaultService(); + resourceAssignmentValidationService.validate(resourceAssignment); + } + } + + private List getResourceAssignments(@NotNull NodeTemplate nodeTemplate) { + + List resourceAssignment = null; + + if (MapUtils.isNotEmpty(nodeTemplate.getCapabilities())) { + + CapabilityAssignment capabilityAssignment = + nodeTemplate.getCapabilities().get(ConfigModelConstant.CAPABILITY_PROPERTY_MAPPING); + if (capabilityAssignment != null && capabilityAssignment.getProperties() != null) { + Object mappingObject = + capabilityAssignment.getProperties().get(ConfigModelConstant.CAPABILITY_PROPERTY_MAPPING); + if (mappingObject != null) { + String mappingContent = JacksonUtils.getJson(mappingObject); + Preconditions.checkArgument(StringUtils.isNotBlank(mappingContent), + String.format("Failed to get capability mapping property (%s) ", ConfigModelConstant.CAPABILITY_PROPERTY_MAPPING)); + + resourceAssignment = JacksonUtils.getListFromJson(mappingContent, ResourceAssignment.class); + + Preconditions.checkNotNull(resourceAssignment, + String.format("Failed to get resource assignment info from the content (%s) ", mappingContent)); + } + } } + return resourceAssignment; } } diff --git a/ms/controllerblueprints/modules/service/src/test/java/org/onap/ccsdk/apps/controllerblueprints/service/validator/ServiceTemplateValidationTest.java b/ms/controllerblueprints/modules/service/src/test/java/org/onap/ccsdk/apps/controllerblueprints/service/validator/ServiceTemplateValidationTest.java index 0ef54452..e41e90a2 100644 --- a/ms/controllerblueprints/modules/service/src/test/java/org/onap/ccsdk/apps/controllerblueprints/service/validator/ServiceTemplateValidationTest.java +++ b/ms/controllerblueprints/modules/service/src/test/java/org/onap/ccsdk/apps/controllerblueprints/service/validator/ServiceTemplateValidationTest.java @@ -20,6 +20,8 @@ package org.onap.ccsdk.apps.controllerblueprints.service.validator; import org.apache.commons.io.FileUtils; import org.junit.Assert; import org.junit.Test; +import org.onap.ccsdk.apps.controllerblueprints.core.data.ServiceTemplate; +import org.onap.ccsdk.apps.controllerblueprints.core.utils.JacksonUtils; import org.onap.ccsdk.apps.controllerblueprints.service.utils.ConfigModelUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -40,9 +42,22 @@ public class ServiceTemplateValidationTest { @Test public void validateServiceTemplate() throws Exception { - String file = "load/blueprints/baseconfiguration/Definitions/activation-blueprint.json"; + validateServiceTemplate("load/blueprints/baseconfiguration/Definitions/activation-blueprint.json"); + validateServiceTemplate("load/blueprints/vrr-test/Definitions/vrr-test.json"); + } + + //@Test + public void validateEnhancedServiceTemplate() throws Exception { + ServiceTemplate serviceTemplate = JacksonUtils + .readValueFromClassPathFile("enhance/enhanced-template.json", ServiceTemplate.class); + ServiceTemplateValidator serviceTemplateValidator = new ServiceTemplateValidator(); + Boolean valid = serviceTemplateValidator.validateServiceTemplate(serviceTemplate); + Assert.assertTrue("Failed to validate blueprints", valid); + } + + private void validateServiceTemplate(String fileName) throws Exception { String serviceTemplateContent = - FileUtils.readFileToString(new File(file), Charset.defaultCharset()); + FileUtils.readFileToString(new File(fileName), Charset.defaultCharset()); ServiceTemplateValidator serviceTemplateValidator = new ServiceTemplateValidator(); serviceTemplateValidator.validateServiceTemplate(serviceTemplateContent); Assert.assertNotNull("Failed to validate blueprints", serviceTemplateValidator); diff --git a/ms/controllerblueprints/modules/service/src/test/resources/enhance/enhance-template.json b/ms/controllerblueprints/modules/service/src/test/resources/enhance/enhance-template.json index a4ba930e..fedf1da2 100644 --- a/ms/controllerblueprints/modules/service/src/test/resources/enhance/enhance-template.json +++ b/ms/controllerblueprints/modules/service/src/test/resources/enhance/enhance-template.json @@ -232,10 +232,7 @@ "dictionary-name": "wan-aggregate-ipv4-addresses", "dictionary-source": "mdsal", "dependencies": [ - "service-instance-id", - "oam-network-role", - "oam-v4-ip-type ", - "oam-vm-type" + "service-instance-id" ], "version": 0 }, diff --git a/ms/controllerblueprints/modules/service/src/test/resources/enhance/enhanced-template.json b/ms/controllerblueprints/modules/service/src/test/resources/enhance/enhanced-template.json index e0033096..0633c64d 100644 --- a/ms/controllerblueprints/modules/service/src/test/resources/enhance/enhanced-template.json +++ b/ms/controllerblueprints/modules/service/src/test/resources/enhance/enhanced-template.json @@ -738,7 +738,7 @@ "input-param" : false, "dictionary-name" : "wan-aggregate-ipv4-addresses", "dictionary-source" : "mdsal", - "dependencies" : [ "service-instance-id", "oam-network-role", "oam-v4-ip-type ", "oam-vm-type" ], + "dependencies" : [ "service-instance-id" ], "version" : 0 }, { "name" : "hostname", -- cgit 1.2.3-korg