[SDC-29] Amdocs OnBoard 1707 initial commit.
[sdc.git] / openecomp-be / lib / openecomp-heat-lib / src / main / java / org / openecomp / sdc / heat / services / HeatStructureUtil.java
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 import org.openecomp.sdc.logging.types.LoggerTragetServiceName;
30
31 import java.util.HashSet;
32 import java.util.List;
33 import java.util.Map;
34 import java.util.Objects;
35 import java.util.Set;
36
37 /**
38  * Created by TALIO on 2/19/2017.
39  */
40 public class HeatStructureUtil {
41
42   /**
43    * Gets referenced values by function name.
44    *
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
50    */
51   public static Set<String> getReferencedValuesByFunctionName(String filename, String functionName,
52                                                               Object propertyValue,
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");
65           return valuesNames;
66         }
67         if (getFunctionValue instanceof String) {
68
69           if (functionName.equals(ResourceReferenceFunctions.GET_FILE.getFunction())) {
70             getFunctionValue = ((String) getFunctionValue).replace("file:///", "");
71           }
72
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:///", ""));
78             } else {
79               valuesNames.addAll(getReferencedValuesByFunctionName(filename, functionName,
80                   ((List) getFunctionValue).get(0), globalContext));
81             }
82
83           }
84         } else {
85           valuesNames.addAll(
86               getReferencedValuesByFunctionName(filename, functionName, getFunctionValue,
87                   globalContext));
88         }
89       } else {
90         for (Map.Entry<String, Object> nestedPropertyMap : currPropertyMap.entrySet()) {
91           valuesNames.addAll(getReferencedValuesByFunctionName(filename, functionName,
92               nestedPropertyMap.getValue(), globalContext));
93         }
94       }
95     } else if (propertyValue instanceof List) {
96       List propertyValueArray = (List) propertyValue;
97       for (Object propValue : propertyValueArray) {
98         valuesNames.addAll(
99             getReferencedValuesByFunctionName(filename, functionName, propValue,
100                 globalContext));
101       }
102     }
103
104     return valuesNames;
105   }
106
107
108   public static boolean isNestedResource(String resourceType) {
109     if(Objects.isNull(resourceType)){
110       return false;
111     }
112     return resourceType.endsWith(".yaml") || resourceType.endsWith(".yml");
113   }
114
115 }