push addional code
[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.core.utilities.yaml.YamlUtil;
25 import org.openecomp.sdc.heat.datatypes.manifest.FileData;
26 import org.openecomp.sdc.heat.datatypes.model.HeatOrchestrationTemplate;
27 import org.openecomp.sdc.heat.datatypes.model.HeatResourcesTypes;
28 import org.openecomp.sdc.heat.datatypes.model.Output;
29 import org.openecomp.sdc.heat.datatypes.model.Resource;
30 import org.openecomp.sdc.translator.datatypes.heattotosca.AttachedResourceId;
31 import org.openecomp.sdc.translator.datatypes.heattotosca.to.ResourceFileDataAndIDs;
32 import org.openecomp.sdc.translator.datatypes.heattotosca.to.TranslateTo;
33 import org.openecomp.sdc.translator.services.heattotosca.HeatToToscaUtil;
34 import org.openecomp.sdc.translator.services.heattotosca.TranslationContext;
35 import org.openecomp.sdc.translator.services.heattotosca.impl.ResourceTranslationBase;
36 import org.slf4j.Logger;
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 public class VolumeTranslationHelper {
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,
63                                                                       String resourceId,
64                                                                       TranslateTo translateTo,
65                                                                       FileData.Type... types) {
66     if (CollectionUtils.isEmpty(filesToSearch)) {
67       return Optional.empty();
68     }
69
70     List<FileData> fileDatas = Objects.isNull(types) ? filesToSearch
71         : HeatToToscaUtil.getFilteredListOfFileDataByTypes(filesToSearch, types);
72     Optional<ResourceFileDataAndIDs> fileDataAndIDs =
73         getResourceFileDataAndIDsForVolumeConnection(resourceId, translateTo, fileDatas);
74     if (fileDataAndIDs.isPresent()) {
75       return fileDataAndIDs;
76     }
77     return Optional.empty();
78   }
79
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)) {
88         continue;
89       }
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)) {
98             continue;
99           }
100           String translatedId = (String) attachedResourceId.getTranslatedId();
101           if (isOutputOfTypeCinderVolume(translateTo, data, heatOrchestrationTemplate,
102               translatedId)) {
103             ResourceFileDataAndIDs fileDataAndIDs =
104                 new ResourceFileDataAndIDs((String) attachedResourceId.getEntityId(),
105                     translatedId,
106                     data);
107             return Optional.of(fileDataAndIDs);
108           } else {
109             logger.warn(
110                 "output: '" + resourceId + "' in file '" + data.getFile() + "' is not of type '"
111                     + HeatResourcesTypes.CINDER_VOLUME_RESOURCE_TYPE.getHeatResource() + "'");
112           }
113         }
114       } else {
115         logger.warn("output: '" + resourceId + "' in file '" + data.getFile() + "' is not found");
116       }
117     }
118     return Optional.empty();
119   }
120
121   private boolean isOutputOfTypeCinderVolume(TranslateTo translateTo, FileData data,
122                                              HeatOrchestrationTemplate heatOrchestrationTemplate,
123                                              String translatedId) {
124     return getResourceByTranslatedResourceId(data.getFile(), heatOrchestrationTemplate,
125         translatedId, translateTo,
126             HeatResourcesTypes.CINDER_VOLUME_RESOURCE_TYPE.getHeatResource())
127             .isPresent();
128   }
129
130   private Optional<List<Map.Entry<String, Resource>>> getResourceByTranslatedResourceId(
131       String fileName, HeatOrchestrationTemplate heatOrchestrationTemplate,
132       String translatedResourceId, TranslateTo translateTo, String heatResourceType) {
133     List<Map.Entry<String, Resource>> list = heatOrchestrationTemplate.getResources().entrySet()
134         .stream()
135         .filter(
136             entry -> getPredicatesForTranslatedIdToResourceId(fileName, heatOrchestrationTemplate,
137                 translatedResourceId, translateTo.getContext(), heatResourceType)
138                 .stream()
139                     .allMatch(p -> p.test(entry)))
140         .collect(Collectors.toList());
141     if (CollectionUtils.isEmpty(list)) {
142       return Optional.empty();
143     } else {
144       return Optional.of(list);
145     }
146   }
147
148   private List<Predicate<Map.Entry<String, Resource>>> getPredicatesForTranslatedIdToResourceId(
149       String fileName, HeatOrchestrationTemplate heatOrchestrationTemplate,
150       String translatedResourceId, TranslationContext context, String heatResourceType) {
151     List<Predicate<Map.Entry<String, Resource>>> list = new ArrayList<>();
152     list.add(entry -> entry.getValue().getType().equals(heatResourceType));
153     list.add(entry -> {
154       Optional<String> resourceTranslatedId = ResourceTranslationBase
155           .getResourceTranslatedId(fileName, heatOrchestrationTemplate, entry.getKey(), context);
156       return resourceTranslatedId.isPresent()
157           && resourceTranslatedId.get().equals(translatedResourceId);
158     });
159     return list;
160   }
161
162   private boolean isOutputIsGetResource(String resourceId, FileData data,
163                                         AttachedResourceId attachedResourceId) {
164     if (attachedResourceId.isGetResource()) {
165       return true;
166     } else {
167       logger.warn("output: '" + resourceId + "' in file '" + data.getFile()
168           + "' is not defined as get_resource and therefor not supported.");
169       return false;
170     }
171   }
172 }