Migrate ccsdk/apps to ccsdk/cds
[ccsdk/cds.git] / ms / blueprintsprocessor / functions / netconf-executor / src / test / kotlin / org / onap / ccsdk / cds / blueprintsprocessor / functions / netconf / executor / mocks / NetconfSubsystemFactory.kt
1 /*
2  * Copyright © 2017-2018 AT&T Intellectual Property.
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 package org.onap.ccsdk.cds.blueprintsprocessor.functions.netconf.executor.utils
17
18
19 import java.io.IOException
20 import java.io.InputStream
21 import java.io.OutputStream
22 import org.apache.sshd.common.NamedFactory;
23 import org.apache.sshd.server.Command;
24 import org.apache.sshd.server.Environment;
25 import org.apache.sshd.server.ExitCallback;
26
27
28 class NetconfSubsystemFactory : NamedFactory<Command> {
29
30     private val END_CHAR_SEQUENCE = "]]>]]>"
31
32     override fun create(): Command {
33         return NetconfSubsystem()
34     }
35
36     override fun getName(): String {
37         return "netconf"
38     }
39
40     /**
41      * Simple implementation of netconf reading 1 request, sending a 'hello' response and quitting
42      */
43     inner class NetconfSubsystem : Command {
44         private var input: InputStream? = null
45         private var out: OutputStream? = null
46         private var clientThread: Thread? = null
47         private var r: Int = 0
48
49         @Throws(IOException::class)
50         override fun start(env: Environment) {
51             clientThread = Thread(object : Runnable {
52
53                 override fun run() {
54                     try {
55                         val message = StringBuilder()
56                         while (true) {
57                             process(createHelloString())
58                             r = input!!.read()
59                             if (r == -1) {
60                                 break
61                             } else {
62                                 val c = r.toChar()
63                                 message.append(c)
64                                 val messageString = message.toString()
65                                 if (messageString.endsWith(END_CHAR_SEQUENCE)) {
66                                     println("Detected end message:\n$messageString")
67                                     process(createHelloString())
68                                     message.setLength(0)
69                                     break
70                                 }
71                             }
72                         }
73                     } catch (e: IOException) {
74                         e.printStackTrace()
75                     }
76
77                 }
78
79                 @Throws(IOException::class)
80                 private fun process(xmlMessage: String) {
81                     println("Sending message:\n$xmlMessage")
82                     out!!.write(xmlMessage.toByteArray(charset("UTF-8")))
83                     out!!.write((END_CHAR_SEQUENCE + "\n").toByteArray(charset("UTF-8")))
84                     out!!.flush()
85                 }
86
87                 private fun createHelloString(): String {
88                     val sessionId = "" + (Math.random() * Integer.MAX_VALUE).toInt()
89                     return ("<hello xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n"
90                             + "<capabilities>\n<capability>urn:ietf:params:netconf:base:1.0</capability>\n"
91                             + "<capability>urn:ietf:params:netconf:base:1.1</capability>\n</capabilities>\n"
92                             + "<session-id>" + sessionId + "</session-id>\n</hello>")
93                 }
94             })
95
96             clientThread!!.start()
97         }
98
99         @Throws(Exception::class)
100         override fun destroy() {
101             try {
102                 clientThread!!.join(2000)
103             } catch (e: InterruptedException) {
104                 // log.warn("Error joining Client thread" + e.getMessage());
105             }
106
107             clientThread!!.interrupt()
108         }
109
110         override fun setInputStream(input: InputStream) {
111             this.input = input
112         }
113
114         override fun setOutputStream(out: OutputStream) {
115             this.out = out
116         }
117
118         override fun setErrorStream(err: OutputStream) {}
119
120         override fun setExitCallback(callback: ExitCallback) {}
121
122
123
124     }
125 }