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