40f9d99b3e3bcf33597988d6e1587f549eea5615
[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 org.onap.dcaegen2.collectors.datafile.model.Counters;
23
24 import io.swagger.annotations.Api;
25 import io.swagger.annotations.ApiOperation;
26 import io.swagger.annotations.ApiResponse;
27 import io.swagger.annotations.ApiResponses;
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(value = { //
65             @ApiResponse(code = 200, message = "DATAFILE service is living"),
66             @ApiResponse(code = 401, message = "You are not authorized to view the resource"),
67             @ApiResponse(code = 403, message = "Accessing the resource you were trying to reach is forbidden"),
68             @ApiResponse(code = 404, message = "The resource you were trying to reach is not found")})
69     public Mono<ResponseEntity<String>> heartbeat(@RequestHeader HttpHeaders headers) {
70         MappedDiagnosticContext.initializeTraceContext(headers);
71         logger.info(ENTRY, "Heartbeat request");
72
73         String statusString = "I'm living!";
74
75         Mono<ResponseEntity<String>> response = Mono.just(new ResponseEntity<>(statusString, HttpStatus.OK));
76         logger.info(EXIT, "Heartbeat request");
77         return response;
78     }
79
80     /**
81      * Returns diagnostics and statistics information. It is intended for testing and trouble
82      * shooting.
83      *
84      * @return information.
85      */
86     @GetMapping("/status")
87     @ApiOperation(value = "Returns status and statistics of DATAFILE service")
88     @ApiResponses(value = { //
89             @ApiResponse(code = 200, message = "DATAFILE service is living"),
90             @ApiResponse(code = 401, message = "You are not authorized to view the resource"),
91             @ApiResponse(code = 403, message = "Accessing the resource you were trying to reach is forbidden"),
92             @ApiResponse(code = 404, message = "The resource you were trying to reach is not found")})
93     public Mono<ResponseEntity<String>> status(@RequestHeader HttpHeaders headers) {
94         MappedDiagnosticContext.initializeTraceContext(headers);
95         logger.info(ENTRY, "Status request");
96
97         Counters counters = scheduledTasks.getCounters();
98         Mono<ResponseEntity<String>> response = Mono.just(new ResponseEntity<>(counters.toString(), HttpStatus.OK));
99         logger.info(EXIT, "Status request");
100         return response;
101     }
102
103
104
105 }