Change code in appc dispatcher for new LCMs in R6
[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-2019 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  * ============LICENSE_END=========================================================
21  */
22
23 package org.onap.appc.dg.ssh.impl;
24
25 import com.fasterxml.jackson.databind.ObjectMapper;
26
27 import java.io.ByteArrayOutputStream;
28 import java.io.IOException;
29 import java.util.Map;
30
31 import org.onap.appc.adapter.ssh.SshAdapter;
32 import org.onap.appc.adapter.ssh.SshConnection;
33 import org.onap.appc.adapter.ssh.SshConnectionDetails;
34 import org.onap.appc.dg.ssh.SshService;
35 import org.onap.appc.exceptions.APPCException;
36 import com.att.eelf.configuration.EELFLogger;
37 import com.att.eelf.configuration.EELFManager;
38 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
39
40
41 public class SshServiceImpl implements SshService {
42
43     private static final EELFLogger logger = EELFManager.getInstance().getApplicationLogger();
44     private static final ObjectMapper mapper = new ObjectMapper();
45
46     private SshAdapter sshAdapter;
47
48     /**
49      * Set sshAdapter.
50      * <p>Used through blueprint.
51      * @param sshAdapter the SshAdapter
52      */
53     public void setSshAdapter(SshAdapter sshAdapter) {
54         this.sshAdapter = sshAdapter;
55     }
56
57     @Override
58     public void exec(Map<String, String> params, SvcLogicContext ctx) throws APPCException {
59         SshConnectionDetails connectionDetails = resolveConnectionDetails(params.get(PARAM_IN_connection_details));
60         String command = params.get(PARAM_IN_command);
61         logger.debug("=> Connecting to SSH server...");
62         SshConnection sshConnection =
63                 sshAdapter.getConnection(connectionDetails.getHost(), connectionDetails.getPort(),
64                     connectionDetails.getUsername(), connectionDetails.getPassword());
65         sshConnection.connect();
66         try {
67             logger.debug("=> Connected to SSH server...");
68             logger.debug("=> Running SSH command...");
69             long timeout = DEF_timeout;
70             String stimeout = params.get(PARAM_IN_timeout);
71             if ((stimeout != null && !stimeout.isEmpty())) {
72                 timeout = Long.parseLong(stimeout);
73             }
74             sshConnection.setExecTimeout(timeout);
75             ByteArrayOutputStream stdout = new ByteArrayOutputStream();
76             ByteArrayOutputStream stderr = new ByteArrayOutputStream();
77             int status = sshConnection.execCommand(command, stdout, stderr);
78             String stdoutRes = stdout.toString();
79             String stderrRes = stderr.toString();
80             logger.debug("=> executed SSH command");
81             ctx.setAttribute(PARAM_OUT_status, String.format("%01d", status));
82             ctx.setAttribute(PARAM_OUT_stdout, stdoutRes);
83             ctx.setAttribute(PARAM_OUT_stderr, stderrRes);
84         } finally {
85             sshConnection.disconnect();
86         }
87     }
88
89     private SshConnectionDetails resolveConnectionDetails(String connectionDetailsStr) throws APPCException {
90         SshConnectionDetails connectionDetails = null;
91         try {
92             connectionDetails = mapper.readValue(connectionDetailsStr, SshConnectionDetails.class);
93             if (0 == connectionDetails.getPort()) connectionDetails.setPort(DEF_port);
94         } catch (IOException e) {
95             throw new APPCException(e);
96         }
97         return connectionDetails;
98     }
99
100     @Override
101     public void execWithStatusCheck(Map<String, String> params, SvcLogicContext ctx) throws APPCException {
102         exec(params, ctx);
103         int status = Integer.parseInt(ctx.getAttribute(PARAM_OUT_status));
104         if (status != DEF_SUCCESS_STATUS) {
105             StringBuilder errmsg = new StringBuilder();
106             errmsg.append("SSH command returned error status [").append(status).append(']');
107             String stderr = ctx.getAttribute(PARAM_OUT_stderr);
108             if ((stderr != null) && !stderr.isEmpty()) {
109                 errmsg.append(". Error: [").append(stderr).append(']');
110             }
111             throw new APPCException(errmsg.toString());
112         }
113     }
114 }