791f0cf1381c4f3a3c1fbfa69ca2f6a34763fe3e
[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 import org.onap.dcaegen2.collectors.datafile.configuration.SchedulerConfig;
24 import org.onap.dcaegen2.collectors.datafile.model.logging.MappedDiagnosticContext;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
27 import org.springframework.beans.factory.annotation.Autowired;
28 import org.springframework.http.HttpHeaders;
29 import org.springframework.http.HttpStatus;
30 import org.springframework.http.ResponseEntity;
31 import org.springframework.web.bind.annotation.GetMapping;
32 import org.springframework.web.bind.annotation.RequestHeader;
33 import org.springframework.web.bind.annotation.RestController;
34 import reactor.core.publisher.Mono;
35
36 /**
37  * The HTTP api to start and stop DFC.
38  *
39  * @author <a href="mailto:przemyslaw.wasala@nokia.com">Przemysław Wąsala</a> on 4/5/18
40  * @author <a href="mailto:henrik.b.andersson@est.tech">Henrik Andersson</a>
41  */
42
43 @RestController
44 @Api(value = "ScheduleController")
45 public class ScheduleController {
46
47     private static final Logger logger = LoggerFactory.getLogger(ScheduleController.class);
48
49     private final SchedulerConfig schedulerConfig;
50
51     @Autowired
52     public ScheduleController(SchedulerConfig schedulerConfig) {
53         this.schedulerConfig = schedulerConfig;
54     }
55
56     /**
57      * Start the DFC.
58      *
59      * @param headers the request headers.
60      * @return the response.
61      */
62     @GetMapping("/start")
63     @ApiOperation(value = "Start scheduling worker request")
64     public Mono<ResponseEntity<String>> startTasks(@RequestHeader HttpHeaders headers) {
65         MappedDiagnosticContext.initializeTraceContext(headers);
66         logger.info(MappedDiagnosticContext.ENTRY, "Start request");
67         Mono<ResponseEntity<String>> response = startTasks();
68         logger.info(MappedDiagnosticContext.EXIT, "Start request");
69         return response;
70     }
71
72     public Mono<ResponseEntity<String>> startTasks() {
73         return Mono.fromSupplier(schedulerConfig::tryToStartTask) //
74                 .map(ScheduleController::createStartTaskResponse);
75     }
76
77     /**
78      * Stop the DFC.
79      *
80      * @return the response.
81      */
82     @GetMapping("/stopDatafile")
83     @ApiOperation(value = "Receiving stop scheduling worker request")
84     public Mono<ResponseEntity<String>> stopTask(@RequestHeader HttpHeaders headers) {
85         MappedDiagnosticContext.initializeTraceContext(headers);
86         logger.info(MappedDiagnosticContext.ENTRY, "Stop request");
87         Mono<ResponseEntity<String>> response =  schedulerConfig.getResponseFromCancellationOfTasks();
88         logger.info(MappedDiagnosticContext.EXIT, "Stop request");
89         return response;
90     }
91
92     @ApiOperation(value = "Sends success or error response on starting task execution")
93     private static ResponseEntity<String> createStartTaskResponse(boolean wasScheduled) {
94         if (wasScheduled) {
95             return new ResponseEntity<>("Datafile Service has been started!", HttpStatus.CREATED);
96         } else {
97             return new ResponseEntity<>("Datafile Service is still running!", HttpStatus.NOT_ACCEPTABLE);
98         }
99     }
100 }