2 * Copyright © 2019-2020 Orange.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package org.onap.ccsdk.cds.blueprintsprocessor.healthapi.service
19 import org.onap.ccsdk.cds.blueprintsprocessor.healthapi.domain.HealthApiResponse
20 import org.onap.ccsdk.cds.blueprintsprocessor.healthapi.domain.HealthCheckResponse
21 import org.onap.ccsdk.cds.blueprintsprocessor.healthapi.domain.HealthCheckStatus
22 import org.onap.ccsdk.cds.blueprintsprocessor.healthapi.domain.ServiceEndpoint
23 import org.onap.ccsdk.cds.blueprintsprocessor.rest.BasicAuthRestClientProperties
24 import org.onap.ccsdk.cds.blueprintsprocessor.rest.service.BasicAuthRestClientService
25 import org.onap.ccsdk.cds.blueprintsprocessor.rest.service.BlueprintWebClientService
26 import org.slf4j.LoggerFactory
27 import org.springframework.beans.factory.annotation.Autowired
28 import org.springframework.http.HttpMethod
29 import org.springframework.stereotype.Service
33 class HealthCheckService {
35 private var logger = LoggerFactory.getLogger(HealthCheckService::class.java)
38 lateinit var basicAuthRestClientService: BasicAuthRestClientService
41 lateinit var restClientProperties: BasicAuthRestClientProperties
44 open fun setupServiceEndpoint(): List<ServiceEndpoint> {
45 return listOf(ServiceEndpoint("Execution service", "http://cds-blueprints-processor-http:8080/api/v1/execution-service/health-check"),
46 ServiceEndpoint("Template service", "http://cds-blueprints-processor-http:8080/api/v1/template/health-check"),
47 ServiceEndpoint("Resources service", "http://cds-blueprints-processor-http:8080/api/v1/resources/health-check"),
48 ServiceEndpoint("SDC Listener service", "http://cds-sdc-listener:8080/api/v1/sdclistener/health-check")
52 fun retrieveSystemStatus(): HealthApiResponse {
53 logger.info("Retrieve System Status")
54 var healthApiResponse: HealthApiResponse
55 val listOfResponse = mutableListOf<HealthCheckResponse>()
56 var systemStatus: HealthCheckStatus = HealthCheckStatus.UP
58 for (serviceEndpoint in setupServiceEndpoint().parallelStream()) {
59 var serviceStatus: HealthCheckStatus = retrieveServiceStatus(serviceEndpoint)
60 if (serviceStatus.equals(HealthCheckStatus.DOWN))
61 systemStatus = HealthCheckStatus.DOWN
63 listOfResponse.add(HealthCheckResponse(serviceEndpoint.serviceName, serviceStatus))
65 healthApiResponse = HealthApiResponse(systemStatus, listOfResponse)
66 return healthApiResponse
69 private fun retrieveServiceStatus(serviceEndpoint: ServiceEndpoint): HealthCheckStatus {
70 var serviceStatus: HealthCheckStatus = HealthCheckStatus.UP
72 addClientPropertiesConfiguration(serviceEndpoint)
73 val result: BlueprintWebClientService.WebClientResponse<String> = basicAuthRestClientService.exchangeResource(HttpMethod.GET.name, "", "")
74 if (result == null || result.status != 200) {
75 serviceStatus = HealthCheckStatus.DOWN
78 } catch (e: Exception) {
79 logger.error("service is down" + e)
80 serviceStatus = HealthCheckStatus.DOWN
86 private fun addClientPropertiesConfiguration(serviceEndpoint: ServiceEndpoint) {
87 restClientProperties.url = serviceEndpoint.serviceLink