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