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