Update license and poms
[aai/sparky-be.git] / sparkybe-onap-service / 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.config.SparkyResourceLoader;
26 import org.onap.aai.sparky.dal.exception.ElasticSearchOperationException;
27 import org.onap.aai.sparky.sync.config.ElasticSearchSchemaConfig;
28
29 import com.fasterxml.jackson.core.JsonProcessingException;
30 import com.fasterxml.jackson.databind.JsonNode;
31 import com.fasterxml.jackson.databind.ObjectMapper;
32 import com.fasterxml.jackson.databind.node.ObjectNode;
33
34 public class ElasticSearchSchemaFactory {
35
36   private static final String SETTINGS = "settings";
37   private static final String MAPPINGS = "mappings";
38   
39   private static ObjectMapper mapper = new ObjectMapper();
40   private SparkyResourceLoader resourceLoader;
41     
42   public String getIndexSchema(ElasticSearchSchemaConfig schemaConfig)
43       throws ElasticSearchOperationException {
44
45     JsonNode esSettingsNode = null;
46     JsonNode esMappingsNodes = null;
47
48     try {
49       
50       if (schemaConfig.getIndexSettingsFileName() != null) {
51         esSettingsNode = mapper
52             .readTree(resourceLoader.getResourceAsString(schemaConfig.getIndexSettingsFileName(),true));
53       }
54
55       if (schemaConfig.getIndexMappingsFileName() != null) {
56         esMappingsNodes = mapper
57             .readTree(resourceLoader.getResourceAsString(schemaConfig.getIndexMappingsFileName(),true));
58       }
59
60     } catch (IOException e1) {
61       
62       throw new ElasticSearchOperationException("Caught an exception building initial ES index. Error: " + e1.getMessage());
63     }
64
65     ObjectNode esConfig = null;
66
67     ObjectNode mappings =
68         (ObjectNode) mapper.createObjectNode().set(schemaConfig.getIndexDocType(), esMappingsNodes);
69
70     if (esSettingsNode == null) {
71       esConfig = (ObjectNode) mapper.createObjectNode().set(MAPPINGS, mappings);
72     } else {
73       esConfig = (ObjectNode) mapper.createObjectNode().set(SETTINGS, esSettingsNode);
74       esConfig.set(MAPPINGS, mappings);
75     }
76
77     try {
78       return mapper.writeValueAsString(esConfig);
79     } catch (JsonProcessingException exc) {
80       throw new ElasticSearchOperationException("Error getting object node as string", exc);
81     }
82
83   }
84
85   public SparkyResourceLoader getResourceLoader() {
86     return resourceLoader;
87   }
88
89   public void setResourceLoader(SparkyResourceLoader resourceLoader) {
90     this.resourceLoader = resourceLoader;
91   }
92
93 }