0fcbdd77684e9d446fdffc55b6d549606ba217b0
[sdc.git] / catalog-be / src / main / java / org / openecomp / sdc / be / datamodel / utils / ConstraintConvertor.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2019 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 package org.openecomp.sdc.be.datamodel.utils;
21
22 import com.google.common.collect.ImmutableMap;
23 import com.google.common.collect.ImmutableSet;
24 import java.util.ArrayList;
25 import java.util.Arrays;
26 import java.util.HashMap;
27 import java.util.List;
28 import java.util.Map;
29 import java.util.Set;
30 import org.openecomp.sdc.be.model.tosca.constraints.ConstraintType;
31 import org.openecomp.sdc.be.ui.model.UIConstraint;
32 import org.openecomp.sdc.tosca.datatypes.ToscaFunctions;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35 import org.yaml.snakeyaml.Yaml;
36
37 public class ConstraintConvertor {
38
39     public static final String EQUAL_OPERATOR = ConstraintType.EQUAL.getTypes().get(1);
40     public static final String GREATER_THAN_OPERATOR = ConstraintType.GREATER_THAN.getTypes().get(1);
41     public static final String LESS_THAN_OPERATOR = ConstraintType.LESS_THAN.getTypes().get(1);
42     public static final String GREATER_OR_EQUAL_OPERATOR = ConstraintType.GREATER_OR_EQUAL.getTypes().get(1);
43     public static final String LESS_OR_EQUAL_OPERATOR = ConstraintType.LESS_OR_EQUAL.getTypes().get(1);
44     public static final String STATIC_CONSTRAINT = "static";
45     public static final String PROPERTY_CONSTRAINT = "property";
46     public static final String SERVICE_INPUT_CONSTRAINT = "service_input";
47     public static final String SELF = "SELF";
48     private static final Logger logger = LoggerFactory.getLogger(ConstraintConvertor.class);
49     private static Set<String> SUPPORTED_CONSTRAINT_LIST = ImmutableSet.of(EQUAL_OPERATOR, GREATER_THAN_OPERATOR, LESS_THAN_OPERATOR, GREATER_OR_EQUAL_OPERATOR, LESS_OR_EQUAL_OPERATOR);
50     private static Set<String> SUPPORTED_FUNCTIONS = ImmutableSet
51         .of(ToscaFunctions.GET_INPUT.getFunctionName(), ToscaFunctions.GET_PROPERTY.getFunctionName());
52
53     public UIConstraint convert(String inConstraint) {
54         Yaml yamlSource = new Yaml();
55         UIConstraint uiConstraint = new UIConstraint();
56         Object content1 = yamlSource.load(inConstraint);
57         if (!(content1 instanceof Map)) {
58             return null;
59         }
60         Map map1 = (Map) content1;
61         Object key = map1.keySet().iterator().next();
62         uiConstraint.setServicePropertyName(key.toString());
63         Object content2 = map1.get(key);
64         if (!(content2 instanceof Map)) {
65             return null;
66         }
67         Map map2 = (Map) content2;
68         Object key2 = map2.keySet().iterator().next();
69         final String operator = key2.toString();
70         if (SUPPORTED_CONSTRAINT_LIST.contains(operator)) {
71             uiConstraint.setConstraintOperator(operator);
72         }
73         Object content3 = map2.get(key2);
74         if (content3 instanceof String || content3 instanceof Number || content3 instanceof Boolean) {
75             uiConstraint.setValue(content3);
76             uiConstraint.setSourceType(STATIC_CONSTRAINT);
77             uiConstraint.setSourceName(STATIC_CONSTRAINT);
78             return uiConstraint;
79         } else if (content3 instanceof List) {
80             List list1 = (List) content3;
81             uiConstraint.setSourceType(STATIC_CONSTRAINT);
82             uiConstraint.setSourceName(STATIC_CONSTRAINT);
83             uiConstraint.setValue(list1);
84             return uiConstraint;
85         } else if (content3 instanceof Map) {
86             return handleMap(uiConstraint, content3);
87         }
88         return null;
89     }
90
91     private UIConstraint handleMap(UIConstraint uiConstraint, Object content3) {
92         Map map3 = (Map) content3;
93         Map.Entry entry = (Map.Entry) map3.entrySet().iterator().next();
94         final String firstKey = entry.getKey().toString().trim();
95         if (!SUPPORTED_FUNCTIONS.contains(firstKey)) {
96             uiConstraint.setValue(content3);
97             return uiConstraint;
98         }
99         if (ToscaFunctions.GET_INPUT.getFunctionName().equals(firstKey)) {
100             uiConstraint.setSourceType(SERVICE_INPUT_CONSTRAINT);
101             uiConstraint.setValue(entry.getValue());
102             return uiConstraint;
103         } else if (ToscaFunctions.GET_PROPERTY.getFunctionName().equals(firstKey)) {
104             uiConstraint.setSourceType(PROPERTY_CONSTRAINT);
105             final List<String> value = (List<String>) entry.getValue();
106             uiConstraint.setSourceName(value.get(0));
107             uiConstraint.setValue(value.get(1));
108             return uiConstraint;
109         }
110         return null;
111     }
112
113     public List<String> convertToList(List<UIConstraint> uiConstraints) {
114         List<String> retVal = new ArrayList<>();
115         for (UIConstraint uiConstraint : uiConstraints) {
116             String constraint = convert(uiConstraint);
117             if (constraint != null) {
118                 retVal.add(constraint);
119             }
120         }
121         return retVal;
122     }
123
124     public String convert(UIConstraint uiConstraint) {
125         try {
126             Map map1 = new HashMap();
127             Map map2 = new HashMap();
128             map1.put(uiConstraint.getServicePropertyName(), map2);
129             if (uiConstraint.getSourceType().equals(STATIC_CONSTRAINT)) {
130                 Object value = uiConstraint.getValue();
131                 if (value instanceof String) {
132                     value = new Yaml().load(value.toString());
133                 }
134                 map2.put(uiConstraint.getConstraintOperator(), value);
135             } else if (uiConstraint.getSourceType().equals(PROPERTY_CONSTRAINT)) {
136                 List list1 = Arrays.asList(uiConstraint.getSourceName(), uiConstraint.getValue());
137                 Map map3 = ImmutableMap.of(ToscaFunctions.GET_PROPERTY.getFunctionName(), list1);
138                 map2.put(uiConstraint.getConstraintOperator(), map3);
139             } else if (uiConstraint.getSourceType().equals(SERVICE_INPUT_CONSTRAINT)) {
140                 Map map3 = ImmutableMap.of(ToscaFunctions.GET_INPUT.getFunctionName(), uiConstraint.getValue());
141                 map2.put(uiConstraint.getConstraintOperator(), map3);
142             }
143             Yaml yamlSource = new Yaml();
144             return yamlSource.dump(map1);
145         } catch (NullPointerException ex) {
146             logger.error(ex.getMessage(), ex);
147         }
148         return null;
149     }
150
151     public UIConstraint getUiConstraint(final String inConstraint, final UIConstraint uiConstraint) {
152         final Object constraintObject = new Yaml().load(inConstraint);
153         if (!(constraintObject instanceof Map)) {
154             return null;
155         }
156         final Map constraintMap = (Map) constraintObject;
157         final Object capabilityName = constraintMap.keySet().iterator().next();
158         uiConstraint.setServicePropertyName(capabilityName.toString());
159         Object capabilityProperties = constraintMap.get(capabilityName);
160         if (!(capabilityProperties instanceof Map)) {
161             return null;
162         }
163         final Map capabilityPropertiesMap = (Map) capabilityProperties;
164         final Object constraintOperator = capabilityPropertiesMap.keySet().iterator().next();
165         final String operator = constraintOperator.toString();
166         if (SUPPORTED_CONSTRAINT_LIST.contains(operator)) {
167             uiConstraint.setConstraintOperator(operator);
168         }
169         final Object constraintValue = capabilityPropertiesMap.get(constraintOperator);
170         if (constraintValue instanceof String || constraintValue instanceof Number || constraintValue instanceof Boolean) {
171             uiConstraint.setValue(constraintValue);
172             uiConstraint.setSourceType(STATIC_CONSTRAINT);
173             uiConstraint.setSourceName(STATIC_CONSTRAINT);
174             return uiConstraint;
175         } else if (constraintValue instanceof List) {
176             final List constraintValueList = (List) constraintValue;
177             uiConstraint.setSourceType(STATIC_CONSTRAINT);
178             uiConstraint.setSourceName(STATIC_CONSTRAINT);
179             uiConstraint.setValue(constraintValueList);
180             return uiConstraint;
181         } else if (constraintValue instanceof Map) {
182             return handleMap(uiConstraint, constraintValue);
183         }
184         return null;
185     }
186 }