9d308202f3889cf9fd7f68d51d20adf991df6429
[ccsdk/cds.git] / ms / blueprintsprocessor / modules / commons / ssh-lib / src / test / kotlin / org / onap / ccsdk / cds / blueprintsprocessor / ssh / service / echoShell / EchoShellFactory.kt
1 /*
2  *  Copyright © 2019 IBM. 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 org.onap.ccsdk.cds.blueprintsprocessor.ssh.service.echoShell
18
19 import org.apache.sshd.common.Factory
20 import org.apache.sshd.server.Environment
21 import org.apache.sshd.server.command.Command
22 import org.apache.sshd.server.ExitCallback
23 import java.io.InputStream
24 import java.io.OutputStream
25 import java.io.IOException
26 import java.io.BufferedReader
27 import java.io.InputStreamReader
28 import java.io.InterruptedIOException
29
30 class EchoShellFactory : Factory<Command> {
31
32     override fun create(): Command {
33         return EchoShell()
34     }
35
36     companion object {
37         val INSTANCE = EchoShellFactory()
38     }
39 }
40
41 class EchoShell : Command, Runnable {
42
43     var `in`: InputStream? = null
44         private set
45     var out: OutputStream? = null
46         private set
47     var err: OutputStream? = null
48         private set
49     private var callback: ExitCallback? = null
50     var environment: Environment? = null
51         private set
52     private var thread: Thread? = null
53
54     override fun setInputStream(`in`: InputStream) {
55         this.`in` = `in`
56     }
57
58     override fun setOutputStream(out: OutputStream) {
59         this.out = out
60     }
61
62     override fun setErrorStream(err: OutputStream) {
63         this.err = err
64     }
65
66     override fun setExitCallback(callback: ExitCallback) {
67         this.callback = callback
68     }
69
70     @Throws(IOException::class)
71     override fun start(env: Environment) {
72         environment = env
73         thread = Thread(this, "EchoShell")
74         thread!!.isDaemon = true
75         thread!!.start()
76     }
77
78     override fun destroy() {
79         thread!!.interrupt()
80     }
81
82     override fun run() {
83         val r = BufferedReader(InputStreamReader(`in`))
84         try {
85             while (true) {
86                 val s = r.readLine() ?: return
87                 out!!.write((s + "\n").toByteArray())
88                 out!!.write("#".toByteArray())
89                 out!!.flush()
90                 if ("exit" == s) {
91                     return
92                 }
93             }
94         } catch (e: InterruptedIOException) {
95             // Ignore
96         } catch (e: Exception) {
97             e.printStackTrace()
98         } finally {
99             callback!!.onExit(0)
100         }
101     }
102 }