Merge "Replace FileUtils.toByteArray() with IOUtils"
[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 com.google.gson.Gson;
24 import com.google.gson.JsonElement;
25 import com.google.gson.JsonParser;
26 import com.google.gson.stream.JsonReader;
27 import fj.data.Either;
28 import org.apache.commons.lang3.StringUtils;
29 import org.openecomp.sdc.be.datatypes.elements.SchemaDefinition;
30 import org.openecomp.sdc.be.model.Component;
31 import org.openecomp.sdc.be.model.DataTypeDefinition;
32 import org.openecomp.sdc.be.model.PropertyDefinition;
33 import org.openecomp.sdc.be.model.Resource;
34 import org.openecomp.sdc.be.model.tosca.ToscaPropertyType;
35 import org.openecomp.sdc.be.model.tosca.converters.DataTypePropertyConverter;
36 import org.openecomp.sdc.be.model.tosca.converters.ToscaMapValueConverter;
37 import org.openecomp.sdc.be.model.tosca.converters.ToscaValueConverter;
38 import org.openecomp.sdc.be.tosca.model.EntrySchema;
39 import org.openecomp.sdc.be.tosca.model.ToscaNodeType;
40 import org.openecomp.sdc.be.tosca.model.ToscaProperty;
41 import org.slf4j.Logger;
42 import org.slf4j.LoggerFactory;
43
44 import java.io.StringReader;
45 import java.util.HashMap;
46 import java.util.List;
47 import java.util.Map;
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         Gson gson = new Gson();
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 -> p.getOwnerId() == null || p.getOwnerId().equals(component.getUniqueId())).forEach(property -> {
75                                         ToscaProperty prop = convertProperty(dataTypes, property, false);
76
77                                         properties.put(property.getName(), prop);
78                                 });
79                                 if (!properties.isEmpty()) {
80                                         toscaNodeType.setProperties(properties);
81                                 }
82                         }
83                 }
84                 return Either.left(toscaNodeType);
85         }
86
87         public ToscaProperty convertProperty(Map<String, DataTypeDefinition> dataTypes, PropertyDefinition property, boolean isCapabiltyProperty) {
88                 ToscaProperty prop = new ToscaProperty();
89
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);
98                 }
99                 log.trace("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());
106                 }
107                 return prop;
108         }
109
110         public Object convertToToscaObject(String propertyType, String value, String innerType, Map<String, DataTypeDefinition> dataTypes) {
111                 log.trace("try to convert propertyType {} , value [{}], innerType {}", propertyType, value, innerType);
112                 if (StringUtils.isEmpty(value)) {
113                         value = DataTypePropertyConverter.getInstance().getDataTypePropertiesDefaultValuesRec(propertyType, dataTypes);
114                         if(StringUtils.isEmpty(value)){
115                                 return null;
116                         }
117
118                 }
119                 try {
120                         ToscaMapValueConverter mapConverterInst = ToscaMapValueConverter.getInstance();
121                         ToscaValueConverter innerConverter = null;
122                         Boolean isScalar = true;
123         
124                         ToscaPropertyType type = ToscaPropertyType.isValidType(propertyType);
125                         if (type == null) {
126                                 log.trace("isn't prederfined type, get from all data types");
127                                 DataTypeDefinition dataTypeDefinition = dataTypes.get(propertyType);
128                                 if (innerType == null) {
129                                         innerType = propertyType;
130                                 }
131         
132                                 if ((type = mapConverterInst.isScalarType(dataTypeDefinition)) != null) {
133                                         log.trace("This is scalar type. get suitable converter for type {}", type);
134                                         innerConverter = type.getValueConverter();
135                                 } else {
136                                         isScalar = false;
137                                 }
138                         } else {
139                                 ToscaPropertyType typeIfScalar = ToscaPropertyType.getTypeIfScalar(type.getType());
140                                 if (typeIfScalar == null) {
141                                         isScalar = false;
142                                 }
143         
144                                 innerConverter = type.getValueConverter();
145                                 if (ToscaPropertyType.STRING.equals(type) && value.startsWith("/")) {
146                                         return innerConverter.convertToToscaValue(value, innerType, dataTypes);
147                                 }
148                         }
149                         JsonElement jsonElement = null;
150                 
151                         StringReader reader = new StringReader(value);
152                         JsonReader jsonReader = new JsonReader(reader);
153                         jsonReader.setLenient(true);
154
155                         jsonElement = jsonParser.parse(jsonReader);
156
157                         if (value.equals("")) {
158                                 return value;
159                         }
160
161                         if (jsonElement.isJsonPrimitive() && isScalar) {
162                                 log.trace("It's well defined type. convert it");
163                                 ToscaValueConverter converter = type.getValueConverter();
164                                 return converter.convertToToscaValue(value, innerType, dataTypes);
165                         } else {
166                                 log.trace("It's data type or inputs in primitive type. convert as map");
167                                 Object convertedValue;
168                                 if (innerConverter != null && (ToscaPropertyType.MAP.equals(type) || ToscaPropertyType.LIST.equals(type))) {
169                                         convertedValue = innerConverter.convertToToscaValue(value, innerType, dataTypes);
170                                 } else {
171                                         if (isScalar) {
172                                                 // complex json for scalar type
173                                                 convertedValue = mapConverterInst.handleComplexJsonValue(jsonElement);
174                                         } else {
175                                                 if (innerConverter != null) {
176                                                         convertedValue = innerConverter.convertToToscaValue(value, innerType, dataTypes);
177                                                 } else {
178                                                         convertedValue = mapConverterInst.convertDataTypeToToscaObject(innerType, dataTypes, innerConverter, isScalar, jsonElement);
179                                                 }
180                                         }
181                                 }
182                                 return convertedValue;
183                         }
184
185                 } catch (Exception e) {
186                         log.debug("convertToToscaValue failed to parse json value :", e);
187                         return null;
188                 }
189
190         }
191
192 }