Merge "SDN-R add updated devicemanager"
[ccsdk/features.git] / sdnr / wt / devicemanager / provider / src / main / java / org / onap / ccsdk / features / sdnr / wt / devicemanager / archiveservice / ArchiveCleanService.java
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.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.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 Runnable doClean;
47
48     private final IEsConfig esConfig;
49     private Future<?> taskReference;
50     private boolean isMaster;
51
52     public ArchiveCleanService(IEsConfig config, ArchiveCleanProvider... indexCleanList) {
53         this.esConfig = config;
54         this.esConfig.registerConfigChangedListener(this);
55
56         this.indexCleanList = indexCleanList;
57         this.doClean = this;
58         this.taskReference = null;
59
60         this.reinit();
61     }
62
63     private void reinit() {
64
65         if (taskReference != null) {
66             taskReference.cancel(false);
67         }
68         if(this.isMaster) {
69         if (this.esConfig.getArchiveCheckIntervalSeconds() > 0) {
70             LOG.info("DBCleanService is turned on for entries older than {} seconds",
71                     this.esConfig.getArchiveLifetimeSeconds());
72             taskReference = this.scheduler.scheduleAtFixedRate(doClean, 0,
73                     this.esConfig.getArchiveCheckIntervalSeconds(), TimeUnit.SECONDS);
74         } else {
75             LOG.info("DBCleanService is turned off");
76         }
77         }
78         else {
79             LOG.info("service is inactive on this node. active on another node.");
80         }
81     }
82
83     public Date getDateForOldElements() {
84         return new Date(System.currentTimeMillis() - esConfig.getArchiveLifetimeSeconds() * 1000);
85     }
86
87     public int countOldEntries() {
88         int cnt = 0;
89         Date olderAreOutdated = getDateForOldElements();
90         for (ArchiveCleanProvider indexCleanElement : indexCleanList) {
91             if (indexCleanElement != null) {
92                 indexCleanElement.getNumberOfOldObjects(olderAreOutdated);
93             }
94         }
95         return cnt;
96     }
97
98     @Override
99     public void run() {
100
101         try {
102             int removed = 0;
103             Date olderElementToBeRemoved = getDateForOldElements();
104             LOG.trace("cleaning logs from entries older than {}", olderElementToBeRemoved);
105
106             for (ArchiveCleanProvider indexCleanElement : indexCleanList) {
107                 if (indexCleanElement != null) {
108                     removed += indexCleanElement.doIndexClean(olderElementToBeRemoved);
109                 }
110             }
111             if (removed > 0) {
112                 LOG.trace("Removed elements: {}",removed);
113             }
114         } catch (Exception e) {
115             LOG.warn("problem executing dbclean", e);
116         }
117     }
118
119     @Override
120     public void onConfigChanged() {
121         LOG.info("Not yet implemented. config changed. reninit timer");
122     };
123
124     @Override
125     public void close() throws Exception {
126         this.esConfig.unregisterConfigChangedListener(this);
127         this.scheduler.shutdown();
128     }
129
130     @Override
131     public String toString() {
132         return "ArchivCleanService [ArchiveCheckIntervalSeconds=" + esConfig.getArchiveCheckIntervalSeconds()
133                 + "ArchiveLifetimeSeconds=" + esConfig.getArchiveLifetimeSeconds() + "]";
134     }
135
136     @SuppressWarnings("null")
137     @Override
138     public @NonNull ServiceGroupIdentifier getIdentifier() {
139          return IDENT;
140     }
141
142     @Override
143     public void instantiateServiceInstance() {
144         LOG.info("We take Leadership");
145         this.isMaster=true;
146         this.reinit();
147     }
148
149     @Override
150     public ListenableFuture<? extends Object> closeServiceInstance() {
151         LOG.info("We lost Leadership");
152         this.isMaster=false;
153         this.reinit();
154         return Futures.immediateFuture(null);
155     }
156 }