b73c7e8b8b101c5350a30520be274b9a7042539b
[sdc.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20
21 package org.openecomp.sdc.heat.services;
22
23 import java.util.HashSet;
24 import java.util.List;
25 import java.util.Map;
26 import java.util.Objects;
27 import java.util.Set;
28
29 import org.apache.commons.collections4.CollectionUtils;
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.ResourceReferenceFunctions;
35
36 /**
37  * Created by TALIO on 2/19/2017.
38  */
39 public class HeatStructureUtil {
40
41     private HeatStructureUtil() {
42         // prevent instantiation
43     }
44
45     /**
46      * Gets referenced values by function name.
47      *
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
53      */
54     public static Set<String> getReferencedValuesByFunctionName(String filename, String functionName,
55                                                                 Object propertyValue,
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()));
68                     return valuesNames;
69                 }
70                 if (getFunctionValue instanceof String) {
71
72                     if (functionName.equals(ResourceReferenceFunctions.GET_FILE.getFunction())) {
73                         getFunctionValue = ((String) getFunctionValue).replace("file:///", "");
74                     }
75
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:///", ""));
81                         } else {
82                             valuesNames.addAll(getReferencedValuesByFunctionName(filename, functionName,
83                                     ((List) getFunctionValue).get(0), globalContext));
84                         }
85
86                     }
87                 } else {
88                     valuesNames.addAll(
89                             getReferencedValuesByFunctionName(filename, functionName, getFunctionValue,
90                                     globalContext));
91                 }
92             } else {
93                 for (Map.Entry<String, Object> nestedPropertyMap : currPropertyMap.entrySet()) {
94                     valuesNames.addAll(getReferencedValuesByFunctionName(filename, functionName,
95                             nestedPropertyMap.getValue(), globalContext));
96                 }
97             }
98         } else if (propertyValue instanceof List) {
99             List propertyValueArray = (List) propertyValue;
100             for (Object propValue : propertyValueArray) {
101                 valuesNames.addAll(
102                         getReferencedValuesByFunctionName(filename, functionName, propValue,
103                                 globalContext));
104             }
105         }
106
107         return valuesNames;
108     }
109
110
111     /**
112      * Is nested resource.
113      *
114      * @param resourceType the resource type
115      * @return the boolean
116      */
117     public static boolean isNestedResource(String resourceType) {
118         if (Objects.isNull(resourceType)) {
119             return false;
120         }
121         return resourceType.endsWith(".yaml") || resourceType.endsWith(".yml");
122     }
123
124 }