2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Copyright (C) 2017 Amdocs
8 * =============================================================================
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
21 * ============LICENSE_END=========================================================
24 package org.onap.appc.adapter.netconf.jsch;
26 import com.jcraft.jsch.Channel;
27 import com.jcraft.jsch.ChannelSubsystem;
28 import com.jcraft.jsch.JSch;
29 import com.jcraft.jsch.Session;
31 import java.io.IOException;
32 import java.util.List;
33 import java.util.Properties;
35 import org.onap.appc.adapter.netconf.NetconfClient;
36 import org.onap.appc.adapter.netconf.NetconfConnectionDetails;
37 import org.onap.appc.adapter.netconf.internal.NetconfAdapter;
38 import org.onap.appc.adapter.netconf.internal.NetconfConstMessages;
39 import org.onap.appc.encryption.EncryptionTool;
40 import org.onap.appc.exceptions.APPCException;
41 import org.onap.appc.i18n.Msg;
42 import com.att.eelf.i18n.EELFResourceManager;
45 * Implementation of NetconfClient interface based on JCraft jsch library.
47 public class NetconfClientJsch implements NetconfClient {
49 private static final int SESSION_CONNECT_TIMEOUT = 30000;
50 private static final int CHANNEL_CONNECT_TIMEOUT = 10000;
52 private Session session;
53 private Channel channel;
54 private NetconfAdapter netconfAdapter;
58 public void connect(NetconfConnectionDetails connectionDetails) throws APPCException {
59 String host = connectionDetails.getHost();
60 int port = connectionDetails.getPort();
61 String username = connectionDetails.getUsername();
62 String password = connectionDetails.getPassword();
64 JSch.setLogger(new JSchLogger());
65 JSch jsch = new JSch();
66 session = jsch.getSession(EncryptionTool.getInstance().decrypt(username), host, port);
67 session.setPassword(EncryptionTool.getInstance().decrypt(password));
68 session.setConfig("StrictHostKeyChecking", "no");
70 Properties additionalProps = connectionDetails.getAdditionalProperties();
71 if((additionalProps != null) && !additionalProps.isEmpty()) {
72 session.setConfig(additionalProps);
75 session.connect(SESSION_CONNECT_TIMEOUT);
76 session.setTimeout(10000);
78 createConnection(connectionDetails);
80 } catch(Exception e) {
81 String message = EELFResourceManager.format(Msg.CANNOT_ESTABLISH_CONNECTION, host, String.valueOf(port), username);
82 throw new APPCException(message, e);
87 public String exchangeMessage(String message) throws APPCException {
89 netconfAdapter.sendMessage(message);
90 return netconfAdapter.receiveMessage();
91 } catch(IOException e) {
92 throw new APPCException(e);
97 public void configure(String configuration) throws APPCException {
99 isOk(exchangeMessage(configuration));
100 } catch(IOException e) {
101 throw new APPCException(e);
106 public String getConfiguration() throws APPCException {
107 return exchangeMessage(NetconfConstMessages.GET_RUNNING_CONFIG);
111 public void disconnect() {
113 if((channel != null) && !channel.isClosed()) {
114 netconfAdapter.sendMessage(NetconfConstMessages.CLOSE_SESSION);
115 isOk(netconfAdapter.receiveMessage());
117 } catch(IOException e) {
118 throw new RuntimeException("Error closing netconf device", e);
120 netconfAdapter = null;
121 if(channel != null) {
122 channel.disconnect();
125 if(session != null) {
126 session.disconnect();
132 private void createConnection(NetconfConnectionDetails connectionDetails) throws APPCException {
134 // session.setServerAliveCountMax(0); // If this is not set to '0', then socket timeout on all reads will not work!!!!
135 channel = session.openChannel("subsystem");
136 ((ChannelSubsystem)channel).setSubsystem("netconf");
137 netconfAdapter = new NetconfAdapter(channel.getInputStream(), channel.getOutputStream());
138 channel.connect(CHANNEL_CONNECT_TIMEOUT);
139 hello(connectionDetails.getCapabilities());
140 } catch(Exception e) {
142 throw new APPCException(e);
146 private void hello(List<String> capabilities) throws IOException {
147 String helloIn = netconfAdapter.receiveMessage();
148 if(helloIn == null) {
149 throw new IOException("Expected hello message, but nothing received error from netconf device");
151 if(helloIn.contains("<rpc-error>")) {
152 throw new IOException("Expected hello message, but received error from netconf device:\n" + helloIn);
154 StringBuilder sb = new StringBuilder();
155 sb.append(NetconfConstMessages.CAPABILITIES_START);
156 sb.append(NetconfConstMessages.CAPABILITIES_BASE);
157 if(capabilities != null) {
158 for(String capability: capabilities) {
159 sb.append(" ").append(capability).append("\n");
162 sb.append(NetconfConstMessages.CAPABILITIES_END);
163 String helloOut = sb.toString();
164 netconfAdapter.sendMessage(helloOut);
167 private void isOk(String response) throws IOException {
168 if(response == null) {
169 throw new IOException("No response from netconf device");
171 if(!response.contains("<ok/>")) {
172 throw new IOException("Error response from netconf device: \n" + response);