Fix for APPC-1271
[appc.git] / appc-adapters / appc-netconf-adapter / appc-netconf-adapter-bundle / src / main / java / org / onap / appc / adapter / netconf / jsch / NetconfClientJsch.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
4  * ================================================================================
5  * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Copyright (C) 2017 Amdocs
8  * ================================================================================
9  * Modifications Copyright (C) 2018 Ericsson
10  * =============================================================================
11  * Licensed under the Apache License, Version 2.0 (the "License");
12  * you may not use this file except in compliance with the License.
13  * You may obtain a copy of the License at
14  * 
15  *      http://www.apache.org/licenses/LICENSE-2.0
16  * 
17  * Unless required by applicable law or agreed to in writing, software
18  * distributed under the License is distributed on an "AS IS" BASIS,
19  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20  * See the License for the specific language governing permissions and
21  * limitations under the License.
22  * 
23  * ============LICENSE_END=========================================================
24  */
25
26 package org.onap.appc.adapter.netconf.jsch;
27
28 import com.jcraft.jsch.Channel;
29 import com.jcraft.jsch.ChannelSubsystem;
30 import com.jcraft.jsch.JSch;
31 import com.jcraft.jsch.Session;
32
33 import java.io.IOException;
34 import java.io.InputStream;
35 import java.io.OutputStream;
36 import java.util.List;
37 import java.util.Properties;
38
39 import org.onap.appc.adapter.netconf.NetconfClient;
40 import org.onap.appc.adapter.netconf.NetconfConnectionDetails;
41 import org.onap.appc.adapter.netconf.internal.NetconfAdapter;
42 import org.onap.appc.adapter.netconf.internal.NetconfConstMessages;
43 import org.onap.appc.encryption.EncryptionTool;
44 import org.onap.appc.exceptions.APPCException;
45 import org.onap.appc.i18n.Msg;
46 import com.att.eelf.i18n.EELFResourceManager;
47
48 /**
49  * Implementation of NetconfClient interface based on JCraft jsch library.
50  */
51 public class NetconfClientJsch implements NetconfClient {
52
53     private static final int SESSION_CONNECT_TIMEOUT = 30000;
54     private static final int CHANNEL_CONNECT_TIMEOUT = 10000;
55
56     private Session session;
57     private Channel channel;
58     private NetconfAdapter netconfAdapter;
59
60
61     @Override
62     public void connect(NetconfConnectionDetails connectionDetails) throws APPCException {
63         String host = connectionDetails.getHost();
64         int port = connectionDetails.getPort();
65         String username = connectionDetails.getUsername();
66         String password = connectionDetails.getPassword();
67         try {
68             JSch.setLogger(new JSchLogger());
69             JSch jsch = getJSch();
70             session = jsch.getSession(EncryptionTool.getInstance().decrypt(username), host, port);
71             session.setPassword(EncryptionTool.getInstance().decrypt(password));
72             session.setConfig("StrictHostKeyChecking", "no");
73
74             Properties additionalProps = connectionDetails.getAdditionalProperties();
75             if((additionalProps != null) && !additionalProps.isEmpty()) {
76                 session.setConfig(additionalProps);
77             }
78
79             session.connect(SESSION_CONNECT_TIMEOUT);
80             session.setTimeout(10000);
81
82             createConnection(connectionDetails);
83
84         } catch(Exception e) {
85             String message = EELFResourceManager.format(Msg.CANNOT_ESTABLISH_CONNECTION, host, String.valueOf(port), username);
86             throw new APPCException(message, e);
87         }
88     }
89
90     @Override
91     public String exchangeMessage(String message) throws APPCException {
92         try {
93             netconfAdapter.sendMessage(message);
94             return netconfAdapter.receiveMessage();
95         } catch(IOException e) {
96             throw new APPCException(e);
97         }
98     }
99
100     @Override
101     public void configure(String configuration) throws APPCException {
102         try {
103             isOk(exchangeMessage(configuration));
104         } catch(IOException e) {
105             throw new APPCException(e);
106         }
107     }
108
109     @Override
110     public String getConfiguration() throws APPCException {
111         return exchangeMessage(NetconfConstMessages.GET_RUNNING_CONFIG);
112     }
113
114     @Override
115     public void disconnect() {
116         try {
117             if((channel != null) && !channel.isClosed()) {
118                 netconfAdapter.sendMessage(NetconfConstMessages.CLOSE_SESSION);
119                 isOk(netconfAdapter.receiveMessage());
120             }
121         } catch(IOException e) {
122             throw new RuntimeException("Error closing netconf device", e);
123         } finally {
124             netconfAdapter = null;
125             if(channel != null) {
126                 channel.disconnect();
127                 channel = null;
128             }
129             if(session != null) {
130                 session.disconnect();
131                 session = null;
132             }
133         }
134     }
135
136     private void createConnection(NetconfConnectionDetails connectionDetails) throws APPCException {
137         try {
138             channel = session.openChannel("subsystem");
139             ((ChannelSubsystem)channel).setSubsystem("netconf");
140             netconfAdapter = getNetconfAdapter(channel.getInputStream(), channel.getOutputStream());
141             channel.connect(CHANNEL_CONNECT_TIMEOUT);
142             hello(connectionDetails.getCapabilities());
143         } catch(Exception e) {
144             disconnect();
145             throw new APPCException(e);
146         }
147     }
148
149     private void hello(List<String> capabilities) throws IOException {
150         String helloIn = netconfAdapter.receiveMessage();
151         if(helloIn == null) {
152             throw new IOException("Expected hello message, but nothing received from netconf device");
153         }
154         if(helloIn.contains("<rpc-error>")) {
155             throw new IOException("Expected hello message, but received error from netconf device:\n" + helloIn);
156         }
157         StringBuilder sb = new StringBuilder();
158         sb.append(NetconfConstMessages.CAPABILITIES_START);
159         sb.append(NetconfConstMessages.CAPABILITIES_BASE);
160         if(capabilities != null) {
161             for(String capability: capabilities) {
162                 sb.append("    ").append(capability).append("\n");
163             }
164         }
165         sb.append(NetconfConstMessages.CAPABILITIES_END);
166         String helloOut = sb.toString();
167         netconfAdapter.sendMessage(helloOut);
168     }
169
170     private void isOk(String response) throws IOException {
171         if(response == null) {
172             throw new IOException("No response from netconf device");
173         }
174         if(!response.contains("<ok/>")) {
175             throw new IOException("Error response from netconf device: \n" + response);
176         }
177     }
178     
179     protected JSch getJSch() {
180         return new JSch();
181     }
182     
183     protected NetconfAdapter getNetconfAdapter(InputStream inputStream, OutputStream outputStream) throws IOException {
184         return new NetconfAdapter(inputStream, outputStream);
185     }
186 }