Sync Integ to Master
[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.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31 import org.springframework.stereotype.Component;
32
33 import javax.annotation.PostConstruct;
34 import javax.annotation.PreDestroy;
35 import java.util.ArrayList;
36 import java.util.List;
37 import java.util.concurrent.ExecutorService;
38 import java.util.concurrent.Executors;
39 import java.util.concurrent.ScheduledExecutorService;
40 import java.util.concurrent.ScheduledFuture;
41 import java.util.concurrent.TimeUnit;
42
43 @Component("asdcComponentsCleaner")
44 public class AsdcComponentsCleanerTask extends AbstractScheduleTaskRunner implements Runnable {
45
46     private static final Logger log = LoggerFactory.getLogger(AsdcComponentsCleanerTask.class);
47
48     @javax.annotation.Resource
49     private ComponentsCleanBusinessLogic componentsCleanBusinessLogic = null;
50
51     private List<NodeTypeEnum> componentsToClean;
52     private long cleaningIntervalInMinutes;
53
54     private ScheduledExecutorService scheduledService = Executors.newScheduledThreadPool(1,
55             new BasicThreadFactory.Builder().namingPattern("ComponentsCleanThread-%d").build());
56     ScheduledFuture<?> scheduledFuture = null;
57
58     @PostConstruct
59     public void init() {
60         log.info("Enter init method of AsdcComponentsCleaner");
61         Configuration configuration = ConfigurationManager.getConfigurationManager().getConfiguration();
62         CleanComponentsConfiguration cleanComponentsConfiguration = configuration.getCleanComponentsConfiguration();
63
64         if (cleanComponentsConfiguration == null) {
65             log.info("ERROR - configuration is not valid!!! missing cleanComponentsConfiguration");
66             BeEcompErrorManager.getInstance().logBeComponentCleanerSystemError("AsdcComponentsCleanerTask-init",
67                     "fecth configuration");
68             return;
69
70         }
71         componentsToClean = new ArrayList<NodeTypeEnum>();
72         List<String> components = cleanComponentsConfiguration.getComponentsToClean();
73         if (components == null) {
74             log.info("no component were configured for cleaning");
75         }
76         for (String component : components) {
77             NodeTypeEnum typeEnum = NodeTypeEnum.getByNameIgnoreCase(component);
78             if (typeEnum != null)
79                 componentsToClean.add(typeEnum);
80         }
81
82         long intervalInMinutes = cleanComponentsConfiguration.getCleanIntervalInMinutes();
83
84         if (intervalInMinutes < 1) {
85             log.warn("cleaningIntervalInMinutes value should be greater than or equal to 1 minute. use default");
86             intervalInMinutes = 60;
87         }
88         cleaningIntervalInMinutes = intervalInMinutes;
89
90         startTask();
91
92         log.info("End init method of AsdcComponentsCleaner");
93     }
94
95     @PreDestroy
96     public void destroy() {
97         this.stopTask();
98         shutdownExecutor();
99     }
100
101     public void startTask() {
102
103         log.debug("start task for cleaning components");
104
105         try {
106
107             if (scheduledService != null) {
108                 log.debug("Start Cleaning components task. interval {} minutes", cleaningIntervalInMinutes);
109                 scheduledFuture = scheduledService.scheduleAtFixedRate(this, 5, cleaningIntervalInMinutes,
110                         TimeUnit.MINUTES);
111
112             }
113         } catch (Exception e) {
114             log.debug("unexpected error occured", e);
115             BeEcompErrorManager.getInstance().logBeComponentCleanerSystemError("AsdcComponentsCleanerTask-startTask",
116                     e.getMessage());
117
118         }
119     }
120
121     private void stopTask() {
122         if (scheduledFuture != null) {
123             boolean cancelTaskSuccessfully = scheduledFuture.cancel(true);
124             log.debug("Stop cleaning task. result = {}", cancelTaskSuccessfully);
125             if (!cancelTaskSuccessfully) {
126                 BeEcompErrorManager.getInstance().logBeComponentCleanerSystemError("AsdcComponentsCleanerTask-stopTask",
127                         "try to stop the polling task");
128             }
129             scheduledFuture = null;
130         }
131
132     }
133
134     @Override
135     public void run() {
136         try {
137             componentsCleanBusinessLogic.cleanComponents(componentsToClean);
138         } catch (Exception e) {
139             log.error("unexpected error occured", e);
140             BeEcompErrorManager.getInstance().logBeComponentCleanerSystemError("AsdcComponentsCleanerTask-run",
141                     e.getMessage());
142         }
143
144     }
145
146     @Override
147     public ExecutorService getExecutorService() {
148         return scheduledService;
149     }
150 }