d1f615b396691c0beda4fb9802b747d1bcf113bf
[nonrtric/plt/ranpm.git] / datafilecollector / src / main / java / org / onap / dcaegen2 / collectors / datafile / controllers / StatusController.java
1 /*-
2  * ============LICENSE_START======================================================================
3  * Copyright (C) 2018 NOKIA Intellectual Property. All rights reserved.
4  * Copyright (C) 2018-2021 Nordix Foundation. All rights reserved.
5  * ===============================================================================================
6  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
7  * in compliance with the License. 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 distributed under the License
12  * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
13  * or implied. See the License for the specific language governing permissions and limitations under
14  * the License.
15  * ============LICENSE_END========================================================================
16  */
17
18 package org.onap.dcaegen2.collectors.datafile.controllers;
19
20 import io.swagger.annotations.ApiResponse;
21 import io.swagger.annotations.ApiResponses;
22 import io.swagger.v3.oas.annotations.Operation;
23 import io.swagger.v3.oas.annotations.tags.Tag;
24
25 import org.onap.dcaegen2.collectors.datafile.model.Counters;
26 import org.onap.dcaegen2.collectors.datafile.tasks.CollectAndReportFiles;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
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;
36
37 /**
38  * REST Controller to check the heart beat and status of the DFC.
39  */
40 @RestController
41 @Tag(name = "StatusController")
42 public class StatusController {
43
44     private static final Logger logger = LoggerFactory.getLogger(StatusController.class);
45
46     private final CollectAndReportFiles collectAndReportFiles;
47
48     public StatusController(CollectAndReportFiles task) {
49         this.collectAndReportFiles = task;
50     }
51
52     /**
53      * Checks the heart beat of DFC.
54      *
55      * @return the heart beat status of DFC.
56      */
57     @GetMapping("/heartbeat")
58     @Operation(summary = "Returns liveness of DATAFILE service")
59     @ApiResponses(
60         value = { //
61             @ApiResponse(code = 200, message = "DATAFILE service is living"),
62             @ApiResponse(code = 401, message = "You are not authorized to view the resource"),
63             @ApiResponse(code = 403, message = "Accessing the resource you were trying to reach is forbidden"),
64             @ApiResponse(code = 404, message = "The resource you were trying to reach is not found")})
65     public Mono<ResponseEntity<String>> heartbeat(@RequestHeader HttpHeaders headers) {
66         logger.info("ENTRY {}", "Heartbeat request");
67
68         String statusString = "I'm living!";
69
70         Mono<ResponseEntity<String>> response = Mono.just(new ResponseEntity<>(statusString, HttpStatus.OK));
71         logger.info("EXIT {}", "Heartbeat request");
72         return response;
73     }
74
75     /**
76      * Returns diagnostics and statistics information. It is intended for testing
77      * and trouble
78      * shooting.
79      *
80      * @return information.
81      */
82     @GetMapping("/status")
83     @Operation(summary = "Returns status and statistics of DATAFILE service")
84     @ApiResponses(
85         value = { //
86             @ApiResponse(code = 200, message = "DATAFILE service is living"),
87             @ApiResponse(code = 401, message = "You are not authorized to view the resource"),
88             @ApiResponse(code = 403, message = "Accessing the resource you were trying to reach is forbidden"),
89             @ApiResponse(code = 404, message = "The resource you were trying to reach is not found")})
90     public Mono<ResponseEntity<String>> status(@RequestHeader HttpHeaders headers) {
91
92         logger.info("ENTRY {}", "Status request");
93
94         Counters counters = collectAndReportFiles.getCounters();
95         Mono<ResponseEntity<String>> response = Mono.just(new ResponseEntity<>(counters.toString(), HttpStatus.OK));
96         logger.info("EXIT {}", "Status request");
97         return response;
98     }
99 }