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.index.impl;
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;
34 public class IndexUpdateService implements AutoCloseable {
36 private static final Logger LOG = LoggerFactory.getLogger(IndexUpdateService.class);
38 private static final String FILENAME = "etc/elasticsearch_update.zip";
40 private final HtDatabaseWebAPIClient webClient;
42 private final boolean autoremove=true;
44 @SuppressWarnings("unused")
45 private ScheduledFuture<?> taskHandle;
46 private final ScheduledExecutorService scheduler;
47 @SuppressWarnings("unused")
48 private final HtDatabaseNode database;
50 private final FileReadCallback onReadUpdateFile = new FileReadCallback()
53 public void read(EsUpdateObject obj,String filename) {
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);
63 public void onerror(String filename,IOException e) {
64 LOG.warn("problem reading content file {} : {}", filename, e.getMessage());
68 private final Runnable checkForUpdateTask = () -> {
69 File f=new File(FILENAME);
72 LOG.debug("found update file {}", f.getAbsolutePath());
74 HtDatabaseUpdateFile updateFile=new HtDatabaseUpdateFile(FILENAME);
75 if(updateFile.readFiles(onReadUpdateFile))
77 LOG.info("update successful");
80 if(IndexUpdateService.this.autoremove)
82 boolean res = f.delete();
83 LOG.debug("autodelete updatefile done {}", res);
86 } catch (IOException e) {
87 LOG.warn("problem with update file: {}", e.getMessage());
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();
101 this.taskHandle = this.scheduler.scheduleAtFixedRate(checkForUpdateTask, 0, 30, TimeUnit.SECONDS);
105 this.scheduler.shutdown();
108 public void close() throws Exception {