1 /*******************************************************************************
2 * ============LICENSE_START========================================================================
3 * ONAP : ccsdk feature sdnr wt
4 * =================================================================================================
5 * Copyright (C) 2019 highstreet technologies GmbH Intellectual Property. All rights reserved.
6 * =================================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
8 * in compliance with the License. You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software distributed under the License
13 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
14 * or implied. See the License for the specific language governing permissions and limitations under
16 * ============LICENSE_END==========================================================================
17 ******************************************************************************/
18 package org.onap.ccsdk.features.sdnr.wt.devicemanager.base.database;
20 import java.util.List;
21 import java.util.concurrent.Executors;
22 import java.util.concurrent.ScheduledExecutorService;
23 import java.util.concurrent.TimeUnit;
25 import org.json.JSONObject;
26 import org.onap.ccsdk.features.sdnr.wt.devicemanager.base.internalTypes.Resources;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
31 * Setup index in the database
35 public class IndexClientBuilder implements AutoCloseable {
37 private static final Logger LOG = LoggerFactory.getLogger(IndexClientBuilder.class);
39 /** Index name to be used */
40 private final String index;
41 /** Location of mapping data **/
42 private String mappingSettingFileName = null;
43 /** Location of configuration data **/
44 private String modelDataDirectory = null;
46 private final ScheduledExecutorService scheduler;
47 private HtDatabaseClientAbstract client;
48 private HtDatabaseNode databaseNode;
51 // --- Construct and initialize
53 public IndexClientBuilder(String index) {
55 this.databaseNode = null;
56 this.scheduler = Executors.newSingleThreadScheduledExecutor();
59 // Additional setter functions
61 public IndexClientBuilder setMappingSettingJsonFileName(String jsonFileName) {
62 this.mappingSettingFileName = jsonFileName;
66 public IndexClientBuilder setModelDataDirectory(String jsonDirectory) {
67 this.modelDataDirectory = jsonDirectory;
71 public HtDatabaseClientAbstract create(HtDatabaseNode database) {
72 LOG.info("Create {} start with node", this.getClass().getSimpleName() );
73 this.databaseNode = database;
74 client = new HtDatabaseClientAbstract(index, database);
81 this.scheduler.shutdown();
85 public void close() throws Exception {
89 private void setupIndex() {
90 if (! client.isExistsIndex()) {
91 LOG.info("Index not existing ... create index");
94 if (mappingSettingFileName != null) {
95 JSONObject indexconfigdata=Resources.getJSONFile(mappingSettingFileName);
96 client.doCreateIndexWithMapping(indexconfigdata);
98 client.doCreateIndex();
100 // Initialisation 2 - start asynchron initialization and let it run
101 scheduler.schedule(fillDatabase, 0, TimeUnit.SECONDS);
105 private final Runnable fillDatabase = new Runnable() {
108 if (databaseNode != null) {
109 databaseNode.setInitializedTarget();
111 try { //Prevent ending task by exception
112 if (modelDataDirectory != null) {
113 LOG.info("... write initial data for index {}",index);
114 List<JSONObject> dataList=Resources.getJSONFiles(modelDataDirectory, false);
115 LOG.debug("received number of objects: {} of index {}", dataList.size(), index);
116 for (JSONObject da: dataList) {
117 client.doWriteJSONObject(da);
119 LOG.debug("wrote all objects for index {}", index);
121 LOG.info("No initial data for index {}",index);
123 } catch (Exception e) {
124 LOG.warn("Problem during initialization of index "+index+" {}", e);
126 if (databaseNode != null) {
127 databaseNode.setInitializedReached();
132 /*---------------------------------------------------------
136 public static IndexClientBuilder getBuilder(String index) {
137 return new IndexClientBuilder(index);