b09a432f7bea3dd8afa4389999ff679a6f2e8b3b
[ccsdk/cds.git] / components / model-catalog / blueprint-model / service-blueprint / vLB / Scripts / kotlin / HealthCheck.kt
1 package org.onap.ccsdk.cds.blueprintsprocessor.services.execution.scripts\r
2 \r
3 /*\r
4  * Copyright © 2019 IBM, Bell Canada, Orange\r
5  *\r
6  * Licensed under the Apache License, Version 2.0 (the "License");\r
7  * you may not use this file except in compliance with the License.\r
8  * You may obtain a copy of the License at\r
9  *\r
10  *     http://www.apache.org/licenses/LICENSE-2.0\r
11  *\r
12  * Unless required by applicable law or agreed to in writing, software\r
13  * distributed under the License is distributed on an "AS IS" BASIS,\r
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
15  * See the License for the specific language governing permissions and\r
16  * limitations under the License.\r
17  */\r
18 \r
19 \r
20 import com.fasterxml.jackson.databind.node.ObjectNode\r
21 import org.onap.ccsdk.cds.blueprintsprocessor.core.api.data.ExecutionServiceInput\r
22 import org.onap.ccsdk.cds.blueprintsprocessor.functions.resource.resolution.storedContentFromResolvedArtifactNB\r
23 import org.onap.ccsdk.cds.blueprintsprocessor.rest.BasicAuthRestClientProperties\r
24 import org.onap.ccsdk.cds.blueprintsprocessor.rest.RestClientProperties\r
25 import org.onap.ccsdk.cds.blueprintsprocessor.rest.service.BasicAuthRestClientService\r
26 import org.onap.ccsdk.cds.blueprintsprocessor.rest.service.BlueprintWebClientService\r
27 import org.onap.ccsdk.cds.blueprintsprocessor.services.execution.AbstractScriptComponentFunction\r
28 import org.onap.ccsdk.cds.controllerblueprints.core.utils.JacksonUtils\r
29 import org.slf4j.LoggerFactory\r
30 import org.springframework.http.HttpMethod\r
31 import org.springframework.web.client.RestTemplate\r
32 import com.fasterxml.jackson.annotation.JsonIgnore\r
33 import com.fasterxml.jackson.annotation.JsonProperty\r
34 import org.apache.http.client.ClientProtocolException\r
35 import org.onap.ccsdk.cds.blueprintsprocessor.core.utils.PayloadUtils\r
36 import org.onap.ccsdk.cds.blueprintsprocessor.services.execution.ComponentRemoteScriptExecutor\r
37 import java.io.IOException\r
38 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintProcessorException\r
39 \r
40 open class HealthCheck : AbstractScriptComponentFunction() {\r
41 \r
42     private val log = LoggerFactory.getLogger(HealthCheck::class.java)!!\r
43 \r
44     override fun getName(): String {\r
45         return "HealthCheck"\r
46     }\r
47 \r
48     override suspend fun processNB(executionRequest: ExecutionServiceInput) {\r
49         log.info("executing script")\r
50         val resolution_key = getDynamicProperties("resolution-key").asText()\r
51         log.info("resolution_key: $resolution_key")\r
52 \r
53         //val payload = storedContentFromResolvedArtifactNB(resolution_key, "baseconfig")\r
54         //log.info("configuration: $payload")\r
55 \r
56         //val payloadObject = JacksonUtils.jsonNode(payload) as ObjectNode\r
57         //val vdns_ip: String = payloadObject.get("vdns-instance")[0].get("ip-addr").asText()\r
58 \r
59 \r
60         val blueprintContext = bluePrintRuntimeService.bluePrintContext()\r
61         val requirement = blueprintContext.nodeTemplateRequirement(nodeTemplateName, "restconf-connection")\r
62         val capabilityProperties = bluePrintRuntimeService.resolveNodeTemplateCapabilityProperties(requirement.node!!, requirement.capability!!)\r
63         val netconfDeviceInfo = JacksonUtils.getInstanceFromMap(capabilityProperties, NetconfDeviceInfo2::class.java)\r
64         //log.info("Waiting for 2 minutes until vLB intializes ...")\r
65         //Thread.sleep(120000)\r
66         val uri = "http://${netconfDeviceInfo.ipAddress}:8183/restconf/operational/health-vnf-onap-plugin:health-vnf-onap-plugin-state/health-check"\r
67         val restTemplate = RestTemplate()\r
68         val mapOfHeaders = hashMapOf<String, String>()\r
69         mapOfHeaders.put("Accept", "application/json")\r
70         mapOfHeaders.put("Content-Type", "application/json")\r
71         //mapOfHeaders.put("cache-control", " no-cache")\r
72         //mapOfHeaders.put("Accept", "application/json")\r
73         val basicAuthRestClientProperties: BasicAuthRestClientProperties = BasicAuthRestClientProperties()\r
74         basicAuthRestClientProperties.username = "admin"\r
75         basicAuthRestClientProperties.password = "admin"\r
76         basicAuthRestClientProperties.url = uri\r
77         basicAuthRestClientProperties.additionalHeaders =mapOfHeaders\r
78         val basicAuthRestClientService: BasicAuthRestClientService= BasicAuthRestClientService(basicAuthRestClientProperties)\r
79         try {\r
80             val result: BlueprintWebClientService.WebClientResponse<String> = basicAuthRestClientService.exchangeResource(HttpMethod.GET.name, "", "")\r
81             log.info(result.body)\r
82             val resultJson = JacksonUtils.jsonNode(result.body) as ObjectNode\r
83             val health: String = resultJson.get("health-check").get("state").asText()\r
84                         super.setAttribute("response-data", resultJson) \r
85             if (health != "healthy") {\r
86                 throw Exception("VNF is not healty!!")\r
87             }\r
88                         \r
89             //basicAuthRestClientProperties.url = //"http://${netconfDeviceInfo.ipAddress}:8183/restconf/config/vlb-business-vnf-onap-plugin:vlb-business-vnf-onap-plugin/vdns-instances"\r
90             //val resultOfGet: BlueprintWebClientService.WebClientResponse<String> = basicAuthRestClientService.exchangeResource(HttpMethod.GET.name, "", "")\r
91             //print(resultOfGet)\r
92         }\r
93         catch (e: Exception) {\r
94             log.info("Caught exception trying to connect to vLB!!")\r
95             throw BluePrintProcessorException("${e.message}")\r
96         }\r
97     }\r
98 \r
99     override suspend fun recoverNB(runtimeException: RuntimeException, executionRequest: ExecutionServiceInput) {\r
100         log.info("Executing Recovery")\r
101         bluePrintRuntimeService.getBluePrintError().addError("${runtimeException.message}")\r
102     }\r
103 }\r
104 \r
105 class NetconfDeviceInfo2 {\r
106     @get:JsonProperty("login-account")\r
107     var username: String? = null\r
108     @get:JsonProperty("login-key")\r
109     var password: String? = null\r
110     @get:JsonProperty("target-ip-address")\r
111     var ipAddress: String? = null\r
112     @get:JsonProperty("port-number")\r
113     var port: Int = 0\r
114     @get:JsonProperty("connection-time-out")\r
115     var connectTimeout: Long = 5\r
116     @get:JsonIgnore\r
117     var source: String? = null\r
118     @get:JsonIgnore\r
119     var replyTimeout: Int = 5\r
120     @get:JsonIgnore\r
121     var idleTimeout: Int = 99999\r
122 \r
123     override fun toString(): String {\r
124         return "$ipAddress:$port"\r
125     }\r
126     //TODO: should this be a data class instead? Is anything using the JSON serdes?\r
127     override fun equals(other: Any?): Boolean {\r
128         if (this === other) return true\r
129         if (javaClass != other?.javaClass) return false\r
130         return true\r
131     }\r
132 \r
133     override fun hashCode(): Int {\r
134         return javaClass.hashCode()\r
135     }\r
136 }