Release version 1.13.7
[sdc.git] / openecomp-be / lib / openecomp-sdc-translator-lib / openecomp-sdc-translator-core / src / main / java / org / openecomp / sdc / translator / services / heattotosca / helper / VolumeTranslationHelper.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20 package org.openecomp.sdc.translator.services.heattotosca.helper;
21
22 import static org.openecomp.sdc.heat.datatypes.model.HeatResourcesTypes.CINDER_VOLUME_RESOURCE_TYPE;
23
24 import java.util.ArrayList;
25 import java.util.List;
26 import java.util.Map;
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;
44
45 public class VolumeTranslationHelper {
46
47     private final Logger logger;
48
49     public VolumeTranslationHelper(Logger logger) {
50         this.logger = logger;
51     }
52
53     /**
54      * Gets file data containing volume.
55      *
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
61      */
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();
66         }
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;
71         }
72         return Optional.empty();
73     }
74
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)) {
82                 continue;
83             }
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)) {
91                         continue;
92                     }
93                     String translatedId = (String) attachedResourceId.getTranslatedId();
94                     if (isOutputOfTypeCinderVolume(translateTo, data, heatOrchestrationTemplate, translatedId)) {
95                         ResourceFileDataAndIDs fileDataAndIDs = new ResourceFileDataAndIDs((String) attachedResourceId.getEntityId(), translatedId,
96                             data);
97                         return Optional.of(fileDataAndIDs);
98                     } else {
99                         logger.warn("output: '" + resourceId + "' in file '" + data.getFile() + "' is not of type '" + CINDER_VOLUME_RESOURCE_TYPE
100                             .getHeatResource() + "'");
101                     }
102                 }
103             } else {
104                 logger.warn("output: '" + resourceId + "' in file '" + data.getFile() + "' is not found");
105             }
106         }
107         return Optional.empty();
108     }
109
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();
114     }
115
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();
125         } else {
126             return Optional.of(list);
127         }
128     }
129
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));
137         list.add(entry -> {
138             Optional<String> resourceTranslatedId = ResourceTranslationBase
139                 .getResourceTranslatedId(fileName, heatOrchestrationTemplate, entry.getKey(), context);
140             return resourceTranslatedId.isPresent() && resourceTranslatedId.get().equals(translatedResourceId);
141         });
142         return list;
143     }
144
145     private boolean isOutputIsGetResource(String resourceId, FileData data, AttachedResourceId attachedResourceId) {
146         if (attachedResourceId.isGetResource()) {
147             return true;
148         } else {
149             logger.warn("output: '" + resourceId + "' in file '" + data.getFile()
150                 + "' is not defined as get_resource and therefore not supported as shared resource.");
151             return false;
152         }
153     }
154 }