2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.openecomp.sdc.be.components.clean;
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;
30 import javax.annotation.PostConstruct;
31 import javax.annotation.PreDestroy;
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;
44 @Component("asdcComponentsCleaner")
45 public class AsdcComponentsCleanerTask implements Runnable {
47 private static Logger log = LoggerFactory.getLogger(AsdcComponentsCleanerTask.class.getName());
49 @javax.annotation.Resource
50 private ComponentsCleanBusinessLogic componentsCleanBusinessLogic = null;
52 private List<NodeTypeEnum> componentsToClean;
53 private long cleaningIntervalInMinutes;
55 private ScheduledExecutorService scheduledService = Executors.newScheduledThreadPool(1, new BasicThreadFactory.Builder().namingPattern("ComponentsCleanThread-%d").build());
56 ScheduledFuture<?> scheduledFuture = null;
60 log.trace("Enter init method of AsdcComponentsCleaner");
61 Configuration configuration = ConfigurationManager.getConfigurationManager().getConfiguration();
62 CleanComponentsConfiguration cleanComponentsConfiguration = configuration.getCleanComponentsConfiguration();
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");
71 componentsToClean = new ArrayList<NodeTypeEnum>();
72 List<String> components = cleanComponentsConfiguration.getComponentsToClean();
73 if (components == null) {
74 log.info("no component were configured for cleaning");
76 for (String component : components) {
77 NodeTypeEnum typeEnum = NodeTypeEnum.getByNameIgnoreCase(component);
79 componentsToClean.add(typeEnum);
82 long intervalInMinutes = cleanComponentsConfiguration.getCleanIntervalInMinutes();
84 if (intervalInMinutes < 1) {
85 log.warn("cleaningIntervalInMinutes value should be greater than or equal to 1 minute. use default");
86 intervalInMinutes = 60;
88 cleaningIntervalInMinutes = intervalInMinutes;
92 log.trace("End init method of AsdcComponentsCleaner");
96 public void destroy() {
101 public void startTask() {
103 log.debug("start task for cleaning components");
107 if (scheduledService != null) {
108 log.debug("Start Cleaning components task. interval {} minutes", cleaningIntervalInMinutes);
109 scheduledFuture = scheduledService.scheduleAtFixedRate(this, 5, cleaningIntervalInMinutes, TimeUnit.MINUTES);
112 } catch (Exception e) {
113 log.debug("unexpected error occured", e);
114 String methodName = new Object() {
115 }.getClass().getEnclosingMethod().getName();
117 BeEcompErrorManager.getInstance().processEcompError(EcompErrorName.BeComponentCleanerSystemError, methodName, e.getMessage());
118 BeEcompErrorManager.getInstance().logBeComponentCleanerSystemError("AsdcComponentsCleanerTask-startTask", e.getMessage());
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();
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");
134 scheduledFuture = null;
139 private void shutdownExecutor() {
140 if (scheduledService == null)
143 scheduledService.shutdown(); // Disable new tasks from being submitted
145 // Wait a while for existing tasks to terminate
146 if (!scheduledService.awaitTermination(60, TimeUnit.SECONDS)) {
147 scheduledService.shutdownNow(); // Cancel currently executing
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");
153 } catch (InterruptedException ie) {
154 // (Re-)Cancel if current thread also interrupted
155 scheduledService.shutdownNow();
156 // Preserve interrupt status
157 Thread.currentThread().interrupt();
164 componentsCleanBusinessLogic.cleanComponents(componentsToClean);
165 } catch (Exception e) {
166 log.error("unexpected error occured", e);
167 String methodName = new Object() {
168 }.getClass().getEnclosingMethod().getName();
170 BeEcompErrorManager.getInstance().processEcompError(EcompErrorName.BeComponentCleanerSystemError, methodName, e.getMessage());
171 BeEcompErrorManager.getInstance().logBeComponentCleanerSystemError("AsdcComponentsCleanerTask-run", e.getMessage());