Adding interfaces in documentation
[aai/sparky-be.git] / src / main / java / org / onap / aai / sparky / sync / ElasticSearchSchemaFactory.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.sparky.sync;
22
23 import java.io.IOException;
24
25 import org.onap.aai.sparky.dal.exception.ElasticSearchOperationException;
26 import org.onap.aai.sparky.sync.config.ElasticSearchSchemaConfig;
27 import org.onap.aai.sparky.util.ConfigHelper;
28 import org.onap.aai.sparky.viewandinspect.config.SparkyConstants;
29
30 import com.fasterxml.jackson.core.JsonProcessingException;
31 import com.fasterxml.jackson.databind.JsonNode;
32 import com.fasterxml.jackson.databind.ObjectMapper;
33 import com.fasterxml.jackson.databind.node.ObjectNode;
34
35 public class ElasticSearchSchemaFactory {
36
37   private static final String SETTINGS = "settings";
38   private static final String MAPPINGS = "mappings";
39
40   private static ObjectMapper mapper = new ObjectMapper();
41
42   protected static String getConfigAsString(String configItem, String configFileName)
43       throws ElasticSearchOperationException {
44     String indexConfig = null;
45
46     try {
47       indexConfig = ConfigHelper.getFileContents(configFileName);
48     } catch (IOException exc) {
49       throw new ElasticSearchOperationException(
50           "Failed to read index " + configItem + " from file = " + configFileName + ".", exc);
51     }
52
53     if (indexConfig == null) {
54       throw new ElasticSearchOperationException(
55           "Failed to load index " + configItem + " with filename = " + configFileName + ".");
56     }
57     return indexConfig;
58   }
59
60   
61   
62   public static String getIndexSchema(ElasticSearchSchemaConfig schemaConfig)
63       throws ElasticSearchOperationException {
64
65     JsonNode esSettingsNode = null;
66     JsonNode esMappingsNodes = null;
67
68     try {
69
70       if (schemaConfig.getIndexSettingsFileName() != null) {
71         esSettingsNode = mapper.readTree(getConfigAsString(SETTINGS,
72             SparkyConstants.getConfigPath(schemaConfig.getIndexSettingsFileName())));
73       }
74
75       if (schemaConfig.getIndexMappingsFileName() != null) {
76         esMappingsNodes = mapper.readTree(getConfigAsString(MAPPINGS,
77             SparkyConstants.getConfigPath(schemaConfig.getIndexMappingsFileName())));
78       }
79
80     } catch (IOException e1) {
81       
82       throw new ElasticSearchOperationException("Caught an exception building initial ES index. Error: " + e1.getMessage());
83     }
84
85     ObjectNode esConfig = null;
86
87     ObjectNode mappings =
88         (ObjectNode) mapper.createObjectNode().set(schemaConfig.getIndexDocType(), esMappingsNodes);
89
90     if (esSettingsNode == null) {
91       esConfig = (ObjectNode) mapper.createObjectNode().set(MAPPINGS, mappings);
92     } else {
93       esConfig = (ObjectNode) mapper.createObjectNode().set(SETTINGS, esSettingsNode);
94       esConfig.set(MAPPINGS, mappings);
95     }
96
97     try {
98       return mapper.writeValueAsString(esConfig);
99     } catch (JsonProcessingException exc) {
100       throw new ElasticSearchOperationException("Error getting object node as string", exc);
101     }
102
103   }
104
105
106 }