Rename packages from openecomp to onap.
[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.onap.sdc.tosca.services.YamlUtil;
30 import org.openecomp.sdc.translator.datatypes.heattotosca.AttachedResourceId;
31 import org.openecomp.sdc.translator.datatypes.heattotosca.TranslationContext;
32 import org.openecomp.sdc.translator.datatypes.heattotosca.to.ResourceFileDataAndIDs;
33 import org.openecomp.sdc.translator.datatypes.heattotosca.to.TranslateTo;
34 import org.openecomp.sdc.translator.services.heattotosca.HeatToToscaUtil;
35 import org.openecomp.sdc.translator.services.heattotosca.impl.resourcetranslation.ResourceTranslationBase;
36
37 import java.util.ArrayList;
38 import java.util.List;
39 import java.util.Map;
40 import java.util.Objects;
41 import java.util.Optional;
42 import java.util.function.Predicate;
43 import java.util.stream.Collectors;
44
45 import static org.openecomp.sdc.heat.datatypes.model.HeatResourcesTypes.CINDER_VOLUME_RESOURCE_TYPE;
46
47 public class VolumeTranslationHelper {
48   private final Logger logger;
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 : HeatToToscaUtil
71         .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                     + 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, CINDER_VOLUME_RESOURCE_TYPE.getHeatResource()).isPresent();
126   }
127
128   private Optional<List<Map.Entry<String, Resource>>> getResourceByTranslatedResourceId(
129       String fileName, HeatOrchestrationTemplate heatOrchestrationTemplate,
130       String translatedResourceId, TranslateTo translateTo, String heatResourceType) {
131     List<Map.Entry<String, Resource>> list = heatOrchestrationTemplate.getResources().entrySet()
132         .stream()
133         .filter(
134             entry -> getPredicatesForTranslatedIdToResourceId(fileName, heatOrchestrationTemplate,
135                 translatedResourceId, translateTo.getContext(), heatResourceType)
136                 .stream()
137                     .allMatch(p -> p.test(entry)))
138         .collect(Collectors.toList());
139     if (CollectionUtils.isEmpty(list)) {
140       return Optional.empty();
141     } else {
142       return Optional.of(list);
143     }
144   }
145
146   private List<Predicate<Map.Entry<String, Resource>>> getPredicatesForTranslatedIdToResourceId(
147       String fileName, HeatOrchestrationTemplate heatOrchestrationTemplate,
148       String translatedResourceId, TranslationContext context, String heatResourceType) {
149     List<Predicate<Map.Entry<String, Resource>>> list = new ArrayList<>();
150     list.add(entry -> entry.getValue().getType().equals(heatResourceType));
151     list.add(entry -> {
152       Optional<String> resourceTranslatedId = ResourceTranslationBase
153           .getResourceTranslatedId(fileName, heatOrchestrationTemplate, entry.getKey(), context);
154       return resourceTranslatedId.isPresent()
155           && resourceTranslatedId.get().equals(translatedResourceId);
156     });
157     return list;
158   }
159
160   private boolean isOutputIsGetResource(String resourceId, FileData data,
161                                         AttachedResourceId attachedResourceId) {
162     if (attachedResourceId.isGetResource()) {
163       return true;
164     } else {
165       logger.warn("output: '" + resourceId + "' in file '" + data.getFile()
166           + "' is not defined as get_resource and therefore not supported as shared resource.");
167       return false;
168     }
169   }
170 }