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;
23 import org.apache.commons.collections4.CollectionUtils;
24 import org.openecomp.core.validation.errors.ErrorMessagesFormatBuilder;
25 import org.openecomp.core.validation.types.GlobalValidationContext;
26 import org.openecomp.sdc.common.errors.Messages;
27 import org.openecomp.sdc.datatypes.error.ErrorLevel;
28 import org.openecomp.sdc.heat.datatypes.model.ResourceReferenceFunctions;
30 import java.util.HashSet;
31 import java.util.List;
33 import java.util.Objects;
37 * Created by TALIO on 2/19/2017.
39 public class HeatStructureUtil {
41 private HeatStructureUtil() {
42 // prevent instantiation
46 * Gets referenced values by function name.
48 * @param filename the filename
49 * @param functionName the function name
50 * @param propertyValue the property value
51 * @param globalContext the global context
52 * @return the referenced values by function name
54 public static Set<String> getReferencedValuesByFunctionName(String filename, String functionName,
56 GlobalValidationContext globalContext) {
57 Set<String> valuesNames = new HashSet<>();
58 if (propertyValue instanceof Map) {
59 Map<String, Object> currPropertyMap = (Map<String, Object>) propertyValue;
60 if (currPropertyMap.containsKey(functionName)) {
61 Object getFunctionValue = currPropertyMap.get(functionName);
62 if (!(getFunctionValue instanceof String) && functionName.equals(
63 ResourceReferenceFunctions.GET_RESOURCE.getFunction())) {
64 globalContext.addMessage(filename, ErrorLevel.ERROR, ErrorMessagesFormatBuilder
65 .getErrorWithParameters(globalContext.getMessageCode(),
66 Messages.INVALID_GET_RESOURCE_SYNTAX.getErrorMessage(),
67 getFunctionValue == null ? "null" : getFunctionValue.toString()));
70 if (getFunctionValue instanceof String) {
72 if (functionName.equals(ResourceReferenceFunctions.GET_FILE.getFunction())) {
73 getFunctionValue = ((String) getFunctionValue).replace("file:///", "");
76 valuesNames.add((String) getFunctionValue);
77 } else if (getFunctionValue instanceof List) {
78 if (CollectionUtils.isNotEmpty((List) getFunctionValue)) {
79 if (((List) getFunctionValue).get(0) instanceof String) {
80 valuesNames.add(((String) ((List) getFunctionValue).get(0)).replace("file:///", ""));
82 valuesNames.addAll(getReferencedValuesByFunctionName(filename, functionName,
83 ((List) getFunctionValue).get(0), globalContext));
89 getReferencedValuesByFunctionName(filename, functionName, getFunctionValue,
93 for (Map.Entry<String, Object> nestedPropertyMap : currPropertyMap.entrySet()) {
94 valuesNames.addAll(getReferencedValuesByFunctionName(filename, functionName,
95 nestedPropertyMap.getValue(), globalContext));
98 } else if (propertyValue instanceof List) {
99 List propertyValueArray = (List) propertyValue;
100 for (Object propValue : propertyValueArray) {
102 getReferencedValuesByFunctionName(filename, functionName, propValue,
111 public static boolean isNestedResource(String resourceType) {
112 if(Objects.isNull(resourceType)){
115 return resourceType.endsWith(".yaml") || resourceType.endsWith(".yml");