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.types.LoggerTragetServiceName;
34 import java.util.Collection;
35 import java.util.HashSet;
37 import java.util.Objects;
40 public class HeatTreeManagerUtil {
42 private static final String TYPE = "type";
43 private HeatTreeManagerUtil() {
48 * Init heat tree manager heat tree manager.
50 * @param fileContentMap the file content map
51 * @return the heat tree manager
53 public static HeatTreeManager initHeatTreeManager(FileContentHandler fileContentMap) {
55 HeatTreeManager heatTreeManager = new HeatTreeManager();
56 fileContentMap.getFileList().stream().forEach(
57 fileName -> heatTreeManager.addFile(fileName, fileContentMap.getFileContent(fileName)));
59 return heatTreeManager;
65 * @param filename the filename
67 * @param globalContext the global context
68 * @return the nested files
70 public static Set<String> getNestedFiles(String filename, HeatOrchestrationTemplate hot,
71 GlobalValidationContext globalContext) {
72 Set<String> nestedFileList = new HashSet<>();
73 hot.getResources().values().stream().filter(
74 resource -> resource.getType().endsWith(".yaml") || resource.getType().endsWith(".yml"))
75 .forEach(resource -> nestedFileList.add(resource.getType()));
77 Set<String> resourceDefNestedFiles = getResourceDefNestedFiles(hot);
78 nestedFileList.addAll(resourceDefNestedFiles);
79 return nestedFileList;
83 * Gets artifact files.
85 * @param filename the filename
87 * @param globalContext the global context
88 * @return the artifact files
90 public static Set<String> getArtifactFiles(String filename, HeatOrchestrationTemplate hot,
91 GlobalValidationContext globalContext) {
92 Set<String> artifactSet = new HashSet<>();
93 Collection<Resource> resourcesValue =
94 hot.getResources() == null ? null : hot.getResources().values();
95 if (CollectionUtils.isNotEmpty(resourcesValue)) {
96 for (Resource resource : resourcesValue) {
97 Collection<Object> properties =
98 resource.getProperties() == null ? null : resource.getProperties().values();
100 artifactSet = getArtifactsFromPropertiesAndAddInArtifactSet(properties,
101 filename, globalContext);
107 private static Set<String> getArtifactsFromPropertiesAndAddInArtifactSet(Collection<Object> properties,
109 GlobalValidationContext globalContext ){
110 Set<String> artifactSet = new HashSet<>();
111 if (CollectionUtils.isNotEmpty(properties)) {
113 for (Object property : properties) {
114 Set<String> artifactNames =
115 HeatStructureUtil.getReferencedValuesByFunctionName(filename, "get_file", property,
117 artifactSet.addAll(artifactNames);
124 private static Set<String> getResourceDefNestedFiles(HeatOrchestrationTemplate hot) {
125 Set<String> resourceDefNestedFiles = new HashSet<>();
127 .entrySet().stream().filter(entry -> entry.getValue().getType()
128 .equals(HeatResourcesTypes.RESOURCE_GROUP_RESOURCE_TYPE.getHeatResource()))
130 getResourceDef(entry.getValue()) != null
131 && HeatStructureUtil.isNestedResource(
132 getResourceDef(entry.getValue())
134 .forEach(entry -> resourceDefNestedFiles.add(
135 getResourceDef( entry.getValue()).getType()));
136 return resourceDefNestedFiles;
142 * @param resource the resource
143 * @return the resource def
145 @SuppressWarnings("unchecked")
146 public static Resource getResourceDef( Resource resource) {
147 Resource resourceDef = null;
148 Map<String, Object> resourceDefValueMap = resource.getProperties() == null ? null
149 : (Map<String, Object>) resource.getProperties().get(
150 PropertiesMapKeyTypes.RESOURCE_DEF.getKeyMap());
151 if (resourceDefValueMap != null && MapUtils.isNotEmpty(resourceDefValueMap) ) {
152 Object resourceDefType = resourceDefValueMap.get(TYPE);
153 if ( resourceDefType instanceof String && isResourceGroupTypeNested((String) resourceDefType)) {
154 resourceDef = new Resource();
155 resourceDef.setType((String) resourceDefType);
156 //noinspection unchecked
157 resourceDef.setProperties((Map<String, Object>) resourceDefValueMap.get("properties"));
164 @SuppressWarnings("unchecked")
165 public static void checkResourceGroupTypeValid(String filename, String resourceName,
167 GlobalValidationContext globalContext) {
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 (Objects.nonNull(resourceDefType) && !(resourceDefType instanceof String) ) {
174 globalContext.addMessage(filename, ErrorLevel.WARNING, ErrorMessagesFormatBuilder
175 .getErrorWithParameters(
176 globalContext.getMessageCode(),
177 Messages.INVALID_RESOURCE_GROUP_TYPE.getErrorMessage(),
178 resourceName, resourceDefType.toString()),
179 LoggerTragetServiceName.VALIDATE_RESOURCE_GROUP_TYPE, "Invalid resource group type");
184 @SuppressWarnings("unchecked")
185 public static void checkResourceTypeValid(String filename, String resourceName,
187 GlobalValidationContext globalContext) {
188 Map<String, Object> resourceDefValueMap = resource.getProperties() == null ? null
189 : (Map<String, Object>) resource.getProperties().get(
190 PropertiesMapKeyTypes.RESOURCE_DEF.getKeyMap());
191 if (resourceDefValueMap != null && MapUtils.isNotEmpty(resourceDefValueMap) ) {
192 Object resourceDefType = resourceDefValueMap.get(TYPE);
193 if (Objects.isNull(resourceDefType)) {
194 globalContext.addMessage(filename, ErrorLevel.WARNING, ErrorMessagesFormatBuilder
195 .getErrorWithParameters(
196 globalContext.getMessageCode(), Messages.INVALID_RESOURCE_TYPE.getErrorMessage(),
197 "null", resourceName), LoggerTragetServiceName.VALIDATE_RESOURCE_GROUP_TYPE,
198 "Invalid resource type");
203 public static boolean isResourceGroupTypeNested(String resourceDefType) {
204 return HeatStructureUtil.isNestedResource(resourceDefType);
207 public static boolean checkIfResourceGroupTypeIsNested(String filename, String resourceName,
209 GlobalValidationContext globalContext) {
210 //noinspection unchecked
211 Map<String, Object> resourceDefValueMap = resource.getProperties() == null ? null
212 : (Map<String, Object>) resource.getProperties().get(
213 PropertiesMapKeyTypes.RESOURCE_DEF.getKeyMap());
214 if (resourceDefValueMap != null && MapUtils.isNotEmpty(resourceDefValueMap) ) {
215 Object resourceDefType = resourceDefValueMap.get(TYPE);
216 if (resourceDefType instanceof String && isResourceGroupTypeNested((String) resourceDefType)) {
218 globalContext.addMessage(filename, ErrorLevel.WARNING, ErrorMessagesFormatBuilder
219 .getErrorWithParameters(
220 globalContext.getMessageCode(),
221 Messages.INVALID_RESOURCE_GROUP_TYPE.getErrorMessage(),
222 resourceName, resourceDefType.toString()),
223 LoggerTragetServiceName.VALIDATE_RESOURCE_GROUP_TYPE,
224 "Invalid resource group type");