e03623730dd0f164c73ab40a9b4a1b51001de6f0
[dcaegen2/collectors/datafile.git] /
1 /*-
2  * ============LICENSE_START======================================================================
3  * Copyright (C) 2018 NOKIA Intellectual Property, 2018-2019 Nordix Foundation. All rights reserved.
4  * ===============================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
6  * in compliance with the License. You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software distributed under the License
11  * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
12  * or implied. See the License for the specific language governing permissions and limitations under
13  * the License.
14  * ============LICENSE_END========================================================================
15  */
16
17 package org.onap.dcaegen2.collectors.datafile.configuration;
18
19 import io.swagger.annotations.ApiOperation;
20 import java.time.Duration;
21 import java.time.Instant;
22 import java.util.ArrayList;
23 import java.util.HashMap;
24 import java.util.List;
25 import java.util.Map;
26 import java.util.concurrent.ScheduledFuture;
27 import javax.annotation.PostConstruct;
28 import org.onap.dcaegen2.collectors.datafile.model.logging.MappedDiagnosticContext;
29 import org.onap.dcaegen2.collectors.datafile.tasks.ScheduledTasks;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32 import org.slf4j.MDC;
33 import org.springframework.beans.factory.annotation.Autowired;
34 import org.springframework.context.annotation.Configuration;
35 import org.springframework.http.HttpStatus;
36 import org.springframework.http.ResponseEntity;
37 import org.springframework.scheduling.TaskScheduler;
38 import org.springframework.scheduling.annotation.EnableScheduling;
39 import reactor.core.publisher.Mono;
40
41 /**
42  * Api for starting and stopping DFC.
43  *
44  * @author <a href="mailto:przemyslaw.wasala@nokia.com">Przemysław Wąsala</a> on 6/13/18
45  * @author <a href="mailto:henrik.b.andersson@est.tech">Henrik Andersson</a>
46  */
47 @Configuration
48 @EnableScheduling
49 public class SchedulerConfig {
50
51     private static final Duration SCHEDULING_DELAY_FOR_DATAFILE_COLLECTOR_TASKS = Duration.ofSeconds(15);
52     private static final Duration SCHEDULING_DELAY_FOR_DATAFILE_PURGE_CACHE = Duration.ofHours(1);
53     private static final Logger logger = LoggerFactory.getLogger(SchedulerConfig.class);
54     private static List<ScheduledFuture<?>> scheduledFutureList = new ArrayList<>();
55     private Map<String, String> contextMap = new HashMap<>();
56
57     private final TaskScheduler taskScheduler;
58     private final ScheduledTasks scheduledTask;
59     private final AppConfig configuration;
60
61     /**
62      * Constructor.
63      *
64      * @param taskScheduler The scheduler used to schedule the tasks.
65      * @param scheduledTasks The scheduler that will actually handle the tasks.
66      * @param configuration The DFC configuration.
67      */
68     @Autowired
69     public SchedulerConfig(TaskScheduler taskScheduler, ScheduledTasks scheduledTasks,
70             AppConfig configuration) {
71         this.taskScheduler = taskScheduler;
72         this.scheduledTask = scheduledTasks;
73         this.configuration = configuration;
74     }
75
76     /**
77      * Function which have to stop tasks execution.
78      *
79      * @return response entity about status of cancellation operation
80      */
81     @ApiOperation(value = "Get response on stopping task execution")
82     public synchronized Mono<ResponseEntity<String>> getResponseFromCancellationOfTasks() {
83         scheduledFutureList.forEach(x -> x.cancel(false));
84         scheduledFutureList.clear();
85         configuration.stop();
86         MDC.setContextMap(contextMap);
87         logger.info("Stopped Datafile workflow");
88         MDC.clear();
89         return Mono.just(new ResponseEntity<>("Datafile Service has already been stopped!", HttpStatus.CREATED));
90     }
91
92     /**
93      * Function for starting scheduling Datafile workflow.
94      *
95      * @return status of operation execution: true - started, false - not started
96      */
97     @PostConstruct
98     @ApiOperation(value = "Start task if possible")
99     public synchronized boolean tryToStartTask() {
100         contextMap = MappedDiagnosticContext.initializeTraceContext();
101         logger.info("Start scheduling Datafile workflow");
102         configuration.initialize();
103
104         if (scheduledFutureList.isEmpty()) {
105             scheduledFutureList.add(taskScheduler.scheduleWithFixedDelay(scheduledTask::executeDatafileMainTask,
106                     SCHEDULING_DELAY_FOR_DATAFILE_COLLECTOR_TASKS));
107             scheduledFutureList
108                     .add(taskScheduler.scheduleWithFixedDelay(() -> scheduledTask.purgeCachedInformation(Instant.now()),
109                             SCHEDULING_DELAY_FOR_DATAFILE_PURGE_CACHE));
110             return true;
111         } else {
112             return false;
113         }
114     }
115
116     static void setScheduledFutureList(List<ScheduledFuture<?>> scheduledFutureList) {
117         SchedulerConfig.scheduledFutureList = scheduledFutureList;
118     }
119 }