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=========================================================
20 package org.openecomp.sdc.translator.services.heattotosca.helper;
22 import static org.openecomp.sdc.heat.datatypes.model.HeatResourcesTypes.CINDER_VOLUME_RESOURCE_TYPE;
24 import java.util.ArrayList;
25 import java.util.List;
27 import java.util.Objects;
28 import java.util.Optional;
29 import java.util.function.Predicate;
30 import java.util.stream.Collectors;
31 import org.apache.commons.collections4.CollectionUtils;
32 import org.onap.sdc.tosca.services.YamlUtil;
33 import org.openecomp.sdc.heat.datatypes.manifest.FileData;
34 import org.openecomp.sdc.heat.datatypes.model.HeatOrchestrationTemplate;
35 import org.openecomp.sdc.heat.datatypes.model.Output;
36 import org.openecomp.sdc.heat.datatypes.model.Resource;
37 import org.openecomp.sdc.logging.api.Logger;
38 import org.openecomp.sdc.translator.datatypes.heattotosca.AttachedResourceId;
39 import org.openecomp.sdc.translator.datatypes.heattotosca.TranslationContext;
40 import org.openecomp.sdc.translator.datatypes.heattotosca.to.ResourceFileDataAndIDs;
41 import org.openecomp.sdc.translator.datatypes.heattotosca.to.TranslateTo;
42 import org.openecomp.sdc.translator.services.heattotosca.HeatToToscaUtil;
43 import org.openecomp.sdc.translator.services.heattotosca.impl.resourcetranslation.ResourceTranslationBase;
45 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, String resourceId, TranslateTo translateTo,
63 FileData.Type... types) {
64 if (CollectionUtils.isEmpty(filesToSearch)) {
65 return Optional.empty();
67 List<FileData> fileDatas = Objects.isNull(types) ? filesToSearch : HeatToToscaUtil.getFilteredListOfFileDataByTypes(filesToSearch, types);
68 Optional<ResourceFileDataAndIDs> fileDataAndIDs = getResourceFileDataAndIDsForVolumeConnection(resourceId, translateTo, fileDatas);
69 if (fileDataAndIDs.isPresent()) {
70 return fileDataAndIDs;
72 return Optional.empty();
75 private Optional<ResourceFileDataAndIDs> getResourceFileDataAndIDsForVolumeConnection(String resourceId, TranslateTo translateTo,
76 List<FileData> fileDatas) {
77 for (FileData data : fileDatas) {
78 HeatOrchestrationTemplate heatOrchestrationTemplate = new YamlUtil()
79 .yamlToObject(translateTo.getContext().getFiles().getFileContentAsStream(data.getFile()), HeatOrchestrationTemplate.class);
80 Map<String, Output> outputs = heatOrchestrationTemplate.getOutputs();
81 if (Objects.isNull(outputs)) {
84 Output output = outputs.get(resourceId);
85 if (Objects.nonNull(output)) {
86 Optional<AttachedResourceId> attachedOutputId = HeatToToscaUtil
87 .extractAttachedResourceId(data.getFile(), heatOrchestrationTemplate, translateTo.getContext(), output.getValue());
88 if (attachedOutputId.isPresent()) {
89 AttachedResourceId attachedResourceId = attachedOutputId.get();
90 if (!isOutputIsGetResource(resourceId, data, attachedResourceId)) {
93 String translatedId = (String) attachedResourceId.getTranslatedId();
94 if (isOutputOfTypeCinderVolume(translateTo, data, heatOrchestrationTemplate, translatedId)) {
95 ResourceFileDataAndIDs fileDataAndIDs = new ResourceFileDataAndIDs((String) attachedResourceId.getEntityId(), translatedId,
97 return Optional.of(fileDataAndIDs);
99 logger.warn("output: '" + resourceId + "' in file '" + data.getFile() + "' is not of type '" + CINDER_VOLUME_RESOURCE_TYPE
100 .getHeatResource() + "'");
104 logger.warn("output: '" + resourceId + "' in file '" + data.getFile() + "' is not found");
107 return Optional.empty();
110 private boolean isOutputOfTypeCinderVolume(TranslateTo translateTo, FileData data, HeatOrchestrationTemplate heatOrchestrationTemplate,
111 String translatedId) {
112 return getResourceByTranslatedResourceId(data.getFile(), heatOrchestrationTemplate, translatedId, translateTo,
113 CINDER_VOLUME_RESOURCE_TYPE.getHeatResource()).isPresent();
116 private Optional<List<Map.Entry<String, Resource>>> getResourceByTranslatedResourceId(String fileName,
117 HeatOrchestrationTemplate heatOrchestrationTemplate,
118 String translatedResourceId, TranslateTo translateTo,
119 String heatResourceType) {
120 List<Map.Entry<String, Resource>> list = heatOrchestrationTemplate.getResources().entrySet().stream().filter(
121 entry -> getPredicatesForTranslatedIdToResourceId(fileName, heatOrchestrationTemplate, translatedResourceId, translateTo.getContext(),
122 heatResourceType).stream().allMatch(p -> p.test(entry))).collect(Collectors.toList());
123 if (CollectionUtils.isEmpty(list)) {
124 return Optional.empty();
126 return Optional.of(list);
130 private List<Predicate<Map.Entry<String, Resource>>> getPredicatesForTranslatedIdToResourceId(String fileName,
131 HeatOrchestrationTemplate heatOrchestrationTemplate,
132 String translatedResourceId,
133 TranslationContext context,
134 String heatResourceType) {
135 List<Predicate<Map.Entry<String, Resource>>> list = new ArrayList<>();
136 list.add(entry -> entry.getValue().getType().equals(heatResourceType));
138 Optional<String> resourceTranslatedId = ResourceTranslationBase
139 .getResourceTranslatedId(fileName, heatOrchestrationTemplate, entry.getKey(), context);
140 return resourceTranslatedId.isPresent() && resourceTranslatedId.get().equals(translatedResourceId);
145 private boolean isOutputIsGetResource(String resourceId, FileData data, AttachedResourceId attachedResourceId) {
146 if (attachedResourceId.isGetResource()) {
149 logger.warn("output: '" + resourceId + "' in file '" + data.getFile()
150 + "' is not defined as get_resource and therefore not supported as shared resource.");