Real vFW CNF config-assign and config-deploy
[demo.git] / heat / vFW_CNF_CDS / templates / cba / Scripts / kotlin / SimpleStatusCheck.kt
1 /*
2  * Copyright © 2021 Orange
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package org.onap.ccsdk.cds.blueprintsprocessor.services.execution.scripts
18
19 import com.fasterxml.jackson.databind.node.ObjectNode
20 import org.onap.ccsdk.cds.blueprintsprocessor.core.BlueprintPropertiesService
21 import org.onap.ccsdk.cds.blueprintsprocessor.core.api.data.ExecutionServiceInput
22 import org.onap.ccsdk.cds.blueprintsprocessor.functions.k8s.K8sConnectionPluginConfiguration
23 import org.onap.ccsdk.cds.blueprintsprocessor.functions.k8s.instance.K8sPluginInstanceApi
24 import org.onap.ccsdk.cds.blueprintsprocessor.functions.k8s.instance.K8sRbInstanceStatus
25 import org.onap.ccsdk.cds.blueprintsprocessor.services.execution.AbstractScriptComponentFunction
26 import org.onap.ccsdk.cds.controllerblueprints.core.BlueprintException
27 import org.slf4j.LoggerFactory
28
29 open class SimpleStatusCheck : AbstractScriptComponentFunction() {
30
31     private val log = LoggerFactory.getLogger(SimpleStatusCheck::class.java)!!
32
33     override fun getName(): String {
34         return "SimpleStatusCheck"
35     }
36
37     override suspend fun processNB(executionRequest: ExecutionServiceInput) {
38         log.info("SIMPLE STATUS CHECK - START")
39
40         val configValueSetup: ObjectNode = getDynamicProperties("config-deploy-setup") as ObjectNode
41
42         val bluePrintPropertiesService: BlueprintPropertiesService =
43                 this.functionDependencyInstanceAsType("blueprintPropertiesService")
44
45         val k8sConfiguration = K8sConnectionPluginConfiguration(bluePrintPropertiesService)
46
47         val instanceApi = K8sPluginInstanceApi(k8sConfiguration)
48
49         var checkCount: Int = 30 // in the future to be read in from the input
50         while (checkCount > 0) {
51             var continueCheck = false
52             configValueSetup.fields().forEach { it ->
53                 val vfModuleName = it.key
54                 val instanceName = it.value.get("k8s-instance-id").asText()
55
56                 val instanceStatus: K8sRbInstanceStatus? = instanceApi.getInstanceStatus(instanceName)
57                 instanceStatus?.resourcesStatus?.forEach {
58                     if (it.gvk?.kind == "Pod") {
59                         var version = it.gvk?.version!!
60                         if (it.gvk?.group!! != "")
61                             version = "${it.gvk?.group}/$version"
62                         // val podStatus = instanceApi.queryInstanceStatus(instanceName, it.gvk?.kind!!, version, it.name, null)
63                         // log.info(podStatus.toString())
64                         val podState = it.status?.get("status") as Map<String, Object>
65
66                         if ((podState["phase"] as String) != "Running") {
67                             continueCheck = true
68                             log.info("Pod ${it.name} [$vfModuleName] has invalid state ${(podState["phase"])}")
69                         }
70                     }
71                 }
72             }
73             if (continueCheck) {
74                 checkCount--
75                 if (checkCount == 0)
76                     throw BlueprintException("Pods State verification failed")
77                 Thread.sleep(10000L)
78             } else
79                 checkCount = 0
80         }
81
82         log.info("SIMPLE STATUS CHECK - END")
83     }
84
85     override suspend fun recoverNB(runtimeException: RuntimeException, executionRequest: ExecutionServiceInput) {
86         log.info("Executing Recovery")
87         this.addError("${runtimeException.message}")
88     }
89 }