facc1c6f702e95c323cad62e1b67499ebf22cb88
[ccsdk/features.git] /
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
9  * 
10  * http://www.apache.org/licenses/LICENSE-2.0
11  * 
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
15  * the License.
16  * ============LICENSE_END==========================================================================
17  ******************************************************************************/
18 package org.onap.ccsdk.features.sdnr.wt.devicemanager.base.database;
19
20 import java.util.List;
21 import java.util.concurrent.Executors;
22 import java.util.concurrent.ScheduledExecutorService;
23 import java.util.concurrent.TimeUnit;
24
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;
29
30 /**
31  * Setup index in the database
32  * @author herbert
33  *
34  */
35 public class IndexClientBuilder implements AutoCloseable {
36
37     private static final Logger LOG = LoggerFactory.getLogger(IndexClientBuilder.class);
38
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;
45
46         private final ScheduledExecutorService scheduler;
47     private HtDatabaseClientAbstract client;
48     private HtDatabaseNode databaseNode;
49
50
51     // --- Construct and initialize
52
53     public IndexClientBuilder(String index) {
54         this.index = index;
55         this.databaseNode = null;
56         this.scheduler = Executors.newSingleThreadScheduledExecutor();
57     }
58
59     // Additional setter functions
60
61     public IndexClientBuilder setMappingSettingJsonFileName(String jsonFileName) {
62         this.mappingSettingFileName = jsonFileName;
63         return(this);
64     }
65
66     public IndexClientBuilder setModelDataDirectory(String jsonDirectory) {
67         this.modelDataDirectory = jsonDirectory;
68         return(this);
69     }
70
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);
75                 setupIndex();
76         return client;
77     }
78
79
80     public void stop() {
81            this.scheduler.shutdown();
82     }
83
84     @Override
85     public void close() throws Exception {
86            stop();
87     }
88
89     private void setupIndex() {
90                 if (! client.isExistsIndex()) {
91                         LOG.info("Index not existing ... create index");
92
93                         // Initialisation 1
94                         if (mappingSettingFileName != null) {
95                                 JSONObject indexconfigdata=Resources.getJSONFile(mappingSettingFileName);
96                         client.doCreateIndexWithMapping(indexconfigdata);
97                         } else
98                                 client.doCreateIndex();
99
100                         // Initialisation 2 - start asynchron initialization and let it run
101                         scheduler.schedule(fillDatabase, 0, TimeUnit.SECONDS);
102                 }
103     }
104
105     private final Runnable fillDatabase = new Runnable() {
106         @Override
107         public void run() {
108                 if (databaseNode != null) {
109                         databaseNode.setInitializedTarget();
110                 }
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);
118                                         }
119                                         LOG.debug("wrote all objects for index {}", index);
120                                 } else {
121                                         LOG.info("No initial data for index {}",index);
122                                 }
123                         } catch (Exception e) {
124                                 LOG.warn("Problem during initialization of index "+index+" {}", e);
125                         }
126                 if (databaseNode != null) {
127                         databaseNode.setInitializedReached();
128                 }
129         }
130     };
131
132     /*---------------------------------------------------------
133      * static files
134      */
135
136     public static IndexClientBuilder getBuilder(String index) {
137         return new IndexClientBuilder(index);
138     }
139
140
141 }