diff options
Diffstat (limited to 'tutorials/ApacheCNF/templates/cba/Scripts/kotlin')
7 files changed, 494 insertions, 0 deletions
diff --git a/tutorials/ApacheCNF/templates/cba/Scripts/kotlin/CollectorScript.kt b/tutorials/ApacheCNF/templates/cba/Scripts/kotlin/CollectorScript.kt new file mode 100644 index 00000000..589b9342 --- /dev/null +++ b/tutorials/ApacheCNF/templates/cba/Scripts/kotlin/CollectorScript.kt @@ -0,0 +1,53 @@ +/* + * Copyright © 2021 Bell Canada. + * + * 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.JsonNode +import org.onap.ccsdk.cds.blueprintsprocessor.core.api.data.ExecutionServiceInput +import org.onap.ccsdk.cds.blueprintsprocessor.services.execution.AbstractScriptComponentFunction +import org.onap.ccsdk.cds.blueprintsprocessor.services.execution.ComponentScriptExecutor +import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintProcessorException +import org.onap.ccsdk.cds.controllerblueprints.core.asJsonNode +import org.onap.ccsdk.cds.controllerblueprints.core.logger + +open class CollectorScript : AbstractScriptComponentFunction() { + + private val log = logger(CollectorScript::class) + + override suspend fun processNB(executionRequest: ExecutionServiceInput) { + bluePrintRuntimeService.bluePrintContext() + .serviceTemplate.topologyTemplate!!.nodeTemplates!! + .keys.filter { it.startsWith("execute-script") } + .associateWith { responseData(it) } + .let { it.asJsonNode() } + .also { log.info("Collected results: $it") } + .let { setAttribute(ComponentScriptExecutor.ATTRIBUTE_RESPONSE_DATA, it) } + } + + private fun responseData(nodeTemplateName: String): JsonNode? { + return try { + bluePrintRuntimeService.getNodeTemplateAttributeValue( + nodeTemplateName, + ComponentScriptExecutor.ATTRIBUTE_RESPONSE_DATA + ) + } catch (exception: BluePrintProcessorException) { null } + } + + override suspend fun recoverNB(runtimeException: RuntimeException, executionRequest: ExecutionServiceInput) { + addError(runtimeException.message ?: "Failed without error message") + } +} diff --git a/tutorials/ApacheCNF/templates/cba/Scripts/kotlin/ConfigDeploy.kt b/tutorials/ApacheCNF/templates/cba/Scripts/kotlin/ConfigDeploy.kt new file mode 100644 index 00000000..96b3c515 --- /dev/null +++ b/tutorials/ApacheCNF/templates/cba/Scripts/kotlin/ConfigDeploy.kt @@ -0,0 +1,48 @@ +/* + * 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 org.onap.ccsdk.cds.blueprintsprocessor.core.api.data.ExecutionServiceInput +import org.onap.ccsdk.cds.blueprintsprocessor.functions.resource.resolution.storedContentFromResolvedArtifactNB +import org.onap.ccsdk.cds.blueprintsprocessor.services.execution.AbstractScriptComponentFunction +import org.slf4j.LoggerFactory + +open class ConfigDeploy : AbstractScriptComponentFunction() { + + private val log = LoggerFactory.getLogger(ConfigDeploy::class.java)!! + + override fun getName(): String { + return "ConfigDeploy" + } + + override suspend fun processNB(executionRequest: ExecutionServiceInput) { + val resolutionKey = getDynamicProperties("resolution-key").asText() + log.info("Got the resolution_key: $resolutionKey from config-deploy going to retrieve the data from DB") + val prefix = "config-deploy" // used in the config-assign resolution + + val payload = storedContentFromResolvedArtifactNB(resolutionKey, prefix) + log.info("cnf configuration data from DB : \n$payload\n") + + println("Run config-deploy") + println("$payload") + } + + override suspend fun recoverNB(runtimeException: RuntimeException, executionRequest: ExecutionServiceInput) { + log.info("Executing Recovery") + this.addError("${runtimeException.message}") + } +} diff --git a/tutorials/ApacheCNF/templates/cba/Scripts/kotlin/ConfigDeploySetup.kt b/tutorials/ApacheCNF/templates/cba/Scripts/kotlin/ConfigDeploySetup.kt new file mode 100644 index 00000000..77d86d04 --- /dev/null +++ b/tutorials/ApacheCNF/templates/cba/Scripts/kotlin/ConfigDeploySetup.kt @@ -0,0 +1,145 @@ +/* + * 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.JsonNode +import com.fasterxml.jackson.databind.node.ObjectNode +import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper +import org.onap.ccsdk.cds.blueprintsprocessor.functions.k8s.definition.template.K8sConfigTemplateComponent +import org.onap.ccsdk.cds.blueprintsprocessor.functions.k8s.definition.template.K8sConfigValueComponent +import org.onap.ccsdk.cds.blueprintsprocessor.functions.resource.resolution.processor.ResourceAssignmentProcessor +import org.onap.ccsdk.cds.blueprintsprocessor.functions.resource.resolution.utils.ResourceAssignmentUtils +import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintProcessorException +import org.onap.ccsdk.cds.controllerblueprints.core.isNullOrMissing +import org.onap.ccsdk.cds.controllerblueprints.resource.dict.ResourceAssignment +import org.slf4j.LoggerFactory + +open class ConfigDeploySetup() : ResourceAssignmentProcessor() { + + private val log = LoggerFactory.getLogger(ConfigDeploySetup::class.java)!! + + override fun getName(): String { + return "ConfigDeploySetup" + } + + override suspend fun processNB(executionRequest: ResourceAssignment) { + + var retValue: Any? = null + + try { + if (executionRequest.name == "service-instance-id") { + var value = raRuntimeService.getInputValue(executionRequest.name) + if (!value.isNullOrMissing()) { + retValue = value.asText() + } else { + val vnfRelationshipList = raRuntimeService.getResolutionStore("vnf-relationship-list") + if (!vnfRelationshipList.isNullOrMissing()) { + vnfRelationshipList["relationship-list"].forEach { relation -> + if (relation["related-to"].asText() == "service-instance") { + relation["relationship-data"].forEach { data -> + if (data["relationship-key"].asText() == "service-instance.service-instance-id") { + retValue = data["relationship-value"].asText() + } + } + } + } + } + } + } else if (executionRequest.name == "vnf-id") { + var value = raRuntimeService.getInputValue(executionRequest.name) + if (!value.isNullOrMissing()) { + retValue = value.asText() + } else { + value = raRuntimeService.getInputValue("generic-vnf.vnf-id") + if (!value.isNullOrMissing()) { + retValue = value.asText() + } + } + } else if (executionRequest.name == "replica-count") { + var value = raRuntimeService.getInputValue(executionRequest.name) + retValue = "1" + if (!value.isNullOrMissing()) { + retValue = value.asText() + } else { + value = raRuntimeService.getInputValue("data") + if (!value.isNullOrMissing()) { + if (value["replicaCount"] != null) { + retValue = value["replicaCount"].asText() + } + } + } + } else if (executionRequest.name == "config-deploy-setup") { + val modulesSdnc = raRuntimeService.getResolutionStore("vf-modules-list-sdnc")["vf-modules"] + val modulesAai = raRuntimeService.getResolutionStore("vf-modules-list-aai")["vf-modules"] + val objectMapper = jacksonObjectMapper() + val result: ObjectNode = objectMapper.createObjectNode() + for (module in modulesSdnc) { + val modelTopology = module.at("/vf-module-data/vf-module-topology") + val moduleParameters = modelTopology.at("/vf-module-parameters/param") + val label: String? = getParamValueByName(moduleParameters, "vf_module_label") + if (label != null) { + val modelInfo = modelTopology["onap-model-information"] + val moduleData: ObjectNode = objectMapper.createObjectNode() + result.put(label, moduleData) + moduleData.put(K8sConfigTemplateComponent.INPUT_K8S_DEFINITION_NAME, modelInfo["model-invariant-uuid"].asText()) + moduleData.put(K8sConfigTemplateComponent.INPUT_K8S_DEFINITION_VERSION, modelInfo["model-customization-uuid"].asText()) + val templateName: String? = getParamValueByName(moduleParameters, K8sConfigTemplateComponent.INPUT_K8S_TEMPLATE_NAME) + val templateSource: String? = getParamValueByName(moduleParameters, K8sConfigTemplateComponent.INPUT_K8S_TEMPLATE_SOURCE) + val configValueSource: String? = getParamValueByName(moduleParameters, K8sConfigValueComponent.INPUT_K8S_CONFIG_VALUE_SOURCE) + val configName: String? = getParamValueByName(moduleParameters, K8sConfigValueComponent.INPUT_K8S_RB_CONFIG_NAME) + + if (templateName != null) + moduleData.put(K8sConfigTemplateComponent.INPUT_K8S_TEMPLATE_NAME, templateName) + if (templateSource != null) + moduleData.put(K8sConfigTemplateComponent.INPUT_K8S_TEMPLATE_SOURCE, templateSource) + if (configValueSource != null) + moduleData.put(K8sConfigValueComponent.INPUT_K8S_CONFIG_VALUE_SOURCE, configValueSource) + if (configName != null) + moduleData.put(K8sConfigValueComponent.INPUT_K8S_RB_CONFIG_NAME, configName) + + for (aaiModule in modulesAai) { + if (aaiModule["vf-module-id"].asText() == module["vf-module-id"].asText() && aaiModule["heat-stack-id"] != null) { + moduleData.put(K8sConfigValueComponent.INPUT_K8S_INSTANCE_ID, aaiModule["heat-stack-id"].asText()) + break + } + } + } + } + retValue = result + } + ResourceAssignmentUtils.setResourceDataValue(executionRequest, raRuntimeService, retValue) + } catch (e: Exception) { + log.error(e.message, e) + ResourceAssignmentUtils.setResourceDataValue(executionRequest, raRuntimeService, "ERROR") + + throw BluePrintProcessorException("Failed in template key ($executionRequest) assignments, cause: ${e.message}", e) + } + } + + private fun getParamValueByName(params: JsonNode, paramName: String): String? { + for (param in params) { + if (param["name"].asText() == paramName && param["value"].asText() != "null") { + return param["value"].asText() + } + } + return null + } + + override suspend fun recoverNB(runtimeException: RuntimeException, executionRequest: ResourceAssignment) { + this.addError("${runtimeException.message}") + } +} diff --git a/tutorials/ApacheCNF/templates/cba/Scripts/kotlin/K8sHealthCheck.kt b/tutorials/ApacheCNF/templates/cba/Scripts/kotlin/K8sHealthCheck.kt new file mode 100644 index 00000000..dd87c6f5 --- /dev/null +++ b/tutorials/ApacheCNF/templates/cba/Scripts/kotlin/K8sHealthCheck.kt @@ -0,0 +1,125 @@ +/* + * Copyright (C) 2021 Samsung Electronics + * 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 kotlinx.coroutines.Job +import kotlinx.coroutines.cancel +import kotlinx.coroutines.delay +import kotlinx.coroutines.joinAll +import kotlinx.coroutines.launch +import kotlinx.coroutines.runBlocking +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.healthcheck.K8sRbInstanceHealthCheck +import org.onap.ccsdk.cds.blueprintsprocessor.functions.k8s.instance.healthcheck.K8sRbInstanceHealthCheckSimple +import org.onap.ccsdk.cds.blueprintsprocessor.services.execution.AbstractScriptComponentFunction +import org.slf4j.LoggerFactory + +open class K8sHealthCheck : AbstractScriptComponentFunction() { + + private val log = LoggerFactory.getLogger(K8sHealthCheck::class.java)!! + + override fun getName(): String { + return "K8sHealthCheck" + } + + private fun initPluginApi(): K8sPluginInstanceApi { + val bluePrintPropertiesService: BluePrintPropertiesService = this.functionDependencyInstanceAsType("bluePrintPropertiesService")!! + val k8sConfiguration = K8sConnectionPluginConfiguration(bluePrintPropertiesService) + + return K8sPluginInstanceApi(k8sConfiguration) + } + + override suspend fun processNB(executionRequest: ExecutionServiceInput) { + val instanceApi = initPluginApi() + + log.info("Health check script execution - START") + val configValueSetup: ObjectNode = getDynamicProperties("config-deploy-setup") as ObjectNode + log.info("Config Value Setup: $configValueSetup") + + val instanceHealthCheckList = startInstanceHealthCheck(configValueSetup, instanceApi) + val statuses = getStatuses(instanceHealthCheckList, instanceApi) + log.info("Health check script execution - END") + } + + private fun startInstanceHealthCheck(configValueSetup: ObjectNode, instanceApi: K8sPluginInstanceApi): List<HealthCheckInstance> { + val healthCheckInstanceList = arrayListOf<HealthCheckInstance>() + + configValueSetup.fields().forEach { + val instanceName = it.value.get("k8s-instance-id").asText() + val response: K8sRbInstanceHealthCheckSimple? = instanceApi.startInstanceHealthCheck(instanceName) + log.debug("K8sRbInstanceHealthCheckSimple response: $$response") + healthCheckInstanceList.add(HealthCheckInstance(instanceName, response?.id)) + } + log.info("healthCheckInstanceList: $healthCheckInstanceList") + + return healthCheckInstanceList + } + + private fun getStatuses(instanceHealthCheckList: List<HealthCheckInstance>, instanceApi: K8sPluginInstanceApi): Map<String, String> { + val statuses = hashMapOf<String, String>() + runBlocking { + val jobs: List<Job> = instanceHealthCheckList.map { + launch { + log.info("Thread started: ${Thread.currentThread().name} for $it") + // WAIT APPROX 5 MINUTES + repeat(30) { _ -> + val response: K8sRbInstanceHealthCheck = instanceApi.getInstanceHealthCheck(it.heatStackId, it.healthCheckInstance!!)!! + log.debug("Response for $it: $response") + val status = response.status!! + if (!"RUNNING".equals(status, true)) { + statuses[it.heatStackId] = status + log.info("Poll status: $status for $it") + instanceApi.deleteInstanceHealthCheck(it.heatStackId, it.healthCheckInstance) + cancel() + } + delay(10_000L) + } + statuses[it.heatStackId] = "Timeout" + log.warn("Send delete hc request") + instanceApi.deleteInstanceHealthCheck(it.heatStackId, it.healthCheckInstance!!) + } + } + jobs.joinAll() + } + var success = true + statuses?.forEach { it -> + if (it.value != "Succeeded") { + success = false + } + } + log.info("---") + if (success) { + log.info("Healthcheck finished successfully") + } else { + log.info("Healthcheck finished with failure") + } + log.info("Detailed results: $statuses") + log.info("---") + return statuses + } + + data class HealthCheckInstance(val heatStackId: String, val healthCheckInstance: String?) { + override fun toString(): String { + return "HealthCheckInstance(heatStackId='$heatStackId', healthCheckInstance='$healthCheckInstance')" + } + } + + override suspend fun recoverNB(runtimeException: RuntimeException, executionRequest: ExecutionServiceInput) { + log.info("Executing Recovery") + bluePrintRuntimeService.getBluePrintError().addError("${runtimeException.message}", getName()) + } +} diff --git a/tutorials/ApacheCNF/templates/cba/Scripts/kotlin/README.md b/tutorials/ApacheCNF/templates/cba/Scripts/kotlin/README.md new file mode 100644 index 00000000..29b7978e --- /dev/null +++ b/tutorials/ApacheCNF/templates/cba/Scripts/kotlin/README.md @@ -0,0 +1 @@ +kotlin Folder
\ No newline at end of file diff --git a/tutorials/ApacheCNF/templates/cba/Scripts/kotlin/SimpleErrorCheck.kt b/tutorials/ApacheCNF/templates/cba/Scripts/kotlin/SimpleErrorCheck.kt new file mode 100644 index 00000000..8e4a58ab --- /dev/null +++ b/tutorials/ApacheCNF/templates/cba/Scripts/kotlin/SimpleErrorCheck.kt @@ -0,0 +1,41 @@ +/* + * 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 org.onap.ccsdk.cds.blueprintsprocessor.core.api.data.ExecutionServiceInput +import org.onap.ccsdk.cds.blueprintsprocessor.services.execution.AbstractScriptComponentFunction +import org.slf4j.LoggerFactory + +open class SimpleErrorCheck : AbstractScriptComponentFunction() { + + private val log = LoggerFactory.getLogger(SimpleErrorCheck::class.java)!! + + override fun getName(): String { + return "SimpleErrorCheck" + } + + override suspend fun processNB(executionRequest: ExecutionServiceInput) { + log.info("SIMPLE ERROR CHECK - START") + + log.info("SIMPLE ERROR CHECK - END") + } + + override suspend fun recoverNB(runtimeException: RuntimeException, executionRequest: ExecutionServiceInput) { + log.info("Executing Recovery") + this.addError("${runtimeException.message}") + } +} diff --git a/tutorials/ApacheCNF/templates/cba/Scripts/kotlin/SimpleStatusCheck.kt b/tutorials/ApacheCNF/templates/cba/Scripts/kotlin/SimpleStatusCheck.kt new file mode 100644 index 00000000..c99dcd4f --- /dev/null +++ b/tutorials/ApacheCNF/templates/cba/Scripts/kotlin/SimpleStatusCheck.kt @@ -0,0 +1,81 @@ +/* + * 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) + + val 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() + + val instanceStatus: K8sRbInstanceStatus? = instanceApi.getInstanceStatus(instanceName) + log.debug("Get status for $vfModuleName ($instanceName)") + if (!instanceStatus?.ready!!) { + continueCheck = true + log.info("VfModule $vfModuleName ($instanceName) is not ready. Please wait...") + } else { + log.info("VfModule $vfModuleName ($instanceName) is ready.") + } + } + if (continueCheck) { + checkCount-- + if (checkCount == 0) + throw BluePrintException("Instance 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}") + } +} |