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=========================================================
21 package org.openecomp.sdc.be.datamodel.utils;
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;
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;
38 public class ConstraintConvertor {
40 private static final Logger logger = LoggerFactory.getLogger(ConstraintConvertor.class);
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);
52 private static Set<String> SUPPORTED_FUNCTIONS =
53 ImmutableSet.of(ToscaFunctions.GET_INPUT.getFunctionName(), ToscaFunctions.GET_PROPERTY.getFunctionName());
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)) {
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)) {
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);
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);
81 } else if (content3 instanceof List) {
82 List list1 = (List) content3;
83 uiConstraint.setSourceType(STATIC_CONSTRAINT);
84 uiConstraint.setValue(list1);
86 } else if (content3 instanceof Map) {
87 return handleMap(uiConstraint, content3);
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);
100 if (ToscaFunctions.GET_INPUT.getFunctionName().equals(firstKey)) {
101 uiConstraint.setSourceType(SERVICE_INPUT_CONSTRAINT);
102 uiConstraint.setValue(entry.getValue());
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));
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);
125 public String convert(UIConstraint uiConstraint) {
127 Map map1 = new HashMap();
128 Map map2 = new HashMap();
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());
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);
147 Yaml yamlSource = new Yaml();
148 return yamlSource.dump(map1);
149 } catch (NullPointerException ex) {
150 logger.error(ex.getMessage(), ex);