aboutsummaryrefslogtreecommitdiffstats
path: root/components/model-catalog/blueprint-model/service-blueprint/vLB/Scripts/kotlin/HealthCheck.kt
blob: 0441a1d13a0ce215bfd87b6093487c66d432fd63 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package org.onap.ccsdk.cds.blueprintsprocessor.services.execution.scripts

/*
 * Copyright © 2019 IBM, Bell Canada, Orange
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import com.fasterxml.jackson.databind.node.ObjectNode
import org.onap.ccsdk.cds.blueprintsprocessor.core.api.data.ExecutionServiceInput
import org.onap.ccsdk.cds.blueprintsprocessor.rest.BasicAuthRestClientProperties
import org.onap.ccsdk.cds.blueprintsprocessor.rest.service.BasicAuthRestClientService
import org.onap.ccsdk.cds.blueprintsprocessor.rest.service.BlueprintWebClientService
import org.onap.ccsdk.cds.blueprintsprocessor.services.execution.AbstractScriptComponentFunction
import org.onap.ccsdk.cds.controllerblueprints.core.utils.JacksonUtils
import org.slf4j.LoggerFactory
import org.springframework.http.HttpMethod
import org.springframework.web.client.RestTemplate
import com.fasterxml.jackson.annotation.JsonIgnore
import com.fasterxml.jackson.annotation.JsonProperty
import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintProcessorException

open class HealthCheck : AbstractScriptComponentFunction() {

    private val log = LoggerFactory.getLogger(HealthCheck::class.java)!!

    override fun getName(): String {
        return "HealthCheck"
    }

    override suspend fun processNB(executionRequest: ExecutionServiceInput) {
        log.info("executing script")
        val resolution_key = getDynamicProperties("resolution-key").asText()
        log.info("resolution_key: $resolution_key")

        // val payload = storedContentFromResolvedArtifactNB(resolution_key, "baseconfig")
        // log.info("configuration: $payload")

        // val payloadObject = JacksonUtils.jsonNode(payload) as ObjectNode
        // val vdns_ip: String = payloadObject.get("vdns-instance")[0].get("ip-addr").asText()

        val blueprintContext = bluePrintRuntimeService.bluePrintContext()
        val requirement = blueprintContext.nodeTemplateRequirement(nodeTemplateName, "netconf-connection")
        val capabilityProperties = bluePrintRuntimeService.resolveNodeTemplateCapabilityProperties(requirement.node!!, requirement.capability!!)
        val netconfDeviceInfo = JacksonUtils.getInstanceFromMap(capabilityProperties, NetconfDeviceInfo2::class.java)
        // log.info("Waiting for 2 minutes until vLB intializes ...")
        // Thread.sleep(120000)
        val uri = "http://${netconfDeviceInfo.ipAddress}:8183/restconf/operational/health-vnf-onap-plugin:health-vnf-onap-plugin-state/health-check"
        val restTemplate = RestTemplate()
        val mapOfHeaders = hashMapOf<String, String>()
        mapOfHeaders.put("Accept", "application/json")
        mapOfHeaders.put("Content-Type", "application/json")
        // mapOfHeaders.put("cache-control", " no-cache")
        // mapOfHeaders.put("Accept", "application/json")
        val basicAuthRestClientProperties: BasicAuthRestClientProperties = BasicAuthRestClientProperties()
        basicAuthRestClientProperties.username = "admin"
        basicAuthRestClientProperties.password = "admin"
        basicAuthRestClientProperties.url = uri
        basicAuthRestClientProperties.additionalHeaders = mapOfHeaders
        val basicAuthRestClientService: BasicAuthRestClientService = BasicAuthRestClientService(basicAuthRestClientProperties)
        try {
            val result: BlueprintWebClientService.WebClientResponse<String> = basicAuthRestClientService.exchangeResource(HttpMethod.GET.name, "", "")
            log.info(result.body)
            val resultJson = JacksonUtils.jsonNode(result.body) as ObjectNode
            val health: String = resultJson.get("health-check").get("state").asText()
            super.setAttribute("response-data", resultJson)
            if (health != "healthy") {
                throw Exception("VNF is not healty!!")
            }

            // basicAuthRestClientProperties.url = //"http://${netconfDeviceInfo.ipAddress}:8183/restconf/config/vlb-business-vnf-onap-plugin:vlb-business-vnf-onap-plugin/vdns-instances"
            // val resultOfGet: BlueprintWebClientService.WebClientResponse<String> = basicAuthRestClientService.exchangeResource(HttpMethod.GET.name, "", "")
            // print(resultOfGet)
        } catch (e: Exception) {
            log.info("Caught exception trying to connect to vLB!!")
            throw BluePrintProcessorException("${e.message}")
        }
    }

    override suspend fun recoverNB(runtimeException: RuntimeException, executionRequest: ExecutionServiceInput) {
        log.info("Executing Recovery")
        bluePrintRuntimeService.getBluePrintError().addError("${runtimeException.message}")
    }
}

class NetconfDeviceInfo2 {

    @get:JsonProperty("login-account")
    var username: String? = null

    @get:JsonProperty("login-key")
    var password: String? = null

    @get:JsonProperty("target-ip-address")
    var ipAddress: String? = null

    @get:JsonProperty("port-number")
    var port: Int = 0

    @get:JsonProperty("connection-time-out")
    var connectTimeout: Long = 5

    @get:JsonIgnore
    var source: String? = null

    @get:JsonIgnore
    var replyTimeout: Int = 5

    @get:JsonIgnore
    var idleTimeout: Int = 99999

    override fun toString(): String {
        return "$ipAddress:$port"
    }

    // TODO: should this be a data class instead? Is anything using the JSON serdes?
    override fun equals(other: Any?): Boolean {
        if (this === other) return true
        if (javaClass != other?.javaClass) return false
        return true
    }

    override fun hashCode(): Int {
        return javaClass.hashCode()
    }
}