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.model.tosca.converters;
 
  23 import com.google.gson.*;
 
  24 import com.google.gson.stream.JsonReader;
 
  25 import org.openecomp.sdc.be.config.BeEcompErrorManager;
 
  26 import org.openecomp.sdc.be.model.DataTypeDefinition;
 
  27 import org.openecomp.sdc.be.model.PropertyDefinition;
 
  28 import org.openecomp.sdc.be.model.tosca.ToscaPropertyType;
 
  29 import org.openecomp.sdc.common.log.wrappers.Logger;
 
  31 import java.io.StringReader;
 
  32 import java.util.ArrayList;
 
  33 import java.util.HashMap;
 
  35 import java.util.Map.Entry;
 
  38 public class ToscaListValueConverter extends ToscaValueBaseConverter implements ToscaValueConverter {
 
  39     private static ToscaListValueConverter listConverter = new ToscaListValueConverter();
 
  40     private JsonParser jsonParser = new JsonParser();
 
  41     private static final Logger log = Logger.getLogger(ToscaListValueConverter.class.getName());
 
  43     public static ToscaListValueConverter getInstance() {
 
  47     private ToscaListValueConverter() {
 
  52     public Object convertToToscaValue(String value, String innerType, Map<String, DataTypeDefinition> dataTypes) {
 
  57             ToscaPropertyType innerToscaType = ToscaPropertyType.isValidType(innerType);
 
  58             ToscaValueConverter innerConverter = null;
 
  59             boolean isScalar = true;
 
  60             if (innerToscaType != null) {
 
  61                 innerConverter = innerToscaType.getValueConverter();
 
  63                 DataTypeDefinition dataTypeDefinition = dataTypes.get(innerType);
 
  65                 if (dataTypeDefinition != null) {
 
  66                     ToscaPropertyType toscaPropertyType = null;
 
  67                     if ((toscaPropertyType = isScalarType(dataTypeDefinition)) != null) {
 
  68                         innerConverter = toscaPropertyType.getValueConverter();
 
  71                         innerConverter = ToscaMapValueConverter.getInstance();
 
  74                     log.debug("inner Tosca Type is null");
 
  78             JsonElement jsonElement = null;
 
  80                 StringReader reader = new StringReader(value);
 
  81                 JsonReader jsonReader = new JsonReader(reader);
 
  82                 jsonReader.setLenient(true);
 
  84                 jsonElement = jsonParser.parse(jsonReader);
 
  85             } catch (JsonSyntaxException e) {
 
  86                 log.debug("convertToToscaValue failed to parse json value :", e);
 
  89             if (jsonElement == null || jsonElement.isJsonNull()) {
 
  90                 log.debug("convertToToscaValue json element is null");
 
  93             if (!jsonElement.isJsonArray()) {
 
  94                 // get_input all array like get_input: qrouter_names
 
  95                 return handleComplexJsonValue(jsonElement);
 
  97             JsonArray asJsonArray = jsonElement.getAsJsonArray();
 
  99             ArrayList<Object> toscaList = new ArrayList<>();
 
 100             final boolean isScalarF = isScalar;
 
 101             final ToscaValueConverter innerConverterFinal = innerConverter;
 
 102             asJsonArray.forEach(e -> {
 
 103                 Object convertedValue = null;
 
 105                     if (e.isJsonPrimitive()) {
 
 106                         String jsonAsString = e.getAsString();
 
 107                         log.debug("try to convert scalar value {}", jsonAsString);
 
 108                         convertedValue = innerConverterFinal.convertToToscaValue(jsonAsString, innerType,
 
 111                         convertedValue = handleComplexJsonValue(e);
 
 115                     JsonObject asJsonObject = e.getAsJsonObject();
 
 116                     Set<Entry<String, JsonElement>> entrySet = asJsonObject.entrySet();
 
 118                     DataTypeDefinition dataTypeDefinition = dataTypes.get(innerType);
 
 119                     Map<String, PropertyDefinition> allProperties = getAllProperties(dataTypeDefinition);
 
 120                     Map<String, Object> toscaObjectPresentation = new HashMap<>();
 
 122                     for (Entry<String, JsonElement> entry : entrySet) {
 
 123                         String propName = entry.getKey();
 
 125                         JsonElement elementValue = entry.getValue();
 
 126                         PropertyDefinition propertyDefinition = allProperties.get(propName);
 
 127                         if (propertyDefinition == null) {
 
 128                             log.debug("The property {} was not found under data type {}",propName,dataTypeDefinition.getName());
 
 132                         String type = propertyDefinition.getType();
 
 133                         ToscaPropertyType propertyType = ToscaPropertyType.isValidType(type);
 
 135                         if (propertyType != null) {
 
 136                             if (elementValue.isJsonPrimitive()) {
 
 137                                 ToscaValueConverter valueConverter = propertyType.getValueConverter();
 
 138                                 convValue = valueConverter.convertToToscaValue(elementValue.getAsString(), type,
 
 141                                 if (ToscaPropertyType.MAP.equals(type) || ToscaPropertyType.LIST.equals(propertyType)) {
 
 142                                     ToscaValueConverter valueConverter = propertyType.getValueConverter();
 
 143                                     String json = gson.toJson(elementValue);
 
 144                                     String innerTypeRecursive = propertyDefinition.getSchema().getProperty().getType();
 
 145                                     convValue = valueConverter.convertToToscaValue(json, innerTypeRecursive, dataTypes);
 
 147                                     convValue = handleComplexJsonValue(elementValue);
 
 151                             String json = gson.toJson(elementValue);
 
 152                             convValue = convertToToscaValue(json, type, dataTypes);
 
 154                         toscaObjectPresentation.put(propName, convValue);
 
 156                     convertedValue = toscaObjectPresentation;
 
 158                 toscaList.add(convertedValue);
 
 163         JsonParseException e) {
 
 164             log.debug("Failed to parse json : {}", value, e);
 
 165             BeEcompErrorManager.getInstance().logBeInvalidJsonInput("List Converter");