2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2016-2018 Ericsson. All rights reserved.
4 * ================================================================================
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package com.ericsson.testvnf.server.netconf;
23 import java.io.IOException;
24 import java.io.InputStream;
25 import java.io.OutputStream;
26 import java.util.HashMap;
29 import org.apache.commons.logging.Log;
30 import org.apache.commons.logging.LogFactory;
31 import org.apache.sshd.common.NamedFactory;
32 import org.apache.sshd.server.Environment;
33 import org.apache.sshd.server.ExitCallback;
34 import org.apache.sshd.server.command.Command;
37 * NetconfSubsystem class
39 public class NetconfSubsystem implements Command {
41 private static final Log log = LogFactory.getLog(Factory.class);
42 private InputStream in;
43 private OutputStream out;
44 private OutputStream error;
45 private Thread netconfHandlerThread;
46 private Map<String, Boolean> connectionResetMap;
47 private NetconfHandler netconfHandler;
49 public NetconfSubsystem(Map<String, Boolean> connectionResetMap) {
50 this.connectionResetMap = connectionResetMap;
53 public void start(Environment env) throws IOException {
54 // initialize netconf handler
55 netconfHandler = new NetconfHandler(in, out, connectionResetMap);
56 netconfHandlerThread = new Thread(netconfHandler, "netconfHandler thread");
57 netconfHandlerThread.start();
60 public void destroy() {
61 netconfHandler.interruptThreads();
63 netconfHandlerThread.join(2000);
64 } catch (InterruptedException e) {
65 log.info("netconfHandler thread joining failed." + e.getMessage());
66 Thread.currentThread().interrupt();
68 netconfHandlerThread.interrupt();
69 log.info("Netconf Subsystem destroyed");
72 public static class Factory implements NamedFactory<Command> {
74 // a connectionResetMap is maintained for each running instance of a netconf system.
75 // this is a simple data structure to determine when sending events to a target needs to be terminated.
76 private static Map<String, Boolean> connectionResetMap = new HashMap<>();
78 public static Factory createFactory() {
82 public String getName() {
86 public Command create() {
87 log.info("Creating subsystem for netconf");
88 return new NetconfSubsystem(connectionResetMap);
93 public InputStream getInputStream() {
97 public void setInputStream(InputStream in) {
101 public OutputStream getOutputStream() {
105 public void setOutputStream(OutputStream out) {
109 public OutputStream getErrorStream() {
113 public void setErrorStream(OutputStream error) {
117 public void setExitCallback(ExitCallback callback) {
118 //Set the callback that the shell has to call when it is closed.