e758f36a213db8f8da8242781bc3eda37adba324
[appc.git] / appc-dg / appc-dg-shared / appc-dg-ssh / src / main / java / org / onap / appc / dg / ssh / impl / SshServiceImpl.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
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
14  * 
15  *      http://www.apache.org/licenses/LICENSE-2.0
16  * 
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.
22  * 
23  * ============LICENSE_END=========================================================
24  */
25
26 package org.onap.appc.dg.ssh.impl;
27
28 import com.fasterxml.jackson.databind.ObjectMapper;
29
30 import java.io.ByteArrayOutputStream;
31 import java.io.IOException;
32 import java.util.Map;
33
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;
42
43
44 public class SshServiceImpl implements SshService {
45
46     private static final EELFLogger logger = EELFManager.getInstance().getApplicationLogger();
47     private static final ObjectMapper mapper = new ObjectMapper();
48
49     private SshAdapter sshAdapter;
50
51     public void setSshAdapter(SshAdapter sshAdapter) {
52         this.sshAdapter = sshAdapter;
53     }
54
55     @Override
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();
62         try {
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);
69             }
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);
80         } finally {
81             sshConnection.disconnect();
82         }
83     }
84
85     private SshConnectionDetails resolveConnectionDetails(String connectionDetailsStr) throws APPCException {
86         SshConnectionDetails connectionDetails = null;
87         try {
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);
92         }
93         return connectionDetails;
94     }
95
96     @Override
97     public void execWithStatusCheck(Map<String, String> params, SvcLogicContext ctx) throws APPCException {
98         exec(params, ctx);
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(']');
106             }
107             throw new APPCException(errmsg.toString());
108         }
109     }
110 }