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