2 * Copyright © 2018 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 java.util.Collection;
21 import java.util.HashMap;
22 import java.util.HashSet;
24 import java.util.Objects;
27 import org.apache.commons.collections4.CollectionUtils;
28 import org.apache.commons.collections4.MapUtils;
29 import org.openecomp.core.utilities.file.FileContentHandler;
30 import org.openecomp.core.validation.errors.ErrorMessagesFormatBuilder;
31 import org.openecomp.core.validation.types.GlobalValidationContext;
32 import org.openecomp.sdc.common.errors.Messages;
33 import org.openecomp.sdc.datatypes.error.ErrorLevel;
34 import org.openecomp.sdc.heat.datatypes.model.HeatOrchestrationTemplate;
35 import org.openecomp.sdc.heat.datatypes.model.HeatResourcesTypes;
36 import org.openecomp.sdc.heat.datatypes.model.PropertiesMapKeyTypes;
37 import org.openecomp.sdc.heat.datatypes.model.Resource;
38 import org.openecomp.sdc.heat.services.HeatStructureUtil;
40 public class HeatTreeManagerUtil {
42 private static final String TYPE = "type";
44 private HeatTreeManagerUtil() {
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().forEach(
58 fileName -> heatTreeManager.addFile(fileName, fileContentMap.getFileContentAsStream(fileName)));
60 return heatTreeManager;
64 * Will verify the resource type and if type is of nested will return the nested file name
66 * @param hot Containing all translated data from heat file
67 * @return the nested files
69 public static Set<String> getNestedFiles(HeatOrchestrationTemplate hot) {
70 Set<String> nestedFileList = new HashSet<>();
71 hot.getResources().values().stream().filter(
72 resource -> resource.getType().endsWith(".yaml") || resource.getType().endsWith(".yml"))
73 .forEach(resource -> nestedFileList.add(resource.getType()));
75 Set<String> resourceDefNestedFiles = getResourceDefNestedFiles(hot);
76 nestedFileList.addAll(resourceDefNestedFiles);
77 return nestedFileList;
81 * Verify if any artifact present in file whose detail is provided and return Artifact name
83 * @param filename name of file where artifact is too be looked for
84 * @param hot translated heat data
85 * @param globalContext the global context
86 * @return the artifact files name
88 public static Set<String> getArtifactFiles(String filename, HeatOrchestrationTemplate hot,
89 GlobalValidationContext globalContext) {
90 Set<String> artifactSet = new HashSet<>();
91 Collection<Resource> resourcesValue =
92 hot.getResources() == null ? null : hot.getResources().values();
93 if (CollectionUtils.isNotEmpty(resourcesValue)) {
94 for (Resource resource : resourcesValue) {
95 Collection<Object> properties =
96 resource.getProperties() == null ? null : resource.getProperties().values();
98 artifactSet.addAll(getArtifactsFromPropertiesAndAddInArtifactSet(properties,
99 filename, globalContext));
105 private static Set<String> getArtifactsFromPropertiesAndAddInArtifactSet(Collection<Object> properties,
107 GlobalValidationContext globalContext) {
108 Set<String> artifactSet = new HashSet<>();
109 if (CollectionUtils.isNotEmpty(properties)) {
111 for (Object property : properties) {
112 Set<String> artifactNames =
113 HeatStructureUtil.getReferencedValuesByFunctionName(filename, "get_file", property,
115 artifactSet.addAll(artifactNames);
122 private static Set<String> getResourceDefNestedFiles(HeatOrchestrationTemplate hot) {
123 Set<String> resourceDefNestedFiles = new HashSet<>();
125 .entrySet().stream().filter(entry -> entry.getValue().getType()
126 .equals(HeatResourcesTypes.RESOURCE_GROUP_RESOURCE_TYPE.getHeatResource()))
128 getResourceDef(entry.getValue()) != null
129 && HeatStructureUtil.isNestedResource(
130 getResourceDef(entry.getValue())
132 .forEach(entry -> resourceDefNestedFiles.add(
133 getResourceDef(entry.getValue()).getType()));
134 return resourceDefNestedFiles;
140 * @param resource the resource
141 * @return the resource def
143 @SuppressWarnings("unchecked")
144 public static Resource getResourceDef(Resource resource) {
145 Resource resourceDef = null;
146 Map<String, Object> resourceDefValueMap = resource.getProperties() == null ? new HashMap<>()
147 : (Map<String, Object>) resource.getProperties().get(
148 PropertiesMapKeyTypes.RESOURCE_DEF.getKeyMap());
149 if (MapUtils.isNotEmpty(resourceDefValueMap)) {
150 Object resourceDefType = resourceDefValueMap.get(TYPE);
151 if (resourceDefType instanceof String && isResourceGroupTypeNested((String) resourceDefType)) {
152 resourceDef = new Resource();
153 resourceDef.setType((String) resourceDefType);
154 //noinspection unchecked
155 resourceDef.setProperties((Map<String, Object>) resourceDefValueMap.get("properties"));
162 @SuppressWarnings("unchecked")
163 public static void checkResourceGroupTypeValid(String filename, String resourceName,
165 GlobalValidationContext globalContext) {
166 Map<String, Object> resourceDefValueMap = resource.getProperties() == null ? new HashMap<>()
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) && !(resourceDefType instanceof String)) {
172 globalContext.addMessage(filename, ErrorLevel.WARNING, ErrorMessagesFormatBuilder
173 .getErrorWithParameters(
174 globalContext.getMessageCode(),
175 Messages.INVALID_RESOURCE_GROUP_TYPE.getErrorMessage(),
176 resourceName, resourceDefType.toString()));
181 @SuppressWarnings("unchecked")
182 public static void checkResourceTypeValid(String filename, String resourceName,
184 GlobalValidationContext globalContext) {
185 Map<String, Object> resourceDefValueMap = resource.getProperties() == null ? new HashMap<>()
186 : (Map<String, Object>) resource.getProperties().get(PropertiesMapKeyTypes.RESOURCE_DEF.getKeyMap());
187 if (MapUtils.isNotEmpty(resourceDefValueMap)) {
188 Object resourceDefType = resourceDefValueMap.get(TYPE);
189 if (Objects.isNull(resourceDefType)) {
190 globalContext.addMessage(filename, ErrorLevel.WARNING, ErrorMessagesFormatBuilder
191 .getErrorWithParameters(
192 globalContext.getMessageCode(), Messages.INVALID_RESOURCE_TYPE.getErrorMessage(),
193 "null", resourceName));
198 public static boolean isResourceGroupTypeNested(String resourceDefType) {
199 return HeatStructureUtil.isNestedResource(resourceDefType);
202 public static boolean checkIfResourceGroupTypeIsNested(String filename, String resourceName,
204 GlobalValidationContext globalContext) {
205 //noinspection unchecked
206 Map<String, Object> resourceDefValueMap = resource.getProperties() == null ? new HashMap<>()
207 : (Map<String, Object>) resource.getProperties().get(PropertiesMapKeyTypes.RESOURCE_DEF.getKeyMap());
208 if (MapUtils.isNotEmpty(resourceDefValueMap)) {
209 Object resourceDefType = resourceDefValueMap.get(TYPE);
210 if (resourceDefType instanceof String && isResourceGroupTypeNested((String) resourceDefType)) {
211 globalContext.addMessage(filename, ErrorLevel.WARNING, ErrorMessagesFormatBuilder
212 .getErrorWithParameters(
213 globalContext.getMessageCode(),
214 Messages.INVALID_RESOURCE_GROUP_TYPE.getErrorMessage(),
215 resourceName, resourceDefType.toString()));