cf27cc2defe98392107f80d4a9b566b009816db4
[ccsdk/cds.git] / ms / blueprintsprocessor / functions / cli-executor / src / main / kotlin / internal / scripts / InternalSimpleCli.kt
1 /*
2  *  Copyright © 2019 IBM.
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 @file:Suppress("unused")
18
19 package internal.scripts
20
21 import org.onap.ccsdk.cds.blueprintsprocessor.core.api.data.ExecutionServiceInput
22 import org.onap.ccsdk.cds.blueprintsprocessor.services.execution.AbstractScriptComponentFunction
23 import org.onap.ccsdk.cds.blueprintsprocessor.services.execution.ComponentScriptExecutor
24 import org.onap.ccsdk.cds.blueprintsprocessor.ssh.sshClientService
25 import org.onap.ccsdk.cds.controllerblueprints.core.asJsonPrimitive
26 import org.onap.ccsdk.cds.controllerblueprints.core.service.BluePrintDependencyService
27 import org.slf4j.LoggerFactory
28
29 open class TestCliScriptFunction : AbstractScriptComponentFunction() {
30
31     private val log = LoggerFactory.getLogger(TestCliScriptFunction::class.java)!!
32
33     override fun getName(): String {
34         return "TestCliScriptFunction"
35     }
36
37     override suspend fun processNB(executionRequest: ExecutionServiceInput) {
38         log.info("Executing process ...")
39     }
40
41     override suspend fun recoverNB(runtimeException: RuntimeException, executionRequest: ExecutionServiceInput) {
42         log.info("Executing Recovery")
43     }
44 }
45
46
47 open class Check : AbstractScriptComponentFunction() {
48
49     private val log = LoggerFactory.getLogger(AbstractScriptComponentFunction::class.java)!!
50
51     override fun getName(): String {
52         return "Check"
53     }
54
55     override suspend fun processNB(executionRequest: ExecutionServiceInput) {
56         // Get the Device Information from the DSL Model
57         val deviceInformation = bluePrintRuntimeService.resolveDSLExpression("device-properties")
58
59         // Get the Client Service
60         val sshClientService = BluePrintDependencyService.sshClientService(deviceInformation)
61
62         sshClientService.startSessionNB()
63
64         // Read Commands
65         val commands = readLinesFromArtifact("command-template")
66
67         // Execute multiple Commands
68         val responseLog = sshClientService.executeCommandsNB(commands, 5000)
69
70         // Close Session
71         sshClientService.closeSessionNB()
72
73         // Set the Response Data
74         setAttribute(ComponentScriptExecutor.ATTRIBUTE_RESPONSE_DATA, responseLog.asJsonPrimitive())
75
76         log.info("Executing process")
77     }
78
79     override suspend fun recoverNB(runtimeException: RuntimeException, executionRequest: ExecutionServiceInput) {
80         log.info("Executing Recovery")
81     }
82 }