c6d56c426961f51b6a1e632442bff998f0a30813
[dcaegen2/collectors/datafile.git] /
1 /*-
2  * ============LICENSE_START======================================================================
3  * Copyright (C) 2018 NOKIA Intellectual Property, 2018 Nordix Foundation. All rights reserved.
4  * ===============================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
6  * in compliance with the License. You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software distributed under the License
11  * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
12  * or implied. See the License for the specific language governing permissions and limitations under
13  * the License.
14  * ============LICENSE_END========================================================================
15  */
16
17 package org.onap.dcaegen2.collectors.datafile.controllers;
18
19 import static org.onap.dcaegen2.collectors.datafile.model.logging.MappedDiagnosticContext.ENTRY;
20 import static org.onap.dcaegen2.collectors.datafile.model.logging.MappedDiagnosticContext.EXIT;
21
22 import io.swagger.annotations.Api;
23 import io.swagger.annotations.ApiOperation;
24 import io.swagger.annotations.ApiResponse;
25 import io.swagger.annotations.ApiResponses;
26 import org.onap.dcaegen2.collectors.datafile.model.logging.MappedDiagnosticContext;
27 import org.onap.dcaegen2.collectors.datafile.tasks.ScheduledTasks;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30 import org.springframework.beans.factory.annotation.Autowired;
31 import org.springframework.http.HttpHeaders;
32 import org.springframework.http.HttpStatus;
33 import org.springframework.http.ResponseEntity;
34 import org.springframework.web.bind.annotation.GetMapping;
35 import org.springframework.web.bind.annotation.RequestHeader;
36 import org.springframework.web.bind.annotation.RestController;
37 import reactor.core.publisher.Mono;
38
39 /**
40  * Controller to check the heartbeat of DFC.
41  *
42  * @author <a href="mailto:przemyslaw.wasala@nokia.com">Przemysław Wąsala</a> on 4/19/18
43  * @author <a href="mailto:henrik.b.andersson@est.tech">Henrik Andersson</a>
44  */
45 @RestController
46 @Api(value = "HeartbeatController")
47 public class HeartbeatController {
48
49     private static final Logger logger = LoggerFactory.getLogger(HeartbeatController.class);
50
51     private final ScheduledTasks scheduledTasks;
52
53     @Autowired
54     public HeartbeatController(ScheduledTasks scheduledTasks) {
55         this.scheduledTasks = scheduledTasks;
56     }
57
58     /**
59      * Checks the heartbeat of DFC.
60      *
61      * @return the heartbeat status of DFC.
62      */
63     @GetMapping("/heartbeat")
64     @ApiOperation(value = "Returns liveness of DATAFILE service")
65     @ApiResponses(value = { //
66             @ApiResponse(code = 200, message = "DATAFILE service is living"),
67             @ApiResponse(code = 401, message = "You are not authorized to view the resource"),
68             @ApiResponse(code = 403, message = "Accessing the resource you were trying to reach is forbidden"),
69             @ApiResponse(code = 404, message = "The resource you were trying to reach is not found") })
70     public Mono<ResponseEntity<String>> heartbeat(@RequestHeader HttpHeaders headers) {
71         MappedDiagnosticContext.initializeTraceContext(headers);
72         logger.info(ENTRY, "Heartbeat request");
73
74         StringBuilder statusString = new StringBuilder("I'm living! Status: ");
75         statusString.append("numberOfFileCollectionTasks=").append(scheduledTasks.getCurrentNumberOfTasks())
76                 .append(",");
77         statusString.append("fileCacheSize=").append(scheduledTasks.publishedFilesCacheSize());
78
79         Mono<ResponseEntity<String>> response = Mono.just(new ResponseEntity<>(statusString.toString(), HttpStatus.OK));
80         logger.info(EXIT, "Heartbeat request");
81         return response;
82     }
83 }