summaryrefslogtreecommitdiffstats
path: root/adaptors/ansible-adapter/ansible-adapter-bundle/src/main/java/org/onap/ccsdk/sli/adaptors/ansible/model/AnsibleMessageParser.java
blob: e448f1c475f9b874b814b7ba13ff62d46c4c5773 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
/*-
 * ============LICENSE_START=======================================================
 * ONAP : SLI
 * ================================================================================
 * Copyright (C) 2021 AT&T Intellectual Property. All rights reserved.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
 * ============LICENSE_END=========================================================
 */

package org.onap.ccsdk.sli.adaptors.ansible.model;

import com.google.common.base.Strings;
import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
import org.apache.commons.lang.StringUtils;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.onap.ccsdk.sli.adaptors.ansible.AnsibleAdapterConstants;
import org.onap.ccsdk.sli.core.sli.SvcLogicException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import static org.onap.ccsdk.sli.adaptors.ansible.AnsibleAdapterConstants.*;

/**
 * Class that validates and constructs requests sent/received from
 * Ansible Server
 */
public class AnsibleMessageParser {


    private static final String JSON_ERROR_MESSAGE = "JSONException: Error parsing response";

    private static final Logger LOGGER = LoggerFactory.getLogger(AnsibleMessageParser.class);

    /**
     * Accepts a map of strings and
     * a) validates if all parameters are appropriate (else, throws an exception) and
     * b) if correct returns a JSON object with appropriate key-value pairs to send to the server.
     * <p>
     * Mandatory parameters, that must be in the supplied information to the Ansible Adapter
     * 1. URL to connect to
     * 2. credentials for URL (assume user pswd for now)
     * 3. Playbook name
     */
    public JSONObject reqMessage(Map<String, String> params) throws SvcLogicException {
        final String[] mandatoryTestParams = {AGENT_URL, PLAYBOOK_NAME, USER, PSWD};
        final String[] optionalTestParams = {ENV_PARAMETERS, NODE_LIST, LOCAL_PARAMETERS, TIMEOUT, VERSION, FILE_PARAMETERS,
                                             ACTION, INVENTORY_NAMES, AUTO_NODE_LIST};
        JSONObject jsonPayload = new JSONObject();

        for (String key : mandatoryTestParams) {
            throwIfMissingMandatoryParam(params, key);
            jsonPayload.put(key, params.get(key));
        }

        parseOptionalParams(params, optionalTestParams, jsonPayload);

        // Generate a unique uuid for the test
        String reqId = UUID.randomUUID().toString();
        jsonPayload.put(ID, reqId);
        return jsonPayload;
    }

    /**
     * Method that validates that the Map has enough information
     * to query Ansible server for a result. If so, it returns
     * the appropriate url, else an empty string.
     */
    public String reqUriResult(Map<String, String> params) throws SvcLogicException {
        final String[] mandatoryTestParams = {AGENT_URL, ID, USER, PSWD};
        for (String key : mandatoryTestParams) {
            throwIfMissingMandatoryParam(params, key);
        }
        return params.get(AGENT_URL) + "?Id=" + params.get(ID) + "&Type=GetResult";
    }

    /**
     * Method that validates that the Map has enough information to query Ansible
     * server for a result. If so, it returns the appropriate url, else an empty
     * string.
     */
    public String reqUriResultWithIP(Map<String, String> params, String serverIP) throws SvcLogicException {
        final String[] mandatoryTestParams = {AGENT_URL, ID, USER, PSWD};
        for (String key : mandatoryTestParams) {
            throwIfMissingMandatoryParam(params, key);
        }
        String[] arr1 = params.get(AGENT_URL).split("//", 2);
        String[] arr2 = arr1[1].split(":", 2);
        return arr1[0] + "//" + serverIP + ":" + arr2[1] + "?Id=" + params.get(ID) + "&Type=GetResult";
    }

    /**
     * Method that validates that the Map has enough information to query Ansible
     * server for logs. If so, it populates the appropriate returns the appropriate
     * url, else an empty string.
     */
    public String reqUriLog(Map<String, String> params) throws SvcLogicException {
        final String[] mandatoryTestParams = {AGENT_URL, ID, USER, PSWD};
        for (String mandatoryParam : mandatoryTestParams) {
            throwIfMissingMandatoryParam(params, mandatoryParam);
        }
        return params.get(AGENT_URL) + "?Id=" + params.get(ID) + "&Type=GetLog";
    }

    /**
     * Method that validates that the Map has enough information
     * to query Ansible server for an output. If so, it returns
     * the appropriate url, else an empty string.
     */
    public String reqUriOutput(Map<String, String> params) throws SvcLogicException {
        final String[] mandatoryTestParams = {AGENT_URL, ID, USER, PSWD};
        for (String mandatoryParam : mandatoryTestParams) {
            throwIfMissingMandatoryParam(params, mandatoryParam);
        }
        return params.get(AGENT_URL) + "?Id=" + params.get(ID) + "&Type=GetOutput";
    }

    /**
     * This method parses response from the Ansible Server when we do a post
     * and returns an AnsibleResult object.
     */
    public AnsibleResult parsePostResponse(String input) throws SvcLogicException {
        AnsibleResult ansibleResult;
        try {
            JSONObject postResponse = new JSONObject(input);
            int code = postResponse.getInt(STATUS_CODE);
            int initResponseValue = AnsibleResultCodes.INITRESPONSE.getValue();
            boolean validCode = AnsibleResultCodes.CODE.checkValidCode(initResponseValue, code);
            if (!validCode) {
                throw new SvcLogicException(String.format("Invalid InitResponse code = %s received. MUST be one of %s",
                        code, AnsibleResultCodes.CODE.getValidCodes(initResponseValue)));
            }

            ansibleResult = new AnsibleResult(code, postResponse.getString(STATUS_MESSAGE));
            if (postResponse.has(ANSIBLE_SERVER) && StringUtils.isNotBlank(postResponse.getString(ANSIBLE_SERVER))) {
                ansibleResult.setServerIp(postResponse.getString(ANSIBLE_SERVER));
            }
            if (!postResponse.isNull(OUTPUT)) {
                LOGGER.info("Processing results-output in post response");
                JSONObject output = postResponse.getJSONObject(OUTPUT);
                ansibleResult.setOutput(output.toString());
            }
        } catch (JSONException e) {
            LOGGER.error(JSON_ERROR_MESSAGE, e);
            ansibleResult = new AnsibleResult(600, "Error parsing response = " + input + ". Error = " + e.getMessage());
        }
        return ansibleResult;
    }

    /**
     * This method parses response from an Ansible server when we do a GET for a result
     * and returns an AnsibleResult object.
     **/
    public AnsibleResult parseGetResponse(String input) throws SvcLogicException {
        AnsibleResult ansibleResult = new AnsibleResult();
        try {
            JSONObject postResponse = new JSONObject(input);
            parseGetResponseNested(ansibleResult, postResponse);
        } catch (JSONException e) {
            LOGGER.error(JSON_ERROR_MESSAGE, e);
            ansibleResult = new AnsibleResult(AnsibleResultCodes.INVALID_PAYLOAD.getValue(),
                    "Error parsing response = " + input + ". Error = " + e.getMessage(), "");
        }
        return ansibleResult;
    }

    private void parseGetResponseNested(AnsibleResult ansibleResult, JSONObject postRsp) throws SvcLogicException {
        String messageStatus = postRsp.getString(STATUS_MESSAGE);
        int codeStatus = postRsp.getInt(STATUS_CODE);
        int finalCode = AnsibleResultCodes.FINAL_SUCCESS.getValue();
        boolean valCode = AnsibleResultCodes.CODE.checkValidCode(AnsibleResultCodes.FINALRESPONSE.getValue(), codeStatus);
        if (!valCode) {
            throw new SvcLogicException(String.format("Invalid InitResponse code = %s received. MUST be one of %s",
                    codeStatus, AnsibleResultCodes.CODE.getValidCodes(AnsibleResultCodes.FINALRESPONSE.getValue())));
        }

        ansibleResult.setStatusCode(codeStatus);
        ansibleResult.setStatusMessage(messageStatus);
        ansibleResult.setConfigData("UNKNOWN");
        LOGGER.info("Received response with code = {}, Message = {}", codeStatus, messageStatus);

        if (!postRsp.isNull("Results")) {

            // Results are available. process them
            // Results is a dictionary of the form

            LOGGER.info("Processing results in response");
            JSONObject results = postRsp.getJSONObject("Results");

            LOGGER.info("Get JSON dictionary from Results by Iterating through hosts");
            Iterator<String> hosts = results.keys();
            while (hosts.hasNext()) {
                String host = hosts.next();
                LOGGER.info("Processing host = {}",
                        (host.matches("^[\\w\\-.]+$")) ? host : "[unexpected value, logging suppressed]");
                try {
                    JSONObject hostResponse = results.getJSONObject(host);
                    int subCode = hostResponse.getInt(STATUS_CODE);
                    String message = hostResponse.getString(STATUS_MESSAGE);

                    LOGGER.info("Code = {}, Message = {}", subCode, message);

                    if (subCode != 200 || !"SUCCESS".equals(message)) {
                        finalCode = AnsibleResultCodes.REQ_FAILURE.getValue();
                    }
                    if ((hostResponse.optJSONObject(OUTPUT)) != null) {
                        JSONObject hostResponseObjectInfo = hostResponse.optJSONObject(OUTPUT).optJSONObject("info");
                        JSONObject hostResponseConfigData = hostResponseObjectInfo.optJSONObject("configData");
                        if (hostResponseConfigData != null) {
                            ansibleResult.setConfigData(hostResponseConfigData.toString());
                        }
                    }
                } catch (JSONException e) {
                    LOGGER.error(JSON_ERROR_MESSAGE, e);
                    ansibleResult.setStatusCode(AnsibleResultCodes.INVALID_RESPONSE.getValue());
                    ansibleResult.setStatusMessage(String.format("Error processing response message = %s from host %s",
                            results.getString(host), host));
                    break;
                }
            }
            ansibleResult.setStatusCode(finalCode);

            // We return entire Results object as message
            ansibleResult.setResults(results.toString());
        } else {
            ansibleResult.setStatusCode(AnsibleResultCodes.INVALID_RESPONSE.getValue());
            ansibleResult.setStatusMessage("Results not found in GET for response");
        }
        if (!postRsp.isNull(OUTPUT)) {
            LOGGER.info("Processing results-output in response");
            JSONObject output = postRsp.getJSONObject(OUTPUT);
            ansibleResult.setOutput(output.toString());
        }
    }

    private void parseOptionalParams(Map<String, String> params, String[] optionalTestParams, JSONObject jsonPayload) {

        Set<String> optionalParamsSet = new HashSet<>();
        Collections.addAll(optionalParamsSet, optionalTestParams);

        //@formatter:off
        params.entrySet().stream().filter(entry -> optionalParamsSet.contains(entry.getKey()))
                .filter(entry -> !Strings.isNullOrEmpty(entry.getValue()))
                .forEach(entry -> parseOptionalParam(entry, jsonPayload));
        //@formatter:on
    }

    private void parseOptionalParam(Map.Entry<String, String> params, JSONObject jsonPayload) {
        String key = params.getKey();
        String payload = params.getValue();

        switch (key) {
            case TIMEOUT:
                if (dataIsVariable(payload)) {
                    break;
                }
                int timeout = Integer.parseInt(payload);
                if (timeout < 0) {
                    throw new NumberFormatException(" : specified negative integer for timeout = " + payload);
                }
                jsonPayload.put(key, payload);
                break;
            case AUTO_NODE_LIST:
                if (payload.equalsIgnoreCase("true") || payload.equalsIgnoreCase("false")) {
                    jsonPayload.put(key, payload);
                } else {
                    throw new IllegalArgumentException(" : specified invalid boolean value of AutoNodeList = " + payload);
                }
                break;
            case VERSION:
                if (dataIsVariable(payload)) {
                    break;
                }
            case INVENTORY_NAMES:
                jsonPayload.put(key, payload);
                break;

            case LOCAL_PARAMETERS:
            case ENV_PARAMETERS:
            case EXTRA_VARS:
                JSONObject paramsJson = new JSONObject(payload);
                jsonDataIsVariable(paramsJson);
                jsonPayload.put(key, paramsJson);
                break;

            case NODE_LIST:
                if (payload.startsWith("$")) {
                    break;
                }
                JSONArray paramsArray = new JSONArray(payload);
                jsonPayload.put(key, paramsArray);
                break;

            case FILE_PARAMETERS:
                if (dataIsVariable(payload)) {
                    break;
                }
                jsonPayload.put(key, getFilePayload(payload));
                break;

            default:
                break;
        }
    }

    /**
     * Return payload with escaped newlines
     */
    private JSONObject getFilePayload(String payload) {
        String formattedPayload = payload.replace("\n", "\\n").replace("\r", "\\r");
        return new JSONObject(formattedPayload);
    }

    private void throwIfMissingMandatoryParam(Map<String, String> params, String key) throws SvcLogicException {
        if (!params.containsKey(key)) {
            throw new SvcLogicException(String.format(
                    "Ansible: Mandatory AnsibleAdapter key %s not found in parameters provided by calling agent !", key));
        }
        if (Strings.isNullOrEmpty(params.get(key))) {
            throw new SvcLogicException(String.format(
                    "Ansible: Mandatory AnsibleAdapter key %s not found in parameters provided by calling agent !", key));
        }
        if (StringUtils.startsWith(params.get(key), "$")) {
            throw new SvcLogicException(String.format(
                    "Ansible: Mandatory AnsibleAdapter key %s is a variable", key));
        }
    }

    private boolean dataIsVariable(String payload) {
        return StringUtils.startsWith(payload, "$") || StringUtils.isEmpty(payload);
    }

    private void jsonDataIsVariable(JSONObject paramsJson) {
        LOGGER.info("input json is " + paramsJson);
        String[] keys = JSONObject.getNames(paramsJson);
        for (String k : keys) {
            Object a = paramsJson.get(k);
            if (a instanceof String) {
                if (StringUtils.startsWith(a.toString(), "$") || StringUtils.isEmpty(a.toString())) {
                    LOGGER.info("removing key " + k);
                    paramsJson.remove(k);
                }
            }
        }
        LOGGER.info("returning json as {}", paramsJson);
    }

}