Add collaboration feature
[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
21 package org.openecomp.sdc.translator.services.heattotosca.helper;
22
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;
37
38 import java.util.ArrayList;
39 import java.util.List;
40 import java.util.Map;
41 import java.util.Objects;
42 import java.util.Optional;
43 import java.util.function.Predicate;
44 import java.util.stream.Collectors;
45
46 import static org.openecomp.sdc.heat.datatypes.model.HeatResourcesTypes.CINDER_VOLUME_RESOURCE_TYPE;
47
48 public class VolumeTranslationHelper {
49   private final Logger logger;
50   private static MdcDataDebugMessage mdcDataDebugMessage = new MdcDataDebugMessage();
51
52   public VolumeTranslationHelper(Logger logger) {
53     this.logger = logger;
54   }
55
56   /**
57    * Gets file data containing volume.
58    *
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
64    */
65   public Optional<ResourceFileDataAndIDs> getFileDataContainingVolume(List<FileData> filesToSearch,
66                                                                       String resourceId,
67                                                                       TranslateTo translateTo,
68                                                                       FileData.Type... types) {
69
70
71     mdcDataDebugMessage.debugEntryMessage(null, null);
72
73     if (CollectionUtils.isEmpty(filesToSearch)) {
74       mdcDataDebugMessage.debugExitMessage(null, null);
75       return Optional.empty();
76     }
77
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;
85     }
86
87     mdcDataDebugMessage.debugExitMessage(null, null);
88     return Optional.empty();
89   }
90
91   private Optional<ResourceFileDataAndIDs> getResourceFileDataAndIDsForVolumeConnection(
92       String resourceId, TranslateTo translateTo, List<FileData> fileDatas) {
93
94
95     mdcDataDebugMessage.debugEntryMessage(null, null);
96
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)) {
103         continue;
104       }
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)) {
113             continue;
114           }
115           String translatedId = (String) attachedResourceId.getTranslatedId();
116           if (isOutputOfTypeCinderVolume(translateTo, data, heatOrchestrationTemplate,
117               translatedId)) {
118             ResourceFileDataAndIDs fileDataAndIDs =
119                 new ResourceFileDataAndIDs((String) attachedResourceId.getEntityId(),
120                     translatedId,
121                     data);
122             return Optional.of(fileDataAndIDs);
123           } else {
124             logger.warn(
125                 "output: '" + resourceId + "' in file '" + data.getFile() + "' is not of type '"
126                     + CINDER_VOLUME_RESOURCE_TYPE.getHeatResource() + "'");
127           }
128         }
129       } else {
130         logger.warn("output: '" + resourceId + "' in file '" + data.getFile() + "' is not found");
131       }
132     }
133
134     mdcDataDebugMessage.debugExitMessage(null, null);
135     return Optional.empty();
136   }
137
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();
143   }
144
145   private Optional<List<Map.Entry<String, Resource>>> getResourceByTranslatedResourceId(
146       String fileName, HeatOrchestrationTemplate heatOrchestrationTemplate,
147       String translatedResourceId, TranslateTo translateTo, String heatResourceType) {
148
149
150     mdcDataDebugMessage.debugEntryMessage("file", fileName);
151
152     List<Map.Entry<String, Resource>> list = heatOrchestrationTemplate.getResources().entrySet()
153         .stream()
154         .filter(
155             entry -> getPredicatesForTranslatedIdToResourceId(fileName, heatOrchestrationTemplate,
156                 translatedResourceId, translateTo.getContext(), heatResourceType)
157                 .stream()
158                     .allMatch(p -> p.test(entry)))
159         .collect(Collectors.toList());
160     if (CollectionUtils.isEmpty(list)) {
161       mdcDataDebugMessage.debugExitMessage("file", fileName);
162       return Optional.empty();
163     } else {
164       mdcDataDebugMessage.debugExitMessage("file", fileName);
165       return Optional.of(list);
166     }
167   }
168
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));
174     list.add(entry -> {
175       Optional<String> resourceTranslatedId = ResourceTranslationBase
176           .getResourceTranslatedId(fileName, heatOrchestrationTemplate, entry.getKey(), context);
177       return resourceTranslatedId.isPresent()
178           && resourceTranslatedId.get().equals(translatedResourceId);
179     });
180     return list;
181   }
182
183   private boolean isOutputIsGetResource(String resourceId, FileData data,
184                                         AttachedResourceId attachedResourceId) {
185     if (attachedResourceId.isGetResource()) {
186       return true;
187     } else {
188       logger.warn("output: '" + resourceId + "' in file '" + data.getFile()
189           + "' is not defined as get_resource and therefore not supported as shared resource.");
190       return false;
191     }
192   }
193 }