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.appc.adapter.ansible.model;
28 * This module implements the APP-C/Ansible Server interface
29 * based on the REST API specifications
31 import java.util.Collections;
32 import java.util.HashSet;
33 import java.util.Iterator;
36 import java.util.UUID;
37 import org.json.JSONArray;
38 import org.json.JSONException;
39 import org.json.JSONObject;
40 import org.onap.appc.exceptions.APPCException;
41 import com.google.common.base.Strings;
44 * Class that validates and constructs requests sent/received from
47 public class AnsibleMessageParser {
49 private static final String STATUS_MESSAGE_KEY = "StatusMessage";
50 private static final String STATUS_CODE_KEY = "StatusCode";
52 private static final String PLAYBOOK_NAME_KEY = "PlaybookName";
53 private static final String AGENT_URL_KEY = "AgentUrl";
54 private static final String PASS_KEY = "Password";
55 private static final String USER_KEY = "User";
56 private static final String ID_KEY = "Id";
58 private static final String LOCAL_PARAMETERS_OPT_KEY = "LocalParameters";
59 private static final String FILE_PARAMETERS_OPT_KEY = "FileParameters";
60 private static final String ENV_PARAMETERS_OPT_KEY = "EnvParameters";
61 private static final String NODE_LIST_OPT_KEY = "NodeList";
62 private static final String TIMEOUT_OPT_KEY = "Timeout";
63 private static final String VERSION_OPT_KEY = "Version";
64 private static final String ACTION_OPT_KEY = "Action";
67 * Accepts a map of strings and
68 * a) validates if all parameters are appropriate (else, throws an exception) and
69 * b) if correct returns a JSON object with appropriate key-value pairs to send to the server.
71 * Mandatory parameters, that must be in the supplied information to the Ansible Adapter
72 * 1. URL to connect to
73 * 2. credentials for URL (assume username password for now)
77 public JSONObject reqMessage(Map<String, String> params) throws APPCException {
78 final String[] mandatoryTestParams = {AGENT_URL_KEY, PLAYBOOK_NAME_KEY, USER_KEY, PASS_KEY};
79 final String[] optionalTestParams = {ENV_PARAMETERS_OPT_KEY, NODE_LIST_OPT_KEY, LOCAL_PARAMETERS_OPT_KEY,
80 TIMEOUT_OPT_KEY, VERSION_OPT_KEY, FILE_PARAMETERS_OPT_KEY, ACTION_OPT_KEY};
82 JSONObject jsonPayload = new JSONObject();
84 for (String key : mandatoryTestParams) {
85 throwIfMissingMandatoryParam(params, key);
86 jsonPayload.put(key, params.get(key));
89 parseOptionalParams(params, optionalTestParams, jsonPayload);
91 // Generate a unique uuid for the test
92 String reqId = UUID.randomUUID().toString();
93 jsonPayload.put(ID_KEY, reqId);
99 * Method that validates that the Map has enough information
100 * to query Ansible server for a result. If so, it returns
101 * the appropriate url, else an empty string.
103 public String reqUriResult(Map<String, String> params) throws APPCException {
105 final String[] mandatoryTestParams = {AGENT_URL_KEY, ID_KEY, USER_KEY, PASS_KEY};
107 for (String key : mandatoryTestParams) {
108 throwIfMissingMandatoryParam(params, key);
110 return params.get(AGENT_URL_KEY) + "?Id=" + params.get(ID_KEY) + "&Type=GetResult";
114 * Method that validates that the Map has enough information
115 * to query Ansible server for logs. If so, it populates the
116 * appropriate returns the appropriate url, else an empty string.
118 public String reqUriOutput(Map<String, String> params) throws APPCException {
120 final String[] mandatoryTestParams = {AGENT_URL_KEY, ID_KEY, USER_KEY, PASS_KEY};
122 for (String mandatoryParam : mandatoryTestParams) {
123 throwIfMissingMandatoryParam(params, mandatoryParam);
125 return params.get(AGENT_URL_KEY) + "?Id=" + params.get(ID_KEY) + "&Type=GetOutput";
129 * Method that validates that the Map has enough information
130 * to query Ansible server for logs. If so, it populates the appropriate
131 * returns the appropriate url, else an empty string.
133 public String reqUriLog(Map<String, String> params) throws APPCException {
135 final String[] mandatoryTestParams = {AGENT_URL_KEY, ID_KEY, USER_KEY, PASS_KEY};
137 for (String mandatoryParam : mandatoryTestParams) {
138 throwIfMissingMandatoryParam(params, mandatoryParam);
140 return params.get(AGENT_URL_KEY) + "?Id=" + params.get(ID_KEY) + "&Type=GetLog";
144 * This method parses response from the Ansible Server when we do a post
145 * and returns an AnsibleResult object.
147 public AnsibleResult parsePostResponse(String input) throws APPCException {
148 AnsibleResult ansibleResult;
150 JSONObject postResponse = new JSONObject(input);
152 int code = postResponse.getInt(STATUS_CODE_KEY);
153 String msg = postResponse.getString(STATUS_MESSAGE_KEY);
155 int initResponseValue = AnsibleResultCodes.INITRESPONSE.getValue();
156 boolean validCode = AnsibleResultCodes.CODE.checkValidCode(initResponseValue, code);
158 throw new APPCException("Invalid InitResponse code = " + code + " received. MUST be one of "
159 + AnsibleResultCodes.CODE.getValidCodes(initResponseValue));
162 ansibleResult = new AnsibleResult(code, msg);
164 } catch (JSONException e) {
165 ansibleResult = new AnsibleResult(600, "Error parsing response = " + input + ". Error = " + e.getMessage());
167 return ansibleResult;
171 * This method parses response from an Ansible server when we do a GET for a result
172 * and returns an AnsibleResult object.
174 public AnsibleResult parseGetResponse(String input) throws APPCException {
176 AnsibleResult ansibleResult = new AnsibleResult();
177 int finalCode = AnsibleResultCodes.FINAL_SUCCESS.getValue();
180 JSONObject postResponse = new JSONObject(input);
182 int codeStatus = postResponse.getInt(STATUS_CODE_KEY);
183 String messageStatus = postResponse.getString(STATUS_MESSAGE_KEY);
186 AnsibleResultCodes.CODE.checkValidCode(AnsibleResultCodes.FINALRESPONSE.getValue(), codeStatus);
189 throw new APPCException("Invalid FinalResponse code = " + codeStatus + " received. MUST be one of "
190 + AnsibleResultCodes.CODE.getValidCodes(AnsibleResultCodes.FINALRESPONSE.getValue()));
193 ansibleResult.setStatusCode(codeStatus);
194 ansibleResult.setStatusMessage(messageStatus);
196 "Received response with code = " + Integer.toString(codeStatus) + " Message = " + messageStatus);
198 if (!postResponse.isNull("Results")) {
200 // Results are available. process them
201 // Results is a dictionary of the form
202 // {host :{status:s, group:g, message:m, hostname:h}, ...}
203 System.out.println("Processing results in response");
204 JSONObject results = postResponse.getJSONObject("Results");
205 System.out.println("Get JSON dictionary from Results ..");
206 Iterator<String> hosts = results.keys();
207 System.out.println("Iterating through hosts");
209 while (hosts.hasNext()) {
210 String host = hosts.next();
211 System.out.println("Processing host = " + host);
214 JSONObject hostResponse = results.getJSONObject(host);
215 int subCode = hostResponse.getInt(STATUS_CODE_KEY);
216 String message = hostResponse.getString(STATUS_MESSAGE_KEY);
218 System.out.println("Code = " + Integer.toString(subCode) + " Message = " + message);
220 if (subCode != 200 || !message.equals("SUCCESS")) {
221 finalCode = AnsibleResultCodes.REQ_FAILURE.getValue();
223 } catch (JSONException e) {
224 ansibleResult.setStatusCode(AnsibleResultCodes.INVALID_RESPONSE.getValue());
225 ansibleResult.setStatusMessage(String.format(
226 "Error processing response message = %s from host %s", results.getString(host), host));
231 ansibleResult.setStatusCode(finalCode);
233 // We return entire Results object as message
234 ansibleResult.setResults(results.toString());
237 ansibleResult.setStatusCode(AnsibleResultCodes.INVALID_RESPONSE.getValue());
238 ansibleResult.setStatusMessage("Results not found in GET for response");
242 } catch (JSONException e) {
243 ansibleResult = new AnsibleResult(AnsibleResultCodes.INVALID_PAYLOAD.getValue(),
244 "Error parsing response = " + input + ". Error = " + e.getMessage(), "");
248 return ansibleResult;
251 private void parseOptionalParams(Map<String, String> params, String[] optionalTestParams, JSONObject jsonPayload) {
253 Set<String> optionalParamsSet = new HashSet<>();
254 Collections.addAll(optionalParamsSet, optionalTestParams);
259 .filter(entry -> optionalParamsSet.contains(entry.getKey()))
260 .filter(entry -> !Strings.isNullOrEmpty(entry.getValue()))
261 .forEach(entry -> parseOptionalParam(entry, jsonPayload));
265 private void parseOptionalParam(Map.Entry<String, String> params, JSONObject jsonPayload) {
266 String key = params.getKey();
267 String payload = params.getValue();
270 case TIMEOUT_OPT_KEY:
271 int timeout = Integer.parseInt(payload);
273 throw new NumberFormatException(" : specified negative integer for timeout = " + payload);
275 jsonPayload.put(key, payload);
278 case VERSION_OPT_KEY:
279 jsonPayload.put(key, payload);
282 case LOCAL_PARAMETERS_OPT_KEY:
283 case ENV_PARAMETERS_OPT_KEY:
284 JSONObject paramsJson = new JSONObject(payload);
285 jsonPayload.put(key, paramsJson);
288 case NODE_LIST_OPT_KEY:
289 JSONArray paramsArray = new JSONArray(payload);
290 jsonPayload.put(key, paramsArray);
293 case FILE_PARAMETERS_OPT_KEY:
294 // Files may have strings with newlines. Escape them as appropriate
295 String formattedPayload = payload.replace("\n", "\\n").replace("\r", "\\r");
296 JSONObject fileParams = new JSONObject(formattedPayload);
297 jsonPayload.put(key, fileParams);
305 private void throwIfMissingMandatoryParam(Map<String, String> params, String key) throws APPCException {
306 if (!params.containsKey(key)) {
307 throw new APPCException(String.format(
308 "Ansible: Mandatory AnsibleAdapter key %s not found in parameters provided by calling agent !",
311 if (Strings.isNullOrEmpty(params.get(key))) {
312 throw new APPCException(String.format(
313 "Ansible: Mandatory AnsibleAdapter key %s not found in parameters provided by calling agent !",