2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017-2018 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 * ============LICENSE_END=========================================================
27 * Class to emulate responses from the Ansible Server that is compliant with the APP-C Ansible Server
28 * Interface. Used for jUnit tests to verify code is working. In tests it can be used
29 * as a replacement for methods from ConnectionBuilder class
32 package org.onap.appc.adapter.ansible.model;
34 import java.util.regex.Matcher;
35 import java.util.regex.Pattern;
36 import org.apache.commons.lang.StringUtils;
37 import org.json.JSONException;
38 import org.json.JSONObject;
39 import com.att.eelf.configuration.EELFLogger;
40 import com.att.eelf.configuration.EELFManager;
42 public class AnsibleServerEmulator {
44 private final EELFLogger logger = EELFManager.getInstance().getLogger(AnsibleServerEmulator.class);
46 private static final String PLAYBOOK_NAME = "PlaybookName";
47 private static final String STATUS_CODE = "StatusCode";
48 private static final String STATUS_MESSAGE = "StatusMessage";
50 private String playbookName = "test_playbook.yaml";
53 * Method that emulates the response from an Ansible Server
54 * when presented with a request to execute a playbook
55 * Returns an ansible object result. The response code is always the http code 200 (i.e connection successful)
56 * payload is json string as would be sent back by Ansible Server
58 public AnsibleResult Post(String agentUrl, String payload) {
59 AnsibleResult result = new AnsibleResult();
62 // Request must be a JSON object
64 JSONObject message = new JSONObject(payload);
65 if (message.isNull("Id")) {
66 rejectRequest(result, "Must provide a valid Id");
67 } else if (message.isNull(PLAYBOOK_NAME)) {
68 rejectRequest(result, "Must provide a playbook Name");
69 } else if (!message.getString(PLAYBOOK_NAME).equals(playbookName)) {
70 rejectRequest(result, "Playbook " + message.getString(PLAYBOOK_NAME) + " not found in catalog");
72 acceptRequest(result);
74 } catch (JSONException e) {
75 logger.error("JSONException caught", e);
76 rejectRequest(result, e.getMessage());
82 * Method to emulate response from an Ansible
83 * Server when presented with a GET request
84 * Returns an ansibl object result. The response code is always the http code 200 (i.e connection successful)
85 * payload is json string as would be sent back by Ansible Server
88 public AnsibleResult Get(String agentUrl) {
90 Pattern pattern = Pattern.compile(".*?\\?Id=(.*?)&Type.*");
91 Matcher matcher = pattern.matcher(agentUrl);
92 String id = StringUtils.EMPTY;
93 String vmAddress = "192.168.1.10";
96 id = matcher.group(1);
99 AnsibleResult getResult = new AnsibleResult();
101 JSONObject response = new JSONObject();
102 response.put(STATUS_CODE, 200);
103 response.put(STATUS_MESSAGE, "FINISHED");
105 JSONObject results = new JSONObject();
107 JSONObject vmResults = new JSONObject();
108 vmResults.put(STATUS_CODE, 200);
109 vmResults.put(STATUS_MESSAGE, "SUCCESS");
110 vmResults.put("Id", id);
111 results.put(vmAddress, vmResults);
113 response.put("Results", results);
115 getResult.setStatusCode(200);
116 getResult.setStatusMessage(response.toString());
121 private void rejectRequest(AnsibleResult result, String Message) {
122 result.setStatusCode(200);
123 JSONObject response = new JSONObject();
124 response.put(STATUS_CODE, AnsibleResultCodes.REJECTED.getValue());
125 response.put(STATUS_MESSAGE, Message);
126 result.setStatusMessage(response.toString());
129 private void acceptRequest(AnsibleResult result) {
130 result.setStatusCode(200);
131 JSONObject response = new JSONObject();
132 response.put(STATUS_CODE, AnsibleResultCodes.PENDING.getValue());
133 response.put(STATUS_MESSAGE, "PENDING");
134 result.setStatusMessage(response.toString());