4af48922e72d36164ced9d89e2ef70d17c5623e8
[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.index.impl;
19
20 import java.io.File;
21 import java.io.IOException;
22 import java.util.concurrent.Executors;
23 import java.util.concurrent.ScheduledExecutorService;
24 import java.util.concurrent.ScheduledFuture;
25 import java.util.concurrent.TimeUnit;
26 import org.onap.ccsdk.features.sdnr.wt.devicemanager.base.database.HtDatabaseNode;
27 import org.onap.ccsdk.features.sdnr.wt.devicemanager.base.database.HtDatabaseUpdateFile;
28 import org.onap.ccsdk.features.sdnr.wt.devicemanager.base.database.HtDatabaseWebAPIClient;
29 import org.onap.ccsdk.features.sdnr.wt.devicemanager.base.database.HtDatabaseUpdateFile.EsUpdateObject;
30 import org.onap.ccsdk.features.sdnr.wt.devicemanager.base.database.HtDatabaseUpdateFile.FileReadCallback;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33
34 public class IndexUpdateService implements AutoCloseable {
35
36     private static final Logger LOG = LoggerFactory.getLogger(IndexUpdateService.class);
37
38     private static final String FILENAME = "etc/elasticsearch_update.zip";
39
40     private final HtDatabaseWebAPIClient webClient;
41
42     private final boolean autoremove=true;
43
44     @SuppressWarnings("unused")
45     private ScheduledFuture<?> taskHandle;
46     private final ScheduledExecutorService scheduler;
47     @SuppressWarnings("unused")
48     private final HtDatabaseNode database;
49
50     private final FileReadCallback onReadUpdateFile = new FileReadCallback()
51     {
52         @Override
53         public void read(EsUpdateObject obj,String filename) {
54             try {
55                 IndexUpdateService.this.webClient.sendRequest(obj.Uri, obj.Method, obj.Body);
56                 LOG.info("run database update file {}", filename);
57             } catch (IOException e) {
58                 LOG.warn("problem for request {}", obj.Uri);
59             }
60         }
61
62         @Override
63         public void onerror(String filename,IOException e) {
64             LOG.warn("problem reading content file {} : {}", filename, e.getMessage());
65         }
66
67     };
68     private final Runnable checkForUpdateTask = () -> {
69         File f=new File(FILENAME);
70         if(f.exists())
71         {
72             LOG.debug("found update file {}", f.getAbsolutePath());
73             try {
74                 HtDatabaseUpdateFile updateFile=new HtDatabaseUpdateFile(FILENAME);
75                 if(updateFile.readFiles(onReadUpdateFile))
76                 {
77                     LOG.info("update successful");
78                 }
79                 updateFile.close();
80                 if(IndexUpdateService.this.autoremove)
81                 {
82                     boolean res = f.delete();
83                     LOG.debug("autodelete updatefile done {}", res);
84                 }
85
86             } catch (IOException e) {
87                 LOG.warn("problem with update file: {}", e.getMessage());
88             }
89         }
90     };
91
92
93
94     public IndexUpdateService(HtDatabaseNode database, String esNodeserverName, String esClusterName, String esNodeName) {
95         this.database = database;
96         this.webClient = new HtDatabaseWebAPIClient();
97         this.scheduler = Executors.newSingleThreadScheduledExecutor();
98     }
99     public void start()
100     {
101         this.taskHandle = this.scheduler.scheduleAtFixedRate(checkForUpdateTask, 0, 30, TimeUnit.SECONDS);
102     }
103     public void stop()
104     {
105         this.scheduler.shutdown();
106     }
107     @Override
108     public void close() throws Exception {
109         stop();
110     }
111 }