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