5835de1afcb62d720fcf5ff922e9f6f3c739a179
[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 java.time.Duration;
20 import java.time.Instant;
21 import java.util.ArrayList;
22 import java.util.HashMap;
23 import java.util.List;
24 import java.util.Map;
25 import java.util.concurrent.ScheduledFuture;
26
27 import javax.annotation.PostConstruct;
28
29 import org.onap.dcaegen2.collectors.datafile.model.logging.MappedDiagnosticContext;
30 import org.onap.dcaegen2.collectors.datafile.tasks.ScheduledTasks;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33 import org.slf4j.MDC;
34 import org.springframework.beans.factory.annotation.Autowired;
35 import org.springframework.context.annotation.Configuration;
36 import org.springframework.http.HttpStatus;
37 import org.springframework.http.ResponseEntity;
38 import org.springframework.scheduling.TaskScheduler;
39 import org.springframework.scheduling.annotation.EnableScheduling;
40
41 import io.swagger.annotations.ApiOperation;
42 import reactor.core.publisher.Mono;
43
44 /**
45  * Api for starting and stopping DFC.
46  *
47  * @author <a href="mailto:przemyslaw.wasala@nokia.com">Przemysław Wąsala</a> on 6/13/18
48  * @author <a href="mailto:henrik.b.andersson@est.tech">Henrik Andersson</a>
49  */
50 @Configuration
51 @EnableScheduling
52 public class SchedulerConfig {
53
54     private static final Duration SCHEDULING_DELAY_FOR_DATAFILE_COLLECTOR_TASKS = Duration.ofSeconds(15);
55     private static final Duration SCHEDULING_DELAY_FOR_DATAFILE_PURGE_CACHE = Duration.ofHours(1);
56     private static final Logger logger = LoggerFactory.getLogger(SchedulerConfig.class);
57     private static List<ScheduledFuture<?>> scheduledFutureList = new ArrayList<>();
58     private Map<String, String> contextMap = new HashMap<>();
59
60     private final TaskScheduler taskScheduler;
61     private final ScheduledTasks scheduledTask;
62     private final AppConfig configuration;
63
64     /**
65      * Constructor.
66      *
67      * @param taskScheduler The scheduler used to schedule the tasks.
68      * @param scheduledTasks The scheduler that will actually handle the tasks.
69      * @param configuration The DFC configuration.
70      */
71     @Autowired
72     public SchedulerConfig(TaskScheduler taskScheduler, ScheduledTasks scheduledTasks,
73             AppConfig configuration) {
74         this.taskScheduler = taskScheduler;
75         this.scheduledTask = scheduledTasks;
76         this.configuration = configuration;
77     }
78
79     /**
80      * Function which have to stop tasks execution.
81      *
82      * @return response entity about status of cancellation operation
83      */
84     @ApiOperation(value = "Get response on stopping task execution")
85     public synchronized Mono<ResponseEntity<String>> getResponseFromCancellationOfTasks() {
86         scheduledFutureList.forEach(x -> x.cancel(false));
87         scheduledFutureList.clear();
88         configuration.stop();
89         MDC.setContextMap(contextMap);
90         logger.info("Stopped Datafile workflow");
91         MDC.clear();
92         return Mono.just(new ResponseEntity<>("Datafile Service has already been stopped!", HttpStatus.CREATED));
93     }
94
95     /**
96      * Function for starting scheduling Datafile workflow.
97      *
98      * @return status of operation execution: true - started, false - not started
99      */
100     @PostConstruct
101     @ApiOperation(value = "Start task if possible")
102     public synchronized boolean tryToStartTask() {
103         contextMap = MappedDiagnosticContext.initializeTraceContext();
104         logger.info("Start scheduling Datafile workflow");
105         configuration.initialize();
106
107         if (scheduledFutureList.isEmpty()) {
108             scheduledFutureList.add(taskScheduler.scheduleWithFixedDelay(scheduledTask::executeDatafileMainTask,
109                     SCHEDULING_DELAY_FOR_DATAFILE_COLLECTOR_TASKS));
110             scheduledFutureList
111                     .add(taskScheduler.scheduleWithFixedDelay(() -> scheduledTask.purgeCachedInformation(Instant.now()),
112                             SCHEDULING_DELAY_FOR_DATAFILE_PURGE_CACHE));
113             return true;
114         } else {
115             return false;
116         }
117     }
118
119     static void setScheduledFutureList(List<ScheduledFuture<?>> scheduledFutureList) {
120         SchedulerConfig.scheduledFutureList = scheduledFutureList;
121     }
122 }