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