92569d5e0ccf3e27d22b48260d080d21f39deb5f
[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  * 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
12  * 
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  * 
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.
20  * 
21  * ============LICENSE_END=========================================================
22  */
23
24 package org.onap.appc.adapter.netconf.jsch;
25
26 import com.jcraft.jsch.Channel;
27 import com.jcraft.jsch.ChannelSubsystem;
28 import com.jcraft.jsch.JSch;
29 import com.jcraft.jsch.Session;
30
31 import java.io.IOException;
32 import java.util.List;
33 import java.util.Properties;
34
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;
43
44 /**
45  * Implementation of NetconfClient interface based on JCraft jsch library.
46  */
47 public class NetconfClientJsch implements NetconfClient {
48
49     private static final int SESSION_CONNECT_TIMEOUT = 30000;
50     private static final int CHANNEL_CONNECT_TIMEOUT = 10000;
51
52     private Session session;
53     private Channel channel;
54     private NetconfAdapter netconfAdapter;
55
56
57     @Override
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();
63         try {
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");
69
70             Properties additionalProps = connectionDetails.getAdditionalProperties();
71             if((additionalProps != null) && !additionalProps.isEmpty()) {
72                 session.setConfig(additionalProps);
73             }
74
75             session.connect(SESSION_CONNECT_TIMEOUT);
76             session.setTimeout(10000);
77
78             createConnection(connectionDetails);
79
80         } catch(Exception e) {
81             String message = EELFResourceManager.format(Msg.CANNOT_ESTABLISH_CONNECTION, host, String.valueOf(port), username);
82             throw new APPCException(message, e);
83         }
84     }
85
86     @Override
87     public String exchangeMessage(String message) throws APPCException {
88         try {
89             netconfAdapter.sendMessage(message);
90             return netconfAdapter.receiveMessage();
91         } catch(IOException e) {
92             throw new APPCException(e);
93         }
94     }
95
96     @Override
97     public void configure(String configuration) throws APPCException {
98         try {
99             isOk(exchangeMessage(configuration));
100         } catch(IOException e) {
101             throw new APPCException(e);
102         }
103     }
104
105     @Override
106     public String getConfiguration() throws APPCException {
107         return exchangeMessage(NetconfConstMessages.GET_RUNNING_CONFIG);
108     }
109
110     @Override
111     public void disconnect() {
112         try {
113             if((channel != null) && !channel.isClosed()) {
114                 netconfAdapter.sendMessage(NetconfConstMessages.CLOSE_SESSION);
115                 isOk(netconfAdapter.receiveMessage());
116             }
117         } catch(IOException e) {
118             throw new RuntimeException("Error closing netconf device", e);
119         } finally {
120             netconfAdapter = null;
121             if(channel != null) {
122                 channel.disconnect();
123                 channel = null;
124             }
125             if(session != null) {
126                 session.disconnect();
127                 session = null;
128             }
129         }
130     }
131
132     private void createConnection(NetconfConnectionDetails connectionDetails) throws APPCException {
133         try {
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) {
141             disconnect();
142             throw new APPCException(e);
143         }
144     }
145
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");
150         }
151         if(helloIn.contains("<rpc-error>")) {
152             throw new IOException("Expected hello message, but received error from netconf device:\n" + helloIn);
153         }
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");
160             }
161         }
162         sb.append(NetconfConstMessages.CAPABILITIES_END);
163         String helloOut = sb.toString();
164         netconfAdapter.sendMessage(helloOut);
165     }
166
167     private void isOk(String response) throws IOException {
168         if(response == null) {
169             throw new IOException("No response from netconf device");
170         }
171         if(!response.contains("<ok/>")) {
172             throw new IOException("Error response from netconf device: \n" + response);
173         }
174     }
175 }