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 org.apache.commons.lang3.concurrent.BasicThreadFactory;
24 import org.openecomp.sdc.be.config.BeEcompErrorManager;
25 import org.openecomp.sdc.be.config.CleanComponentsConfiguration;
26 import org.openecomp.sdc.be.config.Configuration;
27 import org.openecomp.sdc.be.config.ConfigurationManager;
28 import org.openecomp.sdc.be.datatypes.enums.NodeTypeEnum;
29 import org.openecomp.sdc.common.log.wrappers.Logger;
30 import org.springframework.stereotype.Component;
32 import javax.annotation.PostConstruct;
33 import javax.annotation.PreDestroy;
34 import java.util.ArrayList;
35 import java.util.List;
36 import java.util.concurrent.ExecutorService;
37 import java.util.concurrent.Executors;
38 import java.util.concurrent.ScheduledExecutorService;
39 import java.util.concurrent.ScheduledFuture;
40 import java.util.concurrent.TimeUnit;
42 @Component("asdcComponentsCleaner")
43 public class AsdcComponentsCleanerTask extends AbstractScheduleTaskRunner implements Runnable {
45 private static final Logger log = Logger.getLogger(AsdcComponentsCleanerTask.class);
47 @javax.annotation.Resource
48 private ComponentsCleanBusinessLogic componentsCleanBusinessLogic = null;
50 private List<NodeTypeEnum> componentsToClean;
51 private long cleaningIntervalInMinutes;
53 private ScheduledExecutorService scheduledService = Executors.newScheduledThreadPool(1,
54 new BasicThreadFactory.Builder().namingPattern("ComponentsCleanThread-%d").build());
55 ScheduledFuture<?> scheduledFuture = null;
59 log.info("Enter init method of AsdcComponentsCleaner");
60 Configuration configuration = ConfigurationManager.getConfigurationManager().getConfiguration();
61 CleanComponentsConfiguration cleanComponentsConfiguration = configuration.getCleanComponentsConfiguration();
63 if (cleanComponentsConfiguration == null) {
64 log.info("ERROR - configuration is not valid!!! missing cleanComponentsConfiguration");
65 BeEcompErrorManager.getInstance().logBeComponentCleanerSystemError("AsdcComponentsCleanerTask-init",
66 "fecth configuration");
70 componentsToClean = new ArrayList<>();
71 List<String> components = cleanComponentsConfiguration.getComponentsToClean();
72 if (components == null) {
73 log.info("no component were configured for cleaning");
75 for (String component : components) {
76 NodeTypeEnum typeEnum = NodeTypeEnum.getByNameIgnoreCase(component);
78 componentsToClean.add(typeEnum);
81 long intervalInMinutes = cleanComponentsConfiguration.getCleanIntervalInMinutes();
83 if (intervalInMinutes < 1) {
84 log.warn("cleaningIntervalInMinutes value should be greater than or equal to 1 minute. use default");
85 intervalInMinutes = 60;
87 cleaningIntervalInMinutes = intervalInMinutes;
91 log.info("End init method of AsdcComponentsCleaner");
95 public void destroy() {
100 public void startTask() {
102 log.debug("start task for cleaning components");
106 if (scheduledService != null) {
107 log.debug("Start Cleaning components task. interval {} minutes", cleaningIntervalInMinutes);
108 scheduledFuture = scheduledService.scheduleAtFixedRate(this, 5, cleaningIntervalInMinutes,
112 } catch (Exception e) {
113 log.debug("unexpected error occured", e);
114 BeEcompErrorManager.getInstance().logBeComponentCleanerSystemError("AsdcComponentsCleanerTask-startTask",
120 private void stopTask() {
121 if (scheduledFuture != null) {
122 boolean cancelTaskSuccessfully = scheduledFuture.cancel(true);
123 log.debug("Stop cleaning task. result = {}", cancelTaskSuccessfully);
124 if (!cancelTaskSuccessfully) {
125 BeEcompErrorManager.getInstance().logBeComponentCleanerSystemError("AsdcComponentsCleanerTask-stopTask",
126 "try to stop the polling task");
128 scheduledFuture = null;
136 componentsCleanBusinessLogic.cleanComponents(componentsToClean);
137 } catch (Exception e) {
138 log.error("unexpected error occured", e);
139 BeEcompErrorManager.getInstance().logBeComponentCleanerSystemError("AsdcComponentsCleanerTask-run",
146 public ExecutorService getExecutorService() {
147 return scheduledService;