2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Copyright (C) 2017 Amdocs
8 * =============================================================================
9 * Modifications Copyright (C) 2019 IBM
10 * =============================================================================
11 * Modifications Copyright (C) 2019 Orange
12 * =============================================================================
13 * Licensed under the Apache License, Version 2.0 (the "License");
14 * you may not use this file except in compliance with the License.
15 * You may obtain a copy of the License at
17 * http://www.apache.org/licenses/LICENSE-2.0
19 * Unless required by applicable law or agreed to in writing, software
20 * distributed under the License is distributed on an "AS IS" BASIS,
21 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22 * See the License for the specific language governing permissions and
23 * limitations under the License.
25 * ============LICENSE_END=========================================================
28 package org.onap.appc.adapter.ansible.model;
31 * This module implements the APP-C/Ansible Server interface
32 * based on the REST API specifications
34 import java.util.Collections;
35 import java.util.HashSet;
36 import java.util.Iterator;
39 import java.util.UUID;
40 import org.json.JSONArray;
41 import org.json.JSONException;
42 import org.json.JSONObject;
43 import org.onap.appc.exceptions.APPCException;
44 import com.google.common.base.Strings;
45 import org.slf4j.Logger;
46 import org.slf4j.LoggerFactory;
47 import org.apache.commons.lang.StringUtils;
50 * Class that validates and constructs requests sent/received from Ansible
53 public class AnsibleMessageParser {
55 private static final String STATUS_MESSAGE_KEY = "StatusMessage";
56 private static final String STATUS_CODE_KEY = "StatusCode";
57 private static final String SERVER_IP_KEY = "AnsibleServer";
58 private static final String PLAYBOOK_NAME_KEY = "PlaybookName";
59 private static final String AGENT_URL_KEY = "AgentUrl";
60 private static final String PASS_KEY = "Password";
61 private static final String USER_KEY = "User";
62 private static final String ID_KEY = "Id";
63 private static final String LOCAL_PARAMETERS_OPT_KEY = "LocalParameters";
64 private static final String FILE_PARAMETERS_OPT_KEY = "FileParameters";
65 private static final String ENV_PARAMETERS_OPT_KEY = "EnvParameters";
66 private static final String NODE_LIST_OPT_KEY = "NodeList";
67 private static final String AUTO_NODE_LIST_OPT_KEY = "AutoNodeList";
68 private static final String TIMEOUT_OPT_KEY = "Timeout";
69 private static final String VERSION_OPT_KEY = "Version";
70 private static final String INVENTORY_NAMES_OPT_KEY = "InventoryNames";
71 private static final String ACTION_OPT_KEY = "Action";
72 private static final String OUTPUT_OPT_KEY = "Output";
73 private static final String JSON_ERROR_MESSAGE = "JSONException: Error parsing response";
75 private static final Logger LOGGER = LoggerFactory.getLogger(AnsibleMessageParser.class);
78 * Accepts a map of strings and a) validates if all parameters are appropriate
79 * (else, throws an exception) and b) if correct returns a JSON object with
80 * appropriate key-value pairs to send to the server.
82 * Mandatory parameters, that must be in the supplied information to the Ansible
83 * Adapter 1. URL to connect to 2. credentials for URL (assume username password
84 * for now) 3. Playbook name
87 public JSONObject reqMessage(Map<String, String> params) throws APPCException {
88 final String[] mandatoryTestParams = { AGENT_URL_KEY, PLAYBOOK_NAME_KEY, USER_KEY, PASS_KEY };
89 final String[] optionalTestParams = { ENV_PARAMETERS_OPT_KEY, NODE_LIST_OPT_KEY, LOCAL_PARAMETERS_OPT_KEY,
90 TIMEOUT_OPT_KEY, VERSION_OPT_KEY, FILE_PARAMETERS_OPT_KEY, ACTION_OPT_KEY, INVENTORY_NAMES_OPT_KEY,
91 AUTO_NODE_LIST_OPT_KEY };
93 JSONObject jsonPayload = new JSONObject();
95 for (String key : mandatoryTestParams) {
96 throwIfMissingMandatoryParam(params, key);
97 jsonPayload.put(key, params.get(key));
100 parseOptionalParams(params, optionalTestParams, jsonPayload);
102 // Generate a unique uuid for the test
103 String reqId = UUID.randomUUID().toString();
104 jsonPayload.put(ID_KEY, reqId);
110 * Method that validates that the Map has enough information to query Ansible
111 * server for a result. If so, it returns the appropriate url, else an empty
114 public String reqUriResult(Map<String, String> params) throws APPCException {
116 final String[] mandatoryTestParams = { AGENT_URL_KEY, ID_KEY, USER_KEY, PASS_KEY };
118 for (String key : mandatoryTestParams) {
119 throwIfMissingMandatoryParam(params, key);
121 return params.get(AGENT_URL_KEY) + "?Id=" + params.get(ID_KEY) + "&Type=GetResult";
125 * Method that validates that the Map has enough information to query Ansible
126 * server for a result. If so, it returns the appropriate url, else an empty
129 public String reqUriResultWithIP(Map<String, String> params, String serverIP) throws APPCException {
131 final String[] mandatoryTestParams = { AGENT_URL_KEY, ID_KEY, USER_KEY, PASS_KEY };
133 for (String key : mandatoryTestParams) {
134 throwIfMissingMandatoryParam(params, key);
136 String[] arr1 = params.get(AGENT_URL_KEY).split("//", 2);
137 String[] arr2 = arr1[1].split(":", 2);
138 return arr1[0] + "//" + serverIP + ":" + arr2[1] + "?Id=" + params.get(ID_KEY) + "&Type=GetResult";
142 * Method that validates that the Map has enough information to query Ansible
143 * server for logs. If so, it populates the appropriate returns the appropriate
144 * url, else an empty string.
146 public String reqUriLog(Map<String, String> params) throws APPCException {
148 final String[] mandatoryTestParams = { AGENT_URL_KEY, ID_KEY, USER_KEY, PASS_KEY };
150 for (String mandatoryParam : mandatoryTestParams) {
151 throwIfMissingMandatoryParam(params, mandatoryParam);
153 return params.get(AGENT_URL_KEY) + "?Id=" + params.get(ID_KEY) + "&Type=GetLog";
157 * This method parses response from the Ansible Server when we do a post and
158 * returns an AnsibleResult object.
160 public AnsibleResult parsePostResponse(String input) throws APPCException {
161 AnsibleResult ansibleResult;
163 JSONObject postResponse = new JSONObject(input);
165 int code = postResponse.getInt(STATUS_CODE_KEY);
166 String msg = postResponse.getString(STATUS_MESSAGE_KEY);
167 String serverIP = "";
168 if (postResponse.has(SERVER_IP_KEY))
169 serverIP = postResponse.getString(SERVER_IP_KEY);
172 int initResponseValue = AnsibleResultCodes.INITRESPONSE.getValue();
173 boolean validCode = AnsibleResultCodes.CODE.checkValidCode(initResponseValue, code);
175 throw new APPCException("Invalid InitResponse code = " + code + " received. MUST be one of "
176 + AnsibleResultCodes.CODE.getValidCodes(initResponseValue));
179 ansibleResult = new AnsibleResult(code, msg);
180 if (StringUtils.isNotBlank(serverIP))
181 ansibleResult.setServerIp(serverIP);
183 if (!postResponse.isNull(OUTPUT_OPT_KEY)) {
184 LOGGER.info("Processing results-output in post response");
185 JSONObject output = postResponse.getJSONObject(OUTPUT_OPT_KEY);
186 ansibleResult.setOutput(output.toString());
189 } catch (JSONException e) {
190 LOGGER.error(JSON_ERROR_MESSAGE, e);
191 ansibleResult = new AnsibleResult(600, "Error parsing response = " + input + ". Error = " + e.getMessage());
193 return ansibleResult;
197 * This method parses response from an Ansible server when we do a GET for a
198 * result and returns an AnsibleResult object.
200 public AnsibleResult parseGetResponse(String input) throws APPCException {
202 AnsibleResult ansibleResult = new AnsibleResult();
205 JSONObject postResponse = new JSONObject(input);
206 ansibleResult = parseGetResponseNested(ansibleResult, postResponse);
207 } catch (JSONException e) {
208 LOGGER.error(JSON_ERROR_MESSAGE, e);
209 ansibleResult = new AnsibleResult(AnsibleResultCodes.INVALID_PAYLOAD.getValue(),
210 "Error parsing response = " + input + ". Error = " + e.getMessage(), "");
212 return ansibleResult;
215 private AnsibleResult parseGetResponseNested(AnsibleResult ansibleResult, JSONObject postRsp) throws APPCException {
217 int codeStatus = postRsp.getInt(STATUS_CODE_KEY);
218 String messageStatus = postRsp.getString(STATUS_MESSAGE_KEY);
219 int finalCode = AnsibleResultCodes.FINAL_SUCCESS.getValue();
221 boolean valCode = AnsibleResultCodes.CODE.checkValidCode(AnsibleResultCodes.FINALRESPONSE.getValue(),
225 throw new APPCException("Invalid FinalResponse code = " + codeStatus + " received. MUST be one of "
226 + AnsibleResultCodes.CODE.getValidCodes(AnsibleResultCodes.FINALRESPONSE.getValue()));
229 ansibleResult.setStatusCode(codeStatus);
230 ansibleResult.setStatusMessage(messageStatus);
231 LOGGER.info("Received response with code = {}, Message = {}", codeStatus, messageStatus);
233 if (!postRsp.isNull("Results")) {
235 // Results are available. process them
236 // Results is a dictionary of the form
237 // {host :{status:s, group:g, message:m, hostname:h}, ...}
238 LOGGER.info("Processing results in response");
239 JSONObject results = postRsp.getJSONObject("Results");
240 LOGGER.info("Get JSON dictionary from Results ..");
241 Iterator<String> hosts = results.keys();
242 LOGGER.info("Iterating through hosts");
244 while (hosts.hasNext()) {
245 String host = hosts.next();
246 LOGGER.info("Processing host = {}",
247 (host.matches("^[\\w\\-.]+$")) ? host : "[unexpected value, logging suppressed]");
249 JSONObject hostResponse = results.getJSONObject(host);
250 int subCode = hostResponse.getInt(STATUS_CODE_KEY);
251 String message = hostResponse.getString(STATUS_MESSAGE_KEY);
253 LOGGER.info("Code = {}, Message = {}", subCode, message);
255 if (subCode != 200 || !(("SUCCESS").equals(message))) {
256 finalCode = AnsibleResultCodes.REQ_FAILURE.getValue();
258 } catch (JSONException e) {
259 LOGGER.error(JSON_ERROR_MESSAGE, e);
260 ansibleResult.setStatusCode(AnsibleResultCodes.INVALID_RESPONSE.getValue());
261 ansibleResult.setStatusMessage(String.format("Error processing response message = %s from host %s",
262 results.getString(host), host));
267 ansibleResult.setStatusCode(finalCode);
269 // We return entire Results object as message
270 ansibleResult.setResults(results.toString());
273 ansibleResult.setStatusCode(AnsibleResultCodes.INVALID_RESPONSE.getValue());
274 ansibleResult.setStatusMessage("Results not found in GET for response");
276 if (!postRsp.isNull(OUTPUT_OPT_KEY)) {
277 LOGGER.info("Processing results-output in response");
278 JSONObject output = postRsp.getJSONObject(OUTPUT_OPT_KEY);
279 ansibleResult.setOutput(output.toString());
282 return ansibleResult;
285 private void parseOptionalParams(Map<String, String> params, String[] optionalTestParams, JSONObject jsonPayload) {
287 Set<String> optionalParamsSet = new HashSet<>();
288 Collections.addAll(optionalParamsSet, optionalTestParams);
291 params.entrySet().stream().filter(entry -> optionalParamsSet.contains(entry.getKey()))
292 .filter(entry -> !Strings.isNullOrEmpty(entry.getValue()))
293 .forEach(entry -> parseOptionalParam(entry, jsonPayload));
297 private void parseOptionalParam(Map.Entry<String, String> params, JSONObject jsonPayload) {
298 String key = params.getKey();
299 String payload = params.getValue();
302 case TIMEOUT_OPT_KEY:
303 int timeout = Integer.parseInt(payload);
305 throw new NumberFormatException(" : specified negative integer for timeout = " + payload);
307 jsonPayload.put(key, payload);
309 case AUTO_NODE_LIST_OPT_KEY:
310 if (payload.equalsIgnoreCase("true") || payload.equalsIgnoreCase("false")) {
311 jsonPayload.put(key, payload);
313 throw new IllegalArgumentException(" : specified invalid boolean value of AutoNodeList = " + payload);
316 case VERSION_OPT_KEY:
317 case INVENTORY_NAMES_OPT_KEY:
318 jsonPayload.put(key, payload);
321 case LOCAL_PARAMETERS_OPT_KEY:
322 case ENV_PARAMETERS_OPT_KEY:
323 JSONObject paramsJson = new JSONObject(payload);
324 jsonPayload.put(key, paramsJson);
327 case NODE_LIST_OPT_KEY:
328 JSONArray paramsArray = new JSONArray(payload);
329 jsonPayload.put(key, paramsArray);
332 case FILE_PARAMETERS_OPT_KEY:
333 jsonPayload.put(key, getFilePayload(payload));
342 * Return payload with escaped newlines
344 private JSONObject getFilePayload(String payload) {
345 String formattedPayload = payload.replace("\n", "\\n").replace("\r", "\\r");
346 return new JSONObject(formattedPayload);
349 private void throwIfMissingMandatoryParam(Map<String, String> params, String key) throws APPCException {
350 if (!params.containsKey(key)) {
351 throw new APPCException(String.format(
352 "Ansible: Mandatory AnsibleAdapter key %s not found in parameters provided by calling agent !",
355 if (Strings.isNullOrEmpty(params.get(key))) {
356 throw new APPCException(String.format(
357 "Ansible: Mandatory AnsibleAdapter key %s not found in parameters provided by calling agent !",