2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2021 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
19 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
20 * ============LICENSE_END=========================================================
23 package org.onap.ccsdk.sli.adaptors.ansible.model;
25 import com.att.eelf.configuration.EELFLogger;
26 import com.att.eelf.configuration.EELFManager;
27 import java.util.regex.Matcher;
28 import java.util.regex.Pattern;
29 import org.apache.commons.lang.StringUtils;
30 import org.json.JSONException;
31 import org.json.JSONObject;
33 import static org.onap.ccsdk.sli.adaptors.ansible.AnsibleAdapterConstants.PLAYBOOK_NAME;
34 import static org.onap.ccsdk.sli.adaptors.ansible.AnsibleAdapterConstants.STATUS_CODE;
35 import static org.onap.ccsdk.sli.adaptors.ansible.AnsibleAdapterConstants.STATUS_MESSAGE;
37 public class AnsibleServerEmulator {
39 private final EELFLogger logger = EELFManager.getInstance().getLogger(AnsibleServerEmulator.class);
42 * Method that emulates the response from an Ansible Server
43 * when presented with a request to execute a playbook
44 * Returns an ansible object result. The response code is always the http code 200 (i.e connection successful)
45 * payload is json string as would be sent back by Ansible Server
47 public AnsibleResult post(String payload) {
48 AnsibleResult result = new AnsibleResult();
51 // Request must be a JSON object
53 JSONObject message = new JSONObject(payload);
54 String playbookName = "test_playbook.yaml";
55 if (message.isNull("Id")) {
56 rejectRequest(result, "Must provide a valid Id");
57 } else if (message.isNull(PLAYBOOK_NAME)) {
58 rejectRequest(result, "Must provide a playbook Name");
59 } else if (!message.getString(PLAYBOOK_NAME).equals(playbookName)) {
60 rejectRequest(result, "Playbook " + message.getString(PLAYBOOK_NAME) + " not found in catalog");
62 acceptRequest(result);
64 } catch (JSONException e) {
65 logger.error("JSONException caught", e);
66 rejectRequest(result, e.getMessage());
72 * Method to emulate response from an Ansible
73 * Server when presented with a GET request
74 * Returns an ansibl object result. The response code is always the http code 200 (i.e connection successful)
75 * payload is json string as would be sent back by Ansible Server
77 public AnsibleResult get(String agentUrl) {
79 Pattern pattern = Pattern.compile(".*?\\?Id=(.*?)&Type.*");
80 Matcher matcher = pattern.matcher(agentUrl);
81 String id = StringUtils.EMPTY;
82 String vmAddress = "192.168.1.10";
85 id = matcher.group(1);
88 AnsibleResult getResult = new AnsibleResult();
90 JSONObject response = new JSONObject();
91 response.put(STATUS_CODE, 200);
92 response.put(STATUS_MESSAGE, "FINISHED");
94 JSONObject results = new JSONObject();
96 JSONObject vmResults = new JSONObject();
97 vmResults.put(STATUS_CODE, 200);
98 vmResults.put(STATUS_MESSAGE, "SUCCESS");
99 vmResults.put("Id", id);
100 results.put(vmAddress, vmResults);
102 response.put("Results", results);
104 getResult.setStatusCode(200);
105 getResult.setStatusMessage(response.toString());
110 private void rejectRequest(AnsibleResult result, String Message) {
111 result.setStatusCode(200);
112 JSONObject response = new JSONObject();
113 response.put(STATUS_CODE, AnsibleResultCodes.REJECTED.getValue());
114 response.put(STATUS_MESSAGE, Message);
115 result.setStatusMessage(response.toString());
118 private void acceptRequest(AnsibleResult result) {
119 result.setStatusCode(200);
120 JSONObject response = new JSONObject();
121 response.put(STATUS_CODE, AnsibleResultCodes.PENDING.getValue());
122 response.put(STATUS_MESSAGE, "PENDING");
123 result.setStatusMessage(response.toString());