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