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=========================================================
28 * Class to emulate responses from the Ansible Server that is compliant with the APP-C Ansible Server
29 * Interface. Used for jUnit tests to verify code is working. In tests it can be used
30 * as a replacement for methods from ConnectionBuilder class
33 package org.onap.appc.adapter.ansible.model;
35 import java.util.regex.Matcher;
36 import java.util.regex.Pattern;
37 import org.apache.commons.lang.StringUtils;
38 import org.json.JSONException;
39 import org.json.JSONObject;
40 import com.att.eelf.configuration.EELFLogger;
41 import com.att.eelf.configuration.EELFManager;
43 public class AnsibleServerEmulator {
45 private final EELFLogger logger = EELFManager.getInstance().getLogger(AnsibleServerEmulator.class);
47 private static final String PLAYBOOK_NAME = "PlaybookName";
48 private static final String STATUS_CODE = "StatusCode";
49 private static final String STATUS_MESSAGE = "StatusMessage";
51 private String playbookName = "test_playbook.yaml";
54 * Method that emulates the response from an Ansible Server
55 * when presented with a request to execute a playbook
56 * Returns an ansible object result. The response code is always the http code 200 (i.e connection successful)
57 * payload is json string as would be sent back by Ansible Server
59 public AnsibleResult Post(String agentUrl, String payload) {
60 AnsibleResult result = new AnsibleResult();
63 // Request must be a JSON object
65 JSONObject message = new JSONObject(payload);
66 if (message.isNull("Id")) {
67 rejectRequest(result, "Must provide a valid Id");
68 } else if (message.isNull(PLAYBOOK_NAME)) {
69 rejectRequest(result, "Must provide a playbook Name");
70 } else if (!message.getString(PLAYBOOK_NAME).equals(playbookName)) {
71 rejectRequest(result, "Playbook " + message.getString(PLAYBOOK_NAME) + " not found in catalog");
73 acceptRequest(result);
75 } catch (JSONException e) {
76 logger.error("JSONException caught", e);
77 rejectRequest(result, e.getMessage());
83 * Method to emulate response from an Ansible
84 * Server when presented with a GET request
85 * Returns an ansibl object result. The response code is always the http code 200 (i.e connection successful)
86 * payload is json string as would be sent back by Ansible Server
89 public AnsibleResult Get(String agentUrl) {
91 Pattern pattern = Pattern.compile(".*?\\?Id=(.*?)&Type.*");
92 Matcher matcher = pattern.matcher(agentUrl);
93 String id = StringUtils.EMPTY;
94 String vmAddress = "192.168.1.10";
97 id = matcher.group(1);
100 AnsibleResult getResult = new AnsibleResult();
102 JSONObject response = new JSONObject();
103 response.put(STATUS_CODE, 200);
104 response.put(STATUS_MESSAGE, "FINISHED");
106 JSONObject results = new JSONObject();
108 JSONObject vmResults = new JSONObject();
109 vmResults.put(STATUS_CODE, 200);
110 vmResults.put(STATUS_MESSAGE, "SUCCESS");
111 vmResults.put("Id", id);
112 results.put(vmAddress, vmResults);
114 response.put("Results", results);
116 getResult.setStatusCode(200);
117 getResult.setStatusMessage(response.toString());
122 private void rejectRequest(AnsibleResult result, String Message) {
123 result.setStatusCode(200);
124 JSONObject response = new JSONObject();
125 response.put(STATUS_CODE, AnsibleResultCodes.REJECTED.getValue());
126 response.put(STATUS_MESSAGE, Message);
127 result.setStatusMessage(response.toString());
130 private void acceptRequest(AnsibleResult result) {
131 result.setStatusCode(200);
132 JSONObject response = new JSONObject();
133 response.put(STATUS_CODE, AnsibleResultCodes.PENDING.getValue());
134 response.put(STATUS_MESSAGE, "PENDING");
135 result.setStatusMessage(response.toString());