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.*;
38 @Component("asdcComponentsCleaner")
39 public class AsdcComponentsCleanerTask extends AbstractScheduleTaskRunner implements Runnable {
41 private static final Logger log = Logger.getLogger(AsdcComponentsCleanerTask.class);
43 @javax.annotation.Resource
44 private ComponentsCleanBusinessLogic componentsCleanBusinessLogic = null;
46 private List<NodeTypeEnum> componentsToClean;
47 private long cleaningIntervalInMinutes;
49 private ScheduledExecutorService scheduledService = Executors.newScheduledThreadPool(1,
50 new BasicThreadFactory.Builder().namingPattern("ComponentsCleanThread-%d").build());
51 ScheduledFuture<?> scheduledFuture = null;
55 log.info("Enter init method of AsdcComponentsCleaner");
56 Configuration configuration = ConfigurationManager.getConfigurationManager().getConfiguration();
57 CleanComponentsConfiguration cleanComponentsConfiguration = configuration.getCleanComponentsConfiguration();
59 if (cleanComponentsConfiguration == null) {
60 log.info("ERROR - configuration is not valid!!! missing cleanComponentsConfiguration");
61 BeEcompErrorManager.getInstance().logBeComponentCleanerSystemError("AsdcComponentsCleanerTask-init",
62 "fecth configuration");
66 componentsToClean = new ArrayList<>();
67 List<String> components = cleanComponentsConfiguration.getComponentsToClean();
68 if (components == null) {
69 log.info("no component were configured for cleaning");
71 for (String component : components) {
72 NodeTypeEnum typeEnum = NodeTypeEnum.getByNameIgnoreCase(component);
74 componentsToClean.add(typeEnum);
77 long intervalInMinutes = cleanComponentsConfiguration.getCleanIntervalInMinutes();
79 if (intervalInMinutes < 1) {
80 log.warn("cleaningIntervalInMinutes value should be greater than or equal to 1 minute. use default");
81 intervalInMinutes = 60;
83 cleaningIntervalInMinutes = intervalInMinutes;
87 log.info("End init method of AsdcComponentsCleaner");
91 public void destroy() {
96 public void startTask() {
98 log.debug("start task for cleaning components");
102 if (scheduledService != null) {
103 log.debug("Start Cleaning components task. interval {} minutes", cleaningIntervalInMinutes);
104 scheduledFuture = scheduledService.scheduleAtFixedRate(this, 5, cleaningIntervalInMinutes,
108 } catch (Exception e) {
109 log.debug("unexpected error occured", e);
110 BeEcompErrorManager.getInstance().logBeComponentCleanerSystemError("AsdcComponentsCleanerTask-startTask",
116 private void stopTask() {
117 if (scheduledFuture != null) {
118 boolean cancelTaskSuccessfully = scheduledFuture.cancel(true);
119 log.debug("Stop cleaning task. result = {}", cancelTaskSuccessfully);
120 if (!cancelTaskSuccessfully) {
121 BeEcompErrorManager.getInstance().logBeComponentCleanerSystemError("AsdcComponentsCleanerTask-stopTask",
122 "try to stop the polling task");
124 scheduledFuture = null;
132 componentsCleanBusinessLogic.cleanComponents(componentsToClean);
133 } catch (Exception e) {
134 log.error("unexpected error occured", e);
135 BeEcompErrorManager.getInstance().logBeComponentCleanerSystemError("AsdcComponentsCleanerTask-run",
142 public ExecutorService getExecutorService() {
143 return scheduledService;