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