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