2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END=========================================================
22 package org.openecomp.appc.dg.ssh.impl;
24 import com.fasterxml.jackson.databind.ObjectMapper;
26 import java.io.ByteArrayOutputStream;
27 import java.io.IOException;
30 import org.openecomp.appc.adapter.ssh.SshAdapter;
31 import org.openecomp.appc.adapter.ssh.SshConnection;
32 import org.openecomp.appc.adapter.ssh.SshConnectionDetails;
33 import org.openecomp.appc.dg.ssh.SshService;
34 import org.openecomp.appc.exceptions.APPCException;
35 import com.att.eelf.configuration.EELFLogger;
36 import com.att.eelf.configuration.EELFManager;
37 import org.openecomp.sdnc.sli.SvcLogicContext;
40 public class SshServiceImpl implements SshService {
42 private static final EELFLogger logger = EELFManager.getInstance().getApplicationLogger();
43 private static final ObjectMapper mapper = new ObjectMapper();
45 private SshAdapter sshAdapter;
47 public void setSshAdapter(SshAdapter sshAdapter) {
48 this.sshAdapter = sshAdapter;
52 public void exec(Map<String, String> params, SvcLogicContext ctx) throws APPCException {
53 SshConnectionDetails connectionDetails = resolveConnectionDetails(params.get(PARAM_IN_connection_details));
54 String command = params.get(PARAM_IN_command);
55 logger.debug("=> Connecting to SSH server...");
56 SshConnection sshConnection = sshAdapter.getConnection(connectionDetails.getHost(), connectionDetails.getPort(), connectionDetails.getUsername(), connectionDetails.getPassword());
57 sshConnection.connect();
59 logger.debug("=> Connected to SSH server...");
60 logger.debug("=> Running SSH command...");
61 long timeout = DEF_timeout;
62 String stimeout = params.get(PARAM_IN_timeout);
63 if ((stimeout != null && !stimeout.isEmpty())) {
64 timeout = Long.parseLong(stimeout);
66 sshConnection.setExecTimeout(timeout);
67 ByteArrayOutputStream stdout = new ByteArrayOutputStream();
68 ByteArrayOutputStream stderr = new ByteArrayOutputStream();
69 int status = sshConnection.execCommand(command, stdout, stderr);
70 String stdoutRes = stdout.toString();
71 String stderrRes = stderr.toString();
72 logger.debug("=> executed SSH command");
73 ctx.setAttribute(PARAM_OUT_status, String.format("%01d", status));
74 ctx.setAttribute(PARAM_OUT_stdout, stdoutRes);
75 ctx.setAttribute(PARAM_OUT_stderr, stderrRes);
77 sshConnection.disconnect();
81 private SshConnectionDetails resolveConnectionDetails(String connectionDetailsStr) throws APPCException {
82 SshConnectionDetails connectionDetails = null;
84 connectionDetails = mapper.readValue(connectionDetailsStr, SshConnectionDetails.class);
85 if (0 == connectionDetails.getPort()) connectionDetails.setPort(DEF_port);
86 } catch (IOException e) {
87 throw new APPCException(e);
89 return connectionDetails;
93 public void execWithStatusCheck(Map<String, String> params, SvcLogicContext ctx) throws APPCException {
95 int status = Integer.parseInt(ctx.getAttribute(PARAM_OUT_status));
96 if(status != DEF_SUCCESS_STATUS) {
97 StringBuilder errmsg = new StringBuilder();
98 errmsg.append("SSH command returned error status [").append(status).append(']');
99 String stderr = ctx.getAttribute(PARAM_OUT_stderr);
100 if((stderr != null) && !stderr.isEmpty()) {
101 errmsg.append(". Error: [").append(stderr).append(']');
103 throw new APPCException(errmsg.toString());