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=========================================================
22 package org.onap.aai.schemaif.json;
24 import java.util.ArrayList;
25 import java.util.HashMap;
26 import java.util.List;
29 import org.onap.aai.schemaif.SchemaProviderException;
30 import org.onap.aai.schemaif.definitions.PropertySchema;
31 import org.onap.aai.schemaif.definitions.types.BooleanDataType;
32 import org.onap.aai.schemaif.definitions.types.ComplexDataType;
33 import org.onap.aai.schemaif.definitions.types.DataType;
34 import org.onap.aai.schemaif.definitions.types.IntDataType;
35 import org.onap.aai.schemaif.definitions.types.ListDataType;
36 import org.onap.aai.schemaif.definitions.types.LongDataType;
37 import org.onap.aai.schemaif.definitions.types.MapDataType;
38 import org.onap.aai.schemaif.definitions.types.StringDataType;
39 import org.onap.aai.schemaif.json.definitions.DataTypeDefinition;
40 import org.onap.aai.schemaif.json.definitions.JsonPropertySchema;
42 public class FromJsonPropertySchema extends PropertySchema {
44 public void fromJson(JsonPropertySchema pSchema, boolean reserved, List<DataTypeDefinition> dataTypes)
45 throws SchemaProviderException {
46 name = pSchema.getName();
47 defaultValue = pSchema.getDefaultValue() == null ? "" : pSchema.getDefaultValue();
48 required = pSchema.getRequired() == null ? false : pSchema.getRequired();
49 unique = pSchema.getUnique() == null ? false : pSchema.getUnique();
50 isReserved = reserved;
51 dataType = resolveDataType(pSchema.getDataType(), dataTypes);
53 // Populate annotations
54 annotations = new HashMap<String, String>();
55 if (pSchema.getAnnotations() != null) {
56 for (Map.Entry<String, String> entry : pSchema.getAnnotations().entrySet()) {
57 annotations.put(entry.getKey().toLowerCase(), entry.getValue());
62 private DataType resolveDataType(String typeString, List<DataTypeDefinition> dataTypes)
63 throws SchemaProviderException {
64 if (typeString.equalsIgnoreCase("string")) {
65 return new StringDataType();
68 if (typeString.equalsIgnoreCase("integer")) {
69 return new IntDataType();
72 if (typeString.equalsIgnoreCase("long")) {
73 return new LongDataType();
76 if (typeString.equalsIgnoreCase("boolean")) {
77 return new BooleanDataType();
80 if (typeString.startsWith("list:")) {
81 String segments[] = typeString.split(":");
82 DataType subType = resolveDataType(segments[1], dataTypes);
83 return new ListDataType(subType);
86 if (typeString.startsWith("map:")) {
87 String segments[] = typeString.split(":");
88 DataType subType = resolveDataType(segments[1], dataTypes);
89 return new MapDataType(subType);
92 // Must be a complex type
93 return resolveComplexDataType(typeString, dataTypes);
96 private DataType resolveComplexDataType(String typeString, List<DataTypeDefinition> dataTypes)
97 throws SchemaProviderException {
98 // It must be a custom/complex type.
99 DataTypeDefinition dType = null;
100 for (DataTypeDefinition d : dataTypes) {
101 if ((d.getName().equals(typeString))) {
108 throw new SchemaProviderException("Invalid data type: " + typeString);
111 List<PropertySchema> propList = new ArrayList<PropertySchema>();
112 for (JsonPropertySchema p : dType.getProperties()) {
113 FromJsonPropertySchema pSchema = new FromJsonPropertySchema();
114 pSchema.fromJson(p, false, dataTypes);
115 propList.add(pSchema);
118 return new ComplexDataType(propList);