[AAI] Fix doc config files
[aai/aai-common.git] / aai-schema-abstraction / src / main / java / org / onap / aai / schemaif / json / FromJsonPropertySchema.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
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
11  *
12  *       http://www.apache.org/licenses/LICENSE-2.0
13  *
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=========================================================
20  */
21 package org.onap.aai.schemaif.json;
22
23 import java.util.ArrayList;
24 import java.util.HashMap;
25 import java.util.List;
26 import java.util.Map;
27
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;
40
41 public class FromJsonPropertySchema extends PropertySchema {
42
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);
50
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());
56             }
57         }
58     }
59     
60     private DataType resolveDataType(String typeString, List<DataTypeDefinition> dataTypes) throws SchemaProviderException {
61         if (typeString.equalsIgnoreCase("string")) { 
62             return new StringDataType();
63         }
64         
65         if (typeString.equalsIgnoreCase("integer")) {
66             return new IntDataType();
67         }
68
69         if (typeString.equalsIgnoreCase("long")) {
70           return new LongDataType();
71         }
72         
73         if (typeString.equalsIgnoreCase("boolean")) {
74             return new BooleanDataType();
75         }
76         
77         if (typeString.startsWith("list:")) {
78             String segments[] = typeString.split(":");
79             DataType subType = resolveDataType(segments[1], dataTypes);
80             return new ListDataType(subType);
81         }
82         
83         if (typeString.startsWith("map:")) {
84             String segments[] = typeString.split(":");
85             DataType subType = resolveDataType(segments[1], dataTypes);
86             return new MapDataType(subType);
87         }
88         
89         // Must be a complex type
90         return resolveComplexDataType(typeString, dataTypes);
91     }
92     
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)) ) {
98                 dType = d;
99                 break;
100             }
101         }
102         
103         if (dType == null) {
104             throw new SchemaProviderException("Invalid data type: " + typeString);
105         }
106         
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);
112         }
113         
114         return new ComplexDataType(propList);
115     }
116 }