Initial OpenECOMP SDC commit
[sdc.git] / catalog-be / src / main / java / org / openecomp / sdc / be / tosca / PropertyConvertor.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20
21 package org.openecomp.sdc.be.tosca;
22
23 import java.io.StringReader;
24 import java.util.HashMap;
25 import java.util.List;
26 import java.util.Map;
27
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;
41
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;
46
47 import fj.data.Either;
48
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());
53
54         protected PropertyConvertor() {
55
56         }
57
58         public static synchronized PropertyConvertor getInstance() {
59                 if (instance == null) {
60                         instance = new PropertyConvertor();
61                 }
62                 return instance;
63         }
64
65         public Either<ToscaNodeType, ToscaError> convertProperties(Component component, ToscaNodeType toscaNodeType, Map<String, DataTypeDefinition> dataTypes) {
66
67                 if (component instanceof Resource) {
68                         Resource resource = (Resource) component;
69                         List<PropertyDefinition> props = resource.getProperties();
70                         if (props != null) {
71                                 Map<String, ToscaProperty> properties = new HashMap<>();
72
73                                 // take only the properties of this resource
74                                 props.stream().filter(p -> component.getUniqueId().equals(p.getParentUniqueId())).forEach(property -> {
75                                         ToscaProperty prop = convertProperty(dataTypes, property, false);
76
77                                         properties.put(property.getName(), prop);
78
79                                 });
80                                 if (!properties.isEmpty()) {
81                                         toscaNodeType.setProperties(properties);
82                                 }
83                         }
84                 }
85                 return Either.left(toscaNodeType);
86         }
87
88         public ToscaProperty convertProperty(Map<String, DataTypeDefinition> dataTypes, PropertyDefinition property, boolean isCapabiltyProperty) {
89                 ToscaProperty prop = new ToscaProperty();
90
91                 String innerType = null;
92                 SchemaDefinition schema = property.getSchema();
93                 if (schema != null && schema.getProperty() != null && schema.getProperty().getType() != null && !schema.getProperty().getType().isEmpty()) {
94                         innerType = schema.getProperty().getType();
95                         EntrySchema eschema = new EntrySchema();
96                         eschema.setType(innerType);
97                         eschema.setDescription(schema.getProperty().getDescription());
98                         prop.setEntry_schema(eschema);
99                 }
100                 log.debug("try to convert property {} from type {} with default value {}", property.getName(), property.getType(), property.getDefaultValue());
101                 prop.setDefaultp(convertToToscaObject(property.getType(), property.getDefaultValue(), innerType, dataTypes));
102                 prop.setType(property.getType());
103                 prop.setDescription(property.getDescription());
104                 if (isCapabiltyProperty) {
105                         prop.setStatus(property.getStatus());
106                         prop.setRequired(property.isRequired());
107                 }
108                 return prop;
109         }
110
111         public Object convertToToscaObject(String propertyType, String value, String innerType, Map<String, DataTypeDefinition> dataTypes) {
112                 log.debug("try to convert propertyType {} , value {}, innerType {}", propertyType, value, innerType);
113                 if (value == null) {
114                         return value;
115                 }
116
117                 ToscaMapValueConverter mapConverterInst = ToscaMapValueConverter.getInstance();
118                 ToscaValueConverter innerConverter = null;
119                 Boolean isScalar = true;
120
121                 ToscaPropertyType type = ToscaPropertyType.isValidType(propertyType);
122                 if (type == null) {
123                         log.debug("isn't prederfined type, get from all data types");
124                         DataTypeDefinition dataTypeDefinition = dataTypes.get(propertyType);
125                         if (innerType == null) {
126                                 innerType = propertyType;
127                         }
128
129                         if ((type = mapConverterInst.isScalarType(dataTypeDefinition)) != null) {
130                                 log.debug("This is scalar type. get suitable converter for type {}", type);
131                                 innerConverter = type.getValueConverter();
132                         } else {
133                                 isScalar = false;
134                         }
135                 } else {
136                         ToscaPropertyType typeIfScalar = ToscaPropertyType.getTypeIfScalar(type.getType());
137                         if (typeIfScalar == null) {
138                                 isScalar = false;
139                         }
140
141                         innerConverter = type.getValueConverter();
142                         if (ToscaPropertyType.STRING.equals(type) && value.startsWith("/")) {
143                                 return innerConverter.convertToToscaValue(value, innerType, dataTypes);
144                         }
145                 }
146                 JsonElement jsonElement = null;
147                 try {
148                         StringReader reader = new StringReader(value);
149                         JsonReader jsonReader = new JsonReader(reader);
150                         jsonReader.setLenient(true);
151
152                         jsonElement = jsonParser.parse(jsonReader);
153
154                         if (value.equals("")) {
155                                 return value;
156                         }
157
158                         if (jsonElement.isJsonPrimitive() && isScalar) {
159                                 log.debug("It's well defined type. convert it");
160                                 ToscaValueConverter converter = type.getValueConverter();
161                                 return converter.convertToToscaValue(value, innerType, dataTypes);
162                         } else {
163                                 log.debug("It's data type or inputs in primitive type. convert as map");
164                                 Object convertedValue;
165                                 if (innerConverter != null && (ToscaPropertyType.MAP.equals(type) || ToscaPropertyType.LIST.equals(type))) {
166                                         convertedValue = innerConverter.convertToToscaValue(value, innerType, dataTypes);
167                                 } else {
168                                         if (isScalar) {
169                                                 // complex json for scalar type
170                                                 convertedValue = mapConverterInst.handleComplexJsonValue(jsonElement);
171                                         } else {
172                                                 if (innerConverter != null) {
173                                                         convertedValue = innerConverter.convertToToscaValue(value, innerType, dataTypes);
174                                                 } else {
175                                                         convertedValue = mapConverterInst.convertDataTypeToToscaMap(innerType, dataTypes, innerConverter, isScalar, jsonElement);
176                                                 }
177                                         }
178                                 }
179                                 return convertedValue;
180                         }
181
182                 } catch (JsonSyntaxException e) {
183                         log.debug("convertToToscaValue failed to parse json value :", e);
184                         return null;
185                 }
186
187         }
188
189 }