diff options
author | 2018-02-11 12:15:48 +0200 | |
---|---|---|
committer | 2018-02-11 12:16:01 +0200 | |
commit | d5b4901c6a1ae136138c1300d377447e26bac4ef (patch) | |
tree | e25360262affbacfdf1bcaeb1e8976e6d8ebae58 /openecomp-be/lib/openecomp-sdc-translator-lib/openecomp-sdc-translator-core/src/main/java/org/openecomp | |
parent | eae2ba3f5ccfb20a899262562bd91129b6d53423 (diff) |
SRIOV - add annotations
SRIOV - add annotations to input TOSCA parameter converted from HEAT file (2)
Change-Id: Id0b0156730ae64093df42d09e2873de047c13ac9
Issue-ID: SDC-996
Signed-off-by: eleonorali <eleonoral@amdocs.com>
Diffstat (limited to 'openecomp-be/lib/openecomp-sdc-translator-lib/openecomp-sdc-translator-core/src/main/java/org/openecomp')
3 files changed, 369 insertions, 302 deletions
diff --git a/openecomp-be/lib/openecomp-sdc-translator-lib/openecomp-sdc-translator-core/src/main/java/org/openecomp/sdc/translator/services/heattotosca/HeatToToscaUtil.java b/openecomp-be/lib/openecomp-sdc-translator-lib/openecomp-sdc-translator-core/src/main/java/org/openecomp/sdc/translator/services/heattotosca/HeatToToscaUtil.java index 88e96037f5..dafd8e6207 100644 --- a/openecomp-be/lib/openecomp-sdc-translator-lib/openecomp-sdc-translator-core/src/main/java/org/openecomp/sdc/translator/services/heattotosca/HeatToToscaUtil.java +++ b/openecomp-be/lib/openecomp-sdc-translator-lib/openecomp-sdc-translator-core/src/main/java/org/openecomp/sdc/translator/services/heattotosca/HeatToToscaUtil.java @@ -645,7 +645,11 @@ public class HeatToToscaUtil { * @return the boolean */ public static boolean isHeatFileNested(TranslateTo translateTo, String heatFileName) { - return translateTo.getContext().getNestedHeatsFiles().contains(heatFileName); + return isHeatFileNested(translateTo.getContext(), heatFileName); + } + + public static boolean isHeatFileNested(TranslationContext context, String heatFileName) { + return context.getNestedHeatsFiles().contains(heatFileName); } /** @@ -1190,6 +1194,10 @@ public class HeatToToscaUtil { return !capabilityKey.contains(FORWARDER) || ToggleableFeature.FORWARDER_CAPABILITY.isActive(); } + public static boolean shouldAnnotationsToBeAdded() { + return ToggleableFeature.ANNOTATIONS.isActive(); + } + private static void addCapabilityToSubMapping(String templateName, Map.Entry<String, CapabilityDefinition> capabilityNodeEntry, Map<String, CapabilityDefinition> nodeTypeCapabilitiesDefinition, diff --git a/openecomp-be/lib/openecomp-sdc-translator-lib/openecomp-sdc-translator-core/src/main/java/org/openecomp/sdc/translator/services/heattotosca/TranslationService.java b/openecomp-be/lib/openecomp-sdc-translator-lib/openecomp-sdc-translator-core/src/main/java/org/openecomp/sdc/translator/services/heattotosca/TranslationService.java index d403fcbedc..c84ed22011 100644 --- a/openecomp-be/lib/openecomp-sdc-translator-lib/openecomp-sdc-translator-core/src/main/java/org/openecomp/sdc/translator/services/heattotosca/TranslationService.java +++ b/openecomp-be/lib/openecomp-sdc-translator-lib/openecomp-sdc-translator-core/src/main/java/org/openecomp/sdc/translator/services/heattotosca/TranslationService.java @@ -1,21 +1,17 @@ -/*- - * ============LICENSE_START======================================================= - * SDC - * ================================================================================ - * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. - * ================================================================================ +/* + * Copyright © 2018 European Support Limited + * * 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.openecomp.sdc.translator.services.heattotosca; @@ -39,6 +35,7 @@ import org.openecomp.sdc.tosca.datatypes.model.ParameterDefinition; import org.openecomp.sdc.tosca.datatypes.model.PropertyType; import org.openecomp.sdc.tosca.datatypes.model.ServiceTemplate; import org.openecomp.sdc.tosca.datatypes.model.TopologyTemplate; +import org.openecomp.sdc.tosca.datatypes.model.heatextend.ParameterDefinitionExt; import org.openecomp.sdc.tosca.services.DataModelUtil; import org.openecomp.sdc.tosca.services.ToscaConstants; import org.openecomp.sdc.tosca.services.ToscaFileOutputService; @@ -60,308 +57,343 @@ import java.util.Map; import java.util.Objects; import java.util.Optional; import java.util.Set; +import java.util.stream.Collectors; public class TranslationService { - protected static Logger logger = LoggerFactory.getLogger(TranslationService.class); - /** - * Gets types to process by translator. - * - * @return the types to process by translator - */ - public static Set<FileData.Type> getTypesToProcessByTranslator() { - Set<FileData.Type> types = new HashSet<>(); - types.add(FileData.Type.HEAT); - types.add(FileData.Type.HEAT_VOL); - return types; - } - - /** - * Translate heat files translator output. - * - * @param translationContext the translation context - * @return the translator output - */ - public TranslatorOutput translateHeatFiles(TranslationContext translationContext) { - ServiceTemplate mainServiceTemplate = createMainServiceTemplate(translationContext); - List<FileData> fileDataList = translationContext.getManifest().getContent().getData(); - FileDataCollection fileDataCollection = HeatToToscaUtil.getFileCollectionsByFilter(fileDataList, - TranslationService.getTypesToProcessByTranslator(), translationContext); - - if (fileDataCollection.getBaseFile() != null) { - for (FileData fileData : fileDataCollection.getBaseFile()) { - translateHeatFile(mainServiceTemplate, fileData, translationContext); - } + protected static Logger logger = LoggerFactory.getLogger(TranslationService.class); + + /** + * Gets types to process by translator. + * + * @return the types to process by translator + */ + public static Set<FileData.Type> getTypesToProcessByTranslator() { + Set<FileData.Type> types = new HashSet<>(); + types.add(FileData.Type.HEAT); + types.add(FileData.Type.HEAT_VOL); + return types; } - if (fileDataCollection.getAddOnFiles() != null) { - for (FileData fileData : fileDataCollection.getAddOnFiles()) { - translateHeatFile(mainServiceTemplate, fileData, translationContext); - } + + /** + * Translate heat files translator output. + * + * @param translationContext the translation context + * @return the translator output + */ + public TranslatorOutput translateHeatFiles(TranslationContext translationContext) { + ServiceTemplate mainServiceTemplate = createMainServiceTemplate(translationContext); + List<FileData> fileDataList = translationContext.getManifest().getContent().getData(); + FileDataCollection fileDataCollection = HeatToToscaUtil.getFileCollectionsByFilter(fileDataList, + TranslationService.getTypesToProcessByTranslator(), translationContext); + + if (fileDataCollection.getBaseFile() != null) { + for (FileData fileData : fileDataCollection.getBaseFile()) { + translateHeatFile(mainServiceTemplate, fileData, translationContext); + } + } + if (fileDataCollection.getAddOnFiles() != null) { + for (FileData fileData : fileDataCollection.getAddOnFiles()) { + translateHeatFile(mainServiceTemplate, fileData, translationContext); + } + } + + ToscaServiceModel toscaServiceModel = + HeatToToscaUtil.createToscaServiceModel(mainServiceTemplate, translationContext); + + TranslatorOutput translatorOutput = new TranslatorOutput(); + //Keeping a copy of tosca service model after first stage of translation for extraction of + // composition data + translatorOutput.setNonUnifiedToscaServiceModel( + ToscaServiceModel.getClonedServiceModel(toscaServiceModel)); + translatorOutput.setToscaServiceModel(toscaServiceModel); + return translatorOutput; } - ToscaServiceModel toscaServiceModel = - HeatToToscaUtil.createToscaServiceModel(mainServiceTemplate, translationContext); - - TranslatorOutput translatorOutput = new TranslatorOutput(); - //Keeping a copy of tosca service model after first stage of translation for extraction of - // composition data - translatorOutput.setNonUnifiedToscaServiceModel( - ToscaServiceModel.getClonedServiceModel(toscaServiceModel)); - translatorOutput.setToscaServiceModel(toscaServiceModel); - return translatorOutput; - } - - private ServiceTemplate createMainServiceTemplate(TranslationContext translationContext) { - ServiceTemplate mainServiceTemplate = new ServiceTemplate(); - translationContext.getTranslatedServiceTemplates() - .put(Constants.MAIN_TEMPLATE_NAME, mainServiceTemplate); - Map<String, String> templateMetadata = new HashMap<>(); - templateMetadata.put(ToscaConstants.ST_METADATA_TEMPLATE_NAME, Constants.MAIN_TEMPLATE_NAME); - mainServiceTemplate.setTosca_definitions_version(ToscaConstants.TOSCA_DEFINITIONS_VERSION); - mainServiceTemplate.setMetadata(templateMetadata); - mainServiceTemplate.setTopology_template(new TopologyTemplate()); - mainServiceTemplate.setImports(GlobalTypesGenerator.getGlobalTypesImportList()); - return mainServiceTemplate; - } - - /** - * Translate heat file. - * - * @param serviceTemplate the service template - * @param heatFileData the heat file data - * @param context the context - */ - public void translateHeatFile(ServiceTemplate serviceTemplate, FileData heatFileData, - TranslationContext context) { - String heatFileName = heatFileData.getFile(); - HeatOrchestrationTemplate heatOrchestrationTemplate = new YamlUtil() - .yamlToObject(context.getFileContent(heatFileName), HeatOrchestrationTemplate.class); - - translateInputParameters(serviceTemplate, heatOrchestrationTemplate, heatFileData, context, - heatFileName); - translateResources(heatFileName, serviceTemplate, heatOrchestrationTemplate, context); - translateOutputParameters(serviceTemplate, heatOrchestrationTemplate, heatFileData, - heatFileName, context); - createHeatStackGroup(serviceTemplate, heatFileData, heatOrchestrationTemplate, context); - handleHeatPseudoParam(heatFileName, serviceTemplate, context); - - if (Objects.nonNull(heatFileData.getData())) { - heatFileData.getData().stream().filter(data -> data.getType() == FileData.Type.HEAT_VOL) - .forEach(data -> translateHeatFile(serviceTemplate, data, context)); + private ServiceTemplate createMainServiceTemplate(TranslationContext translationContext) { + ServiceTemplate mainServiceTemplate = new ServiceTemplate(); + translationContext.getTranslatedServiceTemplates() + .put(Constants.MAIN_TEMPLATE_NAME, mainServiceTemplate); + Map<String, String> templateMetadata = new HashMap<>(); + templateMetadata.put(ToscaConstants.ST_METADATA_TEMPLATE_NAME, Constants.MAIN_TEMPLATE_NAME); + mainServiceTemplate.setTosca_definitions_version(ToscaConstants.TOSCA_DEFINITIONS_VERSION); + mainServiceTemplate.setMetadata(templateMetadata); + mainServiceTemplate.setTopology_template(new TopologyTemplate()); + mainServiceTemplate.setImports(GlobalTypesGenerator.getGlobalTypesImportList()); + return mainServiceTemplate; } - } - - private void handleHeatPseudoParam(String heatFileName, ServiceTemplate serviceTemplate, - TranslationContext context) { - Map<String, String> translatedHeatPseudoParam = - context.getUsedHeatPseudoParams().get(heatFileName); - if (Objects.nonNull(translatedHeatPseudoParam)) { - for (String heatPseudoParam : translatedHeatPseudoParam.keySet()) { - if (!serviceTemplate.getTopology_template().getInputs().containsKey(heatPseudoParam)) { - ParameterDefinition parameterDefinition = new ParameterDefinition(); - parameterDefinition.setType(PropertyType.STRING.getDisplayName()); - parameterDefinition.setRequired(false); - String parameterDefinitionId = translatedHeatPseudoParam.get(heatPseudoParam); - DataModelUtil.addInputParameterToTopologyTemplate(serviceTemplate, parameterDefinitionId, - parameterDefinition); + + /** + * Translate heat file. + * + * @param serviceTemplate the service template + * @param heatFileData the heat file data + * @param context the context + */ + public void translateHeatFile(ServiceTemplate serviceTemplate, FileData heatFileData, + TranslationContext context) { + String heatFileName = heatFileData.getFile(); + HeatOrchestrationTemplate heatOrchestrationTemplate = new YamlUtil() + .yamlToObject(context.getFileContent(heatFileName), HeatOrchestrationTemplate.class); + + translateInputParameters(serviceTemplate, heatOrchestrationTemplate, heatFileData, context, + heatFileName); + translateResources(heatFileName, serviceTemplate, heatOrchestrationTemplate, context); + translateOutputParameters(serviceTemplate, heatOrchestrationTemplate, heatFileData, + heatFileName, context); + createHeatStackGroup(serviceTemplate, heatFileData, heatOrchestrationTemplate, context); + handleHeatPseudoParam(heatFileName, serviceTemplate, context); + + if (Objects.nonNull(heatFileData.getData())) { + heatFileData.getData().stream().filter(data -> data.getType() == FileData.Type.HEAT_VOL) + .forEach(data -> translateHeatFile(serviceTemplate, data, context)); } - } } - } - private void createHeatStackGroup(ServiceTemplate serviceTemplate, FileData heatFileData, - HeatOrchestrationTemplate heatOrchestrationTemplate, - TranslationContext context) { - ToscaFileOutputService toscaFileOutputService = new ToscaFileOutputServiceCsarImpl(); - final String fileName = heatFileData.getFile(); - final String heatStackGroupId = FileUtils.getFileWithoutExtention(fileName) + "_group"; - - GroupDefinition groupDefinition = new GroupDefinition(); - groupDefinition.setType(ToscaGroupType.HEAT_STACK); - groupDefinition.setProperties(new HashMap<>()); - groupDefinition.getProperties() - .put("heat_file", "../" + toscaFileOutputService.getArtifactsFolderName() + "/" + fileName); - String hotDescription = heatOrchestrationTemplate.getDescription(); - if (hotDescription != null && !hotDescription.isEmpty()) { - groupDefinition.getProperties().put(Constants.DESCRIPTION_PROPERTY_NAME, hotDescription); + private void handleHeatPseudoParam(String heatFileName, ServiceTemplate serviceTemplate, + TranslationContext context) { + Map<String, String> translatedHeatPseudoParam = + context.getUsedHeatPseudoParams().get(heatFileName); + if (Objects.nonNull(translatedHeatPseudoParam)) { + for (String heatPseudoParam : translatedHeatPseudoParam.keySet()) { + if (!serviceTemplate.getTopology_template().getInputs().containsKey(heatPseudoParam)) { + ParameterDefinition parameterDefinition = new ParameterDefinition(); + parameterDefinition.setType(PropertyType.STRING.getDisplayName()); + parameterDefinition.setRequired(false); + String parameterDefinitionId = translatedHeatPseudoParam.get(heatPseudoParam); + DataModelUtil.addInputParameterToTopologyTemplate(serviceTemplate, parameterDefinitionId, + parameterDefinition); + } + } + } } - groupDefinition.setMembers(new ArrayList<>()); - Set<String> heatStackGroupMembersIds = getHeatStackGroupMembers(fileName, - serviceTemplate, context); - if (CollectionUtils.isEmpty(heatStackGroupMembersIds)) { - return; //not creating a group when no resources are present in the heat input + + private void createHeatStackGroup(ServiceTemplate serviceTemplate, FileData heatFileData, + HeatOrchestrationTemplate heatOrchestrationTemplate, + TranslationContext context) { + ToscaFileOutputService toscaFileOutputService = new ToscaFileOutputServiceCsarImpl(); + final String fileName = heatFileData.getFile(); + final String heatStackGroupId = FileUtils.getFileWithoutExtention(fileName) + "_group"; + + GroupDefinition groupDefinition = new GroupDefinition(); + groupDefinition.setType(ToscaGroupType.HEAT_STACK); + groupDefinition.setProperties(new HashMap<>()); + groupDefinition.getProperties() + .put("heat_file", "../" + toscaFileOutputService.getArtifactsFolderName() + "/" + fileName); + String hotDescription = heatOrchestrationTemplate.getDescription(); + if (hotDescription != null && !hotDescription.isEmpty()) { + groupDefinition.getProperties().put(Constants.DESCRIPTION_PROPERTY_NAME, hotDescription); + } + groupDefinition.setMembers(new ArrayList<>()); + Set<String> heatStackGroupMembersIds = getHeatStackGroupMembers(fileName, + serviceTemplate, context); + if (CollectionUtils.isEmpty(heatStackGroupMembersIds)) { + return; //not creating a group when no resources are present in the heat input + } + groupDefinition.getMembers().addAll(heatStackGroupMembersIds); + DataModelUtil + .addGroupDefinitionToTopologyTemplate(serviceTemplate, heatStackGroupId, groupDefinition); } - groupDefinition.getMembers().addAll(heatStackGroupMembersIds); - DataModelUtil - .addGroupDefinitionToTopologyTemplate(serviceTemplate, heatStackGroupId, groupDefinition); - } - private Set<String> getHeatStackGroupMembers(String heatFileName, - ServiceTemplate serviceTemplate, - TranslationContext context){ + private Set<String> getHeatStackGroupMembers(String heatFileName, + ServiceTemplate serviceTemplate, + TranslationContext context) { + + Map<String, Set<String>> heatStackGroupMembers = context.getHeatStackGroupMembers(); + Set<String> groupMembers = MapUtils.isEmpty(heatStackGroupMembers) ? new HashSet<>() + : heatStackGroupMembers.get(heatFileName); + + if (CollectionUtils.isEmpty(groupMembers)) { + return new HashSet<>(); + } + + Set<String> updatedMembersIds = new HashSet<>(); - Map<String, Set<String>> heatStackGroupMembers = context.getHeatStackGroupMembers(); - Set<String> groupMembers = MapUtils.isEmpty(heatStackGroupMembers) ? new HashSet<>() - : heatStackGroupMembers.get(heatFileName); + groupMembers.forEach(member -> { + if (Objects.nonNull(DataModelUtil.getNodeTemplate(serviceTemplate, member))) { + updatedMembersIds.add(member); + } else { + updateSubstitutableGroupMemberId(heatFileName, serviceTemplate, updatedMembersIds); + } + }); - if(CollectionUtils.isEmpty(groupMembers)){ - return new HashSet<>(); + return updatedMembersIds; } - Set<String> updatedMembersIds = new HashSet<>(); - - groupMembers.forEach(member -> { - if (Objects.nonNull(DataModelUtil.getNodeTemplate(serviceTemplate, member))) { - updatedMembersIds.add(member); - } else { - updateSubstitutableGroupMemberId(heatFileName, serviceTemplate, updatedMembersIds); - } - }); - - return updatedMembersIds; - } - - private void updateSubstitutableGroupMemberId(String heatFileName, - ServiceTemplate serviceTemplate, - Set<String> updatedMembersIds) { - Optional<String> substitutableGroupMemberId = - ToscaUtil.getSubstitutableGroupMemberId(heatFileName, serviceTemplate); - - substitutableGroupMemberId.ifPresent(updatedMembersIds::add); - } - - private void translateInputParameters(ServiceTemplate serviceTemplate, - HeatOrchestrationTemplate heatOrchestrationTemplate, - FileData heatFileData, TranslationContext context, - String heatFileName) { - if (heatOrchestrationTemplate.getParameters() == null) { - return; + private void updateSubstitutableGroupMemberId(String heatFileName, + ServiceTemplate serviceTemplate, + Set<String> updatedMembersIds) { + Optional<String> substitutableGroupMemberId = + ToscaUtil.getSubstitutableGroupMemberId(heatFileName, serviceTemplate); + + substitutableGroupMemberId.ifPresent(updatedMembersIds::add); } - Map<String, ParameterDefinition> parameterDefinitionMap = - TranslatorHeatToToscaParameterConverter - .parameterConverter(serviceTemplate,heatOrchestrationTemplate.getParameters(), - heatOrchestrationTemplate, heatFileName, context); - Environment heatEnvFile = getHeatEnvFile(heatFileData, context); - Map<String, Object> parameters = heatEnvFile.getParameters(); - Object parameterValue; - if (parameters != null) { - for (Map.Entry<String, ParameterDefinition> entry : parameterDefinitionMap.entrySet()) { - String paramName = entry.getKey(); - parameterValue = parameters.get(paramName); - if (parameterValue != null) { - entry.getValue().set_default(TranslatorHeatToToscaParameterConverter - .getToscaParameterDefaultValue(null, null, parameterValue, entry.getValue().getType(), - heatFileName, heatOrchestrationTemplate, context)); + private void translateInputParameters(ServiceTemplate serviceTemplate, + HeatOrchestrationTemplate heatOrchestrationTemplate, + FileData heatFileData, TranslationContext context, + String heatFileName) { + if (heatOrchestrationTemplate.getParameters() == null) { + return; + } + + Map<String, ParameterDefinition> parameterDefinitionMap = + TranslatorHeatToToscaParameterConverter + .parameterConverter(serviceTemplate, heatOrchestrationTemplate.getParameters(), + heatOrchestrationTemplate, heatFileName, context); + Environment heatEnvFile = getHeatEnvFile(heatFileData, context); + Map<String, Object> parameters = heatEnvFile.getParameters(); + Object parameterValue; + if (parameters != null) { + for (Map.Entry<String, ParameterDefinition> entry : parameterDefinitionMap.entrySet()) { + String paramName = entry.getKey(); + parameterValue = parameters.get(paramName); + if (parameterValue != null) { + entry.getValue().set_default(TranslatorHeatToToscaParameterConverter + .getToscaParameterDefaultValue(null, null, parameterValue, entry.getValue().getType(), + heatFileName, heatOrchestrationTemplate, context)); + } + } + } + + + Map<String, ParameterDefinition> inputs = serviceTemplate.getTopology_template().getInputs(); + if (Objects.isNull(inputs)) { + serviceTemplate.getTopology_template().setInputs(parameterDefinitionMap); + } else { + setInputs(inputs, parameterDefinitionMap); + } - } + } - Map<String, ParameterDefinition> inputs = serviceTemplate.getTopology_template().getInputs(); - if (Objects.isNull(inputs)) { - serviceTemplate.getTopology_template().setInputs(parameterDefinitionMap); - } else { - inputs.putAll(parameterDefinitionMap); + private void setInputs(Map<String, ParameterDefinition> inputs, Map<String, ParameterDefinition> newParameters) { + + updateAnnotations(inputs, newParameters); + inputs.putAll(newParameters); + } - } - - private void translateOutputParameters(ServiceTemplate serviceTemplate, - HeatOrchestrationTemplate heatOrchestrationTemplate, - FileData heatFileData, String heatFileName, - TranslationContext context) { - if (heatOrchestrationTemplate.getOutputs() == null) { - return; + + private void updateAnnotations(Map<String, ParameterDefinition> inputParameters, Map<String, ParameterDefinition> newParameters) { + + if (Objects.nonNull(newParameters.entrySet())) { + newParameters.entrySet().stream().filter(stringParameterDefinitionEntry -> + inputParameters.containsKey(stringParameterDefinitionEntry.getKey()) && + isHasAnnotation(inputParameters, stringParameterDefinitionEntry)). + forEach(stringParameterDefinitionEntry -> getVFModulesList(stringParameterDefinitionEntry.getValue()). + addAll(getVFModulesList(inputParameters.get(stringParameterDefinitionEntry.getKey())))); + } } - Map<String, ParameterDefinition> parameterDefinitionMap = - TranslatorHeatToToscaParameterConverter - .parameterOutputConverter(serviceTemplate,heatOrchestrationTemplate.getOutputs(), - heatOrchestrationTemplate, heatFileName, context); - if (serviceTemplate.getTopology_template().getOutputs() != null) { - serviceTemplate.getTopology_template().getOutputs().putAll(parameterDefinitionMap); - } else { - serviceTemplate.getTopology_template().setOutputs(parameterDefinitionMap); + + private boolean isHasAnnotation(Map<String, ParameterDefinition> inputParameters, Map.Entry<String, ParameterDefinition> newParameterSet) { + ParameterDefinitionExt inputParameter = (ParameterDefinitionExt) inputParameters.get(newParameterSet.getKey()); + ParameterDefinitionExt newParameter = (ParameterDefinitionExt) newParameterSet.getValue(); + return inputParameter.getAnnotations() != null && newParameter.getAnnotations() != null; } - if (heatFileData.getBase() != null && heatFileData.getBase().equals(true)) { - updateSharedResources(serviceTemplate, heatFileName, heatOrchestrationTemplate, - heatOrchestrationTemplate.getOutputs(), context); + private List getVFModulesList(ParameterDefinition param) { + ParameterDefinitionExt parameterDefinitionExt = (ParameterDefinitionExt) param; + return (List) parameterDefinitionExt.getAnnotations().get(ToscaConstants.SOURCE_ANNOTATION_ID).getProperties(). + get(ToscaConstants.VF_MODULE_LABEL_PROPERTY_NAME); } - } - - private void updateSharedResources(ServiceTemplate serviceTemplate, String heatFileName, - HeatOrchestrationTemplate heatOrchestrationTemplate, - Map<String, Output> outputs, TranslationContext context) { - for (Map.Entry<String, Output> parameter : outputs.entrySet()) { - Optional<AttachedResourceId> attachedSharedResourceId = HeatToToscaUtil - .extractAttachedResourceId(heatFileName, heatOrchestrationTemplate, context, - parameter.getValue().getValue()); - if (attachedSharedResourceId.isPresent() && attachedSharedResourceId.get().isGetResource() - && attachedSharedResourceId.get().getTranslatedId() != null) { - String sharedTranslatedResourceId = - attachedSharedResourceId.get().getTranslatedId().toString(); - updateSharedResource(serviceTemplate, context, parameter, sharedTranslatedResourceId, - heatOrchestrationTemplate.getResources() - .get(attachedSharedResourceId.get().getEntityId())); - } else { - Optional<String> contrailSharedResourceId = HeatToToscaUtil - .extractContrailGetResourceAttachedHeatResourceId(parameter.getValue().getValue()); - if (contrailSharedResourceId.isPresent() - && context.getTranslatedIds().get(heatFileName).get(contrailSharedResourceId.get()) - != null) { - String sharedTranslatedResourceId = context.getTranslatedIds().get(heatFileName).get - (contrailSharedResourceId.get()); - ConsolidationDataUtil.removeSharedResource(serviceTemplate, heatOrchestrationTemplate, - context, parameter.getKey(),contrailSharedResourceId.get(), sharedTranslatedResourceId); - updateSharedResource(serviceTemplate, context, parameter,sharedTranslatedResourceId, - heatOrchestrationTemplate.getResources().get(contrailSharedResourceId.get())); + + private void translateOutputParameters(ServiceTemplate serviceTemplate, + HeatOrchestrationTemplate heatOrchestrationTemplate, + FileData heatFileData, String heatFileName, + TranslationContext context) { + if (heatOrchestrationTemplate.getOutputs() == null) { + return; + } + Map<String, ParameterDefinition> parameterDefinitionMap = + TranslatorHeatToToscaParameterConverter + .parameterOutputConverter(serviceTemplate, heatOrchestrationTemplate.getOutputs(), + heatOrchestrationTemplate, heatFileName, context); + if (serviceTemplate.getTopology_template().getOutputs() != null) { + serviceTemplate.getTopology_template().getOutputs().putAll(parameterDefinitionMap); + } else { + serviceTemplate.getTopology_template().setOutputs(parameterDefinitionMap); } - } - } - if (serviceTemplate.getTopology_template().getOutputs() != null - && serviceTemplate.getTopology_template().getOutputs().size() == 0) { - serviceTemplate.getTopology_template().setOutputs(null); - } - } - private void updateSharedResource(ServiceTemplate serviceTemplate, TranslationContext context, - Map.Entry<String, Output> paramName, - String sharedTranslatedResourceId, Resource resource) { - context.addHeatSharedResourcesByParam(paramName.getKey(), sharedTranslatedResourceId, resource); - serviceTemplate.getTopology_template().getOutputs().remove(paramName.getKey()); - } + if (heatFileData.getBase() != null && heatFileData.getBase().equals(true)) { + updateSharedResources(serviceTemplate, heatFileName, heatOrchestrationTemplate, + heatOrchestrationTemplate.getOutputs(), context); + } + } - private void translateResources(String heatFileName, ServiceTemplate serviceTemplate, - HeatOrchestrationTemplate heatOrchestrationTemplate, - TranslationContext context) { - if(MapUtils.isEmpty(heatOrchestrationTemplate.getResources())){ - return; + private void updateSharedResources(ServiceTemplate serviceTemplate, String heatFileName, + HeatOrchestrationTemplate heatOrchestrationTemplate, + Map<String, Output> outputs, TranslationContext context) { + for (Map.Entry<String, Output> parameter : outputs.entrySet()) { + Optional<AttachedResourceId> attachedSharedResourceId = HeatToToscaUtil + .extractAttachedResourceId(heatFileName, heatOrchestrationTemplate, context, + parameter.getValue().getValue()); + if (attachedSharedResourceId.isPresent() && attachedSharedResourceId.get().isGetResource() + && attachedSharedResourceId.get().getTranslatedId() != null) { + String sharedTranslatedResourceId = + attachedSharedResourceId.get().getTranslatedId().toString(); + updateSharedResource(serviceTemplate, context, parameter, sharedTranslatedResourceId, + heatOrchestrationTemplate.getResources() + .get(attachedSharedResourceId.get().getEntityId())); + } else { + Optional<String> contrailSharedResourceId = HeatToToscaUtil + .extractContrailGetResourceAttachedHeatResourceId(parameter.getValue().getValue()); + if (contrailSharedResourceId.isPresent() + && context.getTranslatedIds().get(heatFileName).get(contrailSharedResourceId.get()) + != null) { + String sharedTranslatedResourceId = context.getTranslatedIds().get(heatFileName).get + (contrailSharedResourceId.get()); + ConsolidationDataUtil.removeSharedResource(serviceTemplate, heatOrchestrationTemplate, + context, parameter.getKey(), contrailSharedResourceId.get(), sharedTranslatedResourceId); + updateSharedResource(serviceTemplate, context, parameter, sharedTranslatedResourceId, + heatOrchestrationTemplate.getResources().get(contrailSharedResourceId.get())); + } + } + } + if (serviceTemplate.getTopology_template().getOutputs() != null + && serviceTemplate.getTopology_template().getOutputs().size() == 0) { + serviceTemplate.getTopology_template().setOutputs(null); + } } - for (String resourceId : heatOrchestrationTemplate.getResources().keySet()) { - Resource resource = heatOrchestrationTemplate.getResources().get(resourceId); - if (resource == null) { - throw new CoreException( - new ResourceNotFoundInHeatFileErrorBuilder(resourceId, heatFileName).build()); - } - ResourceTranslationFactory.getInstance(resource) - .translateResource(heatFileName, serviceTemplate, heatOrchestrationTemplate, resource, - resourceId, context); + private void updateSharedResource(ServiceTemplate serviceTemplate, TranslationContext context, + Map.Entry<String, Output> paramName, + String sharedTranslatedResourceId, Resource resource) { + context.addHeatSharedResourcesByParam(paramName.getKey(), sharedTranslatedResourceId, resource); + serviceTemplate.getTopology_template().getOutputs().remove(paramName.getKey()); } - } - private Environment getHeatEnvFile(FileData heatFileData, TranslationContext context) { - List<FileData> fileRelatedDataList = heatFileData.getData(); - if (fileRelatedDataList == null) { - return new Environment(); + private void translateResources(String heatFileName, ServiceTemplate serviceTemplate, + HeatOrchestrationTemplate heatOrchestrationTemplate, + TranslationContext context) { + if (MapUtils.isEmpty(heatOrchestrationTemplate.getResources())) { + return; + } + + for (String resourceId : heatOrchestrationTemplate.getResources().keySet()) { + Resource resource = heatOrchestrationTemplate.getResources().get(resourceId); + if (resource == null) { + throw new CoreException( + new ResourceNotFoundInHeatFileErrorBuilder(resourceId, heatFileName).build()); + } + ResourceTranslationFactory.getInstance(resource) + .translateResource(heatFileName, serviceTemplate, heatOrchestrationTemplate, resource, + resourceId, context); + } } - for (FileData fileRelatedData : fileRelatedDataList) { - if (fileRelatedData.getType().equals(FileData.Type.HEAT_ENV)) { - return new YamlUtil().yamlToObject(context.getFileContent(fileRelatedData.getFile()), - Environment.class); - } + + private Environment getHeatEnvFile(FileData heatFileData, TranslationContext context) { + List<FileData> fileRelatedDataList = heatFileData.getData(); + if (fileRelatedDataList == null) { + return new Environment(); + } + for (FileData fileRelatedData : fileRelatedDataList) { + if (fileRelatedData.getType().equals(FileData.Type.HEAT_ENV)) { + return new YamlUtil().yamlToObject(context.getFileContent(fileRelatedData.getFile()), + Environment.class); + } + } + return new Environment(); } - return new Environment(); - } } diff --git a/openecomp-be/lib/openecomp-sdc-translator-lib/openecomp-sdc-translator-core/src/main/java/org/openecomp/sdc/translator/services/heattotosca/mapping/TranslatorHeatToToscaParameterConverter.java b/openecomp-be/lib/openecomp-sdc-translator-lib/openecomp-sdc-translator-core/src/main/java/org/openecomp/sdc/translator/services/heattotosca/mapping/TranslatorHeatToToscaParameterConverter.java index 35b617be99..930a2dee58 100644 --- a/openecomp-be/lib/openecomp-sdc-translator-lib/openecomp-sdc-translator-core/src/main/java/org/openecomp/sdc/translator/services/heattotosca/mapping/TranslatorHeatToToscaParameterConverter.java +++ b/openecomp-be/lib/openecomp-sdc-translator-lib/openecomp-sdc-translator-core/src/main/java/org/openecomp/sdc/translator/services/heattotosca/mapping/TranslatorHeatToToscaParameterConverter.java @@ -1,42 +1,37 @@ -/*- - * ============LICENSE_START======================================================= - * SDC - * ================================================================================ - * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. - * ================================================================================ +/* + * Copyright © 2018 European Support Limited + * * 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.openecomp.sdc.translator.services.heattotosca.mapping; import org.apache.commons.collections4.MapUtils; +import org.openecomp.core.utilities.file.FileUtils; import org.openecomp.sdc.heat.datatypes.model.HeatOrchestrationTemplate; import org.openecomp.sdc.heat.datatypes.model.Output; import org.openecomp.sdc.heat.datatypes.model.Parameter; -import org.openecomp.sdc.tosca.datatypes.model.Constraint; -import org.openecomp.sdc.tosca.datatypes.model.EntrySchema; -import org.openecomp.sdc.tosca.datatypes.model.ParameterDefinition; -import org.openecomp.sdc.tosca.datatypes.model.ServiceTemplate; +import org.openecomp.sdc.tosca.datatypes.extend.ToscaAnnotationType; +import org.openecomp.sdc.tosca.datatypes.model.*; +import org.openecomp.sdc.tosca.datatypes.model.heatextend.AnnotationDefinition; import org.openecomp.sdc.tosca.datatypes.model.heatextend.ParameterDefinitionExt; +import org.openecomp.sdc.tosca.services.ToscaConstants; +import org.openecomp.sdc.tosca.services.ToscaUtil; import org.openecomp.sdc.translator.datatypes.heattotosca.TranslationContext; import org.openecomp.sdc.translator.services.heattotosca.FunctionTranslationFactory; +import org.openecomp.sdc.translator.services.heattotosca.HeatToToscaUtil; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.HashMap; -import java.util.List; -import java.util.Map; +import java.util.*; public class TranslatorHeatToToscaParameterConverter { @@ -73,11 +68,9 @@ public class TranslatorHeatToToscaParameterConverter { String heatFileName, TranslationContext context) { Map<String, ParameterDefinition> parameterDefinitionMap = new HashMap<>(); for (Map.Entry<String, Parameter> entry : parameters.entrySet()) { - //parameterDefinitionMap.put(entry.getKey()+"_"+ FileUtils.getFileWithoutExtention - // (heatFileName),getToscaParameter(entry.getValue(), heatOrchestrationTemplate, - // heatFileName, context)); - parameterDefinitionMap.put(entry.getKey(), - getToscaParameter(serviceTemplate,entry.getKey(), entry.getValue(), + String heatParamName = entry.getKey(); + parameterDefinitionMap.put(heatParamName, + getToscaParameter(serviceTemplate,heatParamName, entry.getValue(), heatOrchestrationTemplate, heatFileName, context)); } @@ -118,7 +111,7 @@ public class TranslatorHeatToToscaParameterConverter { * @return the tosca parameter */ public static ParameterDefinitionExt getToscaParameter(ServiceTemplate serviceTemplate, - String parameterName, + String heatParameterName, Parameter heatParameter, HeatOrchestrationTemplate heatOrchestrationTemplate, @@ -131,14 +124,48 @@ public class TranslatorHeatToToscaParameterConverter { toscaParameter.setLabel(heatParameter.getLabel()); toscaParameter.setDescription(heatParameter.getDescription()); toscaParameter.set_default( - getToscaParameterDefaultValue(serviceTemplate, parameterName, heatParameter.get_default(), + getToscaParameterDefaultValue(serviceTemplate, heatParameterName, heatParameter.get_default(), toscaParameter.getType(), heatFileName, heatOrchestrationTemplate, context)); toscaParameter.setHidden(heatParameter.isHidden()); toscaParameter.setImmutable(heatParameter.isImmutable()); toscaParameter.setConstraints(getToscaConstrains(heatParameter.getConstraints())); + Optional<Map<String, AnnotationDefinition>> annotations = getToscaAnnotations(context, serviceTemplate, heatFileName, heatParameterName); + annotations.ifPresent(ant->toscaParameter.setAnnotations(annotations.get())); + + return toscaParameter; } + private static Optional<Map<String, AnnotationDefinition> > getToscaAnnotations (TranslationContext context, ServiceTemplate serviceTemplate, String heatFileName, String heatParameterName){ + + if(!isAnnotationRequired(context, serviceTemplate, heatFileName)){ + return Optional.empty(); + } + + AnnotationDefinition annotationDefinition = new AnnotationDefinition(); + annotationDefinition.setType(ToscaAnnotationType.SOURCE); + annotationDefinition.setProperties(new HashMap<>()); + List<String> vfModuleList = new ArrayList<>(); + vfModuleList.add( FileUtils.getFileWithoutExtention(heatFileName)); + annotationDefinition.getProperties().put(ToscaConstants.VF_MODULE_LABEL_PROPERTY_NAME, vfModuleList); + annotationDefinition.getProperties().put(ToscaConstants.SOURCE_TYPE_PROPERTY_NAME, ToscaConstants.HEAT_SOURCE_TYPE); + annotationDefinition.getProperties().put(ToscaConstants.PARAM_NAME_PROPERTY_NAME, heatParameterName); + Map<String, AnnotationDefinition> annotationMap = new HashMap<>(); + annotationMap.put(ToscaConstants.SOURCE_ANNOTATION_ID, annotationDefinition); + return Optional.of(annotationMap); + + } + + private static boolean isAnnotationRequired(TranslationContext context, ServiceTemplate serviceTemplate, String heatFileName){ + return HeatToToscaUtil.shouldAnnotationsToBeAdded() && !isNestedServiceTemplate(context, serviceTemplate, heatFileName); + } + + private static boolean isNestedServiceTemplate(TranslationContext context, ServiceTemplate serviceTemplate, String heatFileName) { + String serviceTemplateFileName = ToscaUtil.getServiceTemplateFileName(serviceTemplate.getMetadata()); + return HeatToToscaUtil.isHeatFileNested(context, heatFileName) || context.getNestedHeatFileName().containsKey(serviceTemplateFileName); + } + + /** * Gets tosca output parameter. * @@ -148,7 +175,7 @@ public class TranslatorHeatToToscaParameterConverter { * @param context the context * @return the tosca output parameter */ - public static ParameterDefinitionExt getToscaOutputParameter(ServiceTemplate serviceTemplate, + private static ParameterDefinitionExt getToscaOutputParameter(ServiceTemplate serviceTemplate, String parameterName, Output heatOutputParameter, HeatOrchestrationTemplate |