Fix for radio buttons
[sdc.git] / catalog-be / src / main / java / org / openecomp / sdc / be / components / clean / 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.clean;
22
23 import java.util.ArrayList;
24 import java.util.List;
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
30 import javax.annotation.PostConstruct;
31 import javax.annotation.PreDestroy;
32
33 import org.apache.commons.lang3.concurrent.BasicThreadFactory;
34 import org.openecomp.sdc.be.config.BeEcompErrorManager;
35 import org.openecomp.sdc.be.config.CleanComponentsConfiguration;
36 import org.openecomp.sdc.be.config.Configuration;
37 import org.openecomp.sdc.be.config.ConfigurationManager;
38 import org.openecomp.sdc.be.datatypes.enums.NodeTypeEnum;
39 import org.openecomp.sdc.common.config.EcompErrorName;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
42 import org.springframework.stereotype.Component;
43
44 @Component("asdcComponentsCleaner")
45 public class AsdcComponentsCleanerTask implements Runnable {
46
47         private static Logger log = LoggerFactory.getLogger(AsdcComponentsCleanerTask.class.getName());
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, new BasicThreadFactory.Builder().namingPattern("ComponentsCleanThread-%d").build());
56         ScheduledFuture<?> scheduledFuture = null;
57
58         @PostConstruct
59         public void init() {
60                 log.trace("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().processEcompError(EcompErrorName.BeComponentCleanerSystemError, "AsdcComponentsCleanerTask.init()", "AsdcComponentsCleanerTask.init()");
67                         BeEcompErrorManager.getInstance().logBeComponentCleanerSystemError("AsdcComponentsCleanerTask-init", "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.trace("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, TimeUnit.MINUTES);
110
111                         }
112                 } catch (Exception e) {
113                         log.debug("unexpected error occured", e);
114                         String methodName = new Object() {
115                         }.getClass().getEnclosingMethod().getName();
116
117                         BeEcompErrorManager.getInstance().processEcompError(EcompErrorName.BeComponentCleanerSystemError, methodName, e.getMessage());
118                         BeEcompErrorManager.getInstance().logBeComponentCleanerSystemError("AsdcComponentsCleanerTask-startTask", e.getMessage());
119
120                 }
121         }
122
123         public void stopTask() {
124                 if (scheduledFuture != null) {
125                         boolean result = scheduledFuture.cancel(true);
126                         log.debug("Stop cleaning task. result = {}", result);
127                         String methodName = new Object() {
128                         }.getClass().getEnclosingMethod().getName();
129
130                         if (false == result) {
131                                 BeEcompErrorManager.getInstance().processEcompError(EcompErrorName.BeComponentCleanerSystemError, methodName, "try to stop the polling task");
132                                 BeEcompErrorManager.getInstance().logBeComponentCleanerSystemError("AsdcComponentsCleanerTask-stopTask", "try to stop the polling task");
133                         }
134                         scheduledFuture = null;
135                 }
136
137         }
138
139         private void shutdownExecutor() {
140                 if (scheduledService == null)
141                         return;
142
143                 scheduledService.shutdown(); // Disable new tasks from being submitted
144                 try {
145                         // Wait a while for existing tasks to terminate
146                         if (!scheduledService.awaitTermination(60, TimeUnit.SECONDS)) {
147                                 scheduledService.shutdownNow(); // Cancel currently executing
148                                                                                                 // tasks
149                                 // Wait a while for tasks to respond to being cancelled
150                                 if (!scheduledService.awaitTermination(60, TimeUnit.SECONDS))
151                                         log.debug("Pool did not terminate");
152                         }
153                 } catch (InterruptedException ie) {
154                         // (Re-)Cancel if current thread also interrupted
155                         scheduledService.shutdownNow();
156                         // Preserve interrupt status
157                         Thread.currentThread().interrupt();
158                 }
159         }
160
161         @Override
162         public void run() {
163                 try {
164                         componentsCleanBusinessLogic.cleanComponents(componentsToClean);
165                 } catch (Exception e) {
166                         log.error("unexpected error occured", e);
167                         String methodName = new Object() {
168                         }.getClass().getEnclosingMethod().getName();
169
170                         BeEcompErrorManager.getInstance().processEcompError(EcompErrorName.BeComponentCleanerSystemError, methodName, e.getMessage());
171                         BeEcompErrorManager.getInstance().logBeComponentCleanerSystemError("AsdcComponentsCleanerTask-run", e.getMessage());
172                 }
173
174         }
175 }