c60fbbd1ff49672b0297859b84753b1610ce7917
[appc.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
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
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  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22  * ============LICENSE_END=========================================================
23  */
24
25 package org.onap.appc.adapter.netconf.jsch;
26
27 import com.jcraft.jsch.Channel;
28 import com.jcraft.jsch.ChannelSubsystem;
29 import com.jcraft.jsch.JSch;
30 import com.jcraft.jsch.Session;
31
32 import java.io.IOException;
33 import java.util.List;
34 import java.util.Properties;
35
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;
44
45 /**
46  * Implementation of NetconfClient interface based on JCraft jsch library.
47  */
48 public class NetconfClientJsch implements NetconfClient {
49
50     private static final int SESSION_CONNECT_TIMEOUT = 30000;
51     private static final int CHANNEL_CONNECT_TIMEOUT = 10000;
52
53     private Session session;
54     private Channel channel;
55     private NetconfAdapter netconfAdapter;
56
57
58     @Override
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();
64         try {
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");
70
71             Properties additionalProps = connectionDetails.getAdditionalProperties();
72             if((additionalProps != null) && !additionalProps.isEmpty()) {
73                 session.setConfig(additionalProps);
74             }
75
76             session.connect(SESSION_CONNECT_TIMEOUT);
77             session.setTimeout(10000);
78
79             createConnection(connectionDetails);
80
81         } catch(Exception e) {
82             String message = EELFResourceManager.format(Msg.CANNOT_ESTABLISH_CONNECTION, host, String.valueOf(port), username);
83             throw new APPCException(message, e);
84         }
85     }
86
87     @Override
88     public String exchangeMessage(String message) throws APPCException {
89         try {
90             netconfAdapter.sendMessage(message);
91             return netconfAdapter.receiveMessage();
92         } catch(IOException e) {
93             throw new APPCException(e);
94         }
95     }
96
97     @Override
98     public void configure(String configuration) throws APPCException {
99         try {
100             isOk(exchangeMessage(configuration));
101         } catch(IOException e) {
102             throw new APPCException(e);
103         }
104     }
105
106     @Override
107     public String getConfiguration() throws APPCException {
108         return exchangeMessage(NetconfConstMessages.GET_RUNNING_CONFIG);
109     }
110
111     @Override
112     public void disconnect() {
113         try {
114             if((channel != null) && !channel.isClosed()) {
115                 netconfAdapter.sendMessage(NetconfConstMessages.CLOSE_SESSION);
116                 isOk(netconfAdapter.receiveMessage());
117             }
118         } catch(IOException e) {
119             throw new RuntimeException("Error closing netconf device", e);
120         } finally {
121             netconfAdapter = null;
122             if(channel != null) {
123                 channel.disconnect();
124                 channel = null;
125             }
126             if(session != null) {
127                 session.disconnect();
128                 session = null;
129             }
130         }
131     }
132
133     private void createConnection(NetconfConnectionDetails connectionDetails) throws APPCException {
134         try {
135 //          session.setServerAliveCountMax(0); // If this is not set to '0', then socket timeout on all reads will not work!!!!
136             channel = session.openChannel("subsystem");
137             ((ChannelSubsystem)channel).setSubsystem("netconf");
138             netconfAdapter = new NetconfAdapter(channel.getInputStream(), channel.getOutputStream());
139             channel.connect(CHANNEL_CONNECT_TIMEOUT);
140             hello(connectionDetails.getCapabilities());
141         } catch(Exception e) {
142             disconnect();
143             throw new APPCException(e);
144         }
145     }
146
147     private void hello(List<String> capabilities) throws IOException {
148         String helloIn = netconfAdapter.receiveMessage();
149         if(helloIn == null) {
150             throw new IOException("Expected hello message, but nothing received error from netconf device");
151         }
152         if(helloIn.contains("<rpc-error>")) {
153             throw new IOException("Expected hello message, but received error from netconf device:\n" + helloIn);
154         }
155         StringBuilder sb = new StringBuilder();
156         sb.append(NetconfConstMessages.CAPABILITIES_START);
157         sb.append(NetconfConstMessages.CAPABILITIES_BASE);
158         if(capabilities != null) {
159             for(String capability: capabilities) {
160                 sb.append("    ").append(capability).append("\n");
161             }
162         }
163         sb.append(NetconfConstMessages.CAPABILITIES_END);
164         String helloOut = sb.toString();
165         netconfAdapter.sendMessage(helloOut);
166     }
167
168     private void isOk(String response) throws IOException {
169         if(response == null) {
170             throw new IOException("No response from netconf device");
171         }
172         if(!response.contains("<ok/>")) {
173             throw new IOException("Error response from netconf device: \n" + response);
174         }
175     }
176 }