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.scheduledtasks;
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;
31 import javax.annotation.PostConstruct;
32 import javax.annotation.PreDestroy;
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;
44 @Component("asdcComponentsCleaner")
45 public class AsdcComponentsCleanerTask extends AbstractScheduleTaskRunner implements Runnable {
47 private static final Logger log = LoggerFactory.getLogger(AsdcComponentsCleanerTask.class);
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,
56 new BasicThreadFactory.Builder().namingPattern("ComponentsCleanThread-%d").build());
57 ScheduledFuture<?> scheduledFuture = null;
61 log.info("Enter init method of AsdcComponentsCleaner");
62 Configuration configuration = ConfigurationManager.getConfigurationManager().getConfiguration();
63 CleanComponentsConfiguration cleanComponentsConfiguration = configuration.getCleanComponentsConfiguration();
65 if (cleanComponentsConfiguration == null) {
66 log.info("ERROR - configuration is not valid!!! missing cleanComponentsConfiguration");
67 BeEcompErrorManager.getInstance().logBeComponentCleanerSystemError("AsdcComponentsCleanerTask-init",
68 "fecth configuration");
72 componentsToClean = new ArrayList<NodeTypeEnum>();
73 List<String> components = cleanComponentsConfiguration.getComponentsToClean();
74 if (components == null) {
75 log.info("no component were configured for cleaning");
77 for (String component : components) {
78 NodeTypeEnum typeEnum = NodeTypeEnum.getByNameIgnoreCase(component);
80 componentsToClean.add(typeEnum);
83 long intervalInMinutes = cleanComponentsConfiguration.getCleanIntervalInMinutes();
85 if (intervalInMinutes < 1) {
86 log.warn("cleaningIntervalInMinutes value should be greater than or equal to 1 minute. use default");
87 intervalInMinutes = 60;
89 cleaningIntervalInMinutes = intervalInMinutes;
93 log.info("End init method of AsdcComponentsCleaner");
97 public void destroy() {
102 public void startTask() {
104 log.debug("start task for cleaning components");
108 if (scheduledService != null) {
109 log.debug("Start Cleaning components task. interval {} minutes", cleaningIntervalInMinutes);
110 scheduledFuture = scheduledService.scheduleAtFixedRate(this, 5, cleaningIntervalInMinutes,
114 } catch (Exception e) {
115 log.debug("unexpected error occured", e);
116 BeEcompErrorManager.getInstance().logBeComponentCleanerSystemError("AsdcComponentsCleanerTask-startTask",
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");
130 scheduledFuture = null;
138 componentsCleanBusinessLogic.cleanComponents(componentsToClean);
139 } catch (Exception e) {
140 log.error("unexpected error occured", e);
141 BeEcompErrorManager.getInstance().logBeComponentCleanerSystemError("AsdcComponentsCleanerTask-run",
148 public ExecutorService getExecutorService() {
149 return scheduledService;