Convert Sparky to Spring-Boot
[aai/sparky-be.git] / sparkybe-onap-service / src / main / java / org / onap / aai / sparky / sync / ElasticSearchSchemaFactory.java
1 /**
2  * ============LICENSE_START===================================================
3  * SPARKY (AAI UI service)
4  * ============================================================================
5  * Copyright © 2017 AT&T Intellectual Property.
6  * Copyright © 2017 Amdocs
7  * All rights reserved.
8  * ============================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  * ============LICENSE_END=====================================================
21  *
22  * ECOMP and OpenECOMP are trademarks
23  * and service marks of AT&T Intellectual Property.
24  */
25 package org.onap.aai.sparky.sync;
26
27 import java.io.IOException;
28
29 import org.onap.aai.sparky.config.SparkyResourceLoader;
30 import org.onap.aai.sparky.dal.exception.ElasticSearchOperationException;
31 import org.onap.aai.sparky.sync.config.ElasticSearchSchemaConfig;
32
33 import com.fasterxml.jackson.core.JsonProcessingException;
34 import com.fasterxml.jackson.databind.JsonNode;
35 import com.fasterxml.jackson.databind.ObjectMapper;
36 import com.fasterxml.jackson.databind.node.ObjectNode;
37
38 public class ElasticSearchSchemaFactory {
39
40   private static final String SETTINGS = "settings";
41   private static final String MAPPINGS = "mappings";
42   
43   private static ObjectMapper mapper = new ObjectMapper();
44   private SparkyResourceLoader resourceLoader;
45     
46   public String getIndexSchema(ElasticSearchSchemaConfig schemaConfig)
47       throws ElasticSearchOperationException {
48
49     JsonNode esSettingsNode = null;
50     JsonNode esMappingsNodes = null;
51
52     try {
53       
54       if (schemaConfig.getIndexSettingsFileName() != null) {
55         esSettingsNode = mapper
56             .readTree(resourceLoader.getResourceAsString(schemaConfig.getIndexSettingsFileName(),true));
57       }
58
59       if (schemaConfig.getIndexMappingsFileName() != null) {
60         esMappingsNodes = mapper
61             .readTree(resourceLoader.getResourceAsString(schemaConfig.getIndexMappingsFileName(),true));
62       }
63
64     } catch (IOException e1) {
65       
66       throw new ElasticSearchOperationException("Caught an exception building initial ES index. Error: " + e1.getMessage());
67     }
68
69     ObjectNode esConfig = null;
70
71     ObjectNode mappings =
72         (ObjectNode) mapper.createObjectNode().set(schemaConfig.getIndexDocType(), esMappingsNodes);
73
74     if (esSettingsNode == null) {
75       esConfig = (ObjectNode) mapper.createObjectNode().set(MAPPINGS, mappings);
76     } else {
77       esConfig = (ObjectNode) mapper.createObjectNode().set(SETTINGS, esSettingsNode);
78       esConfig.set(MAPPINGS, mappings);
79     }
80
81     try {
82       return mapper.writeValueAsString(esConfig);
83     } catch (JsonProcessingException exc) {
84       throw new ElasticSearchOperationException("Error getting object node as string", exc);
85     }
86
87   }
88
89   public SparkyResourceLoader getResourceLoader() {
90     return resourceLoader;
91   }
92
93   public void setResourceLoader(SparkyResourceLoader resourceLoader) {
94     this.resourceLoader = resourceLoader;
95   }
96
97 }