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