ef098417272f2625f8413b57e85a356d4f51c87c
[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 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
30 import java.util.HashSet;
31 import java.util.List;
32 import java.util.Map;
33 import java.util.Objects;
34 import java.util.Set;
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   public static boolean isNestedResource(String resourceType) {
112     if(Objects.isNull(resourceType)){
113       return false;
114     }
115     return resourceType.endsWith(".yaml") || resourceType.endsWith(".yml");
116   }
117
118 }