Update INFO.yaml with new PTL
[demo.git] / tutorials / ApacheCNF / 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("STEP ${executionRequest.stepData?.name} - START")
39
40         val configValueSetup: ObjectNode = getDynamicProperties("config-deploy-setup") as ObjectNode
41         var checkCount: Int = getDynamicProperties("status-check-max-count").asInt()
42
43         val bluePrintPropertiesService: BluePrintPropertiesService =
44             this.functionDependencyInstanceAsType("bluePrintPropertiesService")
45
46         val k8sConfiguration = K8sConnectionPluginConfiguration(bluePrintPropertiesService)
47
48         val instanceApi = K8sPluginInstanceApi(k8sConfiguration)
49
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                 log.debug("Get status for $vfModuleName ($instanceName)")
58                 if (!instanceStatus?.ready!!) {
59                     continueCheck = true
60                     log.info("VfModule $vfModuleName ($instanceName) is not ready. Please wait...")
61                 } else {
62                     log.info("VfModule $vfModuleName ($instanceName) is ready.")
63                 }
64             }
65             if (continueCheck) {
66                 checkCount--
67                 if (checkCount == 0)
68                     throw BluePrintException("Instance State verification failed")
69                 Thread.sleep(10000L)
70             } else
71                 checkCount = 0
72         }
73
74         log.info("STEP ${executionRequest.stepData?.name} - STOP")
75     }
76
77     override suspend fun recoverNB(runtimeException: RuntimeException, executionRequest: ExecutionServiceInput) {
78         this.addError("${runtimeException.message}")
79     }
80 }