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.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31 import org.springframework.stereotype.Component;
33 import javax.annotation.PostConstruct;
34 import javax.annotation.PreDestroy;
35 import java.util.ArrayList;
36 import java.util.List;
37 import java.util.concurrent.ExecutorService;
38 import java.util.concurrent.Executors;
39 import java.util.concurrent.ScheduledExecutorService;
40 import java.util.concurrent.ScheduledFuture;
41 import java.util.concurrent.TimeUnit;
43 @Component("asdcComponentsCleaner")
44 public class AsdcComponentsCleanerTask extends AbstractScheduleTaskRunner implements Runnable {
46 private static final Logger log = LoggerFactory.getLogger(AsdcComponentsCleanerTask.class);
48 @javax.annotation.Resource
49 private ComponentsCleanBusinessLogic componentsCleanBusinessLogic = null;
51 private List<NodeTypeEnum> componentsToClean;
52 private long cleaningIntervalInMinutes;
54 private ScheduledExecutorService scheduledService = Executors.newScheduledThreadPool(1,
55 new BasicThreadFactory.Builder().namingPattern("ComponentsCleanThread-%d").build());
56 ScheduledFuture<?> scheduledFuture = null;
60 log.info("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().logBeComponentCleanerSystemError("AsdcComponentsCleanerTask-init",
67 "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.info("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,
113 } catch (Exception e) {
114 log.debug("unexpected error occured", e);
115 BeEcompErrorManager.getInstance().logBeComponentCleanerSystemError("AsdcComponentsCleanerTask-startTask",
121 private void stopTask() {
122 if (scheduledFuture != null) {
123 boolean cancelTaskSuccessfully = scheduledFuture.cancel(true);
124 log.debug("Stop cleaning task. result = {}", cancelTaskSuccessfully);
125 if (!cancelTaskSuccessfully) {
126 BeEcompErrorManager.getInstance().logBeComponentCleanerSystemError("AsdcComponentsCleanerTask-stopTask",
127 "try to stop the polling task");
129 scheduledFuture = null;
137 componentsCleanBusinessLogic.cleanComponents(componentsToClean);
138 } catch (Exception e) {
139 log.error("unexpected error occured", e);
140 BeEcompErrorManager.getInstance().logBeComponentCleanerSystemError("AsdcComponentsCleanerTask-run",
147 public ExecutorService getExecutorService() {
148 return scheduledService;