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