1 package org.opendaylight.mwtn.base.database;
4 import java.util.concurrent.Executors;
5 import java.util.concurrent.ScheduledExecutorService;
6 import java.util.concurrent.TimeUnit;
8 import org.json.JSONObject;
9 import org.opendaylight.mwtn.base.internalTypes.Resources;
10 import org.slf4j.Logger;
11 import org.slf4j.LoggerFactory;
14 * Setup index in the database
18 public class IndexClientBuilder implements AutoCloseable {
20 private static final Logger LOG = LoggerFactory.getLogger(IndexClientBuilder.class);
22 /** Index name to be used */
23 private final String index;
24 /** Location of mapping data **/
25 private String mappingSettingFileName = null;
26 /** Location of configuration data **/
27 private String modelDataDirectory = null;
29 private final ScheduledExecutorService scheduler;
30 private HtDatabaseClientAbstract client;
31 private HtDatabaseNode database;
34 // --- Construct and initialize
36 public IndexClientBuilder(String index) {
39 this.scheduler = Executors.newSingleThreadScheduledExecutor();
42 // Additional setter functions
44 public IndexClientBuilder setMappingSettingJsonFileName(String jsonFileName) {
45 this.mappingSettingFileName = jsonFileName;
49 public IndexClientBuilder setModelDataDirectory(String jsonDirectory) {
50 this.modelDataDirectory = jsonDirectory;
55 public IndexClientBuilder setDatabase(HtDatabaseNode database) {
56 this.database = database;
61 public HtDatabaseClientAbstract create(String esNodeserverName, String esClusterName,String esNodeName) {
62 LOG.info("Create {} start with name parameters server/cluster/node {} {} {}",
63 this.getClass().getSimpleName(), esNodeserverName, esClusterName, esNodeName);
68 // Create control structure
69 client = new HtDatabaseClientAbstract(index, esNodeserverName, esClusterName, esNodeName);
71 } catch (Exception e) {
72 LOG.error("Can not start database client. Exception: {} {}", e.getMessage(), e.toString());
75 LOG.info("Create {} finished. DB Service {} started.", this.getClass().getSimpleName(), client != null ? "sucessfully" : "not" );
80 public HtDatabaseClientAbstract create(HtDatabaseNode database) {
81 LOG.info("Create {} start with node", this.getClass().getSimpleName() );
82 this.database = database;
83 client = new HtDatabaseClientAbstract(index, database);
90 this.scheduler.shutdown();
94 public void close() throws Exception {
98 private void setupIndex() {
99 if (! client.isExistsIndex()) {
100 LOG.info("Index not existing ... create index");
103 if (mappingSettingFileName != null) {
104 JSONObject indexconfigdata=Resources.getJSONFile(mappingSettingFileName);
105 client.doCreateIndexWithMapping(indexconfigdata);
107 client.doCreateIndex();
109 // Initialisation 2 - start asynchron initialization and let it run
110 scheduler.schedule(fillDatabase, 0, TimeUnit.SECONDS);
114 private final Runnable fillDatabase = new Runnable() {
117 if (database != null) {
118 database.setInitializedTarget();
120 try { //Prevent ending task by exception
121 if (modelDataDirectory != null) {
122 LOG.info("... write initial data for index {}",index);
123 List<JSONObject> dataList=Resources.getJSONFiles(modelDataDirectory, false);
124 LOG.debug("received number of objects: {} of index {}", dataList.size(), index);
125 for (JSONObject da: dataList) {
126 client.doWriteJSONObject(da);
128 LOG.debug("wrote all objects for index {}", index);
130 LOG.info("No initial data for index {}",index);
132 } catch (Exception e) {
133 LOG.warn("Problem during initialization of index "+index+" {}", e);
135 if (database != null) {
136 database.setInitializedReached();
141 /*---------------------------------------------------------
145 public static IndexClientBuilder getBuilder(String index) {
146 return new IndexClientBuilder(index);