2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.openecomp.sdc.translator.services.heattotosca.helper;
23 import org.apache.commons.collections4.CollectionUtils;
24 import org.openecomp.core.utilities.yaml.YamlUtil;
25 import org.openecomp.sdc.heat.datatypes.manifest.FileData;
26 import org.openecomp.sdc.heat.datatypes.model.HeatOrchestrationTemplate;
27 import org.openecomp.sdc.heat.datatypes.model.HeatResourcesTypes;
28 import org.openecomp.sdc.heat.datatypes.model.Output;
29 import org.openecomp.sdc.heat.datatypes.model.Resource;
30 import org.openecomp.sdc.translator.datatypes.heattotosca.AttachedResourceId;
31 import org.openecomp.sdc.translator.datatypes.heattotosca.to.ResourceFileDataAndIDs;
32 import org.openecomp.sdc.translator.datatypes.heattotosca.to.TranslateTo;
33 import org.openecomp.sdc.translator.services.heattotosca.HeatToToscaUtil;
34 import org.openecomp.sdc.translator.services.heattotosca.TranslationContext;
35 import org.openecomp.sdc.translator.services.heattotosca.impl.ResourceTranslationBase;
36 import org.slf4j.Logger;
38 import java.util.ArrayList;
39 import java.util.List;
41 import java.util.Objects;
42 import java.util.Optional;
43 import java.util.function.Predicate;
44 import java.util.stream.Collectors;
46 public class VolumeTranslationHelper {
47 private final Logger logger;
49 public VolumeTranslationHelper(Logger logger) {
54 * Gets file data containing volume.
56 * @param filesToSearch the files to search
57 * @param resourceId the resource id
58 * @param translateTo the translate to
59 * @param types the types
60 * @return the file data containing volume
62 public Optional<ResourceFileDataAndIDs> getFileDataContainingVolume(List<FileData> filesToSearch,
64 TranslateTo translateTo,
65 FileData.Type... types) {
66 if (CollectionUtils.isEmpty(filesToSearch)) {
67 return Optional.empty();
70 List<FileData> fileDatas = Objects.isNull(types) ? filesToSearch
71 : HeatToToscaUtil.getFilteredListOfFileDataByTypes(filesToSearch, types);
72 Optional<ResourceFileDataAndIDs> fileDataAndIDs =
73 getResourceFileDataAndIDsForVolumeConnection(resourceId, translateTo, fileDatas);
74 if (fileDataAndIDs.isPresent()) {
75 return fileDataAndIDs;
77 return Optional.empty();
80 private Optional<ResourceFileDataAndIDs> getResourceFileDataAndIDsForVolumeConnection(
81 String resourceId, TranslateTo translateTo, List<FileData> fileDatas) {
82 for (FileData data : fileDatas) {
83 HeatOrchestrationTemplate heatOrchestrationTemplate = new YamlUtil()
84 .yamlToObject(translateTo.getContext().getFiles().getFileContent(data.getFile()),
85 HeatOrchestrationTemplate.class);
86 Map<String, Output> outputs = heatOrchestrationTemplate.getOutputs();
87 if (Objects.isNull(outputs)) {
90 Output output = outputs.get(resourceId);
91 if (Objects.nonNull(output)) {
92 Optional<AttachedResourceId> attachedOutputId = HeatToToscaUtil
93 .extractAttachedResourceId(data.getFile(), heatOrchestrationTemplate,
94 translateTo.getContext(), output.getValue());
95 if (attachedOutputId.isPresent()) {
96 AttachedResourceId attachedResourceId = attachedOutputId.get();
97 if (!isOutputIsGetResource(resourceId, data, attachedResourceId)) {
100 String translatedId = (String) attachedResourceId.getTranslatedId();
101 if (isOutputOfTypeCinderVolume(translateTo, data, heatOrchestrationTemplate,
103 ResourceFileDataAndIDs fileDataAndIDs =
104 new ResourceFileDataAndIDs((String) attachedResourceId.getEntityId(),
107 return Optional.of(fileDataAndIDs);
110 "output: '" + resourceId + "' in file '" + data.getFile() + "' is not of type '"
111 + HeatResourcesTypes.CINDER_VOLUME_RESOURCE_TYPE.getHeatResource() + "'");
115 logger.warn("output: '" + resourceId + "' in file '" + data.getFile() + "' is not found");
118 return Optional.empty();
121 private boolean isOutputOfTypeCinderVolume(TranslateTo translateTo, FileData data,
122 HeatOrchestrationTemplate heatOrchestrationTemplate,
123 String translatedId) {
124 return getResourceByTranslatedResourceId(data.getFile(), heatOrchestrationTemplate,
125 translatedId, translateTo,
126 HeatResourcesTypes.CINDER_VOLUME_RESOURCE_TYPE.getHeatResource())
130 private Optional<List<Map.Entry<String, Resource>>> getResourceByTranslatedResourceId(
131 String fileName, HeatOrchestrationTemplate heatOrchestrationTemplate,
132 String translatedResourceId, TranslateTo translateTo, String heatResourceType) {
133 List<Map.Entry<String, Resource>> list = heatOrchestrationTemplate.getResources().entrySet()
136 entry -> getPredicatesForTranslatedIdToResourceId(fileName, heatOrchestrationTemplate,
137 translatedResourceId, translateTo.getContext(), heatResourceType)
139 .allMatch(p -> p.test(entry)))
140 .collect(Collectors.toList());
141 if (CollectionUtils.isEmpty(list)) {
142 return Optional.empty();
144 return Optional.of(list);
148 private List<Predicate<Map.Entry<String, Resource>>> getPredicatesForTranslatedIdToResourceId(
149 String fileName, HeatOrchestrationTemplate heatOrchestrationTemplate,
150 String translatedResourceId, TranslationContext context, String heatResourceType) {
151 List<Predicate<Map.Entry<String, Resource>>> list = new ArrayList<>();
152 list.add(entry -> entry.getValue().getType().equals(heatResourceType));
154 Optional<String> resourceTranslatedId = ResourceTranslationBase
155 .getResourceTranslatedId(fileName, heatOrchestrationTemplate, entry.getKey(), context);
156 return resourceTranslatedId.isPresent()
157 && resourceTranslatedId.get().equals(translatedResourceId);
162 private boolean isOutputIsGetResource(String resourceId, FileData data,
163 AttachedResourceId attachedResourceId) {
164 if (attachedResourceId.isGetResource()) {
167 logger.warn("output: '" + resourceId + "' in file '" + data.getFile()
168 + "' is not defined as get_resource and therefor not supported.");