From 6addc71625d4ae32d74908fbeb0ce975f2fded9a Mon Sep 17 00:00:00 2001 From: subhash kumar singh Date: Fri, 14 Dec 2018 20:02:29 +0530 Subject: update request input and sequence to DB Update request input and sequence to DB. Resource sequence has been added as new column in service table. Resource input has been added as new column in resp resource. Change-Id: I97c8227636f06692190a843a87178517db038bbd Issue-ID: SO-1248 Signed-off-by: subhash kumar singh --- .../so/asdc/installer/ToscaResourceStructure.java | 2 +- .../installer/heat/ToscaResourceInstaller.java | 153 ++++++++++++++++++++- .../client/test/rest/ASDCRestInterfaceTest.java | 1 + .../installer/heat/ToscaResourceInputTest.java | 136 ++++++++++++++++++ asdc-controller/src/test/resources/schema.sql | 4 + 5 files changed, 289 insertions(+), 7 deletions(-) create mode 100644 asdc-controller/src/test/java/org/onap/so/asdc/installer/heat/ToscaResourceInputTest.java (limited to 'asdc-controller') diff --git a/asdc-controller/src/main/java/org/onap/so/asdc/installer/ToscaResourceStructure.java b/asdc-controller/src/main/java/org/onap/so/asdc/installer/ToscaResourceStructure.java index 030035157d..c7c6ecacc0 100644 --- a/asdc-controller/src/main/java/org/onap/so/asdc/installer/ToscaResourceStructure.java +++ b/asdc-controller/src/main/java/org/onap/so/asdc/installer/ToscaResourceStructure.java @@ -134,7 +134,7 @@ public class ToscaResourceStructure { LOGGER.info(MessageEnum.ASDC_RECEIVE_SERVICE_NOTIF, "***PATH", "ASDC", spoolFile.getAbsolutePath()); - sdcCsarHelper = factory.getSdcCsarHelper(spoolFile.getAbsolutePath()); + sdcCsarHelper = factory.getSdcCsarHelper(spoolFile.getAbsolutePath(),false); }catch(Exception e){ System.out.println("System out " + e.getMessage()); diff --git a/asdc-controller/src/main/java/org/onap/so/asdc/installer/heat/ToscaResourceInstaller.java b/asdc-controller/src/main/java/org/onap/so/asdc/installer/heat/ToscaResourceInstaller.java index 529ce30357..c26d4ad0b3 100644 --- a/asdc-controller/src/main/java/org/onap/so/asdc/installer/heat/ToscaResourceInstaller.java +++ b/asdc-controller/src/main/java/org/onap/so/asdc/installer/heat/ToscaResourceInstaller.java @@ -25,16 +25,24 @@ package org.onap.so.asdc.installer.heat; import java.sql.Timestamp; import java.util.ArrayList; import java.util.Date; +import java.util.HashMap; import java.util.HashSet; +import java.util.LinkedHashMap; import java.util.List; +import java.util.Map; import java.util.Optional; import java.util.Set; +import java.util.stream.Collectors; +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.google.gson.Gson; import org.hibernate.exception.ConstraintViolationException; import org.hibernate.exception.LockAcquisitionException; import org.onap.sdc.api.notification.IArtifactInfo; import org.onap.sdc.api.notification.IResourceInstance; import org.onap.sdc.api.notification.IStatusData; +import org.onap.sdc.tosca.parser.api.ISdcCsarHelper; import org.onap.sdc.tosca.parser.impl.SdcPropertyNames; import org.onap.sdc.tosca.parser.impl.SdcTypes; import org.onap.sdc.toscaparser.api.CapabilityAssignment; @@ -42,8 +50,12 @@ import org.onap.sdc.toscaparser.api.CapabilityAssignments; import org.onap.sdc.toscaparser.api.Group; import org.onap.sdc.toscaparser.api.NodeTemplate; import org.onap.sdc.toscaparser.api.Policy; +import org.onap.sdc.toscaparser.api.Property; import org.onap.sdc.toscaparser.api.RequirementAssignment; +import org.onap.sdc.toscaparser.api.RequirementAssignments; import org.onap.sdc.toscaparser.api.elements.Metadata; +import org.onap.sdc.toscaparser.api.functions.GetInput; +import org.onap.sdc.toscaparser.api.parameters.Input; import org.onap.sdc.utils.DistributionStatusEnum; import org.onap.so.asdc.client.ASDCConfiguration; import org.onap.so.asdc.client.exceptions.ArtifactInstallerException; @@ -284,6 +296,7 @@ public class ToscaResourceInstaller { createService(toscaResourceStruct, vfResourceStruct); Service service = toscaResourceStruct.getCatalogService(); + processResourceSequence(toscaResourceStruct, service); processVFResources(toscaResourceStruct, service, vfResourceStructure); processAllottedResources(toscaResourceStruct, service); processNetworks(toscaResourceStruct, service); @@ -332,6 +345,129 @@ public class ToscaResourceInstaller { } } + + List getRequirementList(List resultList, List nodeTemplates, + ISdcCsarHelper iSdcCsarHelper) { + + List nodes = new ArrayList(); + nodes.addAll(nodeTemplates); + + for (NodeTemplate nodeTemplate : nodeTemplates) { + RequirementAssignments requirement = iSdcCsarHelper.getRequirementsOf(nodeTemplate); + List reqAs = requirement.getAll(); + for (RequirementAssignment ra : reqAs) { + String reqNode = ra.getNodeTemplateName(); + for (NodeTemplate rNode : resultList) { + if (rNode.getName().equals(reqNode)) { + if(!resultList.contains(nodeTemplate)) { + resultList.add(nodeTemplate); + } + if(nodes.contains(nodeTemplate)) { + nodes.remove(nodeTemplate); + } + break; + } + } + } + } + + if (!nodes.isEmpty()) { + getRequirementList(resultList, nodes, iSdcCsarHelper); + } + + return resultList; + } + + // This method retrieve resource sequence from csar file + void processResourceSequence(ToscaResourceStructure toscaResourceStructure, Service service) { + List resouceSequence = new ArrayList(); + List resultList = new ArrayList(); + + ISdcCsarHelper iSdcCsarHelper = toscaResourceStructure.getSdcCsarHelper(); + List nodeTemplates = iSdcCsarHelper.getServiceNodeTemplates(); + List nodes = new ArrayList(); + nodes.addAll(nodeTemplates); + + for (NodeTemplate nodeTemplate : nodeTemplates) { + RequirementAssignments requirement = iSdcCsarHelper.getRequirementsOf(nodeTemplate); + + if (requirement == null || requirement.getAll() == null || requirement.getAll().isEmpty()) { + resultList.add(nodeTemplate); + nodes.remove(nodeTemplate); + } + } + + resultList = getRequirementList(resultList, nodes, iSdcCsarHelper); + + for (NodeTemplate node : resultList) { + String templateName = node.getMetaData().getValue("name"); + if (!resouceSequence.contains(templateName)) { + resouceSequence.add(templateName); + } + } + + String resourceSeqStr = resouceSequence.stream().collect(Collectors.joining(",")); + service.setResourceOrder(resourceSeqStr); + logger.debug(" resourceSeq for service uuid(" + service.getModelUUID() + ") : " + resourceSeqStr); + } + + private static String CUSTOMIZATION_UUID = "customizationUUID"; + + private static String getValue(Object value, List servInputs) { + String output = null; + if(value instanceof Map) { + // currently this logic handles only one level of nesting. + return ((LinkedHashMap) value).values().toArray()[0].toString(); + } else if(value instanceof GetInput) { + String inputName = ((GetInput)value).getInputName(); + + for(Input input : servInputs) { + if(input.getName().equals(inputName)) { + // keep both input name and default value + // if service input does not supplies value the use default value + String defaultValue = input.getDefault() != null ? (String) input.getDefault() : ""; + output = inputName + "|" + defaultValue;// return default value + } + } + + } else { + output = value != null ? value.toString() : ""; + } + return output; // return property value + } + + String getResourceInput(ToscaResourceStructure toscaResourceStructure, String resourceCustomizationUuid) throws ArtifactInstallerException { + Map resouceRequest = new HashMap<>(); + ISdcCsarHelper iSdcCsarHelper = toscaResourceStructure.getSdcCsarHelper(); + + List serInput = iSdcCsarHelper.getServiceInputs(); + Optional nodeTemplateOpt = iSdcCsarHelper.getServiceNodeTemplates().stream() + .filter(e -> e.getMetaData().getValue(CUSTOMIZATION_UUID).equals(resourceCustomizationUuid)).findFirst(); + if(nodeTemplateOpt.isPresent()) { + NodeTemplate nodeTemplate = nodeTemplateOpt.get(); + LinkedHashMap resourceProperties = nodeTemplate.getProperties(); + + for(String key : resourceProperties.keySet()) { + Property property = resourceProperties.get(key); + + String value = getValue(property.getValue(), serInput); + resouceRequest.put(key, value); + } + } + + try { + ObjectMapper objectMapper = new ObjectMapper(); + String jsonStr = objectMapper.writeValueAsString(resouceRequest); + + logger.debug("resource request for resource customization id (" + resourceCustomizationUuid + ") : " + jsonStr); + return jsonStr; + } catch (JsonProcessingException e) { + logger.error("resource input could not be deserialized for resource customization id (" + + resourceCustomizationUuid + ")"); + throw new ArtifactInstallerException("resource input could not be parsed", e); + } + } + protected void processNetworks (ToscaResourceStructure toscaResourceStruct, Service service) throws ArtifactInstallerException { List nodeTemplatesVLList = toscaResourceStruct.getSdcCsarHelper ().getServiceVlList (); @@ -366,7 +502,8 @@ public class ToscaResourceInstaller { null, null, service); - service.getNetworkCustomizations().add (networkCustomization); + networkCustomization.setResourceInput(getResourceInput(toscaResourceStruct, networkCustomization.getModelCustomizationUUID())); + service.getNetworkCustomizations().add (networkCustomization); logger.debug ("No NetworkResourceName found in TempNetworkHeatTemplateLookup for " + networkResourceModelName); } @@ -375,13 +512,14 @@ public class ToscaResourceInstaller { } } - protected void processAllottedResources(ToscaResourceStructure toscaResourceStruct, Service service) { + protected void processAllottedResources(ToscaResourceStructure toscaResourceStruct, Service service) throws ArtifactInstallerException { List allottedResourceList = toscaResourceStruct.getSdcCsarHelper().getAllottedResources(); if (allottedResourceList != null) { - for (NodeTemplate allottedNode : allottedResourceList) { - service.getAllottedCustomizations() - .add(createAllottedResource(allottedNode, toscaResourceStruct, service)); + for (NodeTemplate allottedNode : allottedResourceList) { + AllottedResourceCustomization allottedResource = createAllottedResource(allottedNode, toscaResourceStruct, service); + allottedResource.setResourceInput(getResourceInput(toscaResourceStruct, allottedResource.getModelCustomizationUUID())); + service.getAllottedCustomizations().add(allottedResource); } } } @@ -528,6 +666,7 @@ public class ToscaResourceInstaller { } } + vnfResource.setResourceInput(getResourceInput(toscaResourceStruct, vnfResource.getModelCustomizationUUID())); service.getVnfCustomizations().add(vnfResource); } @@ -1696,7 +1835,7 @@ public class ToscaResourceInstaller { } protected AllottedResourceCustomization createAllottedResource(NodeTemplate nodeTemplate, - ToscaResourceStructure toscaResourceStructure, Service service) { + ToscaResourceStructure toscaResourceStructure, Service service) throws ArtifactInstallerException { AllottedResourceCustomization allottedResourceCustomization = allottedCustomizationRepo .findOneByModelCustomizationUUID( nodeTemplate.getMetaData().getValue(SdcPropertyNames.PROPERTY_NAME_CUSTOMIZATIONUUID)); @@ -1713,6 +1852,8 @@ public class ToscaResourceInstaller { allottedResourceCustomization.setAllottedResource(allottedResource); allottedResource.getAllotedResourceCustomization().add(allottedResourceCustomization); } + + allottedResourceCustomization.setResourceInput(getResourceInput(toscaResourceStructure, allottedResourceCustomization.getModelCustomizationUUID())); return allottedResourceCustomization; } diff --git a/asdc-controller/src/test/java/org/onap/so/asdc/client/test/rest/ASDCRestInterfaceTest.java b/asdc-controller/src/test/java/org/onap/so/asdc/client/test/rest/ASDCRestInterfaceTest.java index 1944c3af73..e61957d8c2 100644 --- a/asdc-controller/src/test/java/org/onap/so/asdc/client/test/rest/ASDCRestInterfaceTest.java +++ b/asdc-controller/src/test/java/org/onap/so/asdc/client/test/rest/ASDCRestInterfaceTest.java @@ -148,6 +148,7 @@ public class ASDCRestInterfaceTest extends BaseTest { AllottedResourceCustomization arCustomization = new AllottedResourceCustomization(); arCustomization.setModelCustomizationUUID("f62bb612-c5d4-4406-865c-0abec30631ba"); arCustomization.setModelInstanceName("rege1802pnf 0"); + arCustomization.setResourceInput("{}"); arCustomizationSet.add(arCustomization); arCustomization.setAllottedResource(expectedService); diff --git a/asdc-controller/src/test/java/org/onap/so/asdc/installer/heat/ToscaResourceInputTest.java b/asdc-controller/src/test/java/org/onap/so/asdc/installer/heat/ToscaResourceInputTest.java new file mode 100644 index 0000000000..cecf70f916 --- /dev/null +++ b/asdc-controller/src/test/java/org/onap/so/asdc/installer/heat/ToscaResourceInputTest.java @@ -0,0 +1,136 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP - SO + * ================================================================================ + * Copyright (C) 2019 Huawei 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. + * ============LICENSE_END========================================================= + */ +package org.onap.so.asdc.installer.heat; + +import org.junit.Rule; +import org.junit.Test; +import org.mockito.Mock; +import org.mockito.junit.MockitoJUnit; +import org.mockito.junit.MockitoRule; +import org.onap.sdc.tosca.parser.api.ISdcCsarHelper; +import org.onap.sdc.toscaparser.api.NodeTemplate; +import org.onap.sdc.toscaparser.api.Property; +import org.onap.sdc.toscaparser.api.elements.Metadata; +import org.onap.sdc.toscaparser.api.functions.GetInput; +import org.onap.sdc.toscaparser.api.parameters.Input; +import org.onap.so.asdc.client.exceptions.ArtifactInstallerException; +import org.onap.so.asdc.installer.ToscaResourceStructure; +import org.onap.so.db.catalog.beans.Service; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashMap; +import java.util.LinkedHashMap; + +import static org.junit.Assert.assertEquals; +import static org.mockito.Matchers.any; +import static org.mockito.Mockito.when; + +public class ToscaResourceInputTest { + @Rule + public MockitoRule rule = MockitoJUnit.rule(); + + @Mock + ISdcCsarHelper sdcCsarHelper; + + @Mock + NodeTemplate nodeTemplate; + + @Mock + Property property; + + @Mock + GetInput getInput; + + @Mock + Input input; + + @Test + public void processResourceSequenceTest() { + ToscaResourceInstaller toscaResourceInstaller = new ToscaResourceInstaller(); + ToscaResourceStructure toscaResourceStructure = new ToscaResourceStructure(); + toscaResourceStructure.setSdcCsarHelper(sdcCsarHelper); + ArrayList inputs = new ArrayList<>(); + Service service = new Service(); + + HashMap hashMap = new HashMap(); + hashMap.put("name", "node1"); + + Metadata metadata = new Metadata(hashMap); + when(nodeTemplate.getMetaData()).thenReturn(metadata); + when(sdcCsarHelper.getServiceInputs()).thenReturn(inputs); + when(sdcCsarHelper.getServiceNodeTemplates()).thenReturn(Arrays.asList(nodeTemplate)); + when(sdcCsarHelper.getRequirementsOf(any())).thenReturn(null); + + + toscaResourceInstaller.processResourceSequence(toscaResourceStructure, service); + assertEquals(service.getResourceOrder(), "node1"); + } + + @Test + public void resouceInputTest() throws ArtifactInstallerException { + ToscaResourceInstaller toscaResourceInstaller = new ToscaResourceInstaller(); + ToscaResourceStructure toscaResourceStructure = new ToscaResourceStructure(); + + toscaResourceStructure.setSdcCsarHelper(sdcCsarHelper); + + HashMap hashMap = new HashMap(); + hashMap.put("customizationUUID", "id1"); + Metadata metadata = new Metadata(hashMap); + + LinkedHashMap propertyMap = new LinkedHashMap(); + propertyMap.put("prop1", property); + + when(sdcCsarHelper.getServiceNodeTemplates()).thenReturn(Arrays.asList(nodeTemplate)); + when(nodeTemplate.getMetaData()).thenReturn(metadata); + when(nodeTemplate.getProperties()).thenReturn(propertyMap); + when(property.getValue()).thenReturn("value1"); + + String resourceInput = toscaResourceInstaller.getResourceInput(toscaResourceStructure, "id1"); + assertEquals(resourceInput, "{\"prop1\":\"value1\"}"); + } + + @Test + public void resouceInputGetInputTest() throws ArtifactInstallerException { + ToscaResourceInstaller toscaResourceInstaller = new ToscaResourceInstaller(); + ToscaResourceStructure toscaResourceStructure = new ToscaResourceStructure(); + + toscaResourceStructure.setSdcCsarHelper(sdcCsarHelper); + + HashMap hashMap = new HashMap(); + hashMap.put("customizationUUID", "id1"); + Metadata metadata = new Metadata(hashMap); + + LinkedHashMap propertyMap = new LinkedHashMap(); + propertyMap.put("prop1", property); + + when(sdcCsarHelper.getServiceNodeTemplates()).thenReturn(Arrays.asList(nodeTemplate)); + when(sdcCsarHelper.getServiceInputs()).thenReturn(Arrays.asList(input)); + when(nodeTemplate.getMetaData()).thenReturn(metadata); + when(nodeTemplate.getProperties()).thenReturn(propertyMap); + when(property.getValue()).thenReturn(getInput); + when(getInput.getInputName()).thenReturn("res_key"); + when(input.getName()).thenReturn("res_key"); + when(input.getDefault()).thenReturn("default_value"); + + String resourceInput = toscaResourceInstaller.getResourceInput(toscaResourceStructure, "id1"); + assertEquals(resourceInput, "{\"prop1\":\"res_key|default_value\"}"); + } +} diff --git a/asdc-controller/src/test/resources/schema.sql b/asdc-controller/src/test/resources/schema.sql index 0372887f15..58772c4998 100644 --- a/asdc-controller/src/test/resources/schema.sql +++ b/asdc-controller/src/test/resources/schema.sql @@ -31,6 +31,7 @@ create table `allotted_resource_customization` ( `min_instances` int(11) default null, `max_instances` int(11) default null, `ar_model_uuid` varchar(200) not null, + `resource_input` varchar(2000) default null, `creation_timestamp` datetime not null default current_timestamp, primary key (`model_customization_uuid`), key `fk_allotted_resource_customization__allotted_resource1_idx` (`ar_model_uuid`), @@ -171,6 +172,7 @@ create table `network_resource_customization` ( `network_scope` varchar(45) default null, `creation_timestamp` datetime not null default current_timestamp, `network_resource_model_uuid` varchar(200) not null, + `resource_input` varchar(2000) default null, primary key (`model_customization_uuid`), key `fk_network_resource_customization__network_resource1_idx` (`network_resource_model_uuid`), constraint `fk_network_resource_customization__network_resource1` foreign key (`network_resource_model_uuid`) references `network_resource` (`model_uuid`) on delete cascade on update cascade @@ -207,6 +209,7 @@ create table `service` ( `environment_context` varchar(200) default null, `workload_context` varchar(200) default null, `service_category` varchar(200) default null, + `resource_order` varchar(200) default null, primary key (`model_uuid`), key `fk_service__tosca_csar1_idx` (`tosca_csar_artifact_uuid`), constraint `fk_service__tosca_csar1` foreign key (`tosca_csar_artifact_uuid`) references `tosca_csar` (`artifact_uuid`) on delete cascade on update cascade @@ -396,6 +399,7 @@ create table `vnf_resource_customization` ( `creation_timestamp` datetime not null default current_timestamp, `vnf_resource_model_uuid` varchar(200) not null, `multi_stage_design` varchar(20) default null, + `resource_input` varchar(2000) default null, primary key (`model_customization_uuid`), key `fk_vnf_resource_customization__vnf_resource1_idx` (`vnf_resource_model_uuid`), constraint `fk_vnf_resource_customization__vnf_resource1` foreign key (`vnf_resource_model_uuid`) references `vnf_resource` (`model_uuid`) on delete cascade on update cascade -- cgit 1.2.3-korg