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.openecomp.appc.adapter.ansible.model;
36 import java.util.regex.Pattern;
37 import java.util.regex.Matcher;
38 import com.google.common.base.Strings;
40 import org.json.JSONObject;
41 import org.json.JSONArray;
42 import org.json.JSONException;
43 import org.openecomp.appc.exceptions.APPCException;
44 import org.openecomp.appc.adapter.ansible.model.AnsibleResult;
46 public class AnsibleServerEmulator {
49 private String playbookName = "test_playbook.yaml";
50 private String TestId;
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
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");
69 else if(message.isNull("PlaybookName")){
70 RejectRequest(result, "Must provide a playbook Name");
72 else if(!message.getString("PlaybookName").equals(playbookName)){
73 RejectRequest(result, "Playbook " + message.getString("PlaybookName") + " not found in catalog");
76 AcceptRequest(result);
79 catch (JSONException e){
80 RejectRequest(result, e.getMessage());
87 /** Method to emulate response from an Ansible
88 Server when presented with a GET request
89 Returns an ansibl object result. The response code is always the http code 200 (i.e connection successful)
90 payload is json string as would be sent back by Ansible Server
93 public AnsibleResult Get(String AgentUrl){
96 Pattern pattern = Pattern.compile(".*?\\?Id=(.*?)&Type.*");
97 Matcher matcher = pattern.matcher(AgentUrl);
101 Id = matcher.group(1);
104 AnsibleResult get_result = new AnsibleResult();
106 JSONObject response = new JSONObject();
107 response.put("StatusCode", 200);
108 response.put("StatusMessage", "FINISHED");
110 JSONObject results = new JSONObject();
112 JSONObject vm_results = new JSONObject();
113 vm_results.put("StatusCode", 200);
114 vm_results.put("StatusMessage", "SUCCESS");
115 vm_results.put("Id", Id);
116 results.put("192.168.1.10", vm_results);
119 response.put("Results", results);
121 get_result.setStatusCode(200);
122 get_result.setStatusMessage(response.toString());
129 private void RejectRequest(AnsibleResult result, String Message){
130 result.setStatusCode(200);
131 JSONObject response = new JSONObject();
132 response.put("StatusCode", AnsibleResultCodes.REJECTED.getValue());
133 response.put("StatusMessage", Message);
134 result.setStatusMessage(response.toString());
138 private void AcceptRequest(AnsibleResult result){
139 result.setStatusCode(200);
140 JSONObject response = new JSONObject();
141 response.put("StatusCode", AnsibleResultCodes.PENDING.getValue());
142 response.put("StatusMessage", "PENDING");
143 result.setStatusMessage(response.toString());