2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 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.tosca;
23 import java.io.StringReader;
24 import java.util.HashMap;
25 import java.util.List;
28 import org.openecomp.sdc.be.datatypes.elements.SchemaDefinition;
29 import org.openecomp.sdc.be.model.Component;
30 import org.openecomp.sdc.be.model.DataTypeDefinition;
31 import org.openecomp.sdc.be.model.PropertyDefinition;
32 import org.openecomp.sdc.be.model.Resource;
33 import org.openecomp.sdc.be.model.tosca.ToscaPropertyType;
34 import org.openecomp.sdc.be.model.tosca.converters.ToscaMapValueConverter;
35 import org.openecomp.sdc.be.model.tosca.converters.ToscaValueConverter;
36 import org.openecomp.sdc.be.tosca.model.EntrySchema;
37 import org.openecomp.sdc.be.tosca.model.ToscaNodeType;
38 import org.openecomp.sdc.be.tosca.model.ToscaProperty;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
42 import com.google.gson.JsonElement;
43 import com.google.gson.JsonParser;
44 import com.google.gson.JsonSyntaxException;
45 import com.google.gson.stream.JsonReader;
47 import fj.data.Either;
49 public class PropertyConvertor {
50 private static PropertyConvertor instance;
51 private JsonParser jsonParser = new JsonParser();
52 private static Logger log = LoggerFactory.getLogger(PropertyConvertor.class.getName());
54 protected PropertyConvertor() {
58 public static synchronized PropertyConvertor getInstance() {
59 if (instance == null) {
60 instance = new PropertyConvertor();
65 public Either<ToscaNodeType, ToscaError> convertProperties(Component component, ToscaNodeType toscaNodeType, Map<String, DataTypeDefinition> dataTypes) {
67 if (component instanceof Resource) {
68 Resource resource = (Resource) component;
69 List<PropertyDefinition> props = resource.getProperties();
71 Map<String, ToscaProperty> properties = new HashMap<>();
73 // take only the properties of this resource
74 props.stream().filter(p -> p.getOwnerId() == null || p.getOwnerId().equals(component.getUniqueId())).forEach(property -> {
75 ToscaProperty prop = convertProperty(dataTypes, property, false);
77 properties.put(property.getName(), prop);
79 if (!properties.isEmpty()) {
80 toscaNodeType.setProperties(properties);
84 return Either.left(toscaNodeType);
87 public ToscaProperty convertProperty(Map<String, DataTypeDefinition> dataTypes, PropertyDefinition property, boolean isCapabiltyProperty) {
88 ToscaProperty prop = new ToscaProperty();
90 String innerType = null;
91 SchemaDefinition schema = property.getSchema();
92 if (schema != null && schema.getProperty() != null && schema.getProperty().getType() != null && !schema.getProperty().getType().isEmpty()) {
93 innerType = schema.getProperty().getType();
94 EntrySchema eschema = new EntrySchema();
95 eschema.setType(innerType);
96 eschema.setDescription(schema.getProperty().getDescription());
97 prop.setEntry_schema(eschema);
99 log.debug("try to convert property {} from type {} with default value {}", property.getName(), property.getType(), property.getDefaultValue());
100 prop.setDefaultp(convertToToscaObject(property.getType(), property.getDefaultValue(), innerType, dataTypes));
101 prop.setType(property.getType());
102 prop.setDescription(property.getDescription());
103 if (isCapabiltyProperty) {
104 prop.setStatus(property.getStatus());
105 prop.setRequired(property.isRequired());
110 public Object convertToToscaObject(String propertyType, String value, String innerType, Map<String, DataTypeDefinition> dataTypes) {
111 log.debug("try to convert propertyType {} , value {}, innerType {}", propertyType, value, innerType);
116 ToscaMapValueConverter mapConverterInst = ToscaMapValueConverter.getInstance();
117 ToscaValueConverter innerConverter = null;
118 Boolean isScalar = true;
120 ToscaPropertyType type = ToscaPropertyType.isValidType(propertyType);
122 log.debug("isn't prederfined type, get from all data types");
123 DataTypeDefinition dataTypeDefinition = dataTypes.get(propertyType);
124 if (innerType == null) {
125 innerType = propertyType;
128 if ((type = mapConverterInst.isScalarType(dataTypeDefinition)) != null) {
129 log.debug("This is scalar type. get suitable converter for type {}", type);
130 innerConverter = type.getValueConverter();
135 ToscaPropertyType typeIfScalar = ToscaPropertyType.getTypeIfScalar(type.getType());
136 if (typeIfScalar == null) {
140 innerConverter = type.getValueConverter();
141 if (ToscaPropertyType.STRING.equals(type) && value.startsWith("/")) {
142 return innerConverter.convertToToscaValue(value, innerType, dataTypes);
145 JsonElement jsonElement = null;
147 StringReader reader = new StringReader(value);
148 JsonReader jsonReader = new JsonReader(reader);
149 jsonReader.setLenient(true);
151 jsonElement = jsonParser.parse(jsonReader);
153 if (value.equals("")) {
157 if (jsonElement.isJsonPrimitive() && isScalar) {
158 log.debug("It's well defined type. convert it");
159 ToscaValueConverter converter = type.getValueConverter();
160 return converter.convertToToscaValue(value, innerType, dataTypes);
162 log.debug("It's data type or inputs in primitive type. convert as map");
163 Object convertedValue;
164 if (innerConverter != null && (ToscaPropertyType.MAP.equals(type) || ToscaPropertyType.LIST.equals(type))) {
165 convertedValue = innerConverter.convertToToscaValue(value, innerType, dataTypes);
168 // complex json for scalar type
169 convertedValue = mapConverterInst.handleComplexJsonValue(jsonElement);
171 if (innerConverter != null) {
172 convertedValue = innerConverter.convertToToscaValue(value, innerType, dataTypes);
174 convertedValue = mapConverterInst.convertDataTypeToToscaObject(innerType, dataTypes, innerConverter, isScalar, jsonElement);
178 return convertedValue;
181 } catch (JsonSyntaxException e) {
182 log.debug("convertToToscaValue failed to parse json value :", e);