Adding TestVNF netconf server
[demo.git] / vnfs / TestVNF / netconfserver / src / main / java / com / ericsson / testvnf / server / netconf / NetconfSubsystem.java
1 /*
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
8  * 
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package com.ericsson.testvnf.server.netconf;
22
23 import java.io.IOException;
24 import java.io.InputStream;
25 import java.io.OutputStream;
26 import java.util.HashMap;
27 import java.util.Map;
28
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;
35
36 /*
37  * NetconfSubsystem class 
38  */
39 public class NetconfSubsystem implements Command {
40
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;
48         
49         public NetconfSubsystem(Map<String, Boolean> connectionResetMap) {
50                 this.connectionResetMap = connectionResetMap;
51         }
52
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();
58         }
59
60         public void destroy() {
61                 netconfHandler.interruptThreads();
62                 try {
63                         netconfHandlerThread.join(2000);
64                 } catch (InterruptedException e) {
65                         log.info("netconfHandler thread joining failed." + e.getMessage());
66                         Thread.currentThread().interrupt();
67                 }
68                 netconfHandlerThread.interrupt();
69                 log.info("Netconf Subsystem destroyed");
70         }
71
72         public static class Factory implements NamedFactory<Command> {
73
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<>();
77
78                 public static Factory createFactory() {
79                         return new Factory();
80                 }
81
82                 public String getName() {
83                         return "netconf";
84                 }
85                 
86                 public Command create() {
87                         log.info("Creating subsystem for netconf");
88                         return new NetconfSubsystem(connectionResetMap);
89                 }
90                 
91         }
92         
93         public InputStream getInputStream() {
94                 return in;
95         }
96
97         public void setInputStream(InputStream in) {
98                 this.in = in;
99         }
100
101         public OutputStream getOutputStream() {
102                 return out;
103         }
104
105         public void setOutputStream(OutputStream out) {
106                 this.out = out;
107         }
108
109         public OutputStream getErrorStream() {
110                 return error;
111         }
112
113         public void setErrorStream(OutputStream error) {
114                 this.error = error;
115         }
116
117         public void setExitCallback(ExitCallback callback) {
118                 //Set the callback that the shell has to call when it is closed.
119         }
120 }