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.HashSet;
23 import java.util.Objects;
26 import org.apache.commons.collections4.CollectionUtils;
27 import org.apache.commons.collections4.MapUtils;
28 import org.openecomp.core.utilities.file.FileContentHandler;
29 import org.openecomp.core.validation.errors.ErrorMessagesFormatBuilder;
30 import org.openecomp.core.validation.types.GlobalValidationContext;
31 import org.openecomp.sdc.common.errors.Messages;
32 import org.openecomp.sdc.datatypes.error.ErrorLevel;
33 import org.openecomp.sdc.heat.datatypes.model.HeatOrchestrationTemplate;
34 import org.openecomp.sdc.heat.datatypes.model.HeatResourcesTypes;
35 import org.openecomp.sdc.heat.datatypes.model.PropertiesMapKeyTypes;
36 import org.openecomp.sdc.heat.datatypes.model.Resource;
37 import org.openecomp.sdc.heat.services.HeatStructureUtil;
39 public class HeatTreeManagerUtil {
41 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().forEach(
57 fileName -> heatTreeManager.addFile(fileName, fileContentMap.getFileContentAsStream(fileName)));
59 return heatTreeManager;
63 * Will verify the resource type and if type is of nested will return the nested file name
65 * @param hot Containing all translated data from heat file
66 * @return the nested files
68 public static Set<String> getNestedFiles(HeatOrchestrationTemplate hot) {
69 Set<String> nestedFileList = new HashSet<>();
70 hot.getResources().values().stream().filter(
71 resource -> resource.getType().endsWith(".yaml") || resource.getType().endsWith(".yml"))
72 .forEach(resource -> nestedFileList.add(resource.getType()));
74 Set<String> resourceDefNestedFiles = getResourceDefNestedFiles(hot);
75 nestedFileList.addAll(resourceDefNestedFiles);
76 return nestedFileList;
80 * Verify if any artifact present in file whose detail is provided and return Artifact name
82 * @param filename name of file where artifact is too be looked for
83 * @param hot translated heat data
84 * @param globalContext the global context
85 * @return the artifact files name
87 public static Set<String> getArtifactFiles(String filename, HeatOrchestrationTemplate hot,
88 GlobalValidationContext globalContext) {
89 Set<String> artifactSet = new HashSet<>();
90 Collection<Resource> resourcesValue =
91 hot.getResources() == null ? null : hot.getResources().values();
92 if (CollectionUtils.isNotEmpty(resourcesValue)) {
93 for (Resource resource : resourcesValue) {
94 Collection<Object> properties =
95 resource.getProperties() == null ? null : resource.getProperties().values();
97 artifactSet.addAll(getArtifactsFromPropertiesAndAddInArtifactSet(properties,
98 filename, globalContext));
104 private static Set<String> getArtifactsFromPropertiesAndAddInArtifactSet(Collection<Object> properties,
106 GlobalValidationContext globalContext) {
107 Set<String> artifactSet = new HashSet<>();
108 if (CollectionUtils.isNotEmpty(properties)) {
110 for (Object property : properties) {
111 Set<String> artifactNames =
112 HeatStructureUtil.getReferencedValuesByFunctionName(filename, "get_file", property,
114 artifactSet.addAll(artifactNames);
121 private static Set<String> getResourceDefNestedFiles(HeatOrchestrationTemplate hot) {
122 Set<String> resourceDefNestedFiles = new HashSet<>();
124 .entrySet().stream().filter(entry -> entry.getValue().getType()
125 .equals(HeatResourcesTypes.RESOURCE_GROUP_RESOURCE_TYPE.getHeatResource()))
127 getResourceDef(entry.getValue()) != null
128 && HeatStructureUtil.isNestedResource(
129 getResourceDef(entry.getValue())
131 .forEach(entry -> resourceDefNestedFiles.add(
132 getResourceDef(entry.getValue()).getType()));
133 return resourceDefNestedFiles;
139 * @param resource the resource
140 * @return the resource def
142 @SuppressWarnings("unchecked")
143 public static Resource getResourceDef(Resource resource) {
144 Resource resourceDef = null;
145 Map<String, Object> resourceDefValueMap = resource.getProperties() == null ? null
146 : (Map<String, Object>) resource.getProperties().get(
147 PropertiesMapKeyTypes.RESOURCE_DEF.getKeyMap());
148 if (MapUtils.isNotEmpty(resourceDefValueMap)) {
149 Object resourceDefType = resourceDefValueMap.get(TYPE);
150 if (resourceDefType instanceof String && isResourceGroupTypeNested((String) resourceDefType)) {
151 resourceDef = new Resource();
152 resourceDef.setType((String) resourceDefType);
153 //noinspection unchecked
154 resourceDef.setProperties((Map<String, Object>) resourceDefValueMap.get("properties"));
161 @SuppressWarnings("unchecked")
162 public static void checkResourceGroupTypeValid(String filename, String resourceName,
164 GlobalValidationContext globalContext) {
165 Map<String, Object> resourceDefValueMap = resource.getProperties() == null ? null
166 : (Map<String, Object>) resource.getProperties().get(
167 PropertiesMapKeyTypes.RESOURCE_DEF.getKeyMap());
168 if (MapUtils.isNotEmpty(resourceDefValueMap)) {
169 Object resourceDefType = resourceDefValueMap.get(TYPE);
170 if (Objects.nonNull(resourceDefType) && !(resourceDefType instanceof String)) {
171 globalContext.addMessage(filename, ErrorLevel.WARNING, ErrorMessagesFormatBuilder
172 .getErrorWithParameters(
173 globalContext.getMessageCode(),
174 Messages.INVALID_RESOURCE_GROUP_TYPE.getErrorMessage(),
175 resourceName, resourceDefType.toString()));
180 @SuppressWarnings("unchecked")
181 public static void checkResourceTypeValid(String filename, String resourceName,
183 GlobalValidationContext globalContext) {
184 Map<String, Object> resourceDefValueMap = resource.getProperties() == null ? null
185 : (Map<String, Object>) resource.getProperties().get(PropertiesMapKeyTypes.RESOURCE_DEF.getKeyMap());
186 if (MapUtils.isNotEmpty(resourceDefValueMap)) {
187 Object resourceDefType = resourceDefValueMap.get(TYPE);
188 if (Objects.isNull(resourceDefType)) {
189 globalContext.addMessage(filename, ErrorLevel.WARNING, ErrorMessagesFormatBuilder
190 .getErrorWithParameters(
191 globalContext.getMessageCode(), Messages.INVALID_RESOURCE_TYPE.getErrorMessage(),
192 "null", resourceName));
197 public static boolean isResourceGroupTypeNested(String resourceDefType) {
198 return HeatStructureUtil.isNestedResource(resourceDefType);
201 public static boolean checkIfResourceGroupTypeIsNested(String filename, String resourceName,
203 GlobalValidationContext globalContext) {
204 //noinspection unchecked
205 Map<String, Object> resourceDefValueMap = resource.getProperties() == null ? null
206 : (Map<String, Object>) resource.getProperties().get(PropertiesMapKeyTypes.RESOURCE_DEF.getKeyMap());
207 if (MapUtils.isNotEmpty(resourceDefValueMap)) {
208 Object resourceDefType = resourceDefValueMap.get(TYPE);
209 if (resourceDefType instanceof String && isResourceGroupTypeNested((String) resourceDefType)) {
210 globalContext.addMessage(filename, ErrorLevel.WARNING, ErrorMessagesFormatBuilder
211 .getErrorWithParameters(
212 globalContext.getMessageCode(),
213 Messages.INVALID_RESOURCE_GROUP_TYPE.getErrorMessage(),
214 resourceName, resourceDefType.toString()));