607319906d92d1af868ba0f4cafcd6c6f0c68e55
[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 import org.openecomp.sdc.logging.types.LoggerTragetServiceName;
33
34 import java.util.Collection;
35 import java.util.HashSet;
36 import java.util.Map;
37 import java.util.Objects;
38 import java.util.Set;
39
40 public class HeatTreeManagerUtil {
41
42   private static final String TYPE = "type";
43   private HeatTreeManagerUtil() {
44
45   }
46
47   /**
48    * Init heat tree manager heat tree manager.
49    *
50    * @param fileContentMap the file content map
51    * @return the heat tree manager
52    */
53   public static HeatTreeManager initHeatTreeManager(FileContentHandler fileContentMap) {
54
55     HeatTreeManager heatTreeManager = new HeatTreeManager();
56     fileContentMap.getFileList().stream().forEach(
57             fileName -> heatTreeManager.addFile(fileName, fileContentMap.getFileContent(fileName)));
58
59     return heatTreeManager;
60   }
61
62   /**
63    * Gets nested files.
64    *
65    * @param filename the filename
66    * @param hot the hot
67    * @param globalContext the global context
68    * @return the nested files
69    */
70   public static Set<String> getNestedFiles(String filename, HeatOrchestrationTemplate hot,
71                                            GlobalValidationContext globalContext) {
72     Set<String> nestedFileList = new HashSet<>();
73     hot.getResources().values().stream().filter(
74             resource -> resource.getType().endsWith(".yaml") || resource.getType().endsWith(".yml"))
75             .forEach(resource -> nestedFileList.add(resource.getType()));
76
77     Set<String> resourceDefNestedFiles = getResourceDefNestedFiles(hot);
78     nestedFileList.addAll(resourceDefNestedFiles);
79     return nestedFileList;
80   }
81
82   /**
83    * Gets artifact files.
84    *
85    * @param filename the filename
86    * @param hot the hot
87    * @param globalContext the global context
88    * @return the artifact files
89    */
90   public static Set<String> getArtifactFiles(String filename, HeatOrchestrationTemplate hot,
91                                              GlobalValidationContext globalContext) {
92     Set<String> artifactSet = new HashSet<>();
93     Collection<Resource> resourcesValue =
94             hot.getResources() == null ? null : hot.getResources().values();
95     if (CollectionUtils.isNotEmpty(resourcesValue)) {
96       for (Resource resource : resourcesValue) {
97         Collection<Object> properties =
98                 resource.getProperties() == null ? null : resource.getProperties().values();
99
100         artifactSet = getArtifactsFromPropertiesAndAddInArtifactSet(properties,
101                                       filename, globalContext);
102       }
103     }
104     return artifactSet;
105   }
106
107   private static Set<String> getArtifactsFromPropertiesAndAddInArtifactSet(Collection<Object> properties,
108                                                     String filename,
109                                                     GlobalValidationContext globalContext ){
110     Set<String> artifactSet = new HashSet<>();
111     if (CollectionUtils.isNotEmpty(properties)) {
112
113       for (Object property : properties) {
114         Set<String> artifactNames =
115                 HeatStructureUtil.getReferencedValuesByFunctionName(filename, "get_file", property,
116                         globalContext);
117         artifactSet.addAll(artifactNames);
118       }
119     }
120
121     return artifactSet;
122   }
123
124   private static Set<String> getResourceDefNestedFiles(HeatOrchestrationTemplate hot) {
125     Set<String> resourceDefNestedFiles = new HashSet<>();
126     hot.getResources()
127             .entrySet().stream().filter(entry -> entry.getValue().getType()
128             .equals(HeatResourcesTypes.RESOURCE_GROUP_RESOURCE_TYPE.getHeatResource()))
129             .filter(entry ->
130                     getResourceDef(entry.getValue()) != null
131                             && HeatStructureUtil.isNestedResource(
132                             getResourceDef(entry.getValue())
133                                     .getType()))
134             .forEach(entry -> resourceDefNestedFiles.add(
135                     getResourceDef( entry.getValue()).getType()));
136     return resourceDefNestedFiles;
137   }
138
139   /**
140    * Gets resource def.
141    *
142    * @param resource the resource
143    * @return the resource def
144    */
145   @SuppressWarnings("unchecked")
146   public static Resource getResourceDef( Resource resource) {
147     Resource resourceDef = null;
148     Map<String, Object> resourceDefValueMap = resource.getProperties() == null ? null
149             : (Map<String, Object>) resource.getProperties().get(
150             PropertiesMapKeyTypes.RESOURCE_DEF.getKeyMap());
151     if (resourceDefValueMap != null && MapUtils.isNotEmpty(resourceDefValueMap) ) {
152       Object resourceDefType = resourceDefValueMap.get(TYPE);
153       if ( resourceDefType instanceof String && isResourceGroupTypeNested((String) resourceDefType)) {
154         resourceDef = new Resource();
155         resourceDef.setType((String) resourceDefType);
156         //noinspection unchecked
157         resourceDef.setProperties((Map<String, Object>) resourceDefValueMap.get("properties"));
158       }
159
160     }
161     return resourceDef;
162   }
163
164   @SuppressWarnings("unchecked")
165   public static void checkResourceGroupTypeValid(String filename, String resourceName,
166                                                  Resource resource,
167                                                  GlobalValidationContext globalContext) {
168     Map<String, Object> resourceDefValueMap = resource.getProperties() == null ? null
169             : (Map<String, Object>) resource.getProperties().get(
170             PropertiesMapKeyTypes.RESOURCE_DEF.getKeyMap());
171     if (resourceDefValueMap != null && MapUtils.isNotEmpty(resourceDefValueMap) ) {
172       Object resourceDefType = resourceDefValueMap.get(TYPE);
173       if (Objects.nonNull(resourceDefType) && !(resourceDefType instanceof String) ) {
174         globalContext.addMessage(filename, ErrorLevel.WARNING, ErrorMessagesFormatBuilder
175                         .getErrorWithParameters(
176                                 globalContext.getMessageCode(),
177                                 Messages.INVALID_RESOURCE_GROUP_TYPE.getErrorMessage(),
178                                 resourceName, resourceDefType.toString()),
179                 LoggerTragetServiceName.VALIDATE_RESOURCE_GROUP_TYPE, "Invalid resource group type");
180       }
181     }
182   }
183
184   @SuppressWarnings("unchecked")
185   public static void checkResourceTypeValid(String filename, String resourceName,
186                                             Resource resource,
187                                             GlobalValidationContext globalContext) {
188     Map<String, Object> resourceDefValueMap = resource.getProperties() == null ? null
189             : (Map<String, Object>) resource.getProperties().get(
190             PropertiesMapKeyTypes.RESOURCE_DEF.getKeyMap());
191     if (resourceDefValueMap != null && MapUtils.isNotEmpty(resourceDefValueMap) ) {
192       Object resourceDefType = resourceDefValueMap.get(TYPE);
193       if (Objects.isNull(resourceDefType)) {
194         globalContext.addMessage(filename, ErrorLevel.WARNING, ErrorMessagesFormatBuilder
195                         .getErrorWithParameters(
196                                 globalContext.getMessageCode(), Messages.INVALID_RESOURCE_TYPE.getErrorMessage(),
197                                 "null", resourceName), LoggerTragetServiceName.VALIDATE_RESOURCE_GROUP_TYPE,
198                 "Invalid resource type");
199       }
200     }
201   }
202
203   public static boolean isResourceGroupTypeNested(String resourceDefType) {
204     return HeatStructureUtil.isNestedResource(resourceDefType);
205   }
206
207   public static boolean checkIfResourceGroupTypeIsNested(String filename, String resourceName,
208                                                          Resource resource,
209                                                          GlobalValidationContext globalContext) {
210     //noinspection unchecked
211     Map<String, Object> resourceDefValueMap = resource.getProperties() == null ? null
212             : (Map<String, Object>) resource.getProperties().get(
213             PropertiesMapKeyTypes.RESOURCE_DEF.getKeyMap());
214     if (resourceDefValueMap != null && MapUtils.isNotEmpty(resourceDefValueMap) ) {
215       Object resourceDefType = resourceDefValueMap.get(TYPE);
216       if (resourceDefType instanceof String && isResourceGroupTypeNested((String) resourceDefType)) {
217
218         globalContext.addMessage(filename, ErrorLevel.WARNING, ErrorMessagesFormatBuilder
219                         .getErrorWithParameters(
220                                 globalContext.getMessageCode(),
221                                 Messages.INVALID_RESOURCE_GROUP_TYPE.getErrorMessage(),
222                                 resourceName, resourceDefType.toString()),
223                 LoggerTragetServiceName.VALIDATE_RESOURCE_GROUP_TYPE,
224                 "Invalid resource group type");
225         return true;
226       }
227     }
228     return false;
229   }
230 }