5b72df1adc27511cefed83900939084dda9b3868
[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
27 import org.onap.dcaegen2.collectors.datafile.model.Counters;
28 import org.onap.dcaegen2.collectors.datafile.model.logging.MappedDiagnosticContext;
29 import org.onap.dcaegen2.collectors.datafile.tasks.ScheduledTasks;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32 import org.springframework.beans.factory.annotation.Autowired;
33 import org.springframework.http.HttpHeaders;
34 import org.springframework.http.HttpStatus;
35 import org.springframework.http.ResponseEntity;
36 import org.springframework.web.bind.annotation.GetMapping;
37 import org.springframework.web.bind.annotation.RequestHeader;
38 import org.springframework.web.bind.annotation.RestController;
39 import reactor.core.publisher.Mono;
40
41 /**
42  * REST Controller to check the heart beat and status of the DFC.
43  */
44 @RestController
45 @Api(value = "StatusController")
46 public class StatusController {
47
48     private static final Logger logger = LoggerFactory.getLogger(StatusController.class);
49
50     private final ScheduledTasks scheduledTasks;
51
52     @Autowired
53     public StatusController(ScheduledTasks scheduledTasks) {
54         this.scheduledTasks = scheduledTasks;
55     }
56
57     /**
58      * Checks the heart beat of DFC.
59      *
60      * @return the heart beat status of DFC.
61      */
62     @GetMapping("/heartbeat")
63     @ApiOperation(value = "Returns liveness of DATAFILE service")
64     @ApiResponses(
65         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         String statusString = "I'm living!";
75
76         Mono<ResponseEntity<String>> response = Mono.just(new ResponseEntity<>(statusString, HttpStatus.OK));
77         logger.info(EXIT, "Heartbeat request");
78         return response;
79     }
80
81     /**
82      * Returns diagnostics and statistics information. It is intended for testing and trouble
83      * shooting.
84      *
85      * @return information.
86      */
87     @GetMapping("/status")
88     @ApiOperation(value = "Returns status and statistics of DATAFILE service")
89     @ApiResponses(
90         value = { //
91             @ApiResponse(code = 200, message = "DATAFILE service is living"),
92             @ApiResponse(code = 401, message = "You are not authorized to view the resource"),
93             @ApiResponse(code = 403, message = "Accessing the resource you were trying to reach is forbidden"),
94             @ApiResponse(code = 404, message = "The resource you were trying to reach is not found")})
95     public Mono<ResponseEntity<String>> status(@RequestHeader HttpHeaders headers) {
96         MappedDiagnosticContext.initializeTraceContext(headers);
97         logger.info(ENTRY, "Status request");
98
99         Counters counters = scheduledTasks.getCounters();
100         Mono<ResponseEntity<String>> response = Mono.just(new ResponseEntity<>(counters.toString(), HttpStatus.OK));
101         logger.info(EXIT, "Status request");
102         return response;
103     }
104
105 }