Merge "[AAI] Fix doc config files"
[aai/aai-common.git] / aai-schema-abstraction / src / main / java / org / onap / aai / schemaif / json / FromJsonVertexSchema.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.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.VertexSchema;
31 import org.onap.aai.schemaif.json.definitions.DataTypeDefinition;
32 import org.onap.aai.schemaif.json.definitions.JsonPropertySchema;
33 import org.onap.aai.schemaif.json.definitions.JsonVertexSchema;
34
35 public class FromJsonVertexSchema extends VertexSchema {
36     public void fromJson(JsonVertexSchema jsonVertex, List<DataTypeDefinition> dataTypes,
37             List<JsonPropertySchema> commonProps) throws SchemaProviderException {
38         name = jsonVertex.getName();
39         properties = new HashMap<String, PropertySchema>();
40         annotations = new HashMap<String, String>();
41
42         // Populate property schema
43         if (jsonVertex.getProperties() != null) {
44             for (JsonPropertySchema pSchema : jsonVertex.getProperties()) {
45                 FromJsonPropertySchema propSchema = new FromJsonPropertySchema();
46                 propSchema.fromJson(pSchema, false, dataTypes);
47                 properties.put(propSchema.getName().toLowerCase(), propSchema);
48             }
49         }
50
51         // Add common properties
52         if (commonProps != null) {
53             for (JsonPropertySchema pSchema : commonProps) {
54                 FromJsonPropertySchema propSchema = new FromJsonPropertySchema();
55                 propSchema.fromJson(pSchema, true, dataTypes);
56                 properties.put(propSchema.getName().toLowerCase(), propSchema);
57             }
58         } else {
59             // TODO: This is a hack until the schema-service return the list of common props
60             addCommonProps();
61         }
62
63         // Populate annotation schema
64         if (jsonVertex.getAnnotations() != null) {
65             for (Map.Entry<String, String> entry : jsonVertex.getAnnotations().entrySet()) {
66                 annotations.put(entry.getKey().toLowerCase(), entry.getValue());
67             }
68         }
69
70         // The searchable and indexed annotations, need to grab these from the property annotations
71         // and store them at the vertex level as well (backwards compatibility with OXM)
72         StringBuilder searchableVal = new StringBuilder();
73         StringBuilder indexedVal = new StringBuilder();
74         for (PropertySchema pSchema : properties.values()) {
75             if ((pSchema.getAnnotationValue("searchable") != null)
76                     && (pSchema.getAnnotationValue("searchable").equalsIgnoreCase("true"))) {
77                 if (searchableVal.length() > 0) {
78                     searchableVal.append(",");
79                 }
80                 searchableVal.append(pSchema.getName());
81             }
82             if ((pSchema.getAnnotationValue("indexed") != null)
83                     && (pSchema.getAnnotationValue("indexed").equalsIgnoreCase("true"))) {
84                 if (indexedVal.length() > 0) {
85                     indexedVal.append(",");
86                 }
87                 indexedVal.append(pSchema.getName());
88             }
89         }
90
91         if (searchableVal.length() > 0) {
92             annotations.put("searchable", searchableVal.toString());
93         }
94         if (indexedVal.length() > 0) {
95             annotations.put("indexedprops", indexedVal.toString());
96         }
97     }
98
99     private void addCommonProps() throws SchemaProviderException {
100         addCommonProperty("aai-uuid", false, true, "string", "true");
101         addCommonProperty("last-mod-source-of-truth", false, false, "string", "false");
102         addCommonProperty("aai-node-type", false, false, "string", "false");
103         addCommonProperty("aai-created-ts", false, false, "string", "false");
104         addCommonProperty("aai-unique-key", false, false, "string", "false");
105         addCommonProperty("aai-last-mod-ts", false, false, "string", "false");
106         addCommonProperty("source-of-truth", false, false, "string", "false");
107         addCommonProperty("aai-uri", false, false, "string", "false");
108
109     }
110
111     private void addCommonProperty(String name, boolean req, boolean unique, String type, String indexed)
112             throws SchemaProviderException {
113         JsonPropertySchema pSchema = new JsonPropertySchema();
114         pSchema.setName(name);
115         pSchema.setRequired(req);
116         pSchema.setUnique(unique);
117         pSchema.setDataType(type);
118         pSchema.setDescription("");
119         pSchema.setDefaultValue("");
120
121         Map<String, String> propAnnotations = new HashMap<String, String>();
122         propAnnotations.put("indexed", indexed);
123         propAnnotations.put("searchable", "false");
124         propAnnotations.put("source_of_truth_type", "AAI");
125
126         pSchema.setAnnotations(propAnnotations);
127
128         FromJsonPropertySchema propSchema = new FromJsonPropertySchema();
129         propSchema.fromJson(pSchema, true, null);
130
131         properties.put(name, propSchema);
132     }
133 }