2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * Copyright (C) 2017 Amdocs
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=========================================================
20 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
23 package org.openecomp.appc.dg.ssh.impl;
25 import com.fasterxml.jackson.databind.ObjectMapper;
27 import java.io.ByteArrayOutputStream;
28 import java.io.IOException;
31 import org.openecomp.appc.adapter.ssh.SshAdapter;
32 import org.openecomp.appc.adapter.ssh.SshConnection;
33 import org.openecomp.appc.adapter.ssh.SshConnectionDetails;
34 import org.openecomp.appc.dg.ssh.SshService;
35 import org.openecomp.appc.exceptions.APPCException;
36 import com.att.eelf.configuration.EELFLogger;
37 import com.att.eelf.configuration.EELFManager;
38 import org.openecomp.sdnc.sli.SvcLogicContext;
41 public class SshServiceImpl implements SshService {
43 private static final EELFLogger logger = EELFManager.getInstance().getApplicationLogger();
44 private static final ObjectMapper mapper = new ObjectMapper();
46 private SshAdapter sshAdapter;
48 public void setSshAdapter(SshAdapter sshAdapter) {
49 this.sshAdapter = sshAdapter;
53 public void exec(Map<String, String> params, SvcLogicContext ctx) throws APPCException {
54 SshConnectionDetails connectionDetails = resolveConnectionDetails(params.get(PARAM_IN_connection_details));
55 String command = params.get(PARAM_IN_command);
56 logger.debug("=> Connecting to SSH server...");
57 SshConnection sshConnection = sshAdapter.getConnection(connectionDetails.getHost(), connectionDetails.getPort(), connectionDetails.getUsername(), connectionDetails.getPassword());
58 sshConnection.connect();
60 logger.debug("=> Connected to SSH server...");
61 logger.debug("=> Running SSH command...");
62 long timeout = DEF_timeout;
63 String stimeout = params.get(PARAM_IN_timeout);
64 if ((stimeout != null && !stimeout.isEmpty())) {
65 timeout = Long.parseLong(stimeout);
67 sshConnection.setExecTimeout(timeout);
68 ByteArrayOutputStream stdout = new ByteArrayOutputStream();
69 ByteArrayOutputStream stderr = new ByteArrayOutputStream();
70 int status = sshConnection.execCommand(command, stdout, stderr);
71 String stdoutRes = stdout.toString();
72 String stderrRes = stderr.toString();
73 logger.debug("=> executed SSH command");
74 ctx.setAttribute(PARAM_OUT_status, String.format("%01d", status));
75 ctx.setAttribute(PARAM_OUT_stdout, stdoutRes);
76 ctx.setAttribute(PARAM_OUT_stderr, stderrRes);
78 sshConnection.disconnect();
82 private SshConnectionDetails resolveConnectionDetails(String connectionDetailsStr) throws APPCException {
83 SshConnectionDetails connectionDetails = null;
85 connectionDetails = mapper.readValue(connectionDetailsStr, SshConnectionDetails.class);
86 if (0 == connectionDetails.getPort()) connectionDetails.setPort(DEF_port);
87 } catch (IOException e) {
88 throw new APPCException(e);
90 return connectionDetails;
94 public void execWithStatusCheck(Map<String, String> params, SvcLogicContext ctx) throws APPCException {
96 int status = Integer.parseInt(ctx.getAttribute(PARAM_OUT_status));
97 if(status != DEF_SUCCESS_STATUS) {
98 StringBuilder errmsg = new StringBuilder();
99 errmsg.append("SSH command returned error status [").append(status).append(']');
100 String stderr = ctx.getAttribute(PARAM_OUT_stderr);
101 if((stderr != null) && !stderr.isEmpty()) {
102 errmsg.append(". Error: [").append(stderr).append(']');
104 throw new APPCException(errmsg.toString());