2 * ============LICENSE_START=======================================================
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
13 * http://www.apache.org/licenses/LICENSE-2.0
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.
21 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22 * ============LICENSE_END=========================================================
25 package org.onap.ccsdk.sli.adaptors.saltstack.impl;
28 import java.util.Properties;
29 import org.apache.commons.lang.StringUtils;
30 import org.json.JSONException;
31 import org.json.JSONObject;
32 import org.onap.ccsdk.sli.adaptors.saltstack.SaltstackAdapter;
33 import org.onap.ccsdk.sli.adaptors.saltstack.SaltstackAdapterPropertiesProvider;
34 import org.onap.ccsdk.sli.adaptors.saltstack.model.SaltstackMessageParser;
35 import org.onap.ccsdk.sli.adaptors.saltstack.model.SaltstackResult;
36 import org.onap.ccsdk.sli.adaptors.saltstack.model.SaltstackResultCodes;
37 import org.onap.ccsdk.sli.adaptors.saltstack.model.SaltstackServerEmulator;
38 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
39 import org.onap.ccsdk.sli.core.sli.SvcLogicException;
40 import com.att.eelf.configuration.EELFLogger;
41 import com.att.eelf.configuration.EELFManager;
44 * This class implements the {@link SaltstackAdapter} interface. This interface defines the behaviors
45 * that our service provides.
47 public class SaltstackAdapterImpl implements SaltstackAdapter {
49 private static final long EXEC_TIMEOUT = 120000;
50 private long timeout = EXEC_TIMEOUT;
53 * The constant used to define the service name in the mapped diagnostic context
55 @SuppressWarnings("nls")
56 public static final String MDC_SERVICE = "service";
59 * The constant for the status code for a failed outcome
61 @SuppressWarnings("nls")
62 public static final String OUTCOME_FAILURE = "failure";
65 * The constant for the status code for a successful outcome
67 @SuppressWarnings("nls")
68 public static final String OUTCOME_SUCCESS = "success";
73 private static final String ADAPTER_NAME = "Saltstack Adapter";
74 private static final String APPC_EXCEPTION_CAUGHT = "APPCException caught";
76 private static final String RESULT_CODE_ATTRIBUTE_NAME = "org.onap.appc.adapter.saltstack.result.code";
77 private static final String MESSAGE_ATTRIBUTE_NAME = "org.onap.appc.adapter.saltstack.message";
78 private static final String RESULTS_ATTRIBUTE_NAME = "org.onap.appc.adapter.saltstack.results";
79 private static final String ID_ATTRIBUTE_NAME = "org.onap.appc.adapter.saltstack.Id";
80 private static final String LOG_ATTRIBUTE_NAME = "org.onap.appc.adapter.saltstack.log";
82 private static final String CLIENT_TYPE_PROPERTY_NAME = "org.onap.appc.adapter.saltstack.clientType";
83 private static final String SS_SERVER_USERNAME = "org.onap.appc.adapter.saltstack.userName";
84 private static final String SS_SERVER_PASSWORD = "org.onap.appc.adapter.saltstack.userPasswd";
85 private static final String SS_SERVER_SSH_KEY = "org.onap.appc.adapter.saltstack.sshKey";
89 * The logger to be used
91 private static final EELFLogger logger = EELFManager.getInstance().getLogger(SaltstackAdapterImpl.class);
97 private ConnectionBuilder sshClient;
100 * Saltstack API Message Handlers
102 private SaltstackMessageParser messageProcessor;
105 * indicator whether in test mode
107 private boolean testMode = false;
110 * server emulator object to be used if in test mode
112 private SaltstackServerEmulator testServer;
115 * This default constructor is used as a work around because the activator wasn't getting called
117 public SaltstackAdapterImpl() {
118 initialize(new SaltstackAdapterPropertiesProviderImpl());
120 public SaltstackAdapterImpl(SaltstackAdapterPropertiesProvider propProvider) {
121 initialize(propProvider);
125 * Used for jUnit test and testing interface
127 public SaltstackAdapterImpl(boolean mode) {
129 testServer = new SaltstackServerEmulator();
130 messageProcessor = new SaltstackMessageParser();
134 * Returns the symbolic name of the adapter
136 * @return The adapter name
137 * @see org.onap.appc.adapter.rest.SaltstackAdapter#getAdapterName()
140 public String getAdapterName() {
145 public void setExecTimeout(long timeout) {
146 this.timeout = timeout;
149 * @param rc Method posts info to Context memory in case of an error and throws a
150 * SvcLogicException causing SLI to register this as a failure
152 @SuppressWarnings("static-method")
153 private void doFailure(SvcLogicContext svcLogic, int code, String message) throws SvcLogicException {
155 svcLogic.setStatus(OUTCOME_FAILURE);
156 svcLogic.setAttribute(RESULT_CODE_ATTRIBUTE_NAME, Integer.toString(code));
157 svcLogic.setAttribute(MESSAGE_ATTRIBUTE_NAME, message);
159 throw new SvcLogicException("Saltstack Adapter Error = " + message);
163 * initialize the Saltstack adapter based on default and over-ride configuration data
165 private void initialize(SaltstackAdapterPropertiesProvider propProvider) {
168 Properties props = propProvider.getProperties();
170 // Create the message processor instance
171 messageProcessor = new SaltstackMessageParser();
173 // Create the ssh client instance
174 // type of client is extracted from the property file parameter
175 // org.onap.appc.adapter.saltstack.clientType
177 // 1. BASIC. SSH Connection using username and password
178 // 2. SSH_CERT (trust only those whose certificates have been stored in the SSH KEY file)
179 // 3. DEFAULT SSH Connection without any authentication
182 String clientType = props.getProperty(CLIENT_TYPE_PROPERTY_NAME);
183 logger.info("Saltstack ssh client type set to " + clientType);
185 if ("BASIC".equals(clientType)) {
186 logger.info("Creating ssh client connection");
187 // set path to keystore file
188 String trustStoreFile = props.getProperty(SS_SERVER_USERNAME);
189 String key = props.getProperty(SS_SERVER_PASSWORD);
190 //TODO: Connect to SSH Saltstack server (using username and password) and return client to execute command
192 } else if ("SSH_CERT".equals(clientType)) {
193 // set path to keystore file
194 String key = props.getProperty(SS_SERVER_SSH_KEY);
195 logger.info("Creating ssh client with ssh KEY from " + key);
196 //TODO: Connect to SSH Saltstack server (using SSH Key) and return client to execute command
199 logger.info("Creating ssh client without any Auth");
200 //TODO: Connect to SSH Saltstack server without any Auth
203 } catch (Exception e) {
204 logger.error("Error Initializing Saltstack Adapter due to Unknown Exception", e);
207 logger.info("Initialized Saltstack Adapter");
210 // Public Method to post single command request to execute saltState. Posts the following back
211 // to Svc context memory
212 // org.onap.appc.adapter.saltstack.req.code : 100 if successful
213 // org.onap.appc.adapter.saltstack.req.messge : any message
214 // org.onap.appc.adapter.saltstack.req.Id : a unique uuid to reference the request
216 public void reqExecCommand(Map<String, String> params, SvcLogicContext ctx) throws SvcLogicException {
221 * Public Method to post SLS file request to execute saltState. Posts the following back
222 * to Svc context memory
224 * org.onap.appc.adapter.saltstack.req.code : 100 if successful
225 * org.onap.appc.adapter.saltstack.req.messge : any message
226 * org.onap.appc.adapter.saltstack.req.Id : a unique uuid to reference the request
229 public void reqExecSLS(Map<String, String> params, SvcLogicContext ctx) throws SvcLogicException {
235 * Public method to get logs from saltState execution for a specific request Posts the following back
236 * to Svc context memory
238 * It blocks till the Saltstack Server responds or the session times out very similar to
239 * reqExecResult logs are returned in the DG context variable org.onap.appc.adapter.saltstack.log
242 public void reqExecLog(Map<String, String> params, SvcLogicContext ctx) throws SvcLogicException {