Revert "Renaming Files having BluePrint to have Blueprint"
[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.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
26
27 /**
28  *Abstract class to execute provided Service Endpoint from class that extends it
29  * by implementing setupServiceEndpoint method
30  *
31  * @author Shaaban Ebrahim
32  * @version 1.0
33  */
34 abstract class AbstractHealthCheck(private val endPointExecution: EndPointExecution) {
35
36     private var logger = LoggerFactory.getLogger(BluePrintProcessorHealthCheck::class.java)
37
38     private fun retrieveSystemStatus(list: List<ServiceEndpoint>): HealthApiResponse {
39         val healthApiResponse: HealthApiResponse
40         val listOfResponse = mutableListOf<ServicesCheckResponse>()
41         var systemStatus: HealthCheckStatus = HealthCheckStatus.UP
42
43         for (serviceEndpoint in list) {
44             val serviceStatus: HealthCheckStatus = retrieveServiceStatus(serviceEndpoint)
45             if (serviceStatus.equals(HealthCheckStatus.DOWN))
46                 systemStatus = HealthCheckStatus.DOWN
47
48             listOfResponse.add(ServicesCheckResponse(serviceEndpoint.serviceName, serviceStatus))
49         }
50         healthApiResponse = HealthApiResponse(systemStatus, listOfResponse)
51         return healthApiResponse
52     }
53
54     private fun retrieveServiceStatus(serviceEndpoint: ServiceEndpoint): HealthCheckStatus {
55         var serviceStatus: HealthCheckStatus = HealthCheckStatus.UP
56         try {
57             val result: WebClientEnpointResponse? = endPointExecution?.retrieveWebClientResponse(serviceEndpoint)
58             if (result == null || result.response?.status != 200) {
59                 serviceStatus = HealthCheckStatus.DOWN
60             }
61         } catch (e: Exception) {
62             logger.error("service name ${serviceEndpoint.serviceName} is down ${e.message}")
63             serviceStatus = HealthCheckStatus.DOWN
64         }
65         return serviceStatus
66     }
67
68     open fun retrieveEndpointExecutionStatus(): HealthApiResponse {
69         return retrieveSystemStatus(setupServiceEndpoint())
70     }
71
72     abstract fun setupServiceEndpoint(): List<ServiceEndpoint>
73 }