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;
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;
38 * The HTTP api to start and stop DFC.
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>
45 @Api(value = "ScheduleController")
46 public class ScheduleController {
48 private static final Logger logger = LoggerFactory.getLogger(ScheduleController.class);
50 private final SchedulerConfig schedulerConfig;
53 public ScheduleController(SchedulerConfig schedulerConfig) {
54 this.schedulerConfig = schedulerConfig;
60 * @param headers the request headers.
61 * @return the response.
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");
73 public Mono<ResponseEntity<String>> startTasks() {
74 return Mono.fromSupplier(schedulerConfig::tryToStartTask) //
75 .map(ScheduleController::createStartTaskResponse);
81 * @return the response.
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");
93 @ApiOperation(value = "Sends success or error response on starting task execution")
94 private static ResponseEntity<String> createStartTaskResponse(boolean wasScheduled) {
96 return new ResponseEntity<>("Datafile Service has been started!", HttpStatus.CREATED);
98 return new ResponseEntity<>("Datafile Service is still running!", HttpStatus.NOT_ACCEPTABLE);