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==========================================================================
18 package org.onap.ccsdk.features.sdnr.wt.devicemanager.archiveservice;
20 import com.google.common.util.concurrent.Futures;
21 import com.google.common.util.concurrent.ListenableFuture;
22 import java.util.Date;
23 import java.util.concurrent.Executors;
24 import java.util.concurrent.Future;
25 import java.util.concurrent.ScheduledExecutorService;
26 import java.util.concurrent.TimeUnit;
27 import org.eclipse.jdt.annotation.NonNull;
28 import org.onap.ccsdk.features.sdnr.wt.common.configuration.filechange.IConfigChangedListener;
29 import org.onap.ccsdk.features.sdnr.wt.dataprovider.model.ArchiveCleanProvider;
30 import org.onap.ccsdk.features.sdnr.wt.dataprovider.model.IEsConfig;
31 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonService;
32 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceProvider;
33 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceRegistration;
34 import org.opendaylight.mdsal.singleton.common.api.ServiceGroupIdentifier;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
38 public class ArchiveCleanService implements AutoCloseable, IConfigChangedListener, Runnable, ClusterSingletonService {
40 private static final Logger LOG = LoggerFactory.getLogger(ArchiveCleanService.class);
41 private static final ServiceGroupIdentifier IDENT =
42 ServiceGroupIdentifier.create("ElasticSearchArchiveCleanService");
44 private final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
45 private final ArchiveCleanProvider[] indexCleanList;
46 private final Runnable doClean;
48 private final IEsConfig esConfig;
49 private Future<?> taskReference;
50 private boolean isMaster;
51 private final ClusterSingletonServiceRegistration cssRegistration;
53 public ArchiveCleanService(IEsConfig config, ClusterSingletonServiceProvider clusterSingletonServiceProvider,
54 ArchiveCleanProvider... indexCleanList) {
56 this.esConfig = config;
57 this.esConfig.registerConfigChangedListener(this);
59 this.indexCleanList = indexCleanList;
61 this.taskReference = null;
65 this.cssRegistration = clusterSingletonServiceProvider.registerClusterSingletonService(this);
69 private void reinit() {
71 if (taskReference != null) {
72 taskReference.cancel(false);
75 if (this.esConfig.getArchiveCheckIntervalSeconds() > 0) {
76 LOG.info("DBCleanService is turned on for entries older than {} seconds",
77 this.esConfig.getArchiveLifetimeSeconds());
78 taskReference = this.scheduler.scheduleAtFixedRate(doClean, 0,
79 this.esConfig.getArchiveCheckIntervalSeconds(), TimeUnit.SECONDS);
81 LOG.info("DBCleanService is turned off");
84 LOG.info("service is inactive on this node. active on another node.");
88 public Date getDateForOldElements() {
89 return new Date(System.currentTimeMillis() - esConfig.getArchiveLifetimeSeconds() * 1000);
92 public int countOldEntries() {
94 Date olderAreOutdated = getDateForOldElements();
95 for (ArchiveCleanProvider indexCleanElement : indexCleanList) {
96 if (indexCleanElement != null) {
97 indexCleanElement.getNumberOfOldObjects(olderAreOutdated);
108 Date olderElementToBeRemoved = getDateForOldElements();
109 LOG.trace("cleaning logs from entries older than {}", olderElementToBeRemoved);
111 for (ArchiveCleanProvider indexCleanElement : indexCleanList) {
112 if (indexCleanElement != null) {
113 removed += indexCleanElement.doIndexClean(olderElementToBeRemoved);
117 LOG.trace("Removed elements: {}", removed);
119 } catch (Exception e) {
120 LOG.warn("problem executing dbclean", e);
125 public void onConfigChanged() {
126 LOG.info("Not yet implemented. config changed. reninit timer");
130 public void close() throws Exception {
131 this.esConfig.unregisterConfigChangedListener(this);
132 this.scheduler.shutdown();
133 if (this.cssRegistration != null)
134 this.cssRegistration.close();
138 public String toString() {
139 return "ArchivCleanService [ArchiveCheckIntervalSeconds=" + esConfig.getArchiveCheckIntervalSeconds()
140 + "ArchiveLifetimeSeconds=" + esConfig.getArchiveLifetimeSeconds() + "]";
144 public @NonNull ServiceGroupIdentifier getIdentifier() {
149 public void instantiateServiceInstance() {
150 LOG.info("We take Leadership");
151 this.isMaster = true;
156 public ListenableFuture<? extends Object> closeServiceInstance() {
157 LOG.info("We lost Leadership");
158 this.isMaster = false;
160 return Futures.immediateFuture(null);