792d9a4854e50d58dd84ddd62d5ce363b5b5f018
[sdc.git] /
1 /*
2  * Copyright © 2018 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 java.util.Collection;
21 import java.util.HashSet;
22 import java.util.Map;
23 import java.util.Objects;
24 import java.util.Set;
25
26 import org.apache.commons.collections4.CollectionUtils;
27 import org.apache.commons.collections4.MapUtils;
28 import org.openecomp.core.utilities.file.FileContentHandler;
29 import org.openecomp.core.validation.errors.ErrorMessagesFormatBuilder;
30 import org.openecomp.core.validation.types.GlobalValidationContext;
31 import org.openecomp.sdc.common.errors.Messages;
32 import org.openecomp.sdc.datatypes.error.ErrorLevel;
33 import org.openecomp.sdc.heat.datatypes.model.HeatOrchestrationTemplate;
34 import org.openecomp.sdc.heat.datatypes.model.HeatResourcesTypes;
35 import org.openecomp.sdc.heat.datatypes.model.PropertiesMapKeyTypes;
36 import org.openecomp.sdc.heat.datatypes.model.Resource;
37 import org.openecomp.sdc.heat.services.HeatStructureUtil;
38
39 public class HeatTreeManagerUtil {
40
41     private static final String TYPE = "type";
42
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().forEach(
57                 fileName -> heatTreeManager.addFile(fileName, fileContentMap.getFileContentAsStream(fileName)));
58
59         return heatTreeManager;
60     }
61
62     /**
63      * Will verify the resource type and if type is of nested will return the nested file name
64      *
65      * @param hot Containing all translated data from heat file
66      * @return the nested files
67      */
68     public static Set<String> getNestedFiles(HeatOrchestrationTemplate hot) {
69         Set<String> nestedFileList = new HashSet<>();
70         hot.getResources().values().stream().filter(
71                 resource -> resource.getType().endsWith(".yaml") || resource.getType().endsWith(".yml"))
72                 .forEach(resource -> nestedFileList.add(resource.getType()));
73
74         Set<String> resourceDefNestedFiles = getResourceDefNestedFiles(hot);
75         nestedFileList.addAll(resourceDefNestedFiles);
76         return nestedFileList;
77     }
78
79     /**
80      * Verify if any artifact present in file whose detail is provided and return Artifact name
81      *
82      * @param filename      name of file where artifact is too be looked for
83      * @param hot           translated heat data
84      * @param globalContext the global context
85      * @return the artifact files name
86      */
87     public static Set<String> getArtifactFiles(String filename, HeatOrchestrationTemplate hot,
88                                                GlobalValidationContext globalContext) {
89         Set<String> artifactSet = new HashSet<>();
90         Collection<Resource> resourcesValue =
91                 hot.getResources() == null ? null : hot.getResources().values();
92         if (CollectionUtils.isNotEmpty(resourcesValue)) {
93             for (Resource resource : resourcesValue) {
94                 Collection<Object> properties =
95                         resource.getProperties() == null ? null : resource.getProperties().values();
96
97                 artifactSet.addAll(getArtifactsFromPropertiesAndAddInArtifactSet(properties,
98                         filename, globalContext));
99             }
100         }
101         return artifactSet;
102     }
103
104     private static Set<String> getArtifactsFromPropertiesAndAddInArtifactSet(Collection<Object> properties,
105                                                                              String filename,
106                                                                              GlobalValidationContext globalContext) {
107         Set<String> artifactSet = new HashSet<>();
108         if (CollectionUtils.isNotEmpty(properties)) {
109
110             for (Object property : properties) {
111                 Set<String> artifactNames =
112                         HeatStructureUtil.getReferencedValuesByFunctionName(filename, "get_file", property,
113                                 globalContext);
114                 artifactSet.addAll(artifactNames);
115             }
116         }
117
118         return artifactSet;
119     }
120
121     private static Set<String> getResourceDefNestedFiles(HeatOrchestrationTemplate hot) {
122         Set<String> resourceDefNestedFiles = new HashSet<>();
123         hot.getResources()
124                 .entrySet().stream().filter(entry -> entry.getValue().getType()
125                 .equals(HeatResourcesTypes.RESOURCE_GROUP_RESOURCE_TYPE.getHeatResource()))
126                 .filter(entry ->
127                         getResourceDef(entry.getValue()) != null
128                                 && HeatStructureUtil.isNestedResource(
129                                 getResourceDef(entry.getValue())
130                                         .getType()))
131                 .forEach(entry -> resourceDefNestedFiles.add(
132                         getResourceDef(entry.getValue()).getType()));
133         return resourceDefNestedFiles;
134     }
135
136     /**
137      * Gets resource def.
138      *
139      * @param resource the resource
140      * @return the resource def
141      */
142     @SuppressWarnings("unchecked")
143     public static Resource getResourceDef(Resource resource) {
144         Resource resourceDef = null;
145         Map<String, Object> resourceDefValueMap = resource.getProperties() == null ? null
146                 : (Map<String, Object>) resource.getProperties().get(
147                         PropertiesMapKeyTypes.RESOURCE_DEF.getKeyMap());
148         if (MapUtils.isNotEmpty(resourceDefValueMap)) {
149             Object resourceDefType = resourceDefValueMap.get(TYPE);
150             if (resourceDefType instanceof String && isResourceGroupTypeNested((String) resourceDefType)) {
151                 resourceDef = new Resource();
152                 resourceDef.setType((String) resourceDefType);
153                 //noinspection unchecked
154                 resourceDef.setProperties((Map<String, Object>) resourceDefValueMap.get("properties"));
155             }
156
157         }
158         return resourceDef;
159     }
160
161     @SuppressWarnings("unchecked")
162     public static void checkResourceGroupTypeValid(String filename, String resourceName,
163                                                    Resource resource,
164                                                    GlobalValidationContext globalContext) {
165         Map<String, Object> resourceDefValueMap = resource.getProperties() == null ? null
166                 : (Map<String, Object>) resource.getProperties().get(
167                         PropertiesMapKeyTypes.RESOURCE_DEF.getKeyMap());
168         if (MapUtils.isNotEmpty(resourceDefValueMap)) {
169             Object resourceDefType = resourceDefValueMap.get(TYPE);
170             if (Objects.nonNull(resourceDefType) && !(resourceDefType instanceof String)) {
171                 globalContext.addMessage(filename, ErrorLevel.WARNING, ErrorMessagesFormatBuilder
172                         .getErrorWithParameters(
173                                 globalContext.getMessageCode(),
174                                 Messages.INVALID_RESOURCE_GROUP_TYPE.getErrorMessage(),
175                                 resourceName, resourceDefType.toString()));
176             }
177         }
178     }
179
180     @SuppressWarnings("unchecked")
181     public static void checkResourceTypeValid(String filename, String resourceName,
182                                               Resource resource,
183                                               GlobalValidationContext globalContext) {
184         Map<String, Object> resourceDefValueMap = resource.getProperties() == null ? null
185                 : (Map<String, Object>) resource.getProperties().get(PropertiesMapKeyTypes.RESOURCE_DEF.getKeyMap());
186         if (MapUtils.isNotEmpty(resourceDefValueMap)) {
187             Object resourceDefType = resourceDefValueMap.get(TYPE);
188             if (Objects.isNull(resourceDefType)) {
189                 globalContext.addMessage(filename, ErrorLevel.WARNING, ErrorMessagesFormatBuilder
190                         .getErrorWithParameters(
191                                 globalContext.getMessageCode(), Messages.INVALID_RESOURCE_TYPE.getErrorMessage(),
192                                 "null", resourceName));
193             }
194         }
195     }
196
197     public static boolean isResourceGroupTypeNested(String resourceDefType) {
198         return HeatStructureUtil.isNestedResource(resourceDefType);
199     }
200
201     public static boolean checkIfResourceGroupTypeIsNested(String filename, String resourceName,
202                                                            Resource resource,
203                                                            GlobalValidationContext globalContext) {
204         //noinspection unchecked
205         Map<String, Object> resourceDefValueMap = resource.getProperties() == null ? null
206                 : (Map<String, Object>) resource.getProperties().get(PropertiesMapKeyTypes.RESOURCE_DEF.getKeyMap());
207         if (MapUtils.isNotEmpty(resourceDefValueMap)) {
208             Object resourceDefType = resourceDefValueMap.get(TYPE);
209             if (resourceDefType instanceof String && isResourceGroupTypeNested((String) resourceDefType)) {
210                 globalContext.addMessage(filename, ErrorLevel.WARNING, ErrorMessagesFormatBuilder
211                         .getErrorWithParameters(
212                                 globalContext.getMessageCode(),
213                                 Messages.INVALID_RESOURCE_GROUP_TYPE.getErrorMessage(),
214                                 resourceName, resourceDefType.toString()));
215                 return true;
216             }
217         }
218         return false;
219     }
220 }