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.HashMap;
24 import java.util.List;
27 import org.onap.aai.schemaif.SchemaProviderException;
28 import org.onap.aai.schemaif.definitions.PropertySchema;
29 import org.onap.aai.schemaif.definitions.VertexSchema;
30 import org.onap.aai.schemaif.json.definitions.DataTypeDefinition;
31 import org.onap.aai.schemaif.json.definitions.JsonPropertySchema;
32 import org.onap.aai.schemaif.json.definitions.JsonVertexSchema;
35 public class FromJsonVertexSchema extends VertexSchema {
36 public void fromJson(JsonVertexSchema jsonVertex, List<DataTypeDefinition> dataTypes, List<JsonPropertySchema> commonProps) throws SchemaProviderException {
37 name = jsonVertex.getName();
38 properties = new HashMap<String,PropertySchema>();
39 annotations = new HashMap<String,String>();
41 // Populate property schema
42 if (jsonVertex.getProperties() != null) {
43 for (JsonPropertySchema pSchema : jsonVertex.getProperties()) {
44 FromJsonPropertySchema propSchema = new FromJsonPropertySchema();
45 propSchema.fromJson(pSchema, false, dataTypes);
46 properties.put(propSchema.getName().toLowerCase(), propSchema);
50 // Add common properties
51 if (commonProps != null) {
52 for (JsonPropertySchema pSchema : commonProps) {
53 FromJsonPropertySchema propSchema = new FromJsonPropertySchema();
54 propSchema.fromJson(pSchema, true, dataTypes);
55 properties.put(propSchema.getName().toLowerCase(), propSchema);
59 // TODO: This is a hack until the schema-service return the list of common props
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());
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(",");
80 searchableVal.append(pSchema.getName());
82 if ( (pSchema.getAnnotationValue("indexed") != null)
83 && (pSchema.getAnnotationValue("indexed").equalsIgnoreCase("true")) ) {
84 if (indexedVal.length() > 0) {
85 indexedVal.append(",");
87 indexedVal.append(pSchema.getName());
91 if (searchableVal.length() > 0) {
92 annotations.put("searchable", searchableVal.toString());
94 if (indexedVal.length() > 0) {
95 annotations.put("indexedprops", indexedVal.toString());
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");
111 private void addCommonProperty(String name, boolean req, boolean unique, String type, String indexed) throws SchemaProviderException {
112 JsonPropertySchema pSchema = new JsonPropertySchema();
113 pSchema.setName(name);
114 pSchema.setRequired(req);
115 pSchema.setUnique(unique);
116 pSchema.setDataType(type);
117 pSchema.setDescription("");
118 pSchema.setDefaultValue("");
120 Map<String,String> propAnnotations = new HashMap<String,String>();
121 propAnnotations.put("indexed", indexed);
122 propAnnotations.put("searchable", "false");
123 propAnnotations.put("source_of_truth_type", "AAI");
125 pSchema.setAnnotations(propAnnotations);
127 FromJsonPropertySchema propSchema = new FromJsonPropertySchema();
128 propSchema.fromJson(pSchema, true, null);
130 properties.put(name, propSchema);