2 * ============LICENSE_START=======================================================
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
13 * http://www.apache.org/licenses/LICENSE-2.0
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.
21 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22 * ============LICENSE_END=========================================================
25 package org.onap.ccsdk.sli.adaptors.saltstack.impl;
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;
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;
43 * Returns a custom SSH client
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
50 //TODO: This class is to be altered completely based on the SALTSTACK server communication.
51 public class ConnectionBuilder {
53 private static final EELFLogger logger = EELFManager.getInstance().getLogger(ConnectionBuilder.class);
54 SshConnection sshConnection;
57 * Constructor that initializes an ssh client based on username and password
59 public ConnectionBuilder(String host, String port, String userName, String userPasswd) {
60 sshConnection = new SshConnection(host, Integer.parseInt(port), userName, userPasswd);
64 * Constructor that initializes an ssh client based on ssh certificate
66 public ConnectionBuilder(String host, String port, String certFile) {
67 sshConnection = new SshConnection(host, Integer.parseInt(port), certFile);
71 * Constructor that initializes an ssh client based on ssh username password and certificate
73 public ConnectionBuilder(String host, String port, String userName, String userPasswd,
76 sshConnection = new SshConnection(host, Integer.parseInt(port), userName, userPasswd, certFile);
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.
84 * @param cmd Commands to execute
85 * @return command execution status
87 public SaltstackResult connectNExecute(String cmd, long execTimeout) throws IOException {
88 return connectNExecute(cmd, -1, -1, execTimeout);
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.
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
101 public SaltstackResult connectNExecute(String cmd, int retryCount, int retryDelay, long execTimeout)
104 SaltstackResult result = new SaltstackResult();
105 OutputStream out = null;
106 OutputStream errs = null;
107 if (execTimeout >= 0) {
108 sshConnection.setExecTimeout(execTimeout);
112 if (retryCount != -1) {
113 result = sshConnection.connectWithRetry(retryCount, retryDelay);
115 result = sshConnection.connect();
117 if (result.getStatusCode() != SaltstackResultCodes.SUCCESS.getValue()) {
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);
129 if (result.getStatusCode() != SaltstackResultCodes.SUCCESS.getValue()) {
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());
147 public SaltstackResult sortExitStatus(int exitStatus, String errFilePath, String cmd) {
148 SaltstackResult result = new SaltstackResult();
150 StringWriter writer = new StringWriter();
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");
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);
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);
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.
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
201 public SaltstackResult connectNExecuteSLS(String commands, String payloadSLS, int retryDelay, int retryCount) {
203 SaltstackResult result = new SaltstackResult();
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());
215 * Disconnect from SSH server.
217 public SaltstackResult disConnect() {
219 SaltstackResult result = new SaltstackResult();
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());
231 * Exec remote command over SSH. Return command execution status.
232 * Command output is written to out or err stream.
234 * @param cmd command to execute
235 * @return command execution status
237 public SaltstackResult connectNExecuteLog(String cmd) {
239 SaltstackResult result = new SaltstackResult();
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);