2 * Copyright © 2017-2018 AT&T Intellectual Property.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package org.onap.ccsdk.apps.blueprintsprocessor.functions.netconf.executor.utils
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;
28 class NetconfSubsystemFactory : NamedFactory<Command> {
30 private val END_CHAR_SEQUENCE = "]]>]]>"
32 override fun create(): Command {
33 return NetconfSubsystem()
36 override fun getName(): String {
41 * Simple implementation of netconf reading 1 request, sending a 'hello' response and quitting
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
49 @Throws(IOException::class)
50 override fun start(env: Environment) {
51 clientThread = Thread(object : Runnable {
55 val message = StringBuilder()
57 process(createHelloString())
64 val messageString = message.toString()
65 if (messageString.endsWith(END_CHAR_SEQUENCE)) {
66 println("Detected end message:\n$messageString")
67 process(createHelloString())
73 } catch (e: IOException) {
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")))
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>")
96 clientThread!!.start()
99 @Throws(Exception::class)
100 override fun destroy() {
102 clientThread!!.join(2000)
103 } catch (e: InterruptedException) {
104 // log.warn("Error joining Client thread" + e.getMessage());
107 clientThread!!.interrupt()
110 override fun setInputStream(input: InputStream) {
114 override fun setOutputStream(out: OutputStream) {
118 override fun setErrorStream(err: OutputStream) {}
120 override fun setExitCallback(callback: ExitCallback) {}