diff options
Diffstat (limited to 'openecomp-be/lib/openecomp-sdc-translator-lib/openecomp-sdc-translator-core/src/main/java/org/openecomp/sdc/translator/services/heattotosca/helper')
5 files changed, 434 insertions, 0 deletions
diff --git a/openecomp-be/lib/openecomp-sdc-translator-lib/openecomp-sdc-translator-core/src/main/java/org/openecomp/sdc/translator/services/heattotosca/helper/ContrailTranslationHelper.java b/openecomp-be/lib/openecomp-sdc-translator-lib/openecomp-sdc-translator-core/src/main/java/org/openecomp/sdc/translator/services/heattotosca/helper/ContrailTranslationHelper.java new file mode 100644 index 0000000000..2e1aa9e372 --- /dev/null +++ b/openecomp-be/lib/openecomp-sdc-translator-lib/openecomp-sdc-translator-core/src/main/java/org/openecomp/sdc/translator/services/heattotosca/helper/ContrailTranslationHelper.java @@ -0,0 +1,65 @@ +/*- + * ============LICENSE_START======================================================= + * SDC + * ================================================================================ + * Copyright (C) 2017 AT&T 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.openecomp.sdc.translator.services.heattotosca.helper; + +import org.openecomp.sdc.heat.datatypes.model.Resource; +import org.openecomp.sdc.tosca.services.ToscaConstants; +import org.openecomp.sdc.translator.services.heattotosca.helper.impl.NameExtractorServiceImpl; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import java.util.Optional; + +public class ContrailTranslationHelper { + /** + * Gets compute node type id. + * + * @param serviceTemplateTranslatedId the service template translated id + * @param serviceTemplateResource the service template resource + * @return the compute node type id + */ + public String getComputeNodeTypeId(String serviceTemplateTranslatedId, + Resource serviceTemplateResource) { + NameExtractorService nodeTypeNameExtractor = new NameExtractorServiceImpl(); + List<PropertyRegexMatcher> propertyRegexMatchers = + getPropertiesAndRegexMatchers(nodeTypeNameExtractor); + Optional<String> extractedNodeTypeName = nodeTypeNameExtractor + .extractNodeTypeNameByPropertiesPriority(serviceTemplateResource.getProperties(), + propertyRegexMatchers); + + return ToscaConstants.NODES_PREFIX + + (extractedNodeTypeName.isPresent() ? extractedNodeTypeName.get() + : "compute_" + serviceTemplateTranslatedId); + } + + private List<PropertyRegexMatcher> getPropertiesAndRegexMatchers( + NameExtractorService nodeTypeNameExtractor) { + List<PropertyRegexMatcher> propertyRegexMatchers = new ArrayList<>(); + propertyRegexMatchers.add(nodeTypeNameExtractor + .getPropertyRegexMatcher("image_name", Collections.singletonList(".+_image_name$"), + "_image_name")); + propertyRegexMatchers.add(nodeTypeNameExtractor + .getPropertyRegexMatcher("flavor", Collections.singletonList(".+_flavor_name$"), + "_flavor_name")); + return propertyRegexMatchers; + } +} diff --git a/openecomp-be/lib/openecomp-sdc-translator-lib/openecomp-sdc-translator-core/src/main/java/org/openecomp/sdc/translator/services/heattotosca/helper/NameExtractorService.java b/openecomp-be/lib/openecomp-sdc-translator-lib/openecomp-sdc-translator-core/src/main/java/org/openecomp/sdc/translator/services/heattotosca/helper/NameExtractorService.java new file mode 100644 index 0000000000..ed338e2bf5 --- /dev/null +++ b/openecomp-be/lib/openecomp-sdc-translator-lib/openecomp-sdc-translator-core/src/main/java/org/openecomp/sdc/translator/services/heattotosca/helper/NameExtractorService.java @@ -0,0 +1,33 @@ +/*- + * ============LICENSE_START======================================================= + * SDC + * ================================================================================ + * Copyright (C) 2017 AT&T 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.openecomp.sdc.translator.services.heattotosca.helper; + +import java.util.List; +import java.util.Map; +import java.util.Optional; + +public interface NameExtractorService { + Optional<String> extractNodeTypeNameByPropertiesPriority( + Map<String, Object> properties,List<PropertyRegexMatcher> propertiesRegexMatchers); + + PropertyRegexMatcher getPropertyRegexMatcher(String propertyName, List<String> regexMatchers, + String propertyValueSearchTerm); +} diff --git a/openecomp-be/lib/openecomp-sdc-translator-lib/openecomp-sdc-translator-core/src/main/java/org/openecomp/sdc/translator/services/heattotosca/helper/PropertyRegexMatcher.java b/openecomp-be/lib/openecomp-sdc-translator-lib/openecomp-sdc-translator-core/src/main/java/org/openecomp/sdc/translator/services/heattotosca/helper/PropertyRegexMatcher.java new file mode 100644 index 0000000000..b04234b413 --- /dev/null +++ b/openecomp-be/lib/openecomp-sdc-translator-lib/openecomp-sdc-translator-core/src/main/java/org/openecomp/sdc/translator/services/heattotosca/helper/PropertyRegexMatcher.java @@ -0,0 +1,69 @@ +/*- + * ============LICENSE_START======================================================= + * SDC + * ================================================================================ + * Copyright (C) 2017 AT&T 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.openecomp.sdc.translator.services.heattotosca.helper; + +import org.apache.commons.collections.CollectionUtils; + +import java.util.ArrayList; +import java.util.List; +import java.util.regex.Pattern; + +public class PropertyRegexMatcher { + private String propertyName; + private List<Pattern> regexPatterns; + private String stringToSearchForPropertyValue; + + public String getPropertyName() { + return propertyName; + } + + public void setPropertyName(String propertyName) { + this.propertyName = propertyName; + } + + /** + * Sets regex. + * + * @param regexPatterns the regex patterns + */ + public void setRegex(List<String> regexPatterns) { + if (CollectionUtils.isEmpty(this.regexPatterns)) { + this.regexPatterns = new ArrayList<>(); + } + + for (String regexPattern : regexPatterns) { + this.regexPatterns.add(Pattern.compile(regexPattern)); + } + } + + public List<Pattern> getRegexPatterns() { + return regexPatterns; + } + + public String getStringToSearchForPropertyValue() { + return stringToSearchForPropertyValue; + } + + public void setStringToSearchForPropertyValue(String stringToSearchForPropertyValue) { + this.stringToSearchForPropertyValue = stringToSearchForPropertyValue; + } + +} diff --git a/openecomp-be/lib/openecomp-sdc-translator-lib/openecomp-sdc-translator-core/src/main/java/org/openecomp/sdc/translator/services/heattotosca/helper/VolumeTranslationHelper.java b/openecomp-be/lib/openecomp-sdc-translator-lib/openecomp-sdc-translator-core/src/main/java/org/openecomp/sdc/translator/services/heattotosca/helper/VolumeTranslationHelper.java new file mode 100644 index 0000000000..ebd38c89d2 --- /dev/null +++ b/openecomp-be/lib/openecomp-sdc-translator-lib/openecomp-sdc-translator-core/src/main/java/org/openecomp/sdc/translator/services/heattotosca/helper/VolumeTranslationHelper.java @@ -0,0 +1,172 @@ +/*- + * ============LICENSE_START======================================================= + * SDC + * ================================================================================ + * Copyright (C) 2017 AT&T 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.openecomp.sdc.translator.services.heattotosca.helper; + +import org.apache.commons.collections4.CollectionUtils; +import org.openecomp.core.utilities.yaml.YamlUtil; +import org.openecomp.sdc.heat.datatypes.manifest.FileData; +import org.openecomp.sdc.heat.datatypes.model.HeatOrchestrationTemplate; +import org.openecomp.sdc.heat.datatypes.model.HeatResourcesTypes; +import org.openecomp.sdc.heat.datatypes.model.Output; +import org.openecomp.sdc.heat.datatypes.model.Resource; +import org.openecomp.sdc.translator.datatypes.heattotosca.AttachedResourceId; +import org.openecomp.sdc.translator.datatypes.heattotosca.to.ResourceFileDataAndIDs; +import org.openecomp.sdc.translator.datatypes.heattotosca.to.TranslateTo; +import org.openecomp.sdc.translator.services.heattotosca.HeatToToscaUtil; +import org.openecomp.sdc.translator.services.heattotosca.TranslationContext; +import org.openecomp.sdc.translator.services.heattotosca.impl.ResourceTranslationBase; +import org.slf4j.Logger; + +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import java.util.Objects; +import java.util.Optional; +import java.util.function.Predicate; +import java.util.stream.Collectors; + +public class VolumeTranslationHelper { + private final Logger logger; + + public VolumeTranslationHelper(Logger logger) { + this.logger = logger; + } + + /** + * Gets file data containing volume. + * + * @param filesToSearch the files to search + * @param resourceId the resource id + * @param translateTo the translate to + * @param types the types + * @return the file data containing volume + */ + public Optional<ResourceFileDataAndIDs> getFileDataContainingVolume(List<FileData> filesToSearch, + String resourceId, + TranslateTo translateTo, + FileData.Type... types) { + if (CollectionUtils.isEmpty(filesToSearch)) { + return Optional.empty(); + } + + List<FileData> fileDatas = Objects.isNull(types) ? filesToSearch + : HeatToToscaUtil.getFilteredListOfFileDataByTypes(filesToSearch, types); + Optional<ResourceFileDataAndIDs> fileDataAndIDs = + getResourceFileDataAndIDsForVolumeConnection(resourceId, translateTo, fileDatas); + if (fileDataAndIDs.isPresent()) { + return fileDataAndIDs; + } + return Optional.empty(); + } + + private Optional<ResourceFileDataAndIDs> getResourceFileDataAndIDsForVolumeConnection( + String resourceId, TranslateTo translateTo, List<FileData> fileDatas) { + for (FileData data : fileDatas) { + HeatOrchestrationTemplate heatOrchestrationTemplate = new YamlUtil() + .yamlToObject(translateTo.getContext().getFiles().getFileContent(data.getFile()), + HeatOrchestrationTemplate.class); + Map<String, Output> outputs = heatOrchestrationTemplate.getOutputs(); + if (Objects.isNull(outputs)) { + continue; + } + Output output = outputs.get(resourceId); + if (Objects.nonNull(output)) { + Optional<AttachedResourceId> attachedOutputId = HeatToToscaUtil + .extractAttachedResourceId(data.getFile(), heatOrchestrationTemplate, + translateTo.getContext(), output.getValue()); + if (attachedOutputId.isPresent()) { + AttachedResourceId attachedResourceId = attachedOutputId.get(); + if (!isOutputIsGetResource(resourceId, data, attachedResourceId)) { + continue; + } + String translatedId = (String) attachedResourceId.getTranslatedId(); + if (isOutputOfTypeCinderVolume(translateTo, data, heatOrchestrationTemplate, + translatedId)) { + ResourceFileDataAndIDs fileDataAndIDs = + new ResourceFileDataAndIDs((String) attachedResourceId.getEntityId(), + translatedId, + data); + return Optional.of(fileDataAndIDs); + } else { + logger.warn( + "output: '" + resourceId + "' in file '" + data.getFile() + "' is not of type '" + + HeatResourcesTypes.CINDER_VOLUME_RESOURCE_TYPE.getHeatResource() + "'"); + } + } + } else { + logger.warn("output: '" + resourceId + "' in file '" + data.getFile() + "' is not found"); + } + } + return Optional.empty(); + } + + private boolean isOutputOfTypeCinderVolume(TranslateTo translateTo, FileData data, + HeatOrchestrationTemplate heatOrchestrationTemplate, + String translatedId) { + return getResourceByTranslatedResourceId(data.getFile(), heatOrchestrationTemplate, + translatedId, translateTo, + HeatResourcesTypes.CINDER_VOLUME_RESOURCE_TYPE.getHeatResource()) + .isPresent(); + } + + private Optional<List<Map.Entry<String, Resource>>> getResourceByTranslatedResourceId( + String fileName, HeatOrchestrationTemplate heatOrchestrationTemplate, + String translatedResourceId, TranslateTo translateTo, String heatResourceType) { + List<Map.Entry<String, Resource>> list = heatOrchestrationTemplate.getResources().entrySet() + .stream() + .filter( + entry -> getPredicatesForTranslatedIdToResourceId(fileName, heatOrchestrationTemplate, + translatedResourceId, translateTo.getContext(), heatResourceType) + .stream() + .allMatch(p -> p.test(entry))) + .collect(Collectors.toList()); + if (CollectionUtils.isEmpty(list)) { + return Optional.empty(); + } else { + return Optional.of(list); + } + } + + private List<Predicate<Map.Entry<String, Resource>>> getPredicatesForTranslatedIdToResourceId( + String fileName, HeatOrchestrationTemplate heatOrchestrationTemplate, + String translatedResourceId, TranslationContext context, String heatResourceType) { + List<Predicate<Map.Entry<String, Resource>>> list = new ArrayList<>(); + list.add(entry -> entry.getValue().getType().equals(heatResourceType)); + list.add(entry -> { + Optional<String> resourceTranslatedId = ResourceTranslationBase + .getResourceTranslatedId(fileName, heatOrchestrationTemplate, entry.getKey(), context); + return resourceTranslatedId.isPresent() + && resourceTranslatedId.get().equals(translatedResourceId); + }); + return list; + } + + private boolean isOutputIsGetResource(String resourceId, FileData data, + AttachedResourceId attachedResourceId) { + if (attachedResourceId.isGetResource()) { + return true; + } else { + logger.warn("output: '" + resourceId + "' in file '" + data.getFile() + + "' is not defined as get_resource and therefor not supported."); + return false; + } + } +} diff --git a/openecomp-be/lib/openecomp-sdc-translator-lib/openecomp-sdc-translator-core/src/main/java/org/openecomp/sdc/translator/services/heattotosca/helper/impl/NameExtractorServiceImpl.java b/openecomp-be/lib/openecomp-sdc-translator-lib/openecomp-sdc-translator-core/src/main/java/org/openecomp/sdc/translator/services/heattotosca/helper/impl/NameExtractorServiceImpl.java new file mode 100644 index 0000000000..9bd3494698 --- /dev/null +++ b/openecomp-be/lib/openecomp-sdc-translator-lib/openecomp-sdc-translator-core/src/main/java/org/openecomp/sdc/translator/services/heattotosca/helper/impl/NameExtractorServiceImpl.java @@ -0,0 +1,95 @@ +/*- + * ============LICENSE_START======================================================= + * SDC + * ================================================================================ + * Copyright (C) 2017 AT&T 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.openecomp.sdc.translator.services.heattotosca.helper.impl; + +import org.openecomp.sdc.translator.datatypes.heattotosca.AttachedResourceId; +import org.openecomp.sdc.translator.services.heattotosca.HeatToToscaUtil; +import org.openecomp.sdc.translator.services.heattotosca.helper.NameExtractorService; +import org.openecomp.sdc.translator.services.heattotosca.helper.PropertyRegexMatcher; + +import java.util.List; +import java.util.Map; +import java.util.Optional; +import java.util.regex.Pattern; + +public class NameExtractorServiceImpl implements NameExtractorService { + + @Override + public Optional<String> extractNodeTypeNameByPropertiesPriority( + Map<String, Object> properties,List<PropertyRegexMatcher> propertiesRegexMatchers) { + for (PropertyRegexMatcher propertyRegexMatcher : propertiesRegexMatchers) { + Optional<String> parameterNameValue = + getPropertyParameterNameValue(properties, propertyRegexMatcher.getPropertyName()); + if (parameterNameValue.isPresent()) { + if (isPropertyValueMatchNamingConvention(propertyRegexMatcher, parameterNameValue.get())) { + return Optional.of(parameterNameValue.get().substring(0, parameterNameValue.get() + .lastIndexOf(propertyRegexMatcher.getStringToSearchForPropertyValue()))); + } + } + } + + return Optional.empty(); + } + + private boolean isPropertyValueMatchNamingConvention(PropertyRegexMatcher propertyRegexMatcher, + String propertyValue) { + for (Pattern pattern : propertyRegexMatcher.getRegexPatterns()) { + if (pattern.matcher(propertyValue).matches()) { + return true; + } + } + return false; + } + + private Optional<String> getPropertyParameterNameValue(Map<String, Object> properties, + String prop) { + Object propObj = properties.get(prop); + Optional<AttachedResourceId> property = HeatToToscaUtil.extractProperty(propObj); + if (property.isPresent()) { + AttachedResourceId extractedProperty = property.get(); + return getParameterName(extractedProperty); + } + return Optional.empty(); + } + + private Optional<String> getParameterName(AttachedResourceId extractedProperty) { + if (!extractedProperty.isGetParam()) { + return Optional.empty(); + } + Object entityId = extractedProperty.getEntityId(); + if (entityId instanceof String) { + return Optional.of((String) entityId); + } else { + return Optional.of((String) ((List) entityId).get(0)); + } + } + + @Override + public PropertyRegexMatcher getPropertyRegexMatcher(String propertyName, + List<String> regexMatchers, + String propertyValueSearchTerm) { + PropertyRegexMatcher propertyRegexMatcher = new PropertyRegexMatcher(); + propertyRegexMatcher.setPropertyName(propertyName); + propertyRegexMatcher.setRegex(regexMatchers); + propertyRegexMatcher.setStringToSearchForPropertyValue(propertyValueSearchTerm); + return propertyRegexMatcher; + } +} |