Fix file formatting issues
[aai/search-data-service.git] / src / main / java / org / onap / aai / sa / searchdbabstraction / util / DocumentSchemaUtil.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
6  * Copyright © 2017-2018 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.sa.searchdbabstraction.util;
22
23 import com.fasterxml.jackson.databind.ObjectMapper;
24 import java.io.File;
25 import java.io.FileInputStream;
26 import java.io.IOException;
27 import java.util.List;
28 import java.util.concurrent.atomic.AtomicBoolean;
29 import org.apache.commons.io.IOUtils;
30 import org.onap.aai.sa.rest.DocumentFieldSchema;
31 import org.onap.aai.sa.rest.DocumentSchema;
32
33 public class DocumentSchemaUtil {
34
35     private static String dynamicCustomMapping = null;
36     private static final String DYNAMIC_CUSTOM_TEMPALTE_FILE =
37             System.getProperty("CONFIG_HOME") + File.separator + "dynamic-custom-template.json";
38
39     private DocumentSchemaUtil() { // Do not instantiate
40     }
41
42     public static String generateDocumentMappings(String documentSchema) throws IOException {
43         // Unmarshal the json content into a document schema object.
44         ObjectMapper mapper = new ObjectMapper();
45         DocumentSchema schema = mapper.readValue(documentSchema, DocumentSchema.class);
46         return generateDocumentMappings(schema);
47     }
48
49     public static String generateDocumentMappings(DocumentSchema schema) throws IOException {
50         // Adding dynamic template to add fielddata=true to dynamic fields of type "string"
51         // in order to avoid aggregation queries breaking in ESv6.1.2
52         if (dynamicCustomMapping == null) {
53             try {
54                 dynamicCustomMapping = IOUtils.toString(new FileInputStream(DYNAMIC_CUSTOM_TEMPALTE_FILE), "UTF-8")
55                         .replaceAll("\\s+", "");
56             } catch (IOException e) {
57                 throw new IOException(
58                         "Dynamic Custom template configuration went wrong! Please check for the correct template file.",
59                         e);
60             }
61         }
62
63         // Now, generate the Elastic Search mapping json and return it.
64         StringBuilder sb = new StringBuilder();
65         sb.append("{");
66         // Adding custom mapping which adds fielddata=true to dynamic fields of type "string"
67         sb.append(dynamicCustomMapping != null ? dynamicCustomMapping : "");
68         sb.append("\"properties\": {");
69
70         generateFieldMappings(schema.getFields(), sb);
71
72         sb.append("}");
73         sb.append("}");
74
75         return sb.toString();
76     }
77
78
79     private static void generateFieldMappings(List<DocumentFieldSchema> fields, StringBuilder sb) {
80
81         AtomicBoolean firstField = new AtomicBoolean(true);
82
83         for (DocumentFieldSchema field : fields) {
84
85             // If this isn't the first field in the list, prepend it with a ','
86             if (!firstField.compareAndSet(true, false)) {
87                 sb.append(", ");
88             }
89
90             // Now, append the translated field contents.
91             generateFieldMapping(field, sb);
92         }
93     }
94
95     private static void generateFieldMapping(DocumentFieldSchema fieldSchema, StringBuilder sb) {
96
97         sb.append("\"").append(fieldSchema.getName()).append("\": {");
98
99         // The field type is mandatory.
100         sb.append("\"type\": \"").append(fieldSchema.getDataType()).append("\"");
101
102         // For date type fields we may optionally supply a format specifier.
103         if (fieldSchema.getDataType().equals("date") && fieldSchema.getFormat() != null) {
104             sb.append(", \"format\": \"").append(fieldSchema.getFormat()).append("\"");
105         }
106
107         // If the index field was specified, then append it.
108         if (fieldSchema.getSearchable() != null) {
109             sb.append(", \"index\": \"").append(fieldSchema.getSearchable() ? "analyzed" : "not_analyzed").append("\"");
110         }
111
112         // If a search analyzer was specified, then append it.
113         if (fieldSchema.getSearchAnalyzer() != null) {
114             sb.append(", \"search_analyzer\": \"").append(fieldSchema.getSearchAnalyzer()).append("\"");
115         }
116
117         // If an indexing analyzer was specified, then append it.
118         if (fieldSchema.getIndexAnalyzer() != null) {
119             sb.append(", \"analyzer\": \"").append(fieldSchema.getIndexAnalyzer()).append("\"");
120         }
121
122
123         if (fieldSchema.getDataType().equals("nested")) {
124
125             sb.append(", \"properties\": {");
126             generateFieldMappings(fieldSchema.getSubFields(), sb);
127             sb.append("}");
128         }
129
130         sb.append("}");
131     }
132
133 }
134