Renaming Files having BluePrint to have Blueprint
[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 import com.fasterxml.jackson.databind.node.ObjectNode\r
20 import org.onap.ccsdk.cds.blueprintsprocessor.core.api.data.ExecutionServiceInput\r
21 import org.onap.ccsdk.cds.blueprintsprocessor.rest.BasicAuthRestClientProperties\r
22 import org.onap.ccsdk.cds.blueprintsprocessor.rest.service.BasicAuthRestClientService\r
23 import org.onap.ccsdk.cds.blueprintsprocessor.rest.service.BlueprintWebClientService\r
24 import org.onap.ccsdk.cds.blueprintsprocessor.services.execution.AbstractScriptComponentFunction\r
25 import org.onap.ccsdk.cds.controllerblueprints.core.utils.JacksonUtils\r
26 import org.slf4j.LoggerFactory\r
27 import org.springframework.http.HttpMethod\r
28 import org.springframework.web.client.RestTemplate\r
29 import com.fasterxml.jackson.annotation.JsonIgnore\r
30 import com.fasterxml.jackson.annotation.JsonProperty\r
31 import org.onap.ccsdk.cds.controllerblueprints.core.BlueprintProcessorException\r
32 \r
33 open class HealthCheck : AbstractScriptComponentFunction() {\r
34 \r
35     private val log = LoggerFactory.getLogger(HealthCheck::class.java)!!\r
36 \r
37     override fun getName(): String {\r
38         return "HealthCheck"\r
39     }\r
40 \r
41     override suspend fun processNB(executionRequest: ExecutionServiceInput) {\r
42         log.info("executing script")\r
43         val resolution_key = getDynamicProperties("resolution-key").asText()\r
44         log.info("resolution_key: $resolution_key")\r
45 \r
46         // val payload = storedContentFromResolvedArtifactNB(resolution_key, "baseconfig")\r
47         // log.info("configuration: $payload")\r
48 \r
49         // val payloadObject = JacksonUtils.jsonNode(payload) as ObjectNode\r
50         // val vdns_ip: String = payloadObject.get("vdns-instance")[0].get("ip-addr").asText()\r
51 \r
52         val blueprintContext = bluePrintRuntimeService.bluePrintContext()\r
53         val requirement = blueprintContext.nodeTemplateRequirement(nodeTemplateName, "netconf-connection")\r
54         val capabilityProperties = bluePrintRuntimeService.resolveNodeTemplateCapabilityProperties(requirement.node!!, requirement.capability!!)\r
55         val netconfDeviceInfo = JacksonUtils.getInstanceFromMap(capabilityProperties, NetconfDeviceInfo2::class.java)\r
56         // log.info("Waiting for 2 minutes until vLB intializes ...")\r
57         // Thread.sleep(120000)\r
58         val uri = "http://${netconfDeviceInfo.ipAddress}:8183/restconf/operational/health-vnf-onap-plugin:health-vnf-onap-plugin-state/health-check"\r
59         val restTemplate = RestTemplate()\r
60         val mapOfHeaders = hashMapOf<String, String>()\r
61         mapOfHeaders.put("Accept", "application/json")\r
62         mapOfHeaders.put("Content-Type", "application/json")\r
63         // mapOfHeaders.put("cache-control", " no-cache")\r
64         // mapOfHeaders.put("Accept", "application/json")\r
65         val basicAuthRestClientProperties: BasicAuthRestClientProperties = BasicAuthRestClientProperties()\r
66         basicAuthRestClientProperties.username = "admin"\r
67         basicAuthRestClientProperties.password = "admin"\r
68         basicAuthRestClientProperties.url = uri\r
69         basicAuthRestClientProperties.additionalHeaders = mapOfHeaders\r
70         val basicAuthRestClientService: BasicAuthRestClientService = BasicAuthRestClientService(basicAuthRestClientProperties)\r
71         try {\r
72             val result: BlueprintWebClientService.WebClientResponse<String> = basicAuthRestClientService.exchangeResource(HttpMethod.GET.name, "", "")\r
73             log.info(result.body)\r
74             val resultJson = JacksonUtils.jsonNode(result.body) as ObjectNode\r
75             val health: String = resultJson.get("health-check").get("state").asText()\r
76             super.setAttribute("response-data", resultJson)\r
77             if (health != "healthy") {\r
78                 throw Exception("VNF is not healty!!")\r
79             }\r
80 \r
81             // basicAuthRestClientProperties.url = //"http://${netconfDeviceInfo.ipAddress}:8183/restconf/config/vlb-business-vnf-onap-plugin:vlb-business-vnf-onap-plugin/vdns-instances"\r
82             // val resultOfGet: BlueprintWebClientService.WebClientResponse<String> = basicAuthRestClientService.exchangeResource(HttpMethod.GET.name, "", "")\r
83             // print(resultOfGet)\r
84         } catch (e: Exception) {\r
85             log.info("Caught exception trying to connect to vLB!!")\r
86             throw BlueprintProcessorException("${e.message}")\r
87         }\r
88     }\r
89 \r
90     override suspend fun recoverNB(runtimeException: RuntimeException, executionRequest: ExecutionServiceInput) {\r
91         log.info("Executing Recovery")\r
92         bluePrintRuntimeService.getBlueprintError().addError("${runtimeException.message}")\r
93     }\r
94 }\r
95 \r
96 class NetconfDeviceInfo2 {\r
97 \r
98     @get:JsonProperty("login-account")\r
99     var username: String? = null\r
100 \r
101     @get:JsonProperty("login-key")\r
102     var password: String? = null\r
103 \r
104     @get:JsonProperty("target-ip-address")\r
105     var ipAddress: String? = null\r
106 \r
107     @get:JsonProperty("port-number")\r
108     var port: Int = 0\r
109 \r
110     @get:JsonProperty("connection-time-out")\r
111     var connectTimeout: Long = 5\r
112 \r
113     @get:JsonIgnore\r
114     var source: String? = null\r
115 \r
116     @get:JsonIgnore\r
117     var replyTimeout: Int = 5\r
118 \r
119     @get:JsonIgnore\r
120     var idleTimeout: Int = 99999\r
121 \r
122     override fun toString(): String {\r
123         return "$ipAddress:$port"\r
124     }\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 }\r