Merge "[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
22 package org.onap.aai.schemaif.json;
23
24 import java.util.ArrayList;
25 import java.util.HashMap;
26 import java.util.List;
27 import java.util.Map;
28
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;
41
42 public class FromJsonPropertySchema extends PropertySchema {
43
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);
52
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());
58             }
59         }
60     }
61
62     private DataType resolveDataType(String typeString, List<DataTypeDefinition> dataTypes)
63             throws SchemaProviderException {
64         if (typeString.equalsIgnoreCase("string")) {
65             return new StringDataType();
66         }
67
68         if (typeString.equalsIgnoreCase("integer")) {
69             return new IntDataType();
70         }
71
72         if (typeString.equalsIgnoreCase("long")) {
73             return new LongDataType();
74         }
75
76         if (typeString.equalsIgnoreCase("boolean")) {
77             return new BooleanDataType();
78         }
79
80         if (typeString.startsWith("list:")) {
81             String segments[] = typeString.split(":");
82             DataType subType = resolveDataType(segments[1], dataTypes);
83             return new ListDataType(subType);
84         }
85
86         if (typeString.startsWith("map:")) {
87             String segments[] = typeString.split(":");
88             DataType subType = resolveDataType(segments[1], dataTypes);
89             return new MapDataType(subType);
90         }
91
92         // Must be a complex type
93         return resolveComplexDataType(typeString, dataTypes);
94     }
95
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))) {
102                 dType = d;
103                 break;
104             }
105         }
106
107         if (dType == null) {
108             throw new SchemaProviderException("Invalid data type: " + typeString);
109         }
110
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);
116         }
117
118         return new ComplexDataType(propList);
119     }
120 }