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;
33 import java.util.Collection;
34 import java.util.HashSet;
36 import java.util.Objects;
39 public class HeatTreeManagerUtil {
41 private static final String TYPE = "type";
42 private HeatTreeManagerUtil() {
47 * Init heat tree manager heat tree manager.
49 * @param fileContentMap the file content map
50 * @return the heat tree manager
52 public static HeatTreeManager initHeatTreeManager(FileContentHandler fileContentMap) {
54 HeatTreeManager heatTreeManager = new HeatTreeManager();
55 fileContentMap.getFileList().stream().forEach(
56 fileName -> heatTreeManager.addFile(fileName, fileContentMap.getFileContent(fileName)));
58 return heatTreeManager;
64 * @param filename the filename
66 * @param globalContext the global context
67 * @return the nested files
69 public static Set<String> getNestedFiles(String filename, HeatOrchestrationTemplate hot,
70 GlobalValidationContext globalContext) {
71 Set<String> nestedFileList = new HashSet<>();
72 hot.getResources().values().stream().filter(
73 resource -> resource.getType().endsWith(".yaml") || resource.getType().endsWith(".yml"))
74 .forEach(resource -> nestedFileList.add(resource.getType()));
76 Set<String> resourceDefNestedFiles = getResourceDefNestedFiles(hot);
77 nestedFileList.addAll(resourceDefNestedFiles);
78 return nestedFileList;
82 * Gets artifact files.
84 * @param filename the filename
86 * @param globalContext the global context
87 * @return the artifact files
89 public static Set<String> getArtifactFiles(String filename, HeatOrchestrationTemplate hot,
90 GlobalValidationContext globalContext) {
91 Set<String> artifactSet = new HashSet<>();
92 Collection<Resource> resourcesValue =
93 hot.getResources() == null ? null : hot.getResources().values();
94 if (CollectionUtils.isNotEmpty(resourcesValue)) {
95 for (Resource resource : resourcesValue) {
96 Collection<Object> properties =
97 resource.getProperties() == null ? null : resource.getProperties().values();
99 artifactSet = getArtifactsFromPropertiesAndAddInArtifactSet(properties,
100 filename, globalContext);
106 private static Set<String> getArtifactsFromPropertiesAndAddInArtifactSet(Collection<Object> properties,
108 GlobalValidationContext globalContext ){
109 Set<String> artifactSet = new HashSet<>();
110 if (CollectionUtils.isNotEmpty(properties)) {
112 for (Object property : properties) {
113 Set<String> artifactNames =
114 HeatStructureUtil.getReferencedValuesByFunctionName(filename, "get_file", property,
116 artifactSet.addAll(artifactNames);
123 private static Set<String> getResourceDefNestedFiles(HeatOrchestrationTemplate hot) {
124 Set<String> resourceDefNestedFiles = new HashSet<>();
126 .entrySet().stream().filter(entry -> entry.getValue().getType()
127 .equals(HeatResourcesTypes.RESOURCE_GROUP_RESOURCE_TYPE.getHeatResource()))
129 getResourceDef(entry.getValue()) != null
130 && HeatStructureUtil.isNestedResource(
131 getResourceDef(entry.getValue())
133 .forEach(entry -> resourceDefNestedFiles.add(
134 getResourceDef( entry.getValue()).getType()));
135 return resourceDefNestedFiles;
141 * @param resource the resource
142 * @return the resource def
144 @SuppressWarnings("unchecked")
145 public static Resource getResourceDef( Resource resource) {
146 Resource resourceDef = null;
147 Map<String, Object> resourceDefValueMap = resource.getProperties() == null ? null
148 : (Map<String, Object>) resource.getProperties().get(
149 PropertiesMapKeyTypes.RESOURCE_DEF.getKeyMap());
150 if (resourceDefValueMap != null && MapUtils.isNotEmpty(resourceDefValueMap) ) {
151 Object resourceDefType = resourceDefValueMap.get(TYPE);
152 if ( resourceDefType instanceof String && isResourceGroupTypeNested((String) resourceDefType)) {
153 resourceDef = new Resource();
154 resourceDef.setType((String) resourceDefType);
155 //noinspection unchecked
156 resourceDef.setProperties((Map<String, Object>) resourceDefValueMap.get("properties"));
163 @SuppressWarnings("unchecked")
164 public static void checkResourceGroupTypeValid(String filename, String resourceName,
166 GlobalValidationContext globalContext) {
167 Map<String, Object> resourceDefValueMap = resource.getProperties() == null ? null
168 : (Map<String, Object>) resource.getProperties().get(
169 PropertiesMapKeyTypes.RESOURCE_DEF.getKeyMap());
170 if (resourceDefValueMap != null && MapUtils.isNotEmpty(resourceDefValueMap) ) {
171 Object resourceDefType = resourceDefValueMap.get(TYPE);
172 if (Objects.nonNull(resourceDefType) && !(resourceDefType instanceof String) ) {
173 globalContext.addMessage(filename, ErrorLevel.WARNING, ErrorMessagesFormatBuilder
174 .getErrorWithParameters(
175 globalContext.getMessageCode(),
176 Messages.INVALID_RESOURCE_GROUP_TYPE.getErrorMessage(),
177 resourceName, resourceDefType.toString()));
182 @SuppressWarnings("unchecked")
183 public static void checkResourceTypeValid(String filename, String resourceName,
185 GlobalValidationContext globalContext) {
186 Map<String, Object> resourceDefValueMap = resource.getProperties() == null ? null
187 : (Map<String, Object>) resource.getProperties().get(
188 PropertiesMapKeyTypes.RESOURCE_DEF.getKeyMap());
189 if (resourceDefValueMap != null && MapUtils.isNotEmpty(resourceDefValueMap) ) {
190 Object resourceDefType = resourceDefValueMap.get(TYPE);
191 if (Objects.isNull(resourceDefType)) {
192 globalContext.addMessage(filename, ErrorLevel.WARNING, ErrorMessagesFormatBuilder
193 .getErrorWithParameters(
194 globalContext.getMessageCode(), Messages.INVALID_RESOURCE_TYPE.getErrorMessage(),
195 "null", resourceName));
200 public static boolean isResourceGroupTypeNested(String resourceDefType) {
201 return HeatStructureUtil.isNestedResource(resourceDefType);
204 public static boolean checkIfResourceGroupTypeIsNested(String filename, String resourceName,
206 GlobalValidationContext globalContext) {
207 //noinspection unchecked
208 Map<String, Object> resourceDefValueMap = resource.getProperties() == null ? null
209 : (Map<String, Object>) resource.getProperties().get(
210 PropertiesMapKeyTypes.RESOURCE_DEF.getKeyMap());
211 if (resourceDefValueMap != null && MapUtils.isNotEmpty(resourceDefValueMap) ) {
212 Object resourceDefType = resourceDefValueMap.get(TYPE);
213 if (resourceDefType instanceof String && isResourceGroupTypeNested((String) resourceDefType)) {
215 globalContext.addMessage(filename, ErrorLevel.WARNING, ErrorMessagesFormatBuilder
216 .getErrorWithParameters(
217 globalContext.getMessageCode(),
218 Messages.INVALID_RESOURCE_GROUP_TYPE.getErrorMessage(),
219 resourceName, resourceDefType.toString()));