09fdb67f2a5048fe8ec459d1f6defda5b85cc843
[ccsdk/cds.git] /
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
18
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
30
31
32 @Service
33 class HealthCheckService {
34
35     private var logger = LoggerFactory.getLogger(HealthCheckService::class.java)
36
37     @Autowired
38     lateinit var basicAuthRestClientService: BasicAuthRestClientService
39
40     @Autowired
41     lateinit var restClientProperties: BasicAuthRestClientProperties
42
43
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")
49         )
50     }
51
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
57
58         for (serviceEndpoint in setupServiceEndpoint().parallelStream()) {
59             var serviceStatus: HealthCheckStatus = retrieveServiceStatus(serviceEndpoint)
60             if (serviceStatus.equals(HealthCheckStatus.DOWN))
61                 systemStatus = HealthCheckStatus.DOWN
62
63             listOfResponse.add(HealthCheckResponse(serviceEndpoint.serviceName, serviceStatus))
64         }
65         healthApiResponse = HealthApiResponse(systemStatus, listOfResponse)
66         return healthApiResponse
67     }
68
69     private fun retrieveServiceStatus(serviceEndpoint: ServiceEndpoint): HealthCheckStatus {
70         var serviceStatus: HealthCheckStatus = HealthCheckStatus.UP
71         try {
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
76
77             }
78         } catch (e: Exception) {
79             logger.error("service is down" + e)
80             serviceStatus = HealthCheckStatus.DOWN
81
82         }
83         return serviceStatus
84     }
85
86     private fun addClientPropertiesConfiguration(serviceEndpoint: ServiceEndpoint) {
87         restClientProperties.url = serviceEndpoint.serviceLink
88     }
89
90
91 }