Continue work with PolicyControl
[nonrtric.git] / policy-agent / src / main / java / org / oransc / policyagent / configuration / ApplicationConfigLoader.java
1 /*-
2  * ========================LICENSE_START=================================
3  * O-RAN-SC
4  * %%
5  * Copyright (C) 2019 Nordix Foundation
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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===================================
19  */
20
21 package org.oransc.policyagent.configuration;
22
23 import io.swagger.annotations.ApiOperation;
24
25 import java.time.Duration;
26 import java.util.ArrayList;
27 import java.util.List;
28 import java.util.concurrent.ScheduledFuture;
29
30 import javax.annotation.PostConstruct;
31
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34 import org.springframework.beans.factory.annotation.Autowired;
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 org.springframework.stereotype.Component;
40 import reactor.core.publisher.Mono;
41
42 @Component
43 @EnableScheduling
44 class ApplicationConfigLoader {
45
46     private static final Logger logger = LoggerFactory.getLogger(ApplicationConfigLoader.class);
47     private static List<ScheduledFuture<?>> scheduledFutureList = new ArrayList<>();
48     private static final Duration CONFIGURATION_REFRESH_INTERVAL = Duration.ofSeconds(15);
49
50     private final TaskScheduler taskScheduler;
51     private final ApplicationConfig configuration;
52
53     @Autowired
54     public ApplicationConfigLoader(TaskScheduler taskScheduler, ApplicationConfig configuration) {
55         this.taskScheduler = taskScheduler;
56         this.configuration = configuration;
57     }
58
59     /**
60      * Function which have to stop tasks execution.
61      *
62      * @return response entity about status of cancellation operation
63      */
64     @ApiOperation(value = "Get response on stopping task execution")
65     public synchronized Mono<ResponseEntity<String>> getResponseFromCancellationOfTasks() {
66         scheduledFutureList.forEach(x -> x.cancel(false));
67         scheduledFutureList.clear();
68         logger.info("Stopped");
69         return Mono.just(new ResponseEntity<>("Service has already been stopped!", HttpStatus.CREATED));
70     }
71
72     @PostConstruct
73     @ApiOperation(value = "Start task if possible")
74     public synchronized boolean start() {
75         logger.info("Start scheduling Datafile workflow");
76         configuration.initialize();
77
78         if (scheduledFutureList.isEmpty()) {
79             scheduledFutureList
80                 .add(taskScheduler.scheduleWithFixedDelay(this::refreshConfiguration, CONFIGURATION_REFRESH_INTERVAL));
81             return true;
82         } else {
83             return false;
84         }
85     }
86
87     private void refreshConfiguration() {
88         // TBD
89
90     }
91
92 }