2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright © 2019 AT&T Intellectual Property. All rights reserved.
6 * Copyright © 2019 Amdocs
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END=========================================================
21 package org.onap.aai.schemaif.json;
23 import java.util.ArrayList;
24 import java.util.HashMap;
25 import java.util.List;
28 import org.onap.aai.schemaif.SchemaProviderException;
29 import org.onap.aai.schemaif.definitions.PropertySchema;
30 import org.onap.aai.schemaif.definitions.types.BooleanDataType;
31 import org.onap.aai.schemaif.definitions.types.ComplexDataType;
32 import org.onap.aai.schemaif.definitions.types.DataType;
33 import org.onap.aai.schemaif.definitions.types.IntDataType;
34 import org.onap.aai.schemaif.definitions.types.ListDataType;
35 import org.onap.aai.schemaif.definitions.types.LongDataType;
36 import org.onap.aai.schemaif.definitions.types.MapDataType;
37 import org.onap.aai.schemaif.definitions.types.StringDataType;
38 import org.onap.aai.schemaif.json.definitions.DataTypeDefinition;
39 import org.onap.aai.schemaif.json.definitions.JsonPropertySchema;
41 public class FromJsonPropertySchema extends PropertySchema {
43 public void fromJson(JsonPropertySchema pSchema, boolean reserved, List<DataTypeDefinition> dataTypes) throws SchemaProviderException {
44 name = pSchema.getName();
45 defaultValue = pSchema.getDefaultValue() == null ? "" : pSchema.getDefaultValue();
46 required = pSchema.getRequired() == null ? false : pSchema.getRequired();
47 unique = pSchema.getUnique() == null ? false : pSchema.getUnique();
48 isReserved = reserved;
49 dataType = resolveDataType(pSchema.getDataType(), dataTypes);
51 // Populate annotations
52 annotations = new HashMap<String,String>();
53 if (pSchema.getAnnotations() != null) {
54 for (Map.Entry<String,String> entry : pSchema.getAnnotations().entrySet()) {
55 annotations.put(entry.getKey().toLowerCase(), entry.getValue());
60 private DataType resolveDataType(String typeString, List<DataTypeDefinition> dataTypes) throws SchemaProviderException {
61 if (typeString.equalsIgnoreCase("string")) {
62 return new StringDataType();
65 if (typeString.equalsIgnoreCase("integer")) {
66 return new IntDataType();
69 if (typeString.equalsIgnoreCase("long")) {
70 return new LongDataType();
73 if (typeString.equalsIgnoreCase("boolean")) {
74 return new BooleanDataType();
77 if (typeString.startsWith("list:")) {
78 String segments[] = typeString.split(":");
79 DataType subType = resolveDataType(segments[1], dataTypes);
80 return new ListDataType(subType);
83 if (typeString.startsWith("map:")) {
84 String segments[] = typeString.split(":");
85 DataType subType = resolveDataType(segments[1], dataTypes);
86 return new MapDataType(subType);
89 // Must be a complex type
90 return resolveComplexDataType(typeString, dataTypes);
93 private DataType resolveComplexDataType(String typeString, List<DataTypeDefinition> dataTypes) throws SchemaProviderException {
94 // It must be a custom/complex type.
95 DataTypeDefinition dType = null;
96 for (DataTypeDefinition d : dataTypes) {
97 if ( (d.getName().equals(typeString)) ) {
104 throw new SchemaProviderException("Invalid data type: " + typeString);
107 List<PropertySchema> propList = new ArrayList<PropertySchema>();
108 for (JsonPropertySchema p : dType.getProperties()) {
109 FromJsonPropertySchema pSchema = new FromJsonPropertySchema();
110 pSchema.fromJson(p, false, dataTypes);
111 propList.add(pSchema);
114 return new ComplexDataType(propList);