1f9eb4b03927a20a063a700f73d6d763720ac491
[dcaegen2/services/sdk.git] /
1 /*
2  * ============LICENSE_START====================================
3  * DCAEGEN2-SERVICES-SDK
4  * =========================================================
5  * Copyright (C) 2019 Nokia. All rights reserved.
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.onap.dcaegen2.services.sdk.standardization.moher.adapters.springwebflux;
22
23 import com.google.gson.Gson;
24 import com.google.gson.GsonBuilder;
25 import org.onap.dcaegen2.services.sdk.standardization.moher.health.api.AliveMessage;
26 import org.onap.dcaegen2.services.sdk.standardization.moher.health.api.GsonAdaptersHealth;
27 import org.onap.dcaegen2.services.sdk.standardization.moher.health.api.Health;
28 import org.onap.dcaegen2.services.sdk.standardization.moher.health.api.HealthProvider;
29 import org.springframework.http.HttpStatus;
30 import org.springframework.http.MediaType;
31 import org.springframework.http.ResponseEntity;
32 import org.springframework.web.bind.annotation.GetMapping;
33 import org.springframework.web.bind.annotation.RequestMapping;
34 import org.springframework.web.bind.annotation.RestController;
35 import reactor.core.publisher.Mono;
36
37 @RestController
38 @RequestMapping(value = "/health", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
39 public class HealthController {
40     private final Gson gson;
41     private final HealthProvider healthProvider;
42
43     public static HealthController create(HealthProvider healthProvider) {
44         GsonBuilder gson = new GsonBuilder();
45         gson.registerTypeAdapterFactory(new GsonAdaptersHealth());
46         return new HealthController(gson.create(), healthProvider);
47     }
48
49     public HealthController(Gson gson, HealthProvider healthProvider) {
50         this.gson = gson;
51         this.healthProvider = healthProvider;
52     }
53
54     @GetMapping("/ready")
55     public Mono<ResponseEntity<String>> readinessCheck() {
56         return healthProvider.currentHealth()
57                 .map(health -> responseStatusForHealth(health)
58                         .contentType(MediaType.APPLICATION_JSON_UTF8)
59                         .body(gson.toJson(health)));
60     }
61
62     @GetMapping("/alive")
63     public Mono<String> livenessCheck() {
64         return Mono.just(AliveMessage.ALIVE_MESSAGE_JSON);
65     }
66
67     private ResponseEntity.BodyBuilder responseStatusForHealth(Health health) {
68         return health.healthy()
69                 ? ResponseEntity.ok()
70                 : ResponseEntity.status(HttpStatus.SERVICE_UNAVAILABLE);
71     }
72 }