2 * Copyright © 2016-2017 European Support Limited
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 package org.openecomp.sdc.heat.services.tree;
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;
35 import java.util.Collection;
36 import java.util.HashSet;
38 import java.util.Objects;
41 public class HeatTreeManagerUtil {
43 private static final String TYPE = "type";
44 private static final MdcDataDebugMessage MDC_DATA_DEBUG_MESSAGE = new MdcDataDebugMessage();
45 private HeatTreeManagerUtil() {
50 * Init heat tree manager heat tree manager.
52 * @param fileContentMap the file content map
53 * @return the heat tree manager
55 public static HeatTreeManager initHeatTreeManager(FileContentHandler fileContentMap) {
57 HeatTreeManager heatTreeManager = new HeatTreeManager();
58 fileContentMap.getFileList().stream().forEach(
59 fileName -> heatTreeManager.addFile(fileName, fileContentMap.getFileContent(fileName)));
61 return heatTreeManager;
67 * @param filename the filename
69 * @param globalContext the global context
70 * @return the nested files
72 public static Set<String> getNestedFiles(String filename, HeatOrchestrationTemplate hot,
73 GlobalValidationContext globalContext) {
75 MDC_DATA_DEBUG_MESSAGE.debugEntryMessage(null, null);
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()));
82 Set<String> resourceDefNestedFiles = getResourceDefNestedFiles(hot);
83 nestedFileList.addAll(resourceDefNestedFiles);
85 MDC_DATA_DEBUG_MESSAGE.debugExitMessage(null, null);
86 return nestedFileList;
90 * Gets artifact files.
92 * @param filename the filename
94 * @param globalContext the global context
95 * @return the artifact files
97 public static Set<String> getArtifactFiles(String filename, HeatOrchestrationTemplate hot,
98 GlobalValidationContext globalContext) {
100 MDC_DATA_DEBUG_MESSAGE.debugEntryMessage(null, null);
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();
110 artifactSet = getArtifactsFromPropertiesAndAddInArtifactSet(properties,
111 filename, globalContext);
115 MDC_DATA_DEBUG_MESSAGE.debugExitMessage(null, null);
119 private static Set<String> getArtifactsFromPropertiesAndAddInArtifactSet(Collection<Object> properties,
121 GlobalValidationContext globalContext ){
122 Set<String> artifactSet = new HashSet<>();
123 if (CollectionUtils.isNotEmpty(properties)) {
125 for (Object property : properties) {
126 Set<String> artifactNames =
127 HeatStructureUtil.getReferencedValuesByFunctionName(filename, "get_file", property,
129 artifactSet.addAll(artifactNames);
136 private static Set<String> getResourceDefNestedFiles(HeatOrchestrationTemplate hot) {
138 MDC_DATA_DEBUG_MESSAGE.debugEntryMessage(null, null);
140 Set<String> resourceDefNestedFiles = new HashSet<>();
142 .entrySet().stream().filter(entry -> entry.getValue().getType()
143 .equals(HeatResourcesTypes.RESOURCE_GROUP_RESOURCE_TYPE.getHeatResource()))
145 getResourceDef(entry.getValue()) != null
146 && HeatStructureUtil.isNestedResource(
147 getResourceDef(entry.getValue())
149 .forEach(entry -> resourceDefNestedFiles.add(
150 getResourceDef( entry.getValue()).getType()));
152 MDC_DATA_DEBUG_MESSAGE.debugExitMessage(null, null);
153 return resourceDefNestedFiles;
159 * @param resource the resource
160 * @return the resource def
162 @SuppressWarnings("unchecked")
163 public static Resource getResourceDef( Resource resource) {
165 MDC_DATA_DEBUG_MESSAGE.debugEntryMessage(null, null);
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"));
182 MDC_DATA_DEBUG_MESSAGE.debugExitMessage(null, null);
186 @SuppressWarnings("unchecked")
187 public static void checkResourceGroupTypeValid(String filename, String resourceName,
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");
206 @SuppressWarnings("unchecked")
207 public static void checkResourceTypeValid(String filename, String resourceName,
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");
225 public static boolean isResourceGroupTypeNested(String resourceDefType) {
226 return HeatStructureUtil.isNestedResource(resourceDefType);
229 public static boolean checkIfResourceGroupTypeIsNested(String filename, String resourceName,
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)) {
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");