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