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