Support TOSCA functions in Node Filters
[sdc.git] / catalog-be / src / main / java / org / openecomp / sdc / be / components / impl / NodeFilterUploadCreator.java
1 /*
2  * Copyright © 2016-2018 European Support Limited
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package org.openecomp.sdc.be.components.impl;
17
18 import java.util.ArrayList;
19 import java.util.HashMap;
20 import java.util.List;
21 import java.util.Map;
22 import java.util.stream.Collectors;
23 import org.onap.sdc.tosca.services.YamlUtil;
24 import org.openecomp.sdc.be.model.UploadNodeFilterCapabilitiesInfo;
25 import org.openecomp.sdc.be.model.UploadNodeFilterInfo;
26 import org.openecomp.sdc.be.model.UploadNodeFilterPropertyInfo;
27 import org.openecomp.sdc.be.utils.TypeUtils;
28
29 public class NodeFilterUploadCreator {
30
31     public UploadNodeFilterInfo createNodeFilterData(Object obj) {
32         if (!(obj instanceof Map)) {
33             return null;
34         }
35         Map<String, Object> nodeFilterMap = (Map<String, Object>) obj;
36         UploadNodeFilterInfo uploadNodeFilterInfo = new UploadNodeFilterInfo();
37         final String propertiesElementName = TypeUtils.ToscaTagNamesEnum.PROPERTIES.getElementName();
38         if (nodeFilterMap.containsKey(propertiesElementName)) {
39             uploadNodeFilterInfo.setProperties(createNodeFilterProperties(nodeFilterMap.get(propertiesElementName)));
40         }
41         final String capabilitiesElementName = TypeUtils.ToscaTagNamesEnum.CAPABILITIES.getElementName();
42         if (nodeFilterMap.containsKey(capabilitiesElementName)) {
43             uploadNodeFilterInfo.setCapabilities(createNodeFilterCapabilities(nodeFilterMap.get(capabilitiesElementName)));
44         }
45         final String toscaId = TypeUtils.ToscaTagNamesEnum.TOSCA_ID.getElementName();
46         if (nodeFilterMap.containsKey(toscaId)) {
47             uploadNodeFilterInfo.setTosca_id(nodeFilterMap.get(toscaId));
48         }
49         return uploadNodeFilterInfo;
50     }
51
52     private List<UploadNodeFilterPropertyInfo> createNodeFilterProperties(Object propertyNodeFilterYamlObject) {
53         if (!(propertyNodeFilterYamlObject instanceof List)) {
54             return null;
55         }
56         List<UploadNodeFilterPropertyInfo> retVal = new ArrayList<>();
57         List<Map<String, Object>> propertiesList = (List<Map<String, Object>>) propertyNodeFilterYamlObject;
58         for (Map<String, Object> map : propertiesList) {
59             final Map.Entry<String, Object> entry = map.entrySet().iterator().next();
60             final Object propertyConstraintClauses = entry.getValue();
61             if (propertyConstraintClauses instanceof Map) {
62                 final List<String> propertyFilterConstraintList = new ArrayList<>();
63                 propertyFilterConstraintList.add(valueToProperty(map));
64                 retVal.add(new UploadNodeFilterPropertyInfo(entry.getKey(), propertyFilterConstraintList));
65             } else if (propertyConstraintClauses instanceof List) {
66                 final List<String> propertyFilterConstraintList = ((List<Object>) propertyConstraintClauses).stream()
67                     .map(propertyConstraintClause -> Map.of(entry.getKey(), propertyConstraintClause))
68                     .map(this::valueToProperty)
69                     .collect(Collectors.toList());
70
71                 retVal.add(new UploadNodeFilterPropertyInfo(entry.getKey(), propertyFilterConstraintList));
72             }
73         }
74         return retVal;
75     }
76
77     private String valueToProperty(Object o) {
78         if (o instanceof Map) {
79             return new YamlUtil().objectToYaml(o);
80         }
81         return null;
82     }
83
84     private Map<String, UploadNodeFilterCapabilitiesInfo> createNodeFilterCapabilities(Object o) {
85         if (!(o instanceof List)) {
86             return null;
87         }
88         Map<String, UploadNodeFilterCapabilitiesInfo> retVal = new HashMap<>();
89         List<Map<String, Object>> capabilitiesMap = (List<Map<String, Object>>) o;
90         for (Map<String, Object> map : capabilitiesMap) {
91             final Map.Entry<String, Object> entry = map.entrySet().iterator().next();
92             UploadNodeFilterCapabilitiesInfo uploadNodeFilterCapabilitiesInfo = new UploadNodeFilterCapabilitiesInfo();
93             uploadNodeFilterCapabilitiesInfo.setName(entry.getKey());
94             uploadNodeFilterCapabilitiesInfo.setProperties(createCapabilitiesProperties(entry.getValue()));
95             retVal.put(entry.getKey(), uploadNodeFilterCapabilitiesInfo);
96         }
97         return retVal;
98     }
99
100     private List<UploadNodeFilterPropertyInfo> createCapabilitiesProperties(Object o) {
101         if (!(o instanceof Map)) {
102             return null;
103         }
104         Map<String, Object> capabilitiesPropertiesMap = (Map<String, Object>) o;
105         final String capabilitiesPropertiesElementName = TypeUtils.ToscaTagNamesEnum.PROPERTIES.getElementName();
106         if (capabilitiesPropertiesMap.containsKey(capabilitiesPropertiesElementName)) {
107             final Object propertiesObject = capabilitiesPropertiesMap.get(capabilitiesPropertiesElementName);
108             return createNodeFilterProperties(propertiesObject);
109         }
110         return null;
111     }
112 }