2 * Copyright © 2021 Orange
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package org.onap.ccsdk.cds.blueprintsprocessor.services.execution.scripts
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
29 open class SimpleStatusCheck : AbstractScriptComponentFunction() {
31 private val log = LoggerFactory.getLogger(SimpleStatusCheck::class.java)!!
33 override fun getName(): String {
34 return "SimpleStatusCheck"
37 override suspend fun processNB(executionRequest: ExecutionServiceInput) {
38 log.info("SIMPLE STATUS CHECK - START")
40 val configValueSetup: ObjectNode = getDynamicProperties("config-deploy-setup") as ObjectNode
42 val bluePrintPropertiesService: BlueprintPropertiesService =
43 this.functionDependencyInstanceAsType("blueprintPropertiesService")
45 val k8sConfiguration = K8sConnectionPluginConfiguration(bluePrintPropertiesService)
47 val instanceApi = K8sPluginInstanceApi(k8sConfiguration)
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()
56 val instanceStatus: K8sRbInstanceStatus? = instanceApi.getInstanceStatus(instanceName)
57 log.debug("Get status for $instanceName")
59 instanceStatus?.resourcesStatus?.forEach {
60 log.debug("Resource: name=$it.name kind=$it.gvk.kind group=$it.gvk.group version=$it.gvk.version")
61 if (it.gvk?.kind == "Pod") {
62 var version = it.gvk?.version!!
63 if (it.gvk?.group!! != "")
64 version = "${it.gvk?.group}/$version"
65 // val podStatus = instanceApi.queryInstanceStatus(instanceName, it.gvk?.kind!!, version, it.name, null)
66 // log.info(podStatus.toString())
67 val podState = it.status?.get("status") as Map<String, Object>
68 status = podState["phase"] as String
69 if (status != "Running") {
71 log.info("Pod ${it.name} [$vfModuleName] has INVALID state ${(podState["phase"])}")
73 log.info("Pod ${it.name} [$vfModuleName] has VALID state ${(podState["phase"])}")
81 throw BlueprintException("Pods State verification failed")
87 log.info("SIMPLE STATUS CHECK - END SUCCESS")
90 override suspend fun recoverNB(runtimeException: RuntimeException, executionRequest: ExecutionServiceInput) {
91 log.info("Executing Recovery")
92 this.addError("${runtimeException.message}")