2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
20 package org.openecomp.sdc.be.datamodel.utils;
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;
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.DumperOptions;
36 import org.yaml.snakeyaml.Yaml;
38 public class ConstraintConvertor {
40 public static final String EQUAL_OPERATOR = ConstraintType.EQUAL.getTypes().get(1);
41 public static final String GREATER_THAN_OPERATOR = ConstraintType.GREATER_THAN.getTypes().get(1);
42 public static final String LESS_THAN_OPERATOR = ConstraintType.LESS_THAN.getTypes().get(1);
43 public static final String GREATER_OR_EQUAL_OPERATOR = ConstraintType.GREATER_OR_EQUAL.getTypes().get(1);
44 public static final String LESS_OR_EQUAL_OPERATOR = ConstraintType.LESS_OR_EQUAL.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 final Logger logger = LoggerFactory.getLogger(ConstraintConvertor.class);
50 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);
51 private static Set<String> SUPPORTED_FUNCTIONS = ImmutableSet
52 .of(ToscaFunctions.GET_INPUT.getFunctionName(), ToscaFunctions.GET_PROPERTY.getFunctionName());
54 public UIConstraint convert(String inConstraint) {
55 return convert(inConstraint, "");
58 public UIConstraint convert(String inConstraint, String valueType) {
59 Yaml yamlSource = new Yaml();
60 UIConstraint uiConstraint = new UIConstraint();
61 Object content1 = yamlSource.load(inConstraint);
62 if (!(content1 instanceof Map)) {
65 Map map1 = (Map) content1;
66 Object key = map1.keySet().iterator().next();
67 uiConstraint.setServicePropertyName(key.toString());
68 Object content2 = map1.get(key);
69 if (!(content2 instanceof Map)) {
72 Map map2 = (Map) content2;
73 Object key2 = map2.keySet().iterator().next();
74 final String operator = key2.toString();
75 if (SUPPORTED_CONSTRAINT_LIST.contains(operator)) {
76 uiConstraint.setConstraintOperator(operator);
78 Object content3 = map2.get(key2);
79 if (content3 instanceof String || content3 instanceof Number || content3 instanceof Boolean) {
80 uiConstraint.setValue(content3);
81 uiConstraint.setSourceType(STATIC_CONSTRAINT);
82 uiConstraint.setSourceName(STATIC_CONSTRAINT);
84 } else if (content3 instanceof List) {
85 List list1 = (List) content3;
86 uiConstraint.setSourceType(STATIC_CONSTRAINT);
87 uiConstraint.setSourceName(STATIC_CONSTRAINT);
88 uiConstraint.setValue(list1);
90 } else if (valueType != null && valueType.equals("string")) {
91 uiConstraint.setSourceType(STATIC_CONSTRAINT);
92 uiConstraint.setSourceName(STATIC_CONSTRAINT);
93 DumperOptions options = new DumperOptions();
94 options.setDefaultFlowStyle(DumperOptions.FlowStyle.FLOW);
95 Yaml yaml = new Yaml(options);
96 String yamlString = yaml.dump(content3);
97 uiConstraint.setValue(yamlString);
99 } else if (content3 instanceof Map) {
100 return handleMap(uiConstraint, content3);
105 private UIConstraint handleMap(UIConstraint uiConstraint, Object content3) {
106 Map map3 = (Map) content3;
107 Map.Entry entry = (Map.Entry) map3.entrySet().iterator().next();
108 final String firstKey = entry.getKey().toString().trim();
109 if (!SUPPORTED_FUNCTIONS.contains(firstKey)) {
110 uiConstraint.setValue(content3);
113 if (ToscaFunctions.GET_INPUT.getFunctionName().equals(firstKey)) {
114 uiConstraint.setSourceType(SERVICE_INPUT_CONSTRAINT);
115 uiConstraint.setValue(entry.getValue());
117 } else if (ToscaFunctions.GET_PROPERTY.getFunctionName().equals(firstKey)) {
118 uiConstraint.setSourceType(PROPERTY_CONSTRAINT);
119 final List<String> value = (List<String>) entry.getValue();
120 uiConstraint.setSourceName(value.get(0));
121 uiConstraint.setValue(value.get(1));
127 public List<String> convertToList(List<UIConstraint> uiConstraints) {
128 List<String> retVal = new ArrayList<>();
129 for (UIConstraint uiConstraint : uiConstraints) {
130 String constraint = convert(uiConstraint);
131 if (constraint != null) {
132 retVal.add(constraint);
138 public String convert(UIConstraint uiConstraint) {
140 Map map1 = new HashMap();
141 Map map2 = new HashMap();
142 map1.put(uiConstraint.getServicePropertyName(), map2);
143 if (uiConstraint.getSourceType().equals(STATIC_CONSTRAINT)) {
144 Object value = uiConstraint.getValue();
145 if (value instanceof String) {
146 value = new Yaml().load(value.toString());
148 map2.put(uiConstraint.getConstraintOperator(), value);
149 } else if (uiConstraint.getSourceType().equals(PROPERTY_CONSTRAINT)) {
150 List list1 = Arrays.asList(uiConstraint.getSourceName(), uiConstraint.getValue());
151 Map map3 = ImmutableMap.of(ToscaFunctions.GET_PROPERTY.getFunctionName(), list1);
152 map2.put(uiConstraint.getConstraintOperator(), map3);
153 } else if (uiConstraint.getSourceType().equals(SERVICE_INPUT_CONSTRAINT)) {
154 Map map3 = ImmutableMap.of(ToscaFunctions.GET_INPUT.getFunctionName(), uiConstraint.getValue());
155 map2.put(uiConstraint.getConstraintOperator(), map3);
157 Yaml yamlSource = new Yaml();
158 return yamlSource.dump(map1);
159 } catch (NullPointerException ex) {
160 logger.error(ex.getMessage(), ex);
165 public UIConstraint getUiConstraint(final String inConstraint, final UIConstraint uiConstraint) {
166 final Object constraintObject = new Yaml().load(inConstraint);
167 if (!(constraintObject instanceof Map)) {
170 final Map constraintMap = (Map) constraintObject;
171 final Object capabilityName = constraintMap.keySet().iterator().next();
172 uiConstraint.setServicePropertyName(capabilityName.toString());
173 Object capabilityProperties = constraintMap.get(capabilityName);
174 if (!(capabilityProperties instanceof Map)) {
177 final Map capabilityPropertiesMap = (Map) capabilityProperties;
178 final Object constraintOperator = capabilityPropertiesMap.keySet().iterator().next();
179 final String operator = constraintOperator.toString();
180 if (SUPPORTED_CONSTRAINT_LIST.contains(operator)) {
181 uiConstraint.setConstraintOperator(operator);
183 final Object constraintValue = capabilityPropertiesMap.get(constraintOperator);
184 if (constraintValue instanceof String || constraintValue instanceof Number || constraintValue instanceof Boolean) {
185 uiConstraint.setValue(constraintValue);
186 uiConstraint.setSourceType(STATIC_CONSTRAINT);
187 uiConstraint.setSourceName(STATIC_CONSTRAINT);
189 } else if (constraintValue instanceof List) {
190 final List constraintValueList = (List) constraintValue;
191 uiConstraint.setSourceType(STATIC_CONSTRAINT);
192 uiConstraint.setSourceName(STATIC_CONSTRAINT);
193 uiConstraint.setValue(constraintValueList);
195 } else if (constraintValue instanceof Map) {
196 return handleMap(uiConstraint, constraintValue);