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