b708a9b32395b76a13d026e1db9a1a9815698db8
[appc.git] / appc-dg / appc-dg-shared / appc-dg-ssh / src / main / java / org / openecomp / appc / dg / ssh / impl / SshServiceImpl.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
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
12  * 
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  * 
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.
20  * 
21  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22  * ============LICENSE_END=========================================================
23  */
24
25 package org.openecomp.appc.dg.ssh.impl;
26
27 import com.fasterxml.jackson.databind.ObjectMapper;
28
29 import java.io.ByteArrayOutputStream;
30 import java.io.IOException;
31 import java.util.Map;
32
33 import org.openecomp.appc.adapter.ssh.SshAdapter;
34 import org.openecomp.appc.adapter.ssh.SshConnection;
35 import org.openecomp.appc.adapter.ssh.SshConnectionDetails;
36 import org.openecomp.appc.dg.ssh.SshService;
37 import org.openecomp.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;
41
42
43 public class SshServiceImpl implements SshService {
44
45         private static final EELFLogger logger = EELFManager.getInstance().getApplicationLogger();
46         private static final ObjectMapper mapper = new ObjectMapper();
47
48         private SshAdapter sshAdapter;
49
50         public void setSshAdapter(SshAdapter sshAdapter) {
51                 this.sshAdapter = sshAdapter;
52         }
53
54         @Override
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();
61                 try {
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);
68                         }
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);
79                 } finally {
80                         sshConnection.disconnect();
81                 }
82         }
83
84         private SshConnectionDetails resolveConnectionDetails(String connectionDetailsStr) throws APPCException {
85                 SshConnectionDetails connectionDetails = null;
86                 try {
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);
91                 }
92                 return connectionDetails;
93         }
94
95         @Override
96         public void execWithStatusCheck(Map<String, String> params, SvcLogicContext ctx) throws APPCException {
97                 exec(params, ctx);
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(']');
105                         }
106                         throw new APPCException(errmsg.toString());
107                 }
108         }
109 }