Implementation of config-assign and config-deploy actions
[demo.git] / heat / vFW_CNF_CDS / templates / cba / Scripts / kotlin / SimpleStatusCheck.kt
diff --git a/heat/vFW_CNF_CDS/templates/cba/Scripts/kotlin/SimpleStatusCheck.kt b/heat/vFW_CNF_CDS/templates/cba/Scripts/kotlin/SimpleStatusCheck.kt
new file mode 100644 (file)
index 0000000..c775bb1
--- /dev/null
@@ -0,0 +1,89 @@
+/*
+ * Copyright © 2021 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.
+ */
+
+package org.onap.ccsdk.cds.blueprintsprocessor.services.execution.scripts
+
+import com.fasterxml.jackson.databind.node.ObjectNode
+import org.onap.ccsdk.cds.blueprintsprocessor.core.BlueprintPropertiesService
+import org.onap.ccsdk.cds.blueprintsprocessor.core.api.data.ExecutionServiceInput
+import org.onap.ccsdk.cds.blueprintsprocessor.functions.k8s.K8sConnectionPluginConfiguration
+import org.onap.ccsdk.cds.blueprintsprocessor.functions.k8s.instance.K8sPluginInstanceApi
+import org.onap.ccsdk.cds.blueprintsprocessor.functions.k8s.instance.K8sRbInstanceStatus
+import org.onap.ccsdk.cds.blueprintsprocessor.services.execution.AbstractScriptComponentFunction
+import org.onap.ccsdk.cds.controllerblueprints.core.BlueprintException
+import org.slf4j.LoggerFactory
+
+open class SimpleStatusCheck : AbstractScriptComponentFunction() {
+
+    private val log = LoggerFactory.getLogger(SimpleStatusCheck::class.java)!!
+
+    override fun getName(): String {
+        return "SimpleStatusCheck"
+    }
+
+    override suspend fun processNB(executionRequest: ExecutionServiceInput) {
+        log.info("SIMPLE STATUS CHECK - START")
+
+        val configValueSetup: ObjectNode = getDynamicProperties("config-deploy-setup") as ObjectNode
+
+        val bluePrintPropertiesService: BlueprintPropertiesService =
+                this.functionDependencyInstanceAsType("blueprintPropertiesService")
+
+        val k8sConfiguration = K8sConnectionPluginConfiguration(bluePrintPropertiesService)
+
+        var instanceApi = K8sPluginInstanceApi(k8sConfiguration)
+
+        var checkCount: Int = 30 // in the future to be read in from the input
+        while(checkCount > 0) {
+            var continueCheck = false
+            configValueSetup.fields().forEach { it ->
+                val vfModuleName = it.key
+                val instanceName = it.value.get("k8s-instance-id").asText()
+
+                var instanceStatus: K8sRbInstanceStatus? = instanceApi.getInstanceStatus(instanceName)
+                instanceStatus?.resourcesStatus?.forEach {
+                    if (it.gvk?.kind == "Pod") {
+                        var version = it.gvk?.version!!
+                        if (it.gvk?.group!! != "")
+                            version = "${it.gvk?.group}/$version"
+                        // val podStatus = instanceApi.queryInstanceStatus(instanceName, it.gvk?.kind!!, version, it.name, null)
+                        // log.info(podStatus.toString())
+                        var podState = it.status?.get("status") as Map<String, Object>
+
+                        if ((podState?.get("phase") as String) != "Running") {
+                            continueCheck = true
+                            log.info("Pod ${it?.name} [$vfModuleName] has invalid state ${(podState?.get("phase"))}")
+                        }
+                    }
+                }
+            }
+            if (continueCheck) {
+                checkCount--
+                if (checkCount == 0)
+                    throw BlueprintException("Pods State verification failed")
+                Thread.sleep(10000L)
+            } else
+                checkCount = 0
+        }
+
+        log.info("SIMPLE STATUS CHECK - END")
+    }
+
+    override suspend fun recoverNB(runtimeException: RuntimeException, executionRequest: ExecutionServiceInput) {
+        log.info("Executing Recovery")
+        this.addError("${runtimeException.message}")
+    }
+}