d84764e897b68e2491fadf7714d948dd43dcf85d
[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.archiveservice;
19
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
26 import org.eclipse.jdt.annotation.NonNull;
27 import org.onap.ccsdk.features.sdnr.wt.common.configuration.filechange.IConfigChangedListener;
28 import org.onap.ccsdk.features.sdnr.wt.dataprovider.model.ArchiveCleanProvider;
29 import org.onap.ccsdk.features.sdnr.wt.dataprovider.model.IEsConfig;
30 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonService;
31 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceProvider;
32 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceRegistration;
33 import org.opendaylight.mdsal.singleton.common.api.ServiceGroupIdentifier;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36
37 import com.google.common.util.concurrent.Futures;
38 import com.google.common.util.concurrent.ListenableFuture;
39
40 public class ArchiveCleanService implements AutoCloseable, IConfigChangedListener, Runnable, ClusterSingletonService {
41
42     private static final Logger LOG = LoggerFactory.getLogger(ArchiveCleanService.class);
43     private static final ServiceGroupIdentifier IDENT =
44             ServiceGroupIdentifier.create("ElasticSearchArchiveCleanService");
45
46     private final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
47     private final ArchiveCleanProvider[] indexCleanList;
48     private final Runnable doClean;
49
50     private final IEsConfig esConfig;
51     private Future<?> taskReference;
52     private boolean isMaster;
53     private final ClusterSingletonServiceRegistration cssRegistration;
54
55     public ArchiveCleanService(IEsConfig config, ClusterSingletonServiceProvider clusterSingletonServiceProvider,
56             ArchiveCleanProvider... indexCleanList) {
57
58         this.esConfig = config;
59         this.esConfig.registerConfigChangedListener(this);
60
61         this.indexCleanList = indexCleanList;
62         this.doClean = this;
63         this.taskReference = null;
64
65         this.reinit();
66
67         this.cssRegistration = clusterSingletonServiceProvider.registerClusterSingletonService(this);
68
69     }
70
71     private void reinit() {
72
73         if (taskReference != null) {
74             taskReference.cancel(false);
75         }
76         if (this.isMaster) {
77             if (this.esConfig.getArchiveCheckIntervalSeconds() > 0) {
78                 LOG.info("DBCleanService is turned on for entries older than {} seconds",
79                         this.esConfig.getArchiveLifetimeSeconds());
80                 taskReference = this.scheduler.scheduleAtFixedRate(doClean, 0,
81                         this.esConfig.getArchiveCheckIntervalSeconds(), TimeUnit.SECONDS);
82             } else {
83                 LOG.info("DBCleanService is turned off");
84             }
85         } else {
86             LOG.info("service is inactive on this node. active on another node.");
87         }
88     }
89
90     public Date getDateForOldElements() {
91         return new Date(System.currentTimeMillis() - esConfig.getArchiveLifetimeSeconds() * 1000);
92     }
93
94     public int countOldEntries() {
95         int cnt = 0;
96         Date olderAreOutdated = getDateForOldElements();
97         for (ArchiveCleanProvider indexCleanElement : indexCleanList) {
98             if (indexCleanElement != null) {
99                 indexCleanElement.getNumberOfOldObjects(olderAreOutdated);
100             }
101         }
102         return cnt;
103     }
104
105     @Override
106     public void run() {
107
108         try {
109             int removed = 0;
110             Date olderElementToBeRemoved = getDateForOldElements();
111             LOG.trace("cleaning logs from entries older than {}", olderElementToBeRemoved);
112
113             for (ArchiveCleanProvider indexCleanElement : indexCleanList) {
114                 if (indexCleanElement != null) {
115                     removed += indexCleanElement.doIndexClean(olderElementToBeRemoved);
116                 }
117             }
118             if (removed > 0) {
119                 LOG.trace("Removed elements: {}", removed);
120             }
121         } catch (Exception e) {
122             LOG.warn("problem executing dbclean", e);
123         }
124     }
125
126     @Override
127     public void onConfigChanged() {
128         LOG.info("Not yet implemented. config changed. reninit timer");
129     };
130
131     @Override
132     public void close() throws Exception {
133         this.esConfig.unregisterConfigChangedListener(this);
134         this.scheduler.shutdown();
135         this.cssRegistration.close();
136     }
137
138     @Override
139     public String toString() {
140         return "ArchivCleanService [ArchiveCheckIntervalSeconds=" + esConfig.getArchiveCheckIntervalSeconds()
141                 + "ArchiveLifetimeSeconds=" + esConfig.getArchiveLifetimeSeconds() + "]";
142     }
143
144     @SuppressWarnings("null")
145     @Override
146     public @NonNull ServiceGroupIdentifier getIdentifier() {
147         return IDENT;
148     }
149
150     @Override
151     public void instantiateServiceInstance() {
152         LOG.info("We take Leadership");
153         this.isMaster = true;
154         this.reinit();
155     }
156
157     @Override
158     public ListenableFuture<? extends Object> closeServiceInstance() {
159         LOG.info("We lost Leadership");
160         this.isMaster = false;
161         this.reinit();
162         return Futures.immediateFuture(null);
163     }
164 }