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.archiveservice;
20 import java.util.Date;
21 import java.util.concurrent.Executors;
22 import java.util.concurrent.Future;
23 import java.util.concurrent.ScheduledExecutorService;
24 import java.util.concurrent.TimeUnit;
25 import org.onap.ccsdk.features.sdnr.wt.devicemanager.config.HtDevicemanagerConfiguration;
26 import org.onap.ccsdk.features.sdnr.wt.devicemanager.config.IConfigChangedListener;
27 import org.onap.ccsdk.features.sdnr.wt.devicemanager.config.impl.EsConfig;
28 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonService;
29 import org.opendaylight.mdsal.singleton.common.api.ServiceGroupIdentifier;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
33 import com.google.common.util.concurrent.Futures;
34 import com.google.common.util.concurrent.ListenableFuture;
36 public class ArchiveCleanService implements AutoCloseable, IConfigChangedListener, Runnable,ClusterSingletonService {
38 private static final Logger LOG = LoggerFactory.getLogger(ArchiveCleanService.class);
39 private static final ServiceGroupIdentifier IDENT = ServiceGroupIdentifier.create("ElasticSearchArchiveCleanService");
41 private final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
42 private final ArchiveCleanProvider[] indexCleanList;
43 private final HtDevicemanagerConfiguration htconfig;
44 private final Runnable doClean;
46 private EsConfig config;
47 private Future<?> taskReference;
48 private boolean isMaster;
50 public ArchiveCleanService(HtDevicemanagerConfiguration config, ArchiveCleanProvider... indexCleanList) {
51 this.config = config.getEs();
52 this.htconfig = config;
53 this.htconfig.registerConfigChangedListener(this);
54 this.indexCleanList = indexCleanList;
56 this.taskReference = null;
61 private void reinit() {
63 if (taskReference != null) {
64 taskReference.cancel(false);
67 if (this.config.getArchiveCheckIntervalSeconds() > 0) {
68 LOG.info("DBCleanService is turned on for entries older than {} seconds",
69 this.config.getArchiveLifetimeSeconds());
70 taskReference = this.scheduler.scheduleAtFixedRate(doClean, 0,
71 this.config.getArchiveCheckIntervalSeconds(), TimeUnit.SECONDS);
73 LOG.info("DBCleanService is turned off");
77 LOG.info("service is inactive on this node. active on another node.");
81 public Date getDateForOldElements() {
82 return new Date(System.currentTimeMillis() - config.getArchiveLifetimeSeconds() * 1000);
85 public int countOldEntries() {
87 Date olderAreOutdated = getDateForOldElements();
88 for (ArchiveCleanProvider indexCleanElement : indexCleanList) {
89 if (indexCleanElement != null) {
90 indexCleanElement.getNumberOfOldObjects(olderAreOutdated);
101 Date olderElementToBeRemoved = getDateForOldElements();
102 LOG.trace("cleaning logs from entries older than {}", olderElementToBeRemoved);
104 for (ArchiveCleanProvider indexCleanElement : indexCleanList) {
105 if (indexCleanElement != null) {
106 removed += indexCleanElement.doIndexClean(olderElementToBeRemoved);
110 LOG.trace("Removed elements: {}",removed);
112 } catch (Exception e) {
113 LOG.warn("problem executing dbclean", e);
118 public void onConfigChanged() {
119 LOG.debug("config changed. reninit timer");
120 ArchiveCleanService.this.config = EsConfig.reload();
121 ArchiveCleanService.this.reinit();
125 public void close() throws Exception {
126 this.htconfig.unregisterConfigChangedListener(this);
127 this.scheduler.shutdown();
131 public String toString() {
132 return "ArchivCleanService [ArchiveCheckIntervalSeconds=" + config.getArchiveCheckIntervalSeconds()
133 + "ArchiveLifetimeSeconds=" + config.getArchiveLifetimeSeconds() + "]";
137 public ServiceGroupIdentifier getIdentifier() {
142 public void instantiateServiceInstance() {
143 LOG.info("We take Leadership");
149 public ListenableFuture<? extends Object> closeServiceInstance() {
150 LOG.info("We lost Leadership");
153 return Futures.immediateFuture(null);