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 java.io.BufferedOutputStream;
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;
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;
46 * Returns a custom SSH client
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
53 //TODO: This class is to be altered completely based on the SALTSTACK server communication.
54 public class ConnectionBuilder {
56 private static final EELFLogger logger = EELFManager.getInstance().getLogger(ConnectionBuilder.class);
57 SshConnection sshConnection;
60 * Constructor that initializes an ssh client based on username and password
62 public ConnectionBuilder(String host, String port, String userName, String userPasswd) {
63 sshConnection = new SshConnection(host, Integer.parseInt(port), userName, userPasswd);
67 * Constructor that initializes an ssh client based on ssh certificate
69 public ConnectionBuilder(String host, String port, String certFile) {
70 sshConnection = new SshConnection(host, Integer.parseInt(port), certFile);
74 * Constructor that initializes an ssh client based on ssh username password and certificate
76 public ConnectionBuilder(String host, String port, String userName, String userPasswd,
79 sshConnection = new SshConnection(host, Integer.parseInt(port), userName, userPasswd, certFile);
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.
87 * @param cmd Commands to execute
88 * @return command execution status
90 public SaltstackResult connectNExecute(String cmd) {
91 return connectNExecute(cmd,-1,-1);
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.
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
104 public SaltstackResult connectNExecute(String cmd, int retryCount, int retryDelay) {
106 SaltstackResult result = new SaltstackResult();
108 if (retryCount != -1) {
109 result = sshConnection.connectWithRetry(retryCount, retryDelay);
111 result = sshConnection.connect();
113 if (result.getStatusCode() != SaltstackResultCodes.SUCCESS.getValue()) {
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();
124 if (result.getSshExitStatus() != 0) {
125 return sortExitStatus(result.getSshExitStatus(), errFilePath, cmd);
127 if (result.getStatusCode() != SaltstackResultCodes.SUCCESS.getValue()) {
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());
140 public SaltstackResult sortExitStatus (int exitStatus, String errFilePath, String cmd) {
141 SaltstackResult result = new SaltstackResult();
143 StringWriter writer = new StringWriter();
145 IOUtils.copy(new FileInputStream(new File(errFilePath)), writer, "UTF-8");
146 err = writer.toString();
147 } catch (Exception e){
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);
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);
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.
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
192 public SaltstackResult connectNExecuteSLS(String commands, String payloadSLS, int retryDelay, int retryCount) {
194 SaltstackResult result = new SaltstackResult();
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());
206 * Disconnect from SSH server.
208 public SaltstackResult disConnect(){
210 SaltstackResult result = new SaltstackResult();
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());
222 * Exec remote command over SSH. Return command execution status.
223 * Command output is written to out or err stream.
225 * @param cmd command to execute
226 * @return command execution status
228 public SaltstackResult execute(String cmd) {
230 SaltstackResult result = new SaltstackResult();
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);