Change nexus values to properties
[appc.git] / app-c / appc / appc-dg / appc-dg-shared / appc-dg-ssh / src / main / java / org / openecomp / appc / dg / ssh / impl / SshServiceImpl.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * openECOMP : APP-C
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights
6  *                                              reserved.
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
11  * 
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  * 
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  */
21
22 package org.openecomp.appc.dg.ssh.impl;
23
24 import com.fasterxml.jackson.databind.ObjectMapper;
25
26 import java.io.ByteArrayOutputStream;
27 import java.io.IOException;
28 import java.util.Map;
29
30 import org.openecomp.appc.adapter.ssh.SshAdapter;
31 import org.openecomp.appc.adapter.ssh.SshConnection;
32 import org.openecomp.appc.adapter.ssh.SshConnectionDetails;
33 import org.openecomp.appc.dg.ssh.SshService;
34 import org.openecomp.appc.exceptions.APPCException;
35 import com.att.eelf.configuration.EELFLogger;
36 import com.att.eelf.configuration.EELFManager;
37 import org.openecomp.sdnc.sli.SvcLogicContext;
38
39
40 public class SshServiceImpl implements SshService {
41
42         private static final EELFLogger logger = EELFManager.getInstance().getApplicationLogger();
43         private static final ObjectMapper mapper = new ObjectMapper();
44
45         private SshAdapter sshAdapter;
46
47         public void setSshAdapter(SshAdapter sshAdapter) {
48                 this.sshAdapter = sshAdapter;
49         }
50
51         @Override
52         public void exec(Map<String, String> params, SvcLogicContext ctx) throws APPCException {
53                 SshConnectionDetails connectionDetails = resolveConnectionDetails(params.get(PARAM_IN_connection_details));
54                 String command = params.get(PARAM_IN_command);
55                 logger.debug("=> Connecting to SSH server...");
56                 SshConnection sshConnection = sshAdapter.getConnection(connectionDetails.getHost(), connectionDetails.getPort(), connectionDetails.getUsername(), connectionDetails.getPassword());
57                 sshConnection.connect();
58                 try {
59                         logger.debug("=> Connected to SSH server...");
60                         logger.debug("=> Running SSH command...");
61                         long timeout = DEF_timeout;
62                         String stimeout = params.get(PARAM_IN_timeout);
63                         if ((stimeout != null && !stimeout.isEmpty())) {
64                                 timeout = Long.parseLong(stimeout);
65                         }
66                         sshConnection.setExecTimeout(timeout);
67                         ByteArrayOutputStream stdout = new ByteArrayOutputStream();
68                         ByteArrayOutputStream stderr = new ByteArrayOutputStream();
69                         int status = sshConnection.execCommand(command, stdout, stderr);
70                         String stdoutRes = stdout.toString();
71                         String stderrRes = stderr.toString();
72                         logger.debug("=> executed SSH command");
73                         ctx.setAttribute(PARAM_OUT_status, String.format("%01d", status));
74                         ctx.setAttribute(PARAM_OUT_stdout, stdoutRes);
75                         ctx.setAttribute(PARAM_OUT_stderr, stderrRes);
76                 } finally {
77                         sshConnection.disconnect();
78                 }
79         }
80
81         private SshConnectionDetails resolveConnectionDetails(String connectionDetailsStr) throws APPCException {
82                 SshConnectionDetails connectionDetails = null;
83                 try {
84                         connectionDetails = mapper.readValue(connectionDetailsStr, SshConnectionDetails.class);
85                         if (0 == connectionDetails.getPort()) connectionDetails.setPort(DEF_port);
86                 } catch (IOException e) {
87                         throw new APPCException(e);
88                 }
89                 return connectionDetails;
90         }
91
92         @Override
93         public void execWithStatusCheck(Map<String, String> params, SvcLogicContext ctx) throws APPCException {
94                 exec(params, ctx);
95                 int status = Integer.parseInt(ctx.getAttribute(PARAM_OUT_status));
96                 if(status != DEF_SUCCESS_STATUS) {
97                         StringBuilder errmsg = new StringBuilder();
98                         errmsg.append("SSH command returned error status [").append(status).append(']');
99                         String stderr = ctx.getAttribute(PARAM_OUT_stderr);
100                         if((stderr != null) && !stderr.isEmpty()) {
101                                 errmsg.append(". Error: [").append(stderr).append(']');
102                         }
103                         throw new APPCException(errmsg.toString());
104                 }
105         }
106 }