update vlb cds instantiation and scale out package
[ccsdk/cds.git] / components / model-catalog / blueprint-model / service-blueprint / vLB / Scripts / kotlin / HealthCheck.kt
diff --git a/components/model-catalog/blueprint-model/service-blueprint/vLB/Scripts/kotlin/HealthCheck.kt b/components/model-catalog/blueprint-model/service-blueprint/vLB/Scripts/kotlin/HealthCheck.kt
new file mode 100644 (file)
index 0000000..b09a432
--- /dev/null
@@ -0,0 +1,136 @@
+package org.onap.ccsdk.cds.blueprintsprocessor.services.execution.scripts\r
+\r
+/*\r
+ * Copyright © 2019 IBM, Bell Canada, Orange\r
+ *\r
+ * Licensed under the Apache License, Version 2.0 (the "License");\r
+ * you may not use this file except in compliance with the License.\r
+ * You may obtain a copy of the License at\r
+ *\r
+ *     http://www.apache.org/licenses/LICENSE-2.0\r
+ *\r
+ * Unless required by applicable law or agreed to in writing, software\r
+ * distributed under the License is distributed on an "AS IS" BASIS,\r
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ * See the License for the specific language governing permissions and\r
+ * limitations under the License.\r
+ */\r
+\r
+\r
+import com.fasterxml.jackson.databind.node.ObjectNode\r
+import org.onap.ccsdk.cds.blueprintsprocessor.core.api.data.ExecutionServiceInput\r
+import org.onap.ccsdk.cds.blueprintsprocessor.functions.resource.resolution.storedContentFromResolvedArtifactNB\r
+import org.onap.ccsdk.cds.blueprintsprocessor.rest.BasicAuthRestClientProperties\r
+import org.onap.ccsdk.cds.blueprintsprocessor.rest.RestClientProperties\r
+import org.onap.ccsdk.cds.blueprintsprocessor.rest.service.BasicAuthRestClientService\r
+import org.onap.ccsdk.cds.blueprintsprocessor.rest.service.BlueprintWebClientService\r
+import org.onap.ccsdk.cds.blueprintsprocessor.services.execution.AbstractScriptComponentFunction\r
+import org.onap.ccsdk.cds.controllerblueprints.core.utils.JacksonUtils\r
+import org.slf4j.LoggerFactory\r
+import org.springframework.http.HttpMethod\r
+import org.springframework.web.client.RestTemplate\r
+import com.fasterxml.jackson.annotation.JsonIgnore\r
+import com.fasterxml.jackson.annotation.JsonProperty\r
+import org.apache.http.client.ClientProtocolException\r
+import org.onap.ccsdk.cds.blueprintsprocessor.core.utils.PayloadUtils\r
+import org.onap.ccsdk.cds.blueprintsprocessor.services.execution.ComponentRemoteScriptExecutor\r
+import java.io.IOException\r
+import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintProcessorException\r
+\r
+open class HealthCheck : AbstractScriptComponentFunction() {\r
+\r
+    private val log = LoggerFactory.getLogger(HealthCheck::class.java)!!\r
+\r
+    override fun getName(): String {\r
+        return "HealthCheck"\r
+    }\r
+\r
+    override suspend fun processNB(executionRequest: ExecutionServiceInput) {\r
+        log.info("executing script")\r
+        val resolution_key = getDynamicProperties("resolution-key").asText()\r
+        log.info("resolution_key: $resolution_key")\r
+\r
+        //val payload = storedContentFromResolvedArtifactNB(resolution_key, "baseconfig")\r
+        //log.info("configuration: $payload")\r
+\r
+        //val payloadObject = JacksonUtils.jsonNode(payload) as ObjectNode\r
+        //val vdns_ip: String = payloadObject.get("vdns-instance")[0].get("ip-addr").asText()\r
+\r
+\r
+        val blueprintContext = bluePrintRuntimeService.bluePrintContext()\r
+        val requirement = blueprintContext.nodeTemplateRequirement(nodeTemplateName, "restconf-connection")\r
+        val capabilityProperties = bluePrintRuntimeService.resolveNodeTemplateCapabilityProperties(requirement.node!!, requirement.capability!!)\r
+        val netconfDeviceInfo = JacksonUtils.getInstanceFromMap(capabilityProperties, NetconfDeviceInfo2::class.java)\r
+        //log.info("Waiting for 2 minutes until vLB intializes ...")\r
+        //Thread.sleep(120000)\r
+        val uri = "http://${netconfDeviceInfo.ipAddress}:8183/restconf/operational/health-vnf-onap-plugin:health-vnf-onap-plugin-state/health-check"\r
+        val restTemplate = RestTemplate()\r
+        val mapOfHeaders = hashMapOf<String, String>()\r
+        mapOfHeaders.put("Accept", "application/json")\r
+        mapOfHeaders.put("Content-Type", "application/json")\r
+        //mapOfHeaders.put("cache-control", " no-cache")\r
+        //mapOfHeaders.put("Accept", "application/json")\r
+        val basicAuthRestClientProperties: BasicAuthRestClientProperties = BasicAuthRestClientProperties()\r
+        basicAuthRestClientProperties.username = "admin"\r
+        basicAuthRestClientProperties.password = "admin"\r
+        basicAuthRestClientProperties.url = uri\r
+        basicAuthRestClientProperties.additionalHeaders =mapOfHeaders\r
+        val basicAuthRestClientService: BasicAuthRestClientService= BasicAuthRestClientService(basicAuthRestClientProperties)\r
+        try {\r
+            val result: BlueprintWebClientService.WebClientResponse<String> = basicAuthRestClientService.exchangeResource(HttpMethod.GET.name, "", "")\r
+            log.info(result.body)\r
+            val resultJson = JacksonUtils.jsonNode(result.body) as ObjectNode\r
+            val health: String = resultJson.get("health-check").get("state").asText()\r
+                       super.setAttribute("response-data", resultJson) \r
+            if (health != "healthy") {\r
+                throw Exception("VNF is not healty!!")\r
+            }\r
+                       \r
+            //basicAuthRestClientProperties.url = //"http://${netconfDeviceInfo.ipAddress}:8183/restconf/config/vlb-business-vnf-onap-plugin:vlb-business-vnf-onap-plugin/vdns-instances"\r
+            //val resultOfGet: BlueprintWebClientService.WebClientResponse<String> = basicAuthRestClientService.exchangeResource(HttpMethod.GET.name, "", "")\r
+            //print(resultOfGet)\r
+        }\r
+        catch (e: Exception) {\r
+            log.info("Caught exception trying to connect to vLB!!")\r
+            throw BluePrintProcessorException("${e.message}")\r
+        }\r
+    }\r
+\r
+    override suspend fun recoverNB(runtimeException: RuntimeException, executionRequest: ExecutionServiceInput) {\r
+        log.info("Executing Recovery")\r
+        bluePrintRuntimeService.getBluePrintError().addError("${runtimeException.message}")\r
+    }\r
+}\r
+\r
+class NetconfDeviceInfo2 {\r
+    @get:JsonProperty("login-account")\r
+    var username: String? = null\r
+    @get:JsonProperty("login-key")\r
+    var password: String? = null\r
+    @get:JsonProperty("target-ip-address")\r
+    var ipAddress: String? = null\r
+    @get:JsonProperty("port-number")\r
+    var port: Int = 0\r
+    @get:JsonProperty("connection-time-out")\r
+    var connectTimeout: Long = 5\r
+    @get:JsonIgnore\r
+    var source: String? = null\r
+    @get:JsonIgnore\r
+    var replyTimeout: Int = 5\r
+    @get:JsonIgnore\r
+    var idleTimeout: Int = 99999\r
+\r
+    override fun toString(): String {\r
+        return "$ipAddress:$port"\r
+    }\r
+    //TODO: should this be a data class instead? Is anything using the JSON serdes?\r
+    override fun equals(other: Any?): Boolean {\r
+        if (this === other) return true\r
+        if (javaClass != other?.javaClass) return false\r
+        return true\r
+    }\r
+\r
+    override fun hashCode(): Int {\r
+        return javaClass.hashCode()\r
+    }\r
+}
\ No newline at end of file