2 * ============LICENSE_START=======================================================
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
15 * http://www.apache.org/licenses/LICENSE-2.0
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.
23 * ============LICENSE_END=========================================================
26 package org.onap.appc.adapter.netconf.jsch;
28 import com.jcraft.jsch.Channel;
29 import com.jcraft.jsch.ChannelSubsystem;
30 import com.jcraft.jsch.JSch;
31 import com.jcraft.jsch.Session;
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;
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;
49 * Implementation of NetconfClient interface based on JCraft jsch library.
51 public class NetconfClientJsch implements NetconfClient {
53 private static final int SESSION_CONNECT_TIMEOUT = 30000;
54 private static final int CHANNEL_CONNECT_TIMEOUT = 10000;
56 private Session session;
57 private Channel channel;
58 private NetconfAdapter netconfAdapter;
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();
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");
74 Properties additionalProps = connectionDetails.getAdditionalProperties();
75 if((additionalProps != null) && !additionalProps.isEmpty()) {
76 session.setConfig(additionalProps);
79 session.connect(SESSION_CONNECT_TIMEOUT);
80 session.setTimeout(10000);
82 createConnection(connectionDetails);
84 } catch(Exception e) {
85 String message = EELFResourceManager.format(Msg.CANNOT_ESTABLISH_CONNECTION, host, String.valueOf(port), username);
86 throw new APPCException(message, e);
91 public String exchangeMessage(String message) throws APPCException {
93 netconfAdapter.sendMessage(message);
94 return netconfAdapter.receiveMessage();
95 } catch(IOException e) {
96 throw new APPCException(e);
101 public void configure(String configuration) throws APPCException {
103 isOk(exchangeMessage(configuration));
104 } catch(IOException e) {
105 throw new APPCException(e);
110 public String getConfiguration() throws APPCException {
111 return exchangeMessage(NetconfConstMessages.GET_RUNNING_CONFIG);
115 public void disconnect() {
117 if((channel != null) && !channel.isClosed()) {
118 netconfAdapter.sendMessage(NetconfConstMessages.CLOSE_SESSION);
119 isOk(netconfAdapter.receiveMessage());
121 } catch(IOException e) {
122 throw new RuntimeException("Error closing netconf device", e);
124 netconfAdapter = null;
125 if(channel != null) {
126 channel.disconnect();
129 if(session != null) {
130 session.disconnect();
136 private void createConnection(NetconfConnectionDetails connectionDetails) throws APPCException {
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) {
145 throw new APPCException(e);
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");
154 if(helloIn.contains("<rpc-error>")) {
155 throw new IOException("Expected hello message, but received error from netconf device:\n" + helloIn);
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");
165 sb.append(NetconfConstMessages.CAPABILITIES_END);
166 String helloOut = sb.toString();
167 netconfAdapter.sendMessage(helloOut);
170 private void isOk(String response) throws IOException {
171 if(response == null) {
172 throw new IOException("No response from netconf device");
174 if(!response.contains("<ok/>")) {
175 throw new IOException("Error response from netconf device: \n" + response);
179 protected JSch getJSch() {
183 protected NetconfAdapter getNetconfAdapter(InputStream inputStream, OutputStream outputStream) throws IOException {
184 return new NetconfAdapter(inputStream, outputStream);