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.Yaml;
37 public class ConstraintConvertor {
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 STATIC_CONSTRAINT = "static";
43 public static final String PROPERTY_CONSTRAINT = "property";
44 public static final String SERVICE_INPUT_CONSTRAINT = "service_input";
45 public static final String SELF = "SELF";
46 private static final Logger logger = LoggerFactory.getLogger(ConstraintConvertor.class);
47 private static Set<String> SUPPORTED_CONSTRAINT_LIST = ImmutableSet.of(EQUAL_OPERATOR, GREATER_THAN_OPERATOR, LESS_THAN_OPERATOR);
48 private static Set<String> SUPPORTED_FUNCTIONS = ImmutableSet
49 .of(ToscaFunctions.GET_INPUT.getFunctionName(), ToscaFunctions.GET_PROPERTY.getFunctionName());
51 public UIConstraint convert(String inConstraint) {
52 Yaml yamlSource = new Yaml();
53 UIConstraint uiConstraint = new UIConstraint();
54 Object content1 = yamlSource.load(inConstraint);
55 if (!(content1 instanceof Map)) {
58 Map map1 = (Map) content1;
59 Object key = map1.keySet().iterator().next();
60 uiConstraint.setServicePropertyName(key.toString());
61 Object content2 = map1.get(key);
62 if (!(content2 instanceof Map)) {
65 Map map2 = (Map) content2;
66 Object key2 = map2.keySet().iterator().next();
67 final String operator = key2.toString();
68 if (SUPPORTED_CONSTRAINT_LIST.contains(operator)) {
69 uiConstraint.setConstraintOperator(operator);
71 Object content3 = map2.get(key2);
72 if (content3 instanceof String || content3 instanceof Number || content3 instanceof Boolean) {
73 uiConstraint.setValue(content3);
74 uiConstraint.setSourceType(STATIC_CONSTRAINT);
75 uiConstraint.setSourceName(STATIC_CONSTRAINT);
77 } else if (content3 instanceof List) {
78 List list1 = (List) content3;
79 uiConstraint.setSourceType(STATIC_CONSTRAINT);
80 uiConstraint.setSourceName(STATIC_CONSTRAINT);
81 uiConstraint.setValue(list1);
83 } else if (content3 instanceof Map) {
84 return handleMap(uiConstraint, content3);
89 private UIConstraint handleMap(UIConstraint uiConstraint, Object content3) {
90 Map map3 = (Map) content3;
91 Map.Entry entry = (Map.Entry) map3.entrySet().iterator().next();
92 final String firstKey = entry.getKey().toString().trim();
93 if (!SUPPORTED_FUNCTIONS.contains(firstKey)) {
94 uiConstraint.setValue(content3);
97 if (ToscaFunctions.GET_INPUT.getFunctionName().equals(firstKey)) {
98 uiConstraint.setSourceType(SERVICE_INPUT_CONSTRAINT);
99 uiConstraint.setValue(entry.getValue());
101 } else if (ToscaFunctions.GET_PROPERTY.getFunctionName().equals(firstKey)) {
102 uiConstraint.setSourceType(PROPERTY_CONSTRAINT);
103 final List<String> value = (List<String>) entry.getValue();
104 uiConstraint.setSourceName(value.get(0));
105 uiConstraint.setValue(value.get(1));
111 public List<String> convertToList(List<UIConstraint> uiConstraints) {
112 List<String> retVal = new ArrayList<>();
113 for (UIConstraint uiConstraint : uiConstraints) {
114 String constraint = convert(uiConstraint);
115 if (constraint != null) {
116 retVal.add(constraint);
122 public String convert(UIConstraint uiConstraint) {
124 Map map1 = new HashMap();
125 Map map2 = new HashMap();
126 map1.put(uiConstraint.getServicePropertyName(), map2);
127 if (uiConstraint.getSourceType().equals(STATIC_CONSTRAINT)) {
128 Object value = uiConstraint.getValue();
129 if (value instanceof String) {
130 value = new Yaml().load(value.toString());
132 map2.put(uiConstraint.getConstraintOperator(), value);
133 } else if (uiConstraint.getSourceType().equals(PROPERTY_CONSTRAINT)) {
134 List list1 = Arrays.asList(uiConstraint.getSourceName(), uiConstraint.getValue());
135 Map map3 = ImmutableMap.of(ToscaFunctions.GET_PROPERTY.getFunctionName(), list1);
136 map2.put(uiConstraint.getConstraintOperator(), map3);
137 } else if (uiConstraint.getSourceType().equals(SERVICE_INPUT_CONSTRAINT)) {
138 Map map3 = ImmutableMap.of(ToscaFunctions.GET_INPUT.getFunctionName(), uiConstraint.getValue());
139 map2.put(uiConstraint.getConstraintOperator(), map3);
141 Yaml yamlSource = new Yaml();
142 return yamlSource.dump(map1);
143 } catch (NullPointerException ex) {
144 logger.error(ex.getMessage(), ex);
149 public UIConstraint getUiConstraint(final String inConstraint, final UIConstraint uiConstraint) {
150 final Object constraintObject = new Yaml().load(inConstraint);
151 if (!(constraintObject instanceof Map)) {
154 final Map constraintMap = (Map) constraintObject;
155 final Object capabilityName = constraintMap.keySet().iterator().next();
156 uiConstraint.setServicePropertyName(capabilityName.toString());
157 Object capabilityProperties = constraintMap.get(capabilityName);
158 if (!(capabilityProperties instanceof Map)) {
161 final Map capabilityPropertiesMap = (Map) capabilityProperties;
162 final Object constraintOperator = capabilityPropertiesMap.keySet().iterator().next();
163 final String operator = constraintOperator.toString();
164 if (SUPPORTED_CONSTRAINT_LIST.contains(operator)) {
165 uiConstraint.setConstraintOperator(operator);
167 final Object constraintValue = capabilityPropertiesMap.get(constraintOperator);
168 if (constraintValue instanceof String || constraintValue instanceof Number || constraintValue instanceof Boolean) {
169 uiConstraint.setValue(constraintValue);
170 uiConstraint.setSourceType(STATIC_CONSTRAINT);
171 uiConstraint.setSourceName(STATIC_CONSTRAINT);
173 } else if (constraintValue instanceof List) {
174 final List constraintValueList = (List) constraintValue;
175 uiConstraint.setSourceType(STATIC_CONSTRAINT);
176 uiConstraint.setSourceName(STATIC_CONSTRAINT);
177 uiConstraint.setValue(constraintValueList);
179 } else if (constraintValue instanceof Map) {
180 return handleMap(uiConstraint, constraintValue);