Second part of onap rename
[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 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             try {
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) {
86                 disconnect();
87                 throw e;
88             }
89         } catch(Exception e) {
90             String message = EELFResourceManager.format(Msg.CANNOT_ESTABLISH_CONNECTION, host, String.valueOf(port), username);
91             throw new APPCException(message, e);
92         }
93     }
94
95     @Override
96     public String exchangeMessage(String message) throws APPCException {
97         try {
98             netconfAdapter.sendMessage(message);
99             return netconfAdapter.receiveMessage();
100         } catch(IOException e) {
101             throw new APPCException(e);
102         }
103     }
104
105     @Override
106     public void configure(String configuration) throws APPCException {
107         try {
108             isOk(exchangeMessage(configuration));
109         } catch(IOException e) {
110             throw new APPCException(e);
111         }
112     }
113
114     @Override
115     public String getConfiguration() throws APPCException {
116         return exchangeMessage(NetconfConstMessages.GET_RUNNING_CONFIG);
117     }
118
119     @Override
120     public void disconnect() {
121         try {
122             if((channel != null) && !channel.isClosed()) {
123                 netconfAdapter.sendMessage(NetconfConstMessages.CLOSE_SESSION);
124                 isOk(netconfAdapter.receiveMessage());
125             }
126         } catch(IOException e) {
127             throw new RuntimeException("Error closing netconf device", e);
128         } finally {
129             netconfAdapter = null;
130             if(channel != null) {
131                 channel.disconnect();
132                 channel = null;
133             }
134             if(session != null) {
135                 session.disconnect();
136                 session = null;
137             }
138         }
139     }
140
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");
145         }
146         if(helloIn.contains("<rpc-error>")) {
147             throw new IOException("Expected hello message, but received error from netconf device:\n" + helloIn);
148         }
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");
155             }
156         }
157         sb.append(NetconfConstMessages.CAPABILITIES_END);
158         String helloOut = sb.toString();
159         netconfAdapter.sendMessage(helloOut);
160     }
161
162     private void isOk(String response) throws IOException {
163         if(response == null) {
164             throw new IOException("No response from netconf device");
165         }
166         if(!response.contains("<ok/>")) {
167             throw new IOException("Error response from netconf device: \n" + response);
168         }
169     }
170 }