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;
29 import org.openecomp.sdc.logging.types.LoggerTragetServiceName;
31 import java.util.HashSet;
32 import java.util.List;
34 import java.util.Objects;
38 * Created by TALIO on 2/19/2017.
40 public class HeatStructureUtil {
43 * Gets referenced values by function name.
45 * @param filename the filename
46 * @param functionName the function name
47 * @param propertyValue the property value
48 * @param globalContext the global context
49 * @return the referenced values by function name
51 public static Set<String> getReferencedValuesByFunctionName(String filename, String functionName,
53 GlobalValidationContext globalContext) {
54 Set<String> valuesNames = new HashSet<>();
55 if (propertyValue instanceof Map) {
56 Map<String, Object> currPropertyMap = (Map<String, Object>) propertyValue;
57 if (currPropertyMap.containsKey(functionName)) {
58 Object getFunctionValue = currPropertyMap.get(functionName);
59 if (!(getFunctionValue instanceof String) && functionName.equals(
60 ResourceReferenceFunctions.GET_RESOURCE.getFunction())) {
61 globalContext.addMessage(filename, ErrorLevel.ERROR, ErrorMessagesFormatBuilder
62 .getErrorWithParameters(Messages.INVALID_GET_RESOURCE_SYNTAX.getErrorMessage(),
63 getFunctionValue == null ? "null" : getFunctionValue.toString()),
64 LoggerTragetServiceName.VALIDATE_GET_RESOURCE, "Invalid get_resource syntax");
67 if (getFunctionValue instanceof String) {
69 if (functionName.equals(ResourceReferenceFunctions.GET_FILE.getFunction())) {
70 getFunctionValue = ((String) getFunctionValue).replace("file:///", "");
73 valuesNames.add((String) getFunctionValue);
74 } else if (getFunctionValue instanceof List) {
75 if (CollectionUtils.isNotEmpty((List) getFunctionValue)) {
76 if (((List) getFunctionValue).get(0) instanceof String) {
77 valuesNames.add(((String) ((List) getFunctionValue).get(0)).replace("file:///", ""));
79 valuesNames.addAll(getReferencedValuesByFunctionName(filename, functionName,
80 ((List) getFunctionValue).get(0), globalContext));
86 getReferencedValuesByFunctionName(filename, functionName, getFunctionValue,
90 for (Map.Entry<String, Object> nestedPropertyMap : currPropertyMap.entrySet()) {
91 valuesNames.addAll(getReferencedValuesByFunctionName(filename, functionName,
92 nestedPropertyMap.getValue(), globalContext));
95 } else if (propertyValue instanceof List) {
96 List propertyValueArray = (List) propertyValue;
97 for (Object propValue : propertyValueArray) {
99 getReferencedValuesByFunctionName(filename, functionName, propValue,
108 public static boolean isNestedResource(String resourceType) {
109 if(Objects.isNull(resourceType)){
112 return resourceType.endsWith(".yaml") || resourceType.endsWith(".yml");