2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 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 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22 * ============LICENSE_END=========================================================
25 package org.onap.appc.adapter.netconf.jsch;
27 import com.jcraft.jsch.Channel;
28 import com.jcraft.jsch.ChannelSubsystem;
29 import com.jcraft.jsch.JSch;
30 import com.jcraft.jsch.Session;
32 import java.io.IOException;
33 import java.util.List;
34 import java.util.Properties;
36 import org.onap.appc.adapter.netconf.NetconfClient;
37 import org.onap.appc.adapter.netconf.NetconfConnectionDetails;
38 import org.onap.appc.adapter.netconf.internal.NetconfAdapter;
39 import org.onap.appc.adapter.netconf.internal.NetconfConstMessages;
40 import org.onap.appc.encryption.EncryptionTool;
41 import org.onap.appc.exceptions.APPCException;
42 import org.onap.appc.i18n.Msg;
43 import com.att.eelf.i18n.EELFResourceManager;
46 * Implementation of NetconfClient interface based on JCraft jsch library.
48 public class NetconfClientJsch implements NetconfClient {
50 private static final int SESSION_CONNECT_TIMEOUT = 30000;
51 private static final int CHANNEL_CONNECT_TIMEOUT = 10000;
53 private Session session;
54 private Channel channel;
55 private NetconfAdapter netconfAdapter;
59 public void connect(NetconfConnectionDetails connectionDetails) throws APPCException {
60 String host = connectionDetails.getHost();
61 int port = connectionDetails.getPort();
62 String username = connectionDetails.getUsername();
63 String password = connectionDetails.getPassword();
65 JSch.setLogger(new JSchLogger());
66 JSch jsch = new JSch();
67 session = jsch.getSession(EncryptionTool.getInstance().decrypt(username), host, port);
68 session.setPassword(EncryptionTool.getInstance().decrypt(password));
69 session.setConfig("StrictHostKeyChecking", "no");
71 Properties additionalProps = connectionDetails.getAdditionalProperties();
72 if((additionalProps != null) && !additionalProps.isEmpty()) {
73 session.setConfig(additionalProps);
76 session.connect(SESSION_CONNECT_TIMEOUT);
77 session.setTimeout(10000);
79 // session.setServerAliveCountMax(0); // If this is not set to '0', then socket timeout on all reads will not work!!!!
80 channel = session.openChannel("subsystem");
81 ((ChannelSubsystem)channel).setSubsystem("netconf");
82 netconfAdapter = new NetconfAdapter(channel.getInputStream(), channel.getOutputStream());
83 channel.connect(CHANNEL_CONNECT_TIMEOUT);
84 hello(connectionDetails.getCapabilities());
85 } catch(Exception e) {
89 } catch(Exception e) {
90 String message = EELFResourceManager.format(Msg.CANNOT_ESTABLISH_CONNECTION, host, String.valueOf(port), username);
91 throw new APPCException(message, e);
96 public String exchangeMessage(String message) throws APPCException {
98 netconfAdapter.sendMessage(message);
99 return netconfAdapter.receiveMessage();
100 } catch(IOException e) {
101 throw new APPCException(e);
106 public void configure(String configuration) throws APPCException {
108 isOk(exchangeMessage(configuration));
109 } catch(IOException e) {
110 throw new APPCException(e);
115 public String getConfiguration() throws APPCException {
116 return exchangeMessage(NetconfConstMessages.GET_RUNNING_CONFIG);
120 public void disconnect() {
122 if((channel != null) && !channel.isClosed()) {
123 netconfAdapter.sendMessage(NetconfConstMessages.CLOSE_SESSION);
124 isOk(netconfAdapter.receiveMessage());
126 } catch(IOException e) {
127 throw new RuntimeException("Error closing netconf device", e);
129 netconfAdapter = null;
130 if(channel != null) {
131 channel.disconnect();
134 if(session != null) {
135 session.disconnect();
141 private void hello(List<String> capabilities) throws IOException {
142 String helloIn = netconfAdapter.receiveMessage();
143 if(helloIn == null) {
144 throw new IOException("Expected hello message, but nothing received error from netconf device");
146 if(helloIn.contains("<rpc-error>")) {
147 throw new IOException("Expected hello message, but received error from netconf device:\n" + helloIn);
149 StringBuilder sb = new StringBuilder();
150 sb.append(NetconfConstMessages.CAPABILITIES_START);
151 sb.append(NetconfConstMessages.CAPABILITIES_BASE);
152 if(capabilities != null) {
153 for(String capability: capabilities) {
154 sb.append(" ").append(capability).append("\n");
157 sb.append(NetconfConstMessages.CAPABILITIES_END);
158 String helloOut = sb.toString();
159 netconfAdapter.sendMessage(helloOut);
162 private void isOk(String response) throws IOException {
163 if(response == null) {
164 throw new IOException("No response from netconf device");
166 if(!response.contains("<ok/>")) {
167 throw new IOException("Error response from netconf device: \n" + response);