Uplift of PMS source from ONAP
[nonrtric.git] / enrichment-coordinator-service / src / main / java / org / oransc / enrichment / controllers / StatusController.java
1 /*-
2  * ========================LICENSE_START=================================
3  * O-RAN-SC
4  * %%
5  * Copyright (C) 2020 Nordix Foundation
6  * %%
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ========================LICENSE_END===================================
19  */
20
21 package org.oransc.enrichment.controllers;
22
23 import com.fasterxml.jackson.annotation.JsonProperty;
24 import com.google.gson.annotations.SerializedName;
25
26 import io.swagger.v3.oas.annotations.Operation;
27 import io.swagger.v3.oas.annotations.media.Content;
28 import io.swagger.v3.oas.annotations.media.Schema;
29 import io.swagger.v3.oas.annotations.responses.ApiResponse;
30 import io.swagger.v3.oas.annotations.responses.ApiResponses;
31 import io.swagger.v3.oas.annotations.tags.Tag;
32
33 import org.immutables.gson.Gson;
34 import org.oransc.enrichment.repository.InfoJobs;
35 import org.oransc.enrichment.repository.InfoProducers;
36 import org.oransc.enrichment.repository.InfoTypes;
37 import org.springframework.beans.factory.annotation.Autowired;
38 import org.springframework.http.HttpStatus;
39 import org.springframework.http.MediaType;
40 import org.springframework.http.ResponseEntity;
41 import org.springframework.web.bind.annotation.GetMapping;
42 import org.springframework.web.bind.annotation.RestController;
43 import reactor.core.publisher.Mono;
44
45 @RestController("StatusController")
46 @Tag(name = StatusController.API_NAME)
47 public class StatusController {
48
49     public static final String API_NAME = "Service status";
50     public static final String API_DESCRIPTION = "API for monitoring of the service";
51
52     @Autowired
53     private InfoJobs infoJobs;
54
55     @Autowired
56     private InfoTypes infoTypes;
57
58     @Autowired
59     private InfoProducers infoProducers;
60
61     @Gson.TypeAdapters
62     @Schema(name = "service_status_info")
63     public static class StatusInfo {
64         @Schema(name = "status", description = "status text")
65         @SerializedName("status")
66         @JsonProperty(value = "status", required = true)
67         public final String status;
68
69         @Schema(name = "no_of_producers", description = "Number of Information Producers")
70         @SerializedName("no_of_producers")
71         @JsonProperty(value = "no_of_producers", required = true)
72         public final int noOfProducers;
73
74         @Schema(name = "no_of_types", description = "Number of Information Types")
75         @SerializedName("no_of_types")
76         @JsonProperty(value = "no_of_types", required = true)
77         public final int noOfTypes;
78
79         @Schema(name = "no_of_jobs", description = "Number of Information Jobs")
80         @SerializedName("no_of_jobs")
81         @JsonProperty(value = "no_of_jobs", required = true)
82         public final int noOfJobs;
83
84         public StatusInfo(String status, InfoProducers producers, InfoTypes types, InfoJobs jobs) {
85             this.status = status;
86             this.noOfJobs = jobs.size();
87             this.noOfProducers = producers.size();
88             this.noOfTypes = types.size();
89         }
90     }
91
92     @GetMapping(path = "/status", produces = MediaType.APPLICATION_JSON_VALUE)
93     @Operation(summary = "Returns status and statistics of this service")
94     @ApiResponses(
95         value = { //
96             @ApiResponse(
97                 responseCode = "200",
98                 description = "Service is living", //
99                 content = @Content(schema = @Schema(implementation = StatusInfo.class))) //
100         })
101     public Mono<ResponseEntity<Object>> getStatus() {
102         StatusInfo info = new StatusInfo("hunky dory", this.infoProducers, this.infoTypes, this.infoJobs);
103         return Mono.just(new ResponseEntity<>(info, HttpStatus.OK));
104     }
105
106 }