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.health
19 import org.onap.ccsdk.cds.blueprintsprocessor.healthapi.domain.*
20 import org.onap.ccsdk.cds.blueprintsprocessor.healthapi.service.EndPointExecution
21 import org.slf4j.LoggerFactory
24 *Abstract class to execute provided Service Endpoint from class that extends it
25 * by implementing setupServiceEndpoint method
27 * @author Shaaban Ebrahim
30 abstract class AbstractHealthCheck (private val endPointExecution: EndPointExecution) {
32 private var logger = LoggerFactory.getLogger(BluePrintProcessorHealthCheck::class.java)
34 private fun retrieveSystemStatus(list: List<ServiceEndpoint>): HealthApiResponse {
35 val healthApiResponse: HealthApiResponse
36 val listOfResponse = mutableListOf<ServicesCheckResponse>()
37 var systemStatus: HealthCheckStatus = HealthCheckStatus.UP
39 for (serviceEndpoint in list) {
40 val serviceStatus: HealthCheckStatus = retrieveServiceStatus(serviceEndpoint)
41 if (serviceStatus.equals(HealthCheckStatus.DOWN))
42 systemStatus = HealthCheckStatus.DOWN
44 listOfResponse.add(ServicesCheckResponse(serviceEndpoint.serviceName, serviceStatus))
46 healthApiResponse = HealthApiResponse(systemStatus, listOfResponse)
47 return healthApiResponse
52 private fun retrieveServiceStatus(serviceEndpoint: ServiceEndpoint): HealthCheckStatus {
53 var serviceStatus: HealthCheckStatus = HealthCheckStatus.UP
55 val result: WebClientEnpointResponse? = endPointExecution?.retrieveWebClientResponse(serviceEndpoint)
56 if (result == null || result.response?.status != 200) {
57 serviceStatus = HealthCheckStatus.DOWN
59 } catch (e: Exception) {
60 logger.error("service name ${serviceEndpoint.serviceName} is down ${e.message}")
61 serviceStatus = HealthCheckStatus.DOWN
68 open fun retrieveEndpointExecutionStatus(): HealthApiResponse {
69 return retrieveSystemStatus(setupServiceEndpoint())
72 abstract fun setupServiceEndpoint(): List<ServiceEndpoint>