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
9 * http://www.apache.org/licenses/LICENSE-2.0
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========================================================================
19 package org.onap.dcaegen2.collectors.datafile.controllers;
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;
37 * The HTTP api to start and stop DFC.
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>
44 @Api(value = "ScheduleController")
45 public class ScheduleController {
47 private static final Logger logger = LoggerFactory.getLogger(ScheduleController.class);
49 private final SchedulerConfig schedulerConfig;
52 public ScheduleController(SchedulerConfig schedulerConfig) {
53 this.schedulerConfig = schedulerConfig;
59 * @param headers the request headers.
60 * @return the response.
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");
72 public Mono<ResponseEntity<String>> startTasks() {
73 return Mono.fromSupplier(schedulerConfig::tryToStartTask) //
74 .map(ScheduleController::createStartTaskResponse);
80 * @return the response.
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");
92 @ApiOperation(value = "Sends success or error response on starting task execution")
93 private static ResponseEntity<String> createStartTaskResponse(boolean wasScheduled) {
95 return new ResponseEntity<>("Datafile Service has been started!", HttpStatus.CREATED);
97 return new ResponseEntity<>("Datafile Service is still running!", HttpStatus.NOT_ACCEPTABLE);