87022f4e377c680851473ba5d185f7c2662fbfa4
[sdc.git] /
1 /*
2  * Copyright © 2016-2017 European Support Limited
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15 */
16
17
18 package org.openecomp.sdc.heat.services.tree;
19
20 import org.apache.commons.collections4.CollectionUtils;
21 import org.apache.commons.collections4.MapUtils;
22 import org.openecomp.core.utilities.file.FileContentHandler;
23 import org.openecomp.core.validation.errors.ErrorMessagesFormatBuilder;
24 import org.openecomp.core.validation.types.GlobalValidationContext;
25 import org.openecomp.sdc.common.errors.Messages;
26 import org.openecomp.sdc.datatypes.error.ErrorLevel;
27 import org.openecomp.sdc.heat.datatypes.model.HeatOrchestrationTemplate;
28 import org.openecomp.sdc.heat.datatypes.model.HeatResourcesTypes;
29 import org.openecomp.sdc.heat.datatypes.model.PropertiesMapKeyTypes;
30 import org.openecomp.sdc.heat.datatypes.model.Resource;
31 import org.openecomp.sdc.heat.services.HeatStructureUtil;
32
33 import java.util.Collection;
34 import java.util.HashSet;
35 import java.util.Map;
36 import java.util.Objects;
37 import java.util.Set;
38
39 public class HeatTreeManagerUtil {
40
41   private static final String TYPE = "type";
42   private HeatTreeManagerUtil() {
43
44   }
45
46   /**
47    * Init heat tree manager heat tree manager.
48    *
49    * @param fileContentMap the file content map
50    * @return the heat tree manager
51    */
52   public static HeatTreeManager initHeatTreeManager(FileContentHandler fileContentMap) {
53
54     HeatTreeManager heatTreeManager = new HeatTreeManager();
55     fileContentMap.getFileList().stream().forEach(
56             fileName -> heatTreeManager.addFile(fileName, fileContentMap.getFileContent(fileName)));
57
58     return heatTreeManager;
59   }
60
61   /**
62    * Gets nested files.
63    *
64    * @param filename the filename
65    * @param hot the hot
66    * @param globalContext the global context
67    * @return the nested files
68    */
69   public static Set<String> getNestedFiles(String filename, HeatOrchestrationTemplate hot,
70                                            GlobalValidationContext globalContext) {
71     Set<String> nestedFileList = new HashSet<>();
72     hot.getResources().values().stream().filter(
73             resource -> resource.getType().endsWith(".yaml") || resource.getType().endsWith(".yml"))
74             .forEach(resource -> nestedFileList.add(resource.getType()));
75
76     Set<String> resourceDefNestedFiles = getResourceDefNestedFiles(hot);
77     nestedFileList.addAll(resourceDefNestedFiles);
78     return nestedFileList;
79   }
80
81   /**
82    * Gets artifact files.
83    *
84    * @param filename the filename
85    * @param hot the hot
86    * @param globalContext the global context
87    * @return the artifact files
88    */
89   public static Set<String> getArtifactFiles(String filename, HeatOrchestrationTemplate hot,
90                                              GlobalValidationContext globalContext) {
91     Set<String> artifactSet = new HashSet<>();
92     Collection<Resource> resourcesValue =
93             hot.getResources() == null ? null : hot.getResources().values();
94     if (CollectionUtils.isNotEmpty(resourcesValue)) {
95       for (Resource resource : resourcesValue) {
96         Collection<Object> properties =
97                 resource.getProperties() == null ? null : resource.getProperties().values();
98
99         artifactSet = getArtifactsFromPropertiesAndAddInArtifactSet(properties,
100                                       filename, globalContext);
101       }
102     }
103     return artifactSet;
104   }
105
106   private static Set<String> getArtifactsFromPropertiesAndAddInArtifactSet(Collection<Object> properties,
107                                                     String filename,
108                                                     GlobalValidationContext globalContext ){
109     Set<String> artifactSet = new HashSet<>();
110     if (CollectionUtils.isNotEmpty(properties)) {
111
112       for (Object property : properties) {
113         Set<String> artifactNames =
114                 HeatStructureUtil.getReferencedValuesByFunctionName(filename, "get_file", property,
115                         globalContext);
116         artifactSet.addAll(artifactNames);
117       }
118     }
119
120     return artifactSet;
121   }
122
123   private static Set<String> getResourceDefNestedFiles(HeatOrchestrationTemplate hot) {
124     Set<String> resourceDefNestedFiles = new HashSet<>();
125     hot.getResources()
126             .entrySet().stream().filter(entry -> entry.getValue().getType()
127             .equals(HeatResourcesTypes.RESOURCE_GROUP_RESOURCE_TYPE.getHeatResource()))
128             .filter(entry ->
129                     getResourceDef(entry.getValue()) != null
130                             && HeatStructureUtil.isNestedResource(
131                             getResourceDef(entry.getValue())
132                                     .getType()))
133             .forEach(entry -> resourceDefNestedFiles.add(
134                     getResourceDef( entry.getValue()).getType()));
135     return resourceDefNestedFiles;
136   }
137
138   /**
139    * Gets resource def.
140    *
141    * @param resource the resource
142    * @return the resource def
143    */
144   @SuppressWarnings("unchecked")
145   public static Resource getResourceDef( Resource resource) {
146     Resource resourceDef = null;
147     Map<String, Object> resourceDefValueMap = resource.getProperties() == null ? null
148             : (Map<String, Object>) resource.getProperties().get(
149             PropertiesMapKeyTypes.RESOURCE_DEF.getKeyMap());
150     if (resourceDefValueMap != null && MapUtils.isNotEmpty(resourceDefValueMap) ) {
151       Object resourceDefType = resourceDefValueMap.get(TYPE);
152       if ( resourceDefType instanceof String && isResourceGroupTypeNested((String) resourceDefType)) {
153         resourceDef = new Resource();
154         resourceDef.setType((String) resourceDefType);
155         //noinspection unchecked
156         resourceDef.setProperties((Map<String, Object>) resourceDefValueMap.get("properties"));
157       }
158
159     }
160     return resourceDef;
161   }
162
163   @SuppressWarnings("unchecked")
164   public static void checkResourceGroupTypeValid(String filename, String resourceName,
165                                                  Resource resource,
166                                                  GlobalValidationContext globalContext) {
167     Map<String, Object> resourceDefValueMap = resource.getProperties() == null ? null
168             : (Map<String, Object>) resource.getProperties().get(
169             PropertiesMapKeyTypes.RESOURCE_DEF.getKeyMap());
170     if (resourceDefValueMap != null && MapUtils.isNotEmpty(resourceDefValueMap) ) {
171       Object resourceDefType = resourceDefValueMap.get(TYPE);
172       if (Objects.nonNull(resourceDefType) && !(resourceDefType instanceof String) ) {
173         globalContext.addMessage(filename, ErrorLevel.WARNING, ErrorMessagesFormatBuilder
174                         .getErrorWithParameters(
175                                 globalContext.getMessageCode(),
176                                 Messages.INVALID_RESOURCE_GROUP_TYPE.getErrorMessage(),
177                                 resourceName, resourceDefType.toString()));
178       }
179     }
180   }
181
182   @SuppressWarnings("unchecked")
183   public static void checkResourceTypeValid(String filename, String resourceName,
184                                             Resource resource,
185                                             GlobalValidationContext globalContext) {
186     Map<String, Object> resourceDefValueMap = resource.getProperties() == null ? null
187             : (Map<String, Object>) resource.getProperties().get(
188             PropertiesMapKeyTypes.RESOURCE_DEF.getKeyMap());
189     if (resourceDefValueMap != null && MapUtils.isNotEmpty(resourceDefValueMap) ) {
190       Object resourceDefType = resourceDefValueMap.get(TYPE);
191       if (Objects.isNull(resourceDefType)) {
192         globalContext.addMessage(filename, ErrorLevel.WARNING, ErrorMessagesFormatBuilder
193                         .getErrorWithParameters(
194                                 globalContext.getMessageCode(), Messages.INVALID_RESOURCE_TYPE.getErrorMessage(),
195                                 "null", resourceName));
196       }
197     }
198   }
199
200   public static boolean isResourceGroupTypeNested(String resourceDefType) {
201     return HeatStructureUtil.isNestedResource(resourceDefType);
202   }
203
204   public static boolean checkIfResourceGroupTypeIsNested(String filename, String resourceName,
205                                                          Resource resource,
206                                                          GlobalValidationContext globalContext) {
207     //noinspection unchecked
208     Map<String, Object> resourceDefValueMap = resource.getProperties() == null ? null
209             : (Map<String, Object>) resource.getProperties().get(
210             PropertiesMapKeyTypes.RESOURCE_DEF.getKeyMap());
211     if (resourceDefValueMap != null && MapUtils.isNotEmpty(resourceDefValueMap) ) {
212       Object resourceDefType = resourceDefValueMap.get(TYPE);
213       if (resourceDefType instanceof String && isResourceGroupTypeNested((String) resourceDefType)) {
214
215         globalContext.addMessage(filename, ErrorLevel.WARNING, ErrorMessagesFormatBuilder
216                         .getErrorWithParameters(
217                                 globalContext.getMessageCode(),
218                                 Messages.INVALID_RESOURCE_GROUP_TYPE.getErrorMessage(),
219                                 resourceName, resourceDefType.toString()));
220         return true;
221       }
222     }
223     return false;
224   }
225 }