dd8c75e561c809b8310a8da3e9d6acbcb58f2d8a
[ccsdk/apps.git] / sdnr / wireless-transport / code-Carbon-SR1 / apps / devicemanager / impl / src / main / java / org / opendaylight / mwtn / index / impl / IndexUpdateService.java
1 package org.opendaylight.mwtn.index.impl;
2
3 import java.io.File;
4 import java.io.IOException;
5 import java.util.concurrent.Executors;
6 import java.util.concurrent.ScheduledExecutorService;
7 import java.util.concurrent.ScheduledFuture;
8 import java.util.concurrent.TimeUnit;
9
10 import org.opendaylight.mwtn.base.database.HtDatabaseNode;
11 import org.opendaylight.mwtn.base.database.HtDatabaseUpdateFile;
12 import org.opendaylight.mwtn.base.database.HtDatabaseUpdateFile.EsUpdateObject;
13 import org.opendaylight.mwtn.base.database.HtDatabaseUpdateFile.FileReadCallback;
14 import org.opendaylight.mwtn.base.database.HtDatabaseWebAPIClient;
15 import org.slf4j.Logger;
16 import org.slf4j.LoggerFactory;
17
18 public class IndexUpdateService implements AutoCloseable {
19
20         private static final Logger LOG = LoggerFactory.getLogger(IndexUpdateService.class);
21
22         protected static final String FILENAME = "etc/elasticsearch_update.zip";
23
24         private final HtDatabaseWebAPIClient webClient;
25
26         private final boolean autoremove=true;
27
28         @SuppressWarnings("unused")
29     private ScheduledFuture<?> taskHandle;
30         private final ScheduledExecutorService scheduler;
31         private final HtDatabaseNode database;
32
33         private final FileReadCallback onReadUpdateFile = new FileReadCallback()
34         {
35                 @Override
36                 public void read(EsUpdateObject obj,String filename) {
37                         try {
38                                 IndexUpdateService.this.webClient.sendRequest(obj.Uri, obj.Method, obj.Body);
39                                 LOG.info("run database update file "+filename);
40                         } catch (IOException e) {
41                                 LOG.warn("problem for request "+obj.Uri);
42                         }
43                 }
44
45                 @Override
46                 public void onerror(String filename,IOException e) {
47                         LOG.warn("problem reading content file "+filename+ " :"+e.getMessage());
48                 }
49
50         };
51         private final Runnable checkForUpdateTask = new Runnable() {
52
53                 @Override
54                 public void run() {
55                         File f=new File(FILENAME);
56                         if(f.exists())
57                         {
58                                 LOG.debug("found update file "+f.getAbsolutePath());
59                                 try {
60                                         HtDatabaseUpdateFile updateFile=new HtDatabaseUpdateFile(FILENAME);
61                                         if(updateFile.readFiles(onReadUpdateFile))
62                                         {
63                                                 LOG.info("update successful");
64                                         }
65                                         updateFile.close();
66                                         if(IndexUpdateService.this.autoremove)
67                                         {
68                                                 LOG.debug("autodelete updatefile");
69                                                 f.delete();
70                                         }
71
72                                 } catch (IOException e) {
73                                         LOG.warn("problem with update file:"+e.getMessage());
74                                 }
75                         }
76                 }
77
78         };
79
80
81
82         public IndexUpdateService(HtDatabaseNode database, String esNodeserverName, String esClusterName, String esNodeName) {
83                 this.database = database;
84                 this.webClient = new HtDatabaseWebAPIClient();
85                 this.scheduler = Executors.newSingleThreadScheduledExecutor();
86         }
87         public void start()
88         {
89                 this.taskHandle = this.scheduler.scheduleAtFixedRate(checkForUpdateTask, 0, 120, TimeUnit.SECONDS);
90         }
91         public void stop()
92         {
93                 this.scheduler.shutdown();
94         }
95         @Override
96         public void close() throws Exception {
97                 stop();
98         }
99 }