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