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