16cd05deaed49f03e2d39bee73c62e5de40257a8
[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");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  * ============LICENSE_END========================================================================
17  */
18
19 package org.onap.dcaegen2.collectors.datafile.controllers;
20
21 import io.swagger.annotations.Api;
22 import io.swagger.annotations.ApiOperation;
23
24 import org.onap.dcaegen2.collectors.datafile.configuration.SchedulerConfig;
25 import org.onap.dcaegen2.collectors.datafile.model.logging.MappedDiagnosticContext;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
28 import org.springframework.beans.factory.annotation.Autowired;
29 import org.springframework.http.HttpHeaders;
30 import org.springframework.http.HttpStatus;
31 import org.springframework.http.ResponseEntity;
32 import org.springframework.web.bind.annotation.GetMapping;
33 import org.springframework.web.bind.annotation.RequestHeader;
34 import org.springframework.web.bind.annotation.RestController;
35 import reactor.core.publisher.Mono;
36
37 /**
38  * The HTTP api to start and stop DFC.
39  *
40  * @author <a href="mailto:przemyslaw.wasala@nokia.com">Przemysław Wąsala</a> on 4/5/18
41  * @author <a href="mailto:henrik.b.andersson@est.tech">Henrik Andersson</a>
42  */
43
44 @RestController
45 @Api(value = "ScheduleController")
46 public class ScheduleController {
47
48     private static final Logger logger = LoggerFactory.getLogger(ScheduleController.class);
49
50     private final SchedulerConfig schedulerConfig;
51
52     @Autowired
53     public ScheduleController(SchedulerConfig schedulerConfig) {
54         this.schedulerConfig = schedulerConfig;
55     }
56
57     /**
58      * Start the DFC.
59      *
60      * @param headers the request headers.
61      * @return the response.
62      */
63     @GetMapping("/start")
64     @ApiOperation(value = "Start scheduling worker request")
65     public Mono<ResponseEntity<String>> startTasks(@RequestHeader HttpHeaders headers) {
66         MappedDiagnosticContext.initializeTraceContext(headers);
67         logger.info(MappedDiagnosticContext.ENTRY, "Start request");
68         Mono<ResponseEntity<String>> response = startTasks();
69         logger.info(MappedDiagnosticContext.EXIT, "Start request");
70         return response;
71     }
72
73     public Mono<ResponseEntity<String>> startTasks() {
74         return Mono.fromSupplier(schedulerConfig::tryToStartTask) //
75             .map(ScheduleController::createStartTaskResponse);
76     }
77
78     /**
79      * Stop the DFC.
80      *
81      * @return the response.
82      */
83     @GetMapping("/stopDatafile")
84     @ApiOperation(value = "Receiving stop scheduling worker request")
85     public Mono<ResponseEntity<String>> stopTask(@RequestHeader HttpHeaders headers) {
86         MappedDiagnosticContext.initializeTraceContext(headers);
87         logger.info(MappedDiagnosticContext.ENTRY, "Stop request");
88         Mono<ResponseEntity<String>> response = schedulerConfig.getResponseFromCancellationOfTasks();
89         logger.info(MappedDiagnosticContext.EXIT, "Stop request");
90         return response;
91     }
92
93     @ApiOperation(value = "Sends success or error response on starting task execution")
94     private static ResponseEntity<String> createStartTaskResponse(boolean wasScheduled) {
95         if (wasScheduled) {
96             return new ResponseEntity<>("Datafile Service has been started!", HttpStatus.CREATED);
97         } else {
98             return new ResponseEntity<>("Datafile Service is still running!", HttpStatus.NOT_ACCEPTABLE);
99         }
100     }
101 }