Revert "Renaming Files having BluePrint to have Blueprint"
[ccsdk/cds.git] / components / model-catalog / blueprint-model / uat-blueprints / imperative_workflow / Scripts / kotlin / TestScript.kt
1 /*
2  *  Copyright © 20201 Bell Canada.
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 cba.cds.uat
18
19 import com.fasterxml.jackson.databind.JsonNode
20 import org.onap.ccsdk.cds.blueprintsprocessor.core.api.data.ExecutionServiceInput
21 import org.onap.ccsdk.cds.blueprintsprocessor.services.execution.AbstractScriptComponentFunction
22 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintException
23 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintProcessorException
24 import org.onap.ccsdk.cds.controllerblueprints.core.asJsonPrimitive
25 import org.onap.ccsdk.cds.controllerblueprints.core.logger
26
27 open class TestScript : AbstractScriptComponentFunction() {
28
29     private val log = logger(TestScript::class)
30
31     private val FAILED = "FAILED".asJsonPrimitive()
32     private val SUCCEEDED = "SUCCEEDED".asJsonPrimitive()
33
34     override suspend fun processNB(executionRequest: ExecutionServiceInput) {
35         val failingSteps = inputValue("failing-steps")
36         var shouldFail = false
37         if (failingSteps?.has(this.stepName) == true) {
38             shouldFail = failingSteps[this.stepName].asBoolean()
39         }
40         log.info("running step ${this.stepName}, should fail: $shouldFail")
41         setAttribute("response-data", if (shouldFail) FAILED else SUCCEEDED)
42
43         if (shouldFail) {
44             throw BluePrintException("Step failed: ${this.stepName}")
45         }
46     }
47
48     fun inputValue(name: String): JsonNode? {
49         return try {
50             return bluePrintRuntimeService.getInputValue(name)
51         } catch (e: BluePrintProcessorException) { null }
52     }
53
54     override suspend fun recoverNB(runtimeException: RuntimeException, executionRequest: ExecutionServiceInput) {
55         log.info("Executing Recovery for step ${this.stepName}")
56         addError(runtimeException.message ?: "Failed without error message")
57     }
58 }