f793754e3f11d8adb7c8bd797ad3a30df868dcfc
[ccsdk/cds.git] / ms / blueprintsprocessor / modules / inbounds / health-api-common / src / main / kotlin / org / onap / ccsdk / cds / blueprintsprocessor / healthapi / service / health / AbstractHealthCheck.kt
1 /*
2  * Copyright © 2019-2020 Orange.
3  *
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
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 package org.onap.ccsdk.cds.blueprintsprocessor.healthapi.service.health
18
19 import org.onap.ccsdk.cds.blueprintsprocessor.healthapi.domain.*
20 import org.onap.ccsdk.cds.blueprintsprocessor.healthapi.service.EndPointExecution
21 import org.slf4j.LoggerFactory
22
23 /**
24  *Abstract class to execute provided Service Endpoint from class that extends it
25  * by implementing setupServiceEndpoint method
26  *
27  * @author Shaaban Ebrahim
28  * @version 1.0
29  */
30 abstract class AbstractHealthCheck (private val  endPointExecution: EndPointExecution) {
31
32     private var logger = LoggerFactory.getLogger(BluePrintProcessorHealthCheck::class.java)
33
34     private fun retrieveSystemStatus(list: List<ServiceEndpoint>): HealthApiResponse {
35         val healthApiResponse: HealthApiResponse
36         val listOfResponse = mutableListOf<ServicesCheckResponse>()
37         var systemStatus: HealthCheckStatus = HealthCheckStatus.UP
38
39         for (serviceEndpoint in list) {
40             val serviceStatus: HealthCheckStatus = retrieveServiceStatus(serviceEndpoint)
41             if (serviceStatus.equals(HealthCheckStatus.DOWN))
42                 systemStatus = HealthCheckStatus.DOWN
43
44             listOfResponse.add(ServicesCheckResponse(serviceEndpoint.serviceName, serviceStatus))
45         }
46         healthApiResponse = HealthApiResponse(systemStatus, listOfResponse)
47         return healthApiResponse
48
49     }
50
51
52     private fun retrieveServiceStatus(serviceEndpoint: ServiceEndpoint): HealthCheckStatus {
53         var serviceStatus: HealthCheckStatus = HealthCheckStatus.UP
54         try {
55             val result: WebClientEnpointResponse? = endPointExecution?.retrieveWebClientResponse(serviceEndpoint)
56             if (result == null || result.response?.status != 200) {
57                 serviceStatus = HealthCheckStatus.DOWN
58             }
59         } catch (e: Exception) {
60             logger.error("service name ${serviceEndpoint.serviceName} is down ${e.message}")
61             serviceStatus = HealthCheckStatus.DOWN
62
63         }
64         return serviceStatus
65     }
66
67
68     open fun retrieveEndpointExecutionStatus(): HealthApiResponse {
69         return retrieveSystemStatus(setupServiceEndpoint())
70     }
71
72     abstract fun setupServiceEndpoint(): List<ServiceEndpoint>
73
74 }