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 org.openecomp.sdc.be.model.tosca.ToscaFunctions;
26 import org.openecomp.sdc.be.model.tosca.constraints.ConstraintType;
27 import org.openecomp.sdc.be.ui.model.UIConstraint;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30 import org.yaml.snakeyaml.Yaml;
32 import java.util.ArrayList;
33 import java.util.Arrays;
34 import java.util.HashMap;
35 import java.util.List;
39 public class ConstraintConvertor {
41 private static final Logger logger = LoggerFactory.getLogger(ConstraintConvertor.class);
43 public static final String EQUAL_OPERATOR = ConstraintType.EQUAL.getTypes().get(1);
44 public static final String GREATER_THAN_OPERATOR = ConstraintType.GREATER_THAN.getTypes().get(1);
45 public static final String LESS_THAN_OPERATOR = ConstraintType.LESS_THAN.getTypes().get(1);
46 public static final String STATIC_CONSTRAINT = "static";
47 public static final String PROPERTY_CONSTRAINT = "property";
48 public static final String SERVICE_INPUT_CONSTRAINT = "service_input";
49 public static final String SELF = "SELF";
50 private static Set<String> SUPPORTED_CONSTRAINT_LIST =
51 ImmutableSet.of(EQUAL_OPERATOR, GREATER_THAN_OPERATOR, LESS_THAN_OPERATOR);
53 private static Set<String> SUPPORTED_FUNCTIONS =
54 ImmutableSet.of(ToscaFunctions.GET_INPUT.getFunctionName(), ToscaFunctions.GET_PROPERTY.getFunctionName());
57 public UIConstraint convert(String inConstraint) {
58 Yaml yamlSource = new Yaml();
59 UIConstraint uiConstraint = new UIConstraint();
60 Object content1 = yamlSource.load(inConstraint);
61 if (!(content1 instanceof Map)) {
64 Map map1 = (Map) content1;
65 Object key = map1.keySet().iterator().next();
66 uiConstraint.setServicePropertyName(key.toString());
67 Object content2 = map1.get(key);
68 if (!(content2 instanceof Map)) {
71 Map map2 = (Map) content2;
72 Object key2 = map2.keySet().iterator().next();
73 final String operator = key2.toString();
74 if (SUPPORTED_CONSTRAINT_LIST.contains(operator)) {
75 uiConstraint.setConstraintOperator(operator);
77 Object content3 = map2.get(key2);
78 if (content3 instanceof String || content3 instanceof Number || content3 instanceof Boolean) {
79 uiConstraint.setValue(content3);
80 uiConstraint.setSourceType(STATIC_CONSTRAINT);
82 } else if (content3 instanceof List) {
83 List list1 = (List) content3;
84 uiConstraint.setSourceType(STATIC_CONSTRAINT);
85 uiConstraint.setValue(list1);
87 } else if (content3 instanceof Map) {
88 return handleMap(uiConstraint, content3);
93 private UIConstraint handleMap(UIConstraint uiConstraint, Object content3) {
94 Map map3 = (Map) content3;
95 Map.Entry entry = (Map.Entry) map3.entrySet().iterator().next();
96 final String firstKey = entry.getKey().toString().trim();
97 if (!SUPPORTED_FUNCTIONS.contains(firstKey)) {
98 uiConstraint.setValue(content3);
101 if (ToscaFunctions.GET_INPUT.getFunctionName().equals(firstKey)) {
102 uiConstraint.setSourceType(SERVICE_INPUT_CONSTRAINT);
103 uiConstraint.setValue(entry.getValue());
105 } else if (ToscaFunctions.GET_PROPERTY.getFunctionName().equals(firstKey)) {
106 uiConstraint.setSourceType(PROPERTY_CONSTRAINT);
107 final List<String> value = (List<String>) entry.getValue();
108 uiConstraint.setSourceName(value.get(0));
109 uiConstraint.setValue(value.get(1));
115 public List<String> convertToList(List<UIConstraint> uiConstraints) {
116 List<String> retVal = new ArrayList<>();
117 for (UIConstraint uiConstraint : uiConstraints) {
118 String constraint = convert(uiConstraint);
119 if (constraint != null) {
120 retVal.add(constraint);
126 public String convert(UIConstraint uiConstraint) {
128 Map map1 = new HashMap();
129 Map map2 = new HashMap();
131 map1.put(uiConstraint.getServicePropertyName(), map2);
132 if (uiConstraint.getSourceType().equals(STATIC_CONSTRAINT)) {
133 Object value = uiConstraint.getValue();
134 if (value instanceof String) {
135 value = new Yaml().load(value.toString());
137 map2.put(uiConstraint.getConstraintOperator(), value);
138 } else if (uiConstraint.getSourceType().equals(PROPERTY_CONSTRAINT)) {
139 List list1 = Arrays.asList(uiConstraint.getSourceName(), uiConstraint.getValue());
140 Map map3 = ImmutableMap.of(ToscaFunctions.GET_PROPERTY.getFunctionName(), list1);
141 map2.put(uiConstraint.getConstraintOperator(), map3);
142 } else if (uiConstraint.getSourceType().equals(SERVICE_INPUT_CONSTRAINT)) {
143 Map map3 = ImmutableMap.of(ToscaFunctions.GET_INPUT.getFunctionName(), uiConstraint.getValue());
144 map2.put(uiConstraint.getConstraintOperator(), map3);
148 Yaml yamlSource = new Yaml();
149 return yamlSource.dump(map1);
150 } catch (NullPointerException ex) {
151 logger.error(ex.getMessage(), ex);