Assign image keyname and pubkey at vnf level
[ccsdk/apps.git] / sdnr / wireless-transport / code-Carbon-SR1 / apps / devicemanager / impl / src / main / java / org / opendaylight / mwtn / base / database / IndexClientBuilder.java
1 package org.opendaylight.mwtn.base.database;
2
3 import java.util.List;
4 import java.util.concurrent.Executors;
5 import java.util.concurrent.ScheduledExecutorService;
6 import java.util.concurrent.TimeUnit;
7
8 import org.json.JSONObject;
9 import org.opendaylight.mwtn.base.internalTypes.Resources;
10 import org.slf4j.Logger;
11 import org.slf4j.LoggerFactory;
12
13 /**
14  * Setup index in the database
15  * @author herbert
16  *
17  */
18 public class IndexClientBuilder implements AutoCloseable {
19
20     private static final Logger LOG = LoggerFactory.getLogger(IndexClientBuilder.class);
21
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;
28
29         private final ScheduledExecutorService scheduler;
30     private HtDatabaseClientAbstract client;
31     private HtDatabaseNode database;
32
33
34     // --- Construct and initialize
35
36     public IndexClientBuilder(String index) {
37         this.index = index;
38         this.database = null;
39         this.scheduler = Executors.newSingleThreadScheduledExecutor();
40     }
41
42     // Additional setter functions
43
44     public IndexClientBuilder setMappingSettingJsonFileName(String jsonFileName) {
45         this.mappingSettingFileName = jsonFileName;
46         return(this);
47     }
48
49     public IndexClientBuilder setModelDataDirectory(String jsonDirectory) {
50         this.modelDataDirectory = jsonDirectory;
51         return(this);
52     }
53
54     /*
55     public IndexClientBuilder setDatabase(HtDatabaseNode database) {
56         this.database = database;
57         return(this);
58     }
59     */
60
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);
64
65         client = null;
66
67         try {
68                 // Create control structure
69                 client = new HtDatabaseClientAbstract(index, esNodeserverName, esClusterName, esNodeName);
70                 setupIndex();
71         } catch (Exception e) {
72                 LOG.error("Can not start database client. Exception: {} {}", e.getMessage(), e.toString());
73         }
74
75         LOG.info("Create {} finished. DB Service {} started.", this.getClass().getSimpleName(),  client != null ? "sucessfully" : "not" );
76         return(client);
77
78     }
79
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);
84                 setupIndex();
85         return client;
86     }
87
88
89     public void stop() {
90            this.scheduler.shutdown();
91     }
92
93     @Override
94     public void close() throws Exception {
95            stop();
96     }
97
98     private void setupIndex() {
99                 if (! client.isExistsIndex()) {
100                         LOG.info("Index not existing ... create index");
101
102                         // Initialisation 1
103                         if (mappingSettingFileName != null) {
104                                 JSONObject indexconfigdata=Resources.getJSONFile(mappingSettingFileName);
105                         client.doCreateIndexWithMapping(indexconfigdata);
106                         } else
107                                 client.doCreateIndex();
108
109                         // Initialisation 2 - start asynchron initialization and let it run
110                         scheduler.schedule(fillDatabase, 0, TimeUnit.SECONDS);
111                 }
112     }
113
114     private final Runnable fillDatabase = new Runnable() {
115         @Override
116         public void run() {
117                 if (database != null) {
118                         database.setInitializedTarget();
119                 }
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);
127                                         }
128                                         LOG.debug("wrote all objects for index {}", index);
129                                 } else {
130                                         LOG.info("No initial data for index {}",index);
131                                 }
132                         } catch (Exception e) {
133                                 LOG.warn("Problem during initialization of index "+index+" {}", e);
134                         }
135                 if (database != null) {
136                         database.setInitializedReached();
137                 }
138         }
139     };
140
141     /*---------------------------------------------------------
142      * static files
143      */
144
145     public static IndexClientBuilder getBuilder(String index) {
146         return new IndexClientBuilder(index);
147     }
148
149
150 }