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 static org.openecomp.sdc.heat.datatypes.model.HeatResourcesTypes.CINDER_VOLUME_RESOURCE_TYPE;
25 import org.apache.commons.collections4.CollectionUtils;
26 import org.openecomp.sdc.common.utils.CommonUtil;
27 import org.openecomp.sdc.logging.api.Logger;
28 import org.openecomp.core.utilities.yaml.YamlUtil;
29 import org.openecomp.sdc.heat.datatypes.manifest.FileData;
30 import org.openecomp.sdc.heat.datatypes.model.HeatOrchestrationTemplate;
31 import org.openecomp.sdc.heat.datatypes.model.Output;
32 import org.openecomp.sdc.heat.datatypes.model.Resource;
33 import org.openecomp.sdc.logging.context.impl.MdcDataDebugMessage;
34 import org.openecomp.sdc.translator.datatypes.heattotosca.AttachedResourceId;
35 import org.openecomp.sdc.translator.datatypes.heattotosca.to.ResourceFileDataAndIDs;
36 import org.openecomp.sdc.translator.datatypes.heattotosca.to.TranslateTo;
37 import org.openecomp.sdc.translator.services.heattotosca.HeatToToscaUtil;
38 import org.openecomp.sdc.translator.datatypes.heattotosca.TranslationContext;
39 import org.openecomp.sdc.translator.services.heattotosca.impl.resourcetranslation.ResourceTranslationBase;
41 import java.util.ArrayList;
42 import java.util.List;
44 import java.util.Objects;
45 import java.util.Optional;
46 import java.util.function.Predicate;
47 import java.util.stream.Collectors;
49 public class VolumeTranslationHelper {
50 private final Logger logger;
51 private static MdcDataDebugMessage mdcDataDebugMessage = new MdcDataDebugMessage();
53 public VolumeTranslationHelper(Logger logger) {
58 * Gets file data containing volume.
60 * @param filesToSearch the files to search
61 * @param resourceId the resource id
62 * @param translateTo the translate to
63 * @param types the types
64 * @return the file data containing volume
66 public Optional<ResourceFileDataAndIDs> getFileDataContainingVolume(List<FileData> filesToSearch,
68 TranslateTo translateTo,
69 FileData.Type... types) {
72 mdcDataDebugMessage.debugEntryMessage(null, null);
74 if (CollectionUtils.isEmpty(filesToSearch)) {
75 mdcDataDebugMessage.debugExitMessage(null, null);
76 return Optional.empty();
79 List<FileData> fileDatas = Objects.isNull(types) ? filesToSearch : HeatToToscaUtil
80 .getFilteredListOfFileDataByTypes(filesToSearch, types);
81 Optional<ResourceFileDataAndIDs> fileDataAndIDs =
82 getResourceFileDataAndIDsForVolumeConnection(resourceId, translateTo, fileDatas);
83 if (fileDataAndIDs.isPresent()) {
84 mdcDataDebugMessage.debugExitMessage(null, null);
85 return fileDataAndIDs;
88 mdcDataDebugMessage.debugExitMessage(null, null);
89 return Optional.empty();
92 private Optional<ResourceFileDataAndIDs> getResourceFileDataAndIDsForVolumeConnection(
93 String resourceId, TranslateTo translateTo, List<FileData> fileDatas) {
96 mdcDataDebugMessage.debugEntryMessage(null, null);
98 for (FileData data : fileDatas) {
99 HeatOrchestrationTemplate heatOrchestrationTemplate = new YamlUtil()
100 .yamlToObject(translateTo.getContext().getFiles().getFileContent(data.getFile()),
101 HeatOrchestrationTemplate.class);
102 Map<String, Output> outputs = heatOrchestrationTemplate.getOutputs();
103 if (Objects.isNull(outputs)) {
106 Output output = outputs.get(resourceId);
107 if (Objects.nonNull(output)) {
108 Optional<AttachedResourceId> attachedOutputId = HeatToToscaUtil
109 .extractAttachedResourceId(data.getFile(), heatOrchestrationTemplate,
110 translateTo.getContext(), output.getValue());
111 if (attachedOutputId.isPresent()) {
112 AttachedResourceId attachedResourceId = attachedOutputId.get();
113 if (!isOutputIsGetResource(resourceId, data, attachedResourceId)) {
116 String translatedId = (String) attachedResourceId.getTranslatedId();
117 if (isOutputOfTypeCinderVolume(translateTo, data, heatOrchestrationTemplate,
119 ResourceFileDataAndIDs fileDataAndIDs =
120 new ResourceFileDataAndIDs((String) attachedResourceId.getEntityId(),
123 return Optional.of(fileDataAndIDs);
126 "output: '" + resourceId + "' in file '" + data.getFile() + "' is not of type '"
127 + CINDER_VOLUME_RESOURCE_TYPE.getHeatResource() + "'");
131 logger.warn("output: '" + resourceId + "' in file '" + data.getFile() + "' is not found");
135 mdcDataDebugMessage.debugExitMessage(null, null);
136 return Optional.empty();
139 private boolean isOutputOfTypeCinderVolume(TranslateTo translateTo, FileData data,
140 HeatOrchestrationTemplate heatOrchestrationTemplate,
141 String translatedId) {
142 return getResourceByTranslatedResourceId(data.getFile(), heatOrchestrationTemplate,
143 translatedId, translateTo, CINDER_VOLUME_RESOURCE_TYPE.getHeatResource()).isPresent();
146 private Optional<List<Map.Entry<String, Resource>>> getResourceByTranslatedResourceId(
147 String fileName, HeatOrchestrationTemplate heatOrchestrationTemplate,
148 String translatedResourceId, TranslateTo translateTo, String heatResourceType) {
151 mdcDataDebugMessage.debugEntryMessage("file", fileName);
153 List<Map.Entry<String, Resource>> list = heatOrchestrationTemplate.getResources().entrySet()
156 entry -> getPredicatesForTranslatedIdToResourceId(fileName, heatOrchestrationTemplate,
157 translatedResourceId, translateTo.getContext(), heatResourceType)
159 .allMatch(p -> p.test(entry)))
160 .collect(Collectors.toList());
161 if (CollectionUtils.isEmpty(list)) {
162 mdcDataDebugMessage.debugExitMessage("file", fileName);
163 return Optional.empty();
165 mdcDataDebugMessage.debugExitMessage("file", fileName);
166 return Optional.of(list);
170 private List<Predicate<Map.Entry<String, Resource>>> getPredicatesForTranslatedIdToResourceId(
171 String fileName, HeatOrchestrationTemplate heatOrchestrationTemplate,
172 String translatedResourceId, TranslationContext context, String heatResourceType) {
173 List<Predicate<Map.Entry<String, Resource>>> list = new ArrayList<>();
174 list.add(entry -> entry.getValue().getType().equals(heatResourceType));
176 Optional<String> resourceTranslatedId = ResourceTranslationBase
177 .getResourceTranslatedId(fileName, heatOrchestrationTemplate, entry.getKey(), context);
178 return resourceTranslatedId.isPresent()
179 && resourceTranslatedId.get().equals(translatedResourceId);
184 private boolean isOutputIsGetResource(String resourceId, FileData data,
185 AttachedResourceId attachedResourceId) {
186 if (attachedResourceId.isGetResource()) {
189 logger.warn("output: '" + resourceId + "' in file '" + data.getFile()
190 + "' is not defined as get_resource and therefore not supported as shared resource.");