a23c9925bec2f33004367b895ec9a96f88dab8f4
[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 package org.onap.ccsdk.cds.blueprintsprocessor.healthapi.service
17
18 import org.onap.ccsdk.cds.blueprintsprocessor.healthapi.configuration.HealthCheckProperties
19 import org.onap.ccsdk.cds.blueprintsprocessor.healthapi.domain.*
20 import org.onap.ccsdk.cds.blueprintsprocessor.healthapi.utils.ObjectMappingUtils
21 import org.onap.ccsdk.cds.blueprintsprocessor.rest.service.BlueprintWebClientService
22 import org.springframework.stereotype.Service
23
24 /**
25  *Service for combined Metrics for CDS Listener and BluePrintProcessor
26  *
27  * @author Shaaban Ebrahim
28  * @version 1.0
29  */
30 @Service
31 open class CombinedMetricsService(private val endPointExecution: EndPointExecution
32                                   , private val healthCheckProperties: HealthCheckProperties
33                                   , private val objectMappingUtils: ObjectMappingUtils<Metrics>) {
34
35     private fun setupServiceEndpoint(): List<ServiceEndpoint> {
36         return listOf(
37                 ServiceEndpoint("BluePrintProcessor metrics", healthCheckProperties.getBluePrintBaseURL() + "/actuator/metrics")
38                 , ServiceEndpoint("CDS Listener metrics", healthCheckProperties.getCDSListenerBaseURL() + "/actuator/metrics")
39         )
40     }
41
42     open val metricsInfo: MetricsInfo
43         get() {
44             val containerHealthChecks = mutableListOf<ActuatorCheckResponse>()
45             for (serviceEndpoint in setupServiceEndpoint().parallelStream()) {
46                 val webClientResponse = endPointExecution?.retrieveWebClientResponse(serviceEndpoint)
47                 var actuatorsHealthResponse: ActuatorCheckResponse? = null
48                 actuatorsHealthResponse = if (webClientResponse?.response != null &&
49                         webClientResponse.response!!.status?.equals(200)!!) {
50                     var body = gettingCustomizedBody(serviceEndpoint, webClientResponse.response!!)
51                     ActuatorCheckResponse(serviceEndpoint.serviceName, body)
52                 } else {
53                     ActuatorCheckResponse(serviceEndpoint.serviceName, HealthCheckStatus.DOWN)
54                 }
55                 containerHealthChecks.add(actuatorsHealthResponse)
56             }
57             return MetricsInfo(containerHealthChecks)
58         }
59
60     private fun gettingCustomizedBody(serviceEndpoint: ServiceEndpoint?, webClientResponse: BlueprintWebClientService.WebClientResponse<String>): Any {
61         var body: Any
62         val metrics: Metrics = objectMappingUtils.getObjectFromBody(webClientResponse.body, Metrics::class.java)
63         val mapOfMetricsInfo = HashMap<String, String>()
64         for (name in metrics.names!!) {
65             mapOfMetricsInfo.put(name.toString(), serviceEndpoint?.serviceLink + "/" + name)
66         }
67         body = MetricsResponse(mapOfMetricsInfo)
68
69         return body
70     }
71 }
72