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.appc.dg.ssh.impl;
27 import com.fasterxml.jackson.databind.ObjectMapper;
29 import java.io.ByteArrayOutputStream;
30 import java.io.IOException;
33 import org.onap.appc.adapter.ssh.SshAdapter;
34 import org.onap.appc.adapter.ssh.SshConnection;
35 import org.onap.appc.adapter.ssh.SshConnectionDetails;
36 import org.onap.appc.dg.ssh.SshService;
37 import org.onap.appc.exceptions.APPCException;
38 import com.att.eelf.configuration.EELFLogger;
39 import com.att.eelf.configuration.EELFManager;
40 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
43 public class SshServiceImpl implements SshService {
45 private static final EELFLogger logger = EELFManager.getInstance().getApplicationLogger();
46 private static final ObjectMapper mapper = new ObjectMapper();
48 private SshAdapter sshAdapter;
50 public void setSshAdapter(SshAdapter sshAdapter) {
51 this.sshAdapter = sshAdapter;
55 public void exec(Map<String, String> params, SvcLogicContext ctx) throws APPCException {
56 SshConnectionDetails connectionDetails = resolveConnectionDetails(params.get(PARAM_IN_connection_details));
57 String command = params.get(PARAM_IN_command);
58 logger.debug("=> Connecting to SSH server...");
59 SshConnection sshConnection = sshAdapter.getConnection(connectionDetails.getHost(), connectionDetails.getPort(), connectionDetails.getUsername(), connectionDetails.getPassword());
60 sshConnection.connect();
62 logger.debug("=> Connected to SSH server...");
63 logger.debug("=> Running SSH command...");
64 long timeout = DEF_timeout;
65 String stimeout = params.get(PARAM_IN_timeout);
66 if ((stimeout != null && !stimeout.isEmpty())) {
67 timeout = Long.parseLong(stimeout);
69 sshConnection.setExecTimeout(timeout);
70 ByteArrayOutputStream stdout = new ByteArrayOutputStream();
71 ByteArrayOutputStream stderr = new ByteArrayOutputStream();
72 int status = sshConnection.execCommand(command, stdout, stderr);
73 String stdoutRes = stdout.toString();
74 String stderrRes = stderr.toString();
75 logger.debug("=> executed SSH command");
76 ctx.setAttribute(PARAM_OUT_status, String.format("%01d", status));
77 ctx.setAttribute(PARAM_OUT_stdout, stdoutRes);
78 ctx.setAttribute(PARAM_OUT_stderr, stderrRes);
80 sshConnection.disconnect();
84 private SshConnectionDetails resolveConnectionDetails(String connectionDetailsStr) throws APPCException {
85 SshConnectionDetails connectionDetails = null;
87 connectionDetails = mapper.readValue(connectionDetailsStr, SshConnectionDetails.class);
88 if (0 == connectionDetails.getPort()) connectionDetails.setPort(DEF_port);
89 } catch (IOException e) {
90 throw new APPCException(e);
92 return connectionDetails;
96 public void execWithStatusCheck(Map<String, String> params, SvcLogicContext ctx) throws APPCException {
98 int status = Integer.parseInt(ctx.getAttribute(PARAM_OUT_status));
99 if(status != DEF_SUCCESS_STATUS) {
100 StringBuilder errmsg = new StringBuilder();
101 errmsg.append("SSH command returned error status [").append(status).append(']');
102 String stderr = ctx.getAttribute(PARAM_OUT_stderr);
103 if((stderr != null) && !stderr.isEmpty()) {
104 errmsg.append(". Error: [").append(stderr).append(']');
106 throw new APPCException(errmsg.toString());