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.sdc.heat.datatypes.manifest.FileData;
25 import org.openecomp.sdc.heat.datatypes.model.HeatOrchestrationTemplate;
26 import org.openecomp.sdc.heat.datatypes.model.Output;
27 import org.openecomp.sdc.heat.datatypes.model.Resource;
28 import org.openecomp.sdc.logging.api.Logger;
29 import org.openecomp.sdc.logging.context.impl.MdcDataDebugMessage;
30 import org.openecomp.sdc.tosca.services.YamlUtil;
31 import org.openecomp.sdc.translator.datatypes.heattotosca.AttachedResourceId;
32 import org.openecomp.sdc.translator.datatypes.heattotosca.TranslationContext;
33 import org.openecomp.sdc.translator.datatypes.heattotosca.to.ResourceFileDataAndIDs;
34 import org.openecomp.sdc.translator.datatypes.heattotosca.to.TranslateTo;
35 import org.openecomp.sdc.translator.services.heattotosca.HeatToToscaUtil;
36 import org.openecomp.sdc.translator.services.heattotosca.impl.resourcetranslation.ResourceTranslationBase;
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 import static org.openecomp.sdc.heat.datatypes.model.HeatResourcesTypes.CINDER_VOLUME_RESOURCE_TYPE;
48 public class VolumeTranslationHelper {
49 private final Logger logger;
50 private static MdcDataDebugMessage mdcDataDebugMessage = new MdcDataDebugMessage();
52 public VolumeTranslationHelper(Logger logger) {
57 * Gets file data containing volume.
59 * @param filesToSearch the files to search
60 * @param resourceId the resource id
61 * @param translateTo the translate to
62 * @param types the types
63 * @return the file data containing volume
65 public Optional<ResourceFileDataAndIDs> getFileDataContainingVolume(List<FileData> filesToSearch,
67 TranslateTo translateTo,
68 FileData.Type... types) {
71 mdcDataDebugMessage.debugEntryMessage(null, null);
73 if (CollectionUtils.isEmpty(filesToSearch)) {
74 mdcDataDebugMessage.debugExitMessage(null, null);
75 return Optional.empty();
78 List<FileData> fileDatas = Objects.isNull(types) ? filesToSearch : HeatToToscaUtil
79 .getFilteredListOfFileDataByTypes(filesToSearch, types);
80 Optional<ResourceFileDataAndIDs> fileDataAndIDs =
81 getResourceFileDataAndIDsForVolumeConnection(resourceId, translateTo, fileDatas);
82 if (fileDataAndIDs.isPresent()) {
83 mdcDataDebugMessage.debugExitMessage(null, null);
84 return fileDataAndIDs;
87 mdcDataDebugMessage.debugExitMessage(null, null);
88 return Optional.empty();
91 private Optional<ResourceFileDataAndIDs> getResourceFileDataAndIDsForVolumeConnection(
92 String resourceId, TranslateTo translateTo, List<FileData> fileDatas) {
95 mdcDataDebugMessage.debugEntryMessage(null, null);
97 for (FileData data : fileDatas) {
98 HeatOrchestrationTemplate heatOrchestrationTemplate = new YamlUtil()
99 .yamlToObject(translateTo.getContext().getFiles().getFileContent(data.getFile()),
100 HeatOrchestrationTemplate.class);
101 Map<String, Output> outputs = heatOrchestrationTemplate.getOutputs();
102 if (Objects.isNull(outputs)) {
105 Output output = outputs.get(resourceId);
106 if (Objects.nonNull(output)) {
107 Optional<AttachedResourceId> attachedOutputId = HeatToToscaUtil
108 .extractAttachedResourceId(data.getFile(), heatOrchestrationTemplate,
109 translateTo.getContext(), output.getValue());
110 if (attachedOutputId.isPresent()) {
111 AttachedResourceId attachedResourceId = attachedOutputId.get();
112 if (!isOutputIsGetResource(resourceId, data, attachedResourceId)) {
115 String translatedId = (String) attachedResourceId.getTranslatedId();
116 if (isOutputOfTypeCinderVolume(translateTo, data, heatOrchestrationTemplate,
118 ResourceFileDataAndIDs fileDataAndIDs =
119 new ResourceFileDataAndIDs((String) attachedResourceId.getEntityId(),
122 return Optional.of(fileDataAndIDs);
125 "output: '" + resourceId + "' in file '" + data.getFile() + "' is not of type '"
126 + CINDER_VOLUME_RESOURCE_TYPE.getHeatResource() + "'");
130 logger.warn("output: '" + resourceId + "' in file '" + data.getFile() + "' is not found");
134 mdcDataDebugMessage.debugExitMessage(null, null);
135 return Optional.empty();
138 private boolean isOutputOfTypeCinderVolume(TranslateTo translateTo, FileData data,
139 HeatOrchestrationTemplate heatOrchestrationTemplate,
140 String translatedId) {
141 return getResourceByTranslatedResourceId(data.getFile(), heatOrchestrationTemplate,
142 translatedId, translateTo, CINDER_VOLUME_RESOURCE_TYPE.getHeatResource()).isPresent();
145 private Optional<List<Map.Entry<String, Resource>>> getResourceByTranslatedResourceId(
146 String fileName, HeatOrchestrationTemplate heatOrchestrationTemplate,
147 String translatedResourceId, TranslateTo translateTo, String heatResourceType) {
150 mdcDataDebugMessage.debugEntryMessage("file", fileName);
152 List<Map.Entry<String, Resource>> list = heatOrchestrationTemplate.getResources().entrySet()
155 entry -> getPredicatesForTranslatedIdToResourceId(fileName, heatOrchestrationTemplate,
156 translatedResourceId, translateTo.getContext(), heatResourceType)
158 .allMatch(p -> p.test(entry)))
159 .collect(Collectors.toList());
160 if (CollectionUtils.isEmpty(list)) {
161 mdcDataDebugMessage.debugExitMessage("file", fileName);
162 return Optional.empty();
164 mdcDataDebugMessage.debugExitMessage("file", fileName);
165 return Optional.of(list);
169 private List<Predicate<Map.Entry<String, Resource>>> getPredicatesForTranslatedIdToResourceId(
170 String fileName, HeatOrchestrationTemplate heatOrchestrationTemplate,
171 String translatedResourceId, TranslationContext context, String heatResourceType) {
172 List<Predicate<Map.Entry<String, Resource>>> list = new ArrayList<>();
173 list.add(entry -> entry.getValue().getType().equals(heatResourceType));
175 Optional<String> resourceTranslatedId = ResourceTranslationBase
176 .getResourceTranslatedId(fileName, heatOrchestrationTemplate, entry.getKey(), context);
177 return resourceTranslatedId.isPresent()
178 && resourceTranslatedId.get().equals(translatedResourceId);
183 private boolean isOutputIsGetResource(String resourceId, FileData data,
184 AttachedResourceId attachedResourceId) {
185 if (attachedResourceId.isGetResource()) {
188 logger.warn("output: '" + resourceId + "' in file '" + data.getFile()
189 + "' is not defined as get_resource and therefore not supported as shared resource.");