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