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