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.onap.sdc.tosca.services.YamlUtil;
30 import org.openecomp.sdc.translator.datatypes.heattotosca.AttachedResourceId;
31 import org.openecomp.sdc.translator.datatypes.heattotosca.TranslationContext;
32 import org.openecomp.sdc.translator.datatypes.heattotosca.to.ResourceFileDataAndIDs;
33 import org.openecomp.sdc.translator.datatypes.heattotosca.to.TranslateTo;
34 import org.openecomp.sdc.translator.services.heattotosca.HeatToToscaUtil;
35 import org.openecomp.sdc.translator.services.heattotosca.impl.resourcetranslation.ResourceTranslationBase;
37 import java.util.ArrayList;
38 import java.util.List;
40 import java.util.Objects;
41 import java.util.Optional;
42 import java.util.function.Predicate;
43 import java.util.stream.Collectors;
45 import static org.openecomp.sdc.heat.datatypes.model.HeatResourcesTypes.CINDER_VOLUME_RESOURCE_TYPE;
47 public class VolumeTranslationHelper {
48 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 : HeatToToscaUtil
71 .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 + 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, CINDER_VOLUME_RESOURCE_TYPE.getHeatResource()).isPresent();
128 private Optional<List<Map.Entry<String, Resource>>> getResourceByTranslatedResourceId(
129 String fileName, HeatOrchestrationTemplate heatOrchestrationTemplate,
130 String translatedResourceId, TranslateTo translateTo, String heatResourceType) {
131 List<Map.Entry<String, Resource>> list = heatOrchestrationTemplate.getResources().entrySet()
134 entry -> getPredicatesForTranslatedIdToResourceId(fileName, heatOrchestrationTemplate,
135 translatedResourceId, translateTo.getContext(), heatResourceType)
137 .allMatch(p -> p.test(entry)))
138 .collect(Collectors.toList());
139 if (CollectionUtils.isEmpty(list)) {
140 return Optional.empty();
142 return Optional.of(list);
146 private List<Predicate<Map.Entry<String, Resource>>> getPredicatesForTranslatedIdToResourceId(
147 String fileName, HeatOrchestrationTemplate heatOrchestrationTemplate,
148 String translatedResourceId, TranslationContext context, String heatResourceType) {
149 List<Predicate<Map.Entry<String, Resource>>> list = new ArrayList<>();
150 list.add(entry -> entry.getValue().getType().equals(heatResourceType));
152 Optional<String> resourceTranslatedId = ResourceTranslationBase
153 .getResourceTranslatedId(fileName, heatOrchestrationTemplate, entry.getKey(), context);
154 return resourceTranslatedId.isPresent()
155 && resourceTranslatedId.get().equals(translatedResourceId);
160 private boolean isOutputIsGetResource(String resourceId, FileData data,
161 AttachedResourceId attachedResourceId) {
162 if (attachedResourceId.isGetResource()) {
165 logger.warn("output: '" + resourceId + "' in file '" + data.getFile()
166 + "' is not defined as get_resource and therefore not supported as shared resource.");