3469103b5bf682b27c637ad1638c4c3109c015dd
[ccsdk/sli/adaptors.git] / saltstack-adapter / saltstack-adapter-provider / src / main / java / org / onap / ccsdk / sli / adaptors / saltstack / impl / ConnectionBuilder.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.ccsdk.sli.adaptors.saltstack.impl;
26
27 import com.att.eelf.configuration.EELFLogger;
28 import com.att.eelf.configuration.EELFManager;
29 import org.apache.commons.io.IOUtils;
30 import org.apache.commons.lang.RandomStringUtils;
31 import org.onap.ccsdk.sli.adaptors.saltstack.model.SaltstackResult;
32 import org.onap.ccsdk.sli.adaptors.saltstack.model.SaltstackResultCodes;
33
34 import java.io.File;
35 import java.io.FileInputStream;
36 import java.io.FileNotFoundException;
37 import java.io.FileOutputStream;
38 import java.io.IOException;
39 import java.io.OutputStream;
40 import java.io.StringWriter;
41
42 /**
43  * Returns a custom SSH client
44  * - based on options
45  * - can create one with ssl using an X509 certificate that does NOT have a known CA
46  * - create one which trusts ALL SSL certificates
47  * - return default sshclient (which only trusts known CAs from default cacerts file for process) this is the default
48  * option
49  **/
50 //TODO: This class is to be altered completely based on the SALTSTACK server communication.
51 public class ConnectionBuilder {
52
53     private static final EELFLogger logger = EELFManager.getInstance().getLogger(ConnectionBuilder.class);
54     SshConnection sshConnection;
55
56     /**
57      * Constructor that initializes an ssh client based on username and password
58      **/
59     public ConnectionBuilder(String host, String port, String userName, String userPasswd) {
60         sshConnection = new SshConnection(host, Integer.parseInt(port), userName, userPasswd);
61     }
62
63     /**
64      * Constructor that initializes an ssh client based on ssh certificate
65      **/
66     public ConnectionBuilder(String host, String port, String certFile) {
67         sshConnection = new SshConnection(host, Integer.parseInt(port), certFile);
68     }
69
70     /**
71      * Constructor that initializes an ssh client based on ssh username password and certificate
72      **/
73     public ConnectionBuilder(String host, String port, String userName, String userPasswd,
74                              String certFile) {
75
76         sshConnection = new SshConnection(host, Integer.parseInt(port), userName, userPasswd, certFile);
77     }
78
79     /**
80      * 1. Connect to SSH server.
81      * 2. Exec remote command over SSH. Return command execution status.
82      * Command output is written to out or err stream.
83      *
84      * @param cmd Commands to execute
85      * @return command execution status
86      */
87     public SaltstackResult connectNExecute(String cmd, long execTimeout) throws IOException {
88         return connectNExecute(cmd, -1, -1, execTimeout);
89     }
90
91     /**
92      * 1. Connect to SSH server with retry enabled.
93      * 2. Exec remote command over SSH. Return command execution status.
94      * Command output is written to out or err stream.
95      *
96      * @param cmd        Commands to execute
97      * @param retryDelay delay between retry to make a SSH connection.
98      * @param retryCount number of count retry to make a SSH connection.
99      * @return command execution status
100      */
101     public SaltstackResult connectNExecute(String cmd, int retryCount, int retryDelay, long execTimeout)
102                             throws IOException{
103
104         SaltstackResult result = new SaltstackResult();
105         OutputStream out = null;
106         OutputStream errs = null;
107         if (execTimeout >= 0) {
108             sshConnection.setExecTimeout(execTimeout);
109         }
110
111         try {
112             if (retryCount != -1) {
113                 result = sshConnection.connectWithRetry(retryCount, retryDelay);
114             } else {
115                 result = sshConnection.connect();
116             }
117             if (result.getStatusCode() != SaltstackResultCodes.SUCCESS.getValue()) {
118                 return result;
119             }
120             String outFilePath = "/tmp/" + RandomStringUtils.random(5, true, true);
121             String errFilePath = "/tmp/" + RandomStringUtils.random(5, true, true);
122             out = new FileOutputStream(outFilePath);
123             errs = new FileOutputStream(errFilePath);
124             result = sshConnection.execCommand(cmd, out, errs, result);
125             sshConnection.disconnect();
126             if (result.getSshExitStatus() != 0) {
127                 return sortExitStatus(result.getSshExitStatus(), errFilePath, cmd);
128             }
129             if (result.getStatusCode() != SaltstackResultCodes.SUCCESS.getValue()) {
130                 return result;
131             }
132             result.setStatusMessage("Success");
133             result.setOutputFileName(outFilePath);
134         } catch (Exception io) {
135             logger.error("Caught Exception", io);
136             result.setStatusCode(SaltstackResultCodes.UNKNOWN_EXCEPTION.getValue());
137             result.setStatusMessage(io.getMessage());
138         } finally {
139             if( out != null )
140                 out.close();
141             if( errs != null )
142                 errs.close();
143         }
144         return result;
145     }
146
147     public SaltstackResult sortExitStatus(int exitStatus, String errFilePath, String cmd) {
148         SaltstackResult result = new SaltstackResult();
149         String err = "";
150         StringWriter writer = new StringWriter();
151         try {
152             IOUtils.copy(new FileInputStream(new File(errFilePath)), writer, "UTF-8");
153             err = writer.toString();
154         } catch (FileNotFoundException e){
155             logger.info("Error stream file doesn't exist");
156         } catch (IOException e){
157             logger.info("Error stream file doesn't exist");
158         }
159         if (exitStatus == 255 || exitStatus == 1) {
160             String errMessage = "Error executing command [" + cmd + "] over SSH [" + sshConnection.toString()
161                     + "]. Exit Code " + exitStatus + " and Error message : " +
162                     "Malformed configuration. " + err;
163             logger.error(errMessage);
164             result.setStatusCode(SaltstackResultCodes.INVALID_COMMAND.getValue());
165             result.setStatusMessage(errMessage);
166         } else if (exitStatus == 5 || exitStatus == 65) {
167             String errMessage = "Error executing command [" + cmd + "] over SSH [" + sshConnection.toString()
168                     + "]. Exit Code " + exitStatus + " and Error message : " +
169                     "Host not allowed to connect. " + err;
170             logger.error(errMessage);
171             result.setStatusCode(SaltstackResultCodes.USER_UNAUTHORIZED.getValue());
172             result.setStatusMessage(errMessage);
173         } else if (exitStatus == 67 || exitStatus == 73) {
174             String errMessage = "Error executing command [" + cmd + "] over SSH [" + sshConnection.toString()
175                     + "]. Exit Code " + exitStatus + " and Error message : " +
176                     "Key exchange failed. " + err;
177             logger.error(errMessage);
178             result.setStatusCode(SaltstackResultCodes.CERTIFICATE_ERROR.getValue());
179             result.setStatusMessage(errMessage);
180         } else {
181             String errMessage = "Error executing command [" + cmd + "] over SSH [" + sshConnection.toString()
182                     + "]. Exit Code " + exitStatus + " and Error message : " + err;
183             logger.error(errMessage);
184             result.setStatusCode(SaltstackResultCodes.UNKNOWN_EXCEPTION.getValue());
185             result.setStatusMessage(errMessage);
186         }
187         return result;
188     }
189
190     /**
191      * 1. Connect to SSH server.
192      * 2. Exec remote command over SSH. Return command execution status.
193      * Command output is written to out or err stream.
194      *
195      * @param commands   list of commands to execute
196      * @param payloadSLS has the SLS file location that is to be sent to server
197      * @param retryDelay delay between retry to make a SSH connection.
198      * @param retryCount number of count retry to make a SSH connection.
199      * @return command execution status
200      */
201     public SaltstackResult connectNExecuteSLS(String commands, String payloadSLS, int retryDelay, int retryCount) {
202
203         SaltstackResult result = new SaltstackResult();
204         try {
205             //TODO: to implement SSH connected client to Saltstack Server
206         } catch (Exception io) {
207             logger.error("Caught Exception", io);
208             result.setStatusCode(SaltstackResultCodes.IO_EXCEPTION.getValue());
209             result.setStatusMessage(io.getMessage());
210         }
211         return result;
212     }
213
214     /**
215      * Disconnect from SSH server.
216      */
217     public SaltstackResult disConnect() {
218
219         SaltstackResult result = new SaltstackResult();
220         try {
221             //TODO: to implement SSH connected client to Saltstack Server
222         } catch (Exception io) {
223             logger.error("Caught Exception", io);
224             result.setStatusCode(SaltstackResultCodes.IO_EXCEPTION.getValue());
225             result.setStatusMessage(io.getMessage());
226         }
227         return result;
228     }
229
230     /**
231      * Exec remote command over SSH. Return command execution status.
232      * Command output is written to out or err stream.
233      *
234      * @param cmd command to execute
235      * @return command execution status
236      */
237     public SaltstackResult connectNExecuteLog(String cmd) {
238
239         SaltstackResult result = new SaltstackResult();
240
241         try {
242             //TODO: to implement SSH command execute
243         } catch (Exception io) {
244             result.setStatusCode(SaltstackResultCodes.IO_EXCEPTION.getValue());
245             result.setStatusMessage(io.getMessage());
246             logger.error("Caught IOException", io);
247         }
248         return result;
249     }
250 }