VNFC Support for Ansible actions
[appc.git] / appc-adapters / appc-ansible-adapter / appc-ansible-adapter-bundle / src / main / java / org / onap / appc / adapter / ansible / model / AnsibleMessageParser.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
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
16  *
17  *      http://www.apache.org/licenses/LICENSE-2.0
18  *
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.
24  *
25  * ============LICENSE_END=========================================================
26  */
27
28 package org.onap.appc.adapter.ansible.model;
29
30 /**
31  * This module implements the APP-C/Ansible Server interface
32  * based on the REST API specifications
33  */
34 import java.util.Collections;
35 import java.util.HashSet;
36 import java.util.Iterator;
37 import java.util.Map;
38 import java.util.Set;
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;
48
49 /**
50  * Class that validates and constructs requests sent/received from Ansible
51  * Server
52  */
53 public class AnsibleMessageParser {
54
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";
74
75     private static final Logger LOGGER = LoggerFactory.getLogger(AnsibleMessageParser.class);
76
77     /**
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.
81      *
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
85      *
86      */
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 };
92
93         JSONObject jsonPayload = new JSONObject();
94
95         for (String key : mandatoryTestParams) {
96             throwIfMissingMandatoryParam(params, key);
97             jsonPayload.put(key, params.get(key));
98         }
99
100         parseOptionalParams(params, optionalTestParams, jsonPayload);
101
102         // Generate a unique uuid for the test
103         String reqId = UUID.randomUUID().toString();
104         jsonPayload.put(ID_KEY, reqId);
105
106         return jsonPayload;
107     }
108
109     /**
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
112      * string.
113      */
114     public String reqUriResult(Map<String, String> params) throws APPCException {
115
116         final String[] mandatoryTestParams = { AGENT_URL_KEY, ID_KEY, USER_KEY, PASS_KEY };
117
118         for (String key : mandatoryTestParams) {
119             throwIfMissingMandatoryParam(params, key);
120         }
121         return params.get(AGENT_URL_KEY) + "?Id=" + params.get(ID_KEY) + "&Type=GetResult";
122     }
123
124     /**
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
127      * string.
128      */
129     public String reqUriResultWithIP(Map<String, String> params, String serverIP) throws APPCException {
130
131         final String[] mandatoryTestParams = { AGENT_URL_KEY, ID_KEY, USER_KEY, PASS_KEY };
132
133         for (String key : mandatoryTestParams) {
134             throwIfMissingMandatoryParam(params, key);
135         }
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";
139     }
140
141     /**
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.
145      */
146     public String reqUriLog(Map<String, String> params) throws APPCException {
147
148         final String[] mandatoryTestParams = { AGENT_URL_KEY, ID_KEY, USER_KEY, PASS_KEY };
149
150         for (String mandatoryParam : mandatoryTestParams) {
151             throwIfMissingMandatoryParam(params, mandatoryParam);
152         }
153         return params.get(AGENT_URL_KEY) + "?Id=" + params.get(ID_KEY) + "&Type=GetLog";
154     }
155
156     /**
157      * This method parses response from the Ansible Server when we do a post and
158      * returns an AnsibleResult object.
159      */
160     public AnsibleResult parsePostResponse(String input) throws APPCException {
161         AnsibleResult ansibleResult;
162         try {
163             JSONObject postResponse = new JSONObject(input);
164
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);
170             else
171                 serverIP = "";
172             int initResponseValue = AnsibleResultCodes.INITRESPONSE.getValue();
173             boolean validCode = AnsibleResultCodes.CODE.checkValidCode(initResponseValue, code);
174             if (!validCode) {
175                 throw new APPCException("Invalid InitResponse code  = " + code + " received. MUST be one of "
176                         + AnsibleResultCodes.CODE.getValidCodes(initResponseValue));
177             }
178
179             ansibleResult = new AnsibleResult(code, msg);
180             if (StringUtils.isNotBlank(serverIP))
181                 ansibleResult.setServerIp(serverIP);
182
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());
187             }
188
189         } catch (JSONException e) {
190             LOGGER.error(JSON_ERROR_MESSAGE, e);
191             ansibleResult = new AnsibleResult(600, "Error parsing response = " + input + ". Error = " + e.getMessage());
192         }
193         return ansibleResult;
194     }
195
196     /**
197      * This method parses response from an Ansible server when we do a GET for a
198      * result and returns an AnsibleResult object.
199      **/
200     public AnsibleResult parseGetResponse(String input) throws APPCException {
201
202         AnsibleResult ansibleResult = new AnsibleResult();
203
204         try {
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(), "");
211         }
212         return ansibleResult;
213     }
214
215     private AnsibleResult parseGetResponseNested(AnsibleResult ansibleResult, JSONObject postRsp) throws APPCException {
216
217         int codeStatus = postRsp.getInt(STATUS_CODE_KEY);
218         String messageStatus = postRsp.getString(STATUS_MESSAGE_KEY);
219         int finalCode = AnsibleResultCodes.FINAL_SUCCESS.getValue();
220
221         boolean valCode = AnsibleResultCodes.CODE.checkValidCode(AnsibleResultCodes.FINALRESPONSE.getValue(),
222                 codeStatus);
223
224         if (!valCode) {
225             throw new APPCException("Invalid FinalResponse code  = " + codeStatus + " received. MUST be one of "
226                     + AnsibleResultCodes.CODE.getValidCodes(AnsibleResultCodes.FINALRESPONSE.getValue()));
227         }
228
229         ansibleResult.setStatusCode(codeStatus);
230         ansibleResult.setStatusMessage(messageStatus);
231         LOGGER.info("Received response with code = {}, Message = {}", codeStatus, messageStatus);
232
233         if (!postRsp.isNull("Results")) {
234
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");
243
244             while (hosts.hasNext()) {
245                 String host = hosts.next();
246                 LOGGER.info("Processing host = {}",
247                         (host.matches("^[\\w\\-.]+$")) ? host : "[unexpected value, logging suppressed]");
248                 try {
249                     JSONObject hostResponse = results.getJSONObject(host);
250                     int subCode = hostResponse.getInt(STATUS_CODE_KEY);
251                     String message = hostResponse.getString(STATUS_MESSAGE_KEY);
252
253                     LOGGER.info("Code = {}, Message = {}", subCode, message);
254
255                     if (subCode != 200 || !(("SUCCESS").equals(message))) {
256                         finalCode = AnsibleResultCodes.REQ_FAILURE.getValue();
257                     }
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));
263                     break;
264                 }
265             }
266
267             ansibleResult.setStatusCode(finalCode);
268
269             // We return entire Results object as message
270             ansibleResult.setResults(results.toString());
271
272         } else {
273             ansibleResult.setStatusCode(AnsibleResultCodes.INVALID_RESPONSE.getValue());
274             ansibleResult.setStatusMessage("Results not found in GET for response");
275         }
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());
280         }
281
282         return ansibleResult;
283     }
284
285     private void parseOptionalParams(Map<String, String> params, String[] optionalTestParams, JSONObject jsonPayload) {
286
287         Set<String> optionalParamsSet = new HashSet<>();
288         Collections.addAll(optionalParamsSet, optionalTestParams);
289
290         // @formatter:off
291         params.entrySet().stream().filter(entry -> optionalParamsSet.contains(entry.getKey()))
292                 .filter(entry -> !Strings.isNullOrEmpty(entry.getValue()))
293                 .forEach(entry -> parseOptionalParam(entry, jsonPayload));
294         // @formatter:on
295     }
296
297     private void parseOptionalParam(Map.Entry<String, String> params, JSONObject jsonPayload) {
298         String key = params.getKey();
299         String payload = params.getValue();
300
301         switch (key) {
302         case TIMEOUT_OPT_KEY:
303             int timeout = Integer.parseInt(payload);
304             if (timeout < 0) {
305                 throw new NumberFormatException(" : specified negative integer for timeout = " + payload);
306             }
307             jsonPayload.put(key, payload);
308             break;
309         case AUTO_NODE_LIST_OPT_KEY:
310             if (payload.equalsIgnoreCase("true") || payload.equalsIgnoreCase("false")) {
311                 jsonPayload.put(key, payload);
312             } else {
313                 throw new IllegalArgumentException(" : specified invalid boolean value of AutoNodeList = " + payload);
314             }
315             break;
316         case VERSION_OPT_KEY:
317         case INVENTORY_NAMES_OPT_KEY:
318             jsonPayload.put(key, payload);
319             break;
320
321         case LOCAL_PARAMETERS_OPT_KEY:
322         case ENV_PARAMETERS_OPT_KEY:
323             JSONObject paramsJson = new JSONObject(payload);
324             jsonPayload.put(key, paramsJson);
325             break;
326
327         case NODE_LIST_OPT_KEY:
328             JSONArray paramsArray = new JSONArray(payload);
329             jsonPayload.put(key, paramsArray);
330             break;
331
332         case FILE_PARAMETERS_OPT_KEY:
333             jsonPayload.put(key, getFilePayload(payload));
334             break;
335
336         default:
337             break;
338         }
339     }
340
341     /**
342      * Return payload with escaped newlines
343      */
344     private JSONObject getFilePayload(String payload) {
345         String formattedPayload = payload.replace("\n", "\\n").replace("\r", "\\r");
346         return new JSONObject(formattedPayload);
347     }
348
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 !",
353                     key));
354         }
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 !",
358                     key));
359         }
360     }
361 }