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.HashMap;
25 import java.util.List;
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;
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>();
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);
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);
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)
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("");
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");
126 pSchema.setAnnotations(propAnnotations);
128 FromJsonPropertySchema propSchema = new FromJsonPropertySchema();
129 propSchema.fromJson(pSchema, true, null);
131 properties.put(name, propSchema);