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