Catalog alignment
[sdc.git] / catalog-be / src / main / java / org / openecomp / sdc / be / components / scheduledtasks / AsdcComponentsCleanerTask.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.openecomp.sdc.be.components.scheduledtasks;
22
23 import org.apache.commons.lang3.concurrent.BasicThreadFactory;
24 import org.openecomp.sdc.be.config.BeEcompErrorManager;
25 import org.openecomp.sdc.be.config.CleanComponentsConfiguration;
26 import org.openecomp.sdc.be.config.Configuration;
27 import org.openecomp.sdc.be.config.ConfigurationManager;
28 import org.openecomp.sdc.be.datatypes.enums.NodeTypeEnum;
29 import org.openecomp.sdc.common.log.wrappers.Logger;
30 import org.springframework.stereotype.Component;
31
32 import javax.annotation.PostConstruct;
33 import javax.annotation.PreDestroy;
34 import java.util.ArrayList;
35 import java.util.List;
36 import java.util.concurrent.ExecutorService;
37 import java.util.concurrent.Executors;
38 import java.util.concurrent.ScheduledExecutorService;
39 import java.util.concurrent.ScheduledFuture;
40 import java.util.concurrent.TimeUnit;
41
42 @Component("asdcComponentsCleaner")
43 public class AsdcComponentsCleanerTask extends AbstractScheduleTaskRunner implements Runnable {
44
45     private static final Logger log = Logger.getLogger(AsdcComponentsCleanerTask.class);
46
47     @javax.annotation.Resource
48     private ComponentsCleanBusinessLogic componentsCleanBusinessLogic = null;
49
50     private List<NodeTypeEnum> componentsToClean;
51     private long cleaningIntervalInMinutes;
52
53     private ScheduledExecutorService scheduledService = Executors.newScheduledThreadPool(1,
54             new BasicThreadFactory.Builder().namingPattern("ComponentsCleanThread-%d").build());
55     ScheduledFuture<?> scheduledFuture = null;
56
57     @PostConstruct
58     public void init() {
59         log.info("Enter init method of AsdcComponentsCleaner");
60         Configuration configuration = ConfigurationManager.getConfigurationManager().getConfiguration();
61         CleanComponentsConfiguration cleanComponentsConfiguration = configuration.getCleanComponentsConfiguration();
62
63         if (cleanComponentsConfiguration == null) {
64             log.info("ERROR - configuration is not valid!!! missing cleanComponentsConfiguration");
65             BeEcompErrorManager.getInstance().logBeComponentCleanerSystemError("AsdcComponentsCleanerTask-init",
66                     "fecth configuration");
67             return;
68
69         }
70         componentsToClean = new ArrayList<>();
71         List<String> components = cleanComponentsConfiguration.getComponentsToClean();
72         if (components == null) {
73             log.info("no component were configured for cleaning");
74         }
75         for (String component : components) {
76             NodeTypeEnum typeEnum = NodeTypeEnum.getByNameIgnoreCase(component);
77             if (typeEnum != null)
78                 componentsToClean.add(typeEnum);
79         }
80
81         long intervalInMinutes = cleanComponentsConfiguration.getCleanIntervalInMinutes();
82
83         if (intervalInMinutes < 1) {
84             log.warn("cleaningIntervalInMinutes value should be greater than or equal to 1 minute. use default");
85             intervalInMinutes = 60;
86         }
87         cleaningIntervalInMinutes = intervalInMinutes;
88
89         startTask();
90
91         log.info("End init method of AsdcComponentsCleaner");
92     }
93
94     @PreDestroy
95     public void destroy() {
96         this.stopTask();
97         shutdownExecutor();
98     }
99
100     public void startTask() {
101
102         log.debug("start task for cleaning components");
103
104         try {
105
106             if (scheduledService != null) {
107                 log.debug("Start Cleaning components task. interval {} minutes", cleaningIntervalInMinutes);
108                 scheduledFuture = scheduledService.scheduleAtFixedRate(this, 5, cleaningIntervalInMinutes,
109                         TimeUnit.MINUTES);
110
111             }
112         } catch (Exception e) {
113             log.debug("unexpected error occured", e);
114             BeEcompErrorManager.getInstance().logBeComponentCleanerSystemError("AsdcComponentsCleanerTask-startTask",
115                     e.getMessage());
116
117         }
118     }
119
120     private void stopTask() {
121         if (scheduledFuture != null) {
122             boolean cancelTaskSuccessfully = scheduledFuture.cancel(true);
123             log.debug("Stop cleaning task. result = {}", cancelTaskSuccessfully);
124             if (!cancelTaskSuccessfully) {
125                 BeEcompErrorManager.getInstance().logBeComponentCleanerSystemError("AsdcComponentsCleanerTask-stopTask",
126                         "try to stop the polling task");
127             }
128             scheduledFuture = null;
129         }
130
131     }
132
133     @Override
134     public void run() {
135         try {
136             componentsCleanBusinessLogic.cleanComponents(componentsToClean);
137         } catch (Exception e) {
138             log.error("unexpected error occured", e);
139             BeEcompErrorManager.getInstance().logBeComponentCleanerSystemError("AsdcComponentsCleanerTask-run",
140                     e.getMessage());
141         }
142
143     }
144
145     @Override
146     public ExecutorService getExecutorService() {
147         return scheduledService;
148     }
149 }