2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.openecomp.sdc.heat.services.tree;
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;
38 import java.util.Collection;
39 import java.util.HashSet;
41 import java.util.Objects;
44 public class HeatTreeManagerUtil {
46 private static MdcDataDebugMessage mdcDataDebugMessage = new MdcDataDebugMessage();
49 * Init heat tree manager heat tree manager.
51 * @param fileContentMap the file content map
52 * @return the heat tree manager
54 public static HeatTreeManager initHeatTreeManager(FileContentHandler fileContentMap) {
56 HeatTreeManager heatTreeManager = new HeatTreeManager();
57 fileContentMap.getFileList().stream().forEach(
58 fileName -> heatTreeManager.addFile(fileName, fileContentMap.getFileContent(fileName)));
60 return heatTreeManager;
66 * @param filename the filename
68 * @param globalContext the global context
69 * @return the nested files
71 public static Set<String> getNestedFiles(String filename, HeatOrchestrationTemplate hot,
72 GlobalValidationContext globalContext) {
75 mdcDataDebugMessage.debugEntryMessage(null, null);
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()));
83 resourceDefNestedFiles = getResourceDefNestedFiles(filename, hot, globalContext);
84 nestedFileList.addAll(resourceDefNestedFiles);
86 mdcDataDebugMessage.debugExitMessage(null, null);
87 return nestedFileList;
91 * Gets artifact files.
93 * @param filename the filename
95 * @param globalContext the global context
96 * @return the artifact files
98 public static Set<String> getArtifactFiles(String filename, HeatOrchestrationTemplate hot,
99 GlobalValidationContext globalContext) {
102 mdcDataDebugMessage.debugEntryMessage(null, null);
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,
116 artifactSet.addAll(artifactNames);
122 mdcDataDebugMessage.debugExitMessage(null, null);
126 private static Set<String> getResourceDefNestedFiles(String filename,
127 HeatOrchestrationTemplate hot,
128 GlobalValidationContext globalContext) {
131 mdcDataDebugMessage.debugEntryMessage(null, null);
133 Set<String> resourceDefNestedFiles = new HashSet<>();
135 .entrySet().stream().filter(entry -> entry.getValue().getType()
136 .equals(HeatResourcesTypes.RESOURCE_GROUP_RESOURCE_TYPE.getHeatResource()))
138 getResourceDef(filename, entry.getKey(), entry.getValue(), globalContext) != null
139 && HeatStructureUtil.isNestedResource(
140 getResourceDef(filename, entry.getKey(), entry.getValue(), globalContext)
142 .forEach(entry -> resourceDefNestedFiles.add(
143 getResourceDef(filename, entry.getKey(), entry.getValue(), globalContext).getType()));
145 mdcDataDebugMessage.debugExitMessage(null, null);
146 return resourceDefNestedFiles;
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
158 @SuppressWarnings("unchecked")
159 public static Resource getResourceDef(String filename, String resourceName, Resource resource,
160 GlobalValidationContext globalContext) {
163 mdcDataDebugMessage.debugEntryMessage(null, null);
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) {
174 checkIfResourceGroupTypeIsNested(filename, resourceName, (String) resourceDefType,
177 resourceDef = new Resource();
178 resourceDef.setType((String) resourceDefType);
179 //noinspection unchecked
180 resourceDef.setProperties((Map<String, Object>) resourceDefValueMap.get("properties"));
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");
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");
197 mdcDataDebugMessage.debugExitMessage(null, null);
202 * Check if resource group type is nested boolean.
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
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");