7702dc80e266d4747860aefe22b8ee93d4976bfb
[ccsdk/sli/adaptors.git] / saltstack-adapter / saltstack-adapter-provider / src / main / java / org / onap / ccsdk / sli / adaptors / saltstack / impl / ConnectionBuilder.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.onap.ccsdk.sli.adaptors.saltstack.impl;
26
27 import java.io.IOException;
28 import java.security.KeyManagementException;
29 import java.security.KeyStoreException;
30 import java.security.NoSuchAlgorithmException;
31 import java.security.cert.CertificateException;
32 import org.onap.ccsdk.sli.adaptors.saltstack.model.SaltstackResult;
33 import org.onap.ccsdk.sli.adaptors.saltstack.model.SaltstackResultCodes;
34 import org.onap.ccsdk.sli.core.sli.SvcLogicException;
35 import com.att.eelf.configuration.EELFLogger;
36 import com.att.eelf.configuration.EELFManager;
37
38 /**
39  * Returns a custom SSH client
40  * - based on options
41  * - can create one with ssl using an X509 certificate that does NOT have a known CA
42  * - create one which trusts ALL SSL certificates
43  * - return default sshclient (which only trusts known CAs from default cacerts file for process) this is the default
44  * option
45  **/
46 //TODO: This class is to be altered completely based on the SALTSTACK server communication.
47 public class ConnectionBuilder {
48
49     private static final EELFLogger logger = EELFManager.getInstance().getLogger(ConnectionBuilder.class);
50
51
52     /**
53      * Constructor that initializes an ssh client based on certificate
54      **/
55     public ConnectionBuilder(String userName, String userPasswd) throws KeyStoreException, CertificateException, IOException,
56             KeyManagementException, NoSuchAlgorithmException, SvcLogicException {
57
58
59     }
60
61     /**
62      * Constructor which trusts all certificates in a specific java keystore file (assumes a JKS
63      * file)
64      **/
65     public ConnectionBuilder(String certFile) throws KeyStoreException, IOException,
66             KeyManagementException, NoSuchAlgorithmException, CertificateException {
67
68     }
69
70     /**
71      * Connect to SSH server.
72      */
73     public SaltstackResult connect(String agentUrl, String payload) {
74
75         SaltstackResult result = new SaltstackResult();
76         try {
77             //TODO: to implement SSH connected client to Saltstack Server
78         } catch (Exception io) {
79             logger.error("Caught Exception", io);
80             result.setStatusCode(SaltstackResultCodes.IO_EXCEPTION.getValue());
81             result.setStatusMessage(io.getMessage());
82         }
83         return result;
84     }
85
86     /**
87      * Disconnect from SSH server.
88      */
89     public SaltstackResult disConnect(){
90
91         SaltstackResult result = new SaltstackResult();
92         try {
93             //TODO: to implement SSH connected client to Saltstack Server
94         } catch (Exception io) {
95             logger.error("Caught Exception", io);
96             result.setStatusCode(SaltstackResultCodes.IO_EXCEPTION.getValue());
97             result.setStatusMessage(io.getMessage());
98         }
99         return result;
100     }
101
102     /**
103      * Exec remote command over SSH. Return command execution status.
104      * Command output is written to out or err stream.
105      *
106      * @param cmd command to execute
107      * @param out content of sysout will go to this stream
108      * @param err content of syserr will go to this stream
109      * @return command execution status
110      */
111     public SaltstackResult execute(String cmd) {
112
113         SaltstackResult result = new SaltstackResult();
114
115         try {
116             //TODO: to implement SSH command execute
117         } catch (Exception io) {
118             result.setStatusCode(SaltstackResultCodes.IO_EXCEPTION.getValue());
119             result.setStatusMessage(io.getMessage());
120             logger.error("Caught IOException", io);
121         }
122         return result;
123     }
124 }