2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * Copyright (C) 2017 Amdocs
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END=========================================================
20 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
26 /* Class to emulate responses from the Ansible Server that is compliant with the APP-C Ansible Server
27 Interface. Used for jUnit tests to verify code is working. In tests it can be used
28 as a replacement for methods from ConnectionBuilder class
31 package org.openecomp.appc.adapter.ansible.model;
34 import java.util.regex.Pattern;
35 import java.util.regex.Matcher;
36 import com.google.common.base.Strings;
38 import org.json.JSONObject;
39 import org.json.JSONArray;
40 import org.json.JSONException;
41 import org.openecomp.appc.exceptions.APPCException;
42 import org.openecomp.appc.adapter.ansible.model.AnsibleResult;
44 public class AnsibleServerEmulator {
47 private String playbookName = "test_playbook.yaml";
48 private String TestId;
51 * Method that emulates the response from an Ansible Server
52 when presented with a request to execute a playbook
53 Returns an ansible object result. The response code is always the http code 200 (i.e connection successful)
54 payload is json string as would be sent back by Ansible Server
57 public AnsibleResult Post(String AgentUrl, String payload){
58 AnsibleResult result = new AnsibleResult() ;
61 // Request must be a JSON object
63 JSONObject message = new JSONObject(payload);
64 if (message.isNull("Id")){
65 RejectRequest(result, "Must provide a valid Id");
67 else if(message.isNull("PlaybookName")){
68 RejectRequest(result, "Must provide a playbook Name");
70 else if(!message.getString("PlaybookName").equals(playbookName)){
71 RejectRequest(result, "Playbook " + message.getString("PlaybookName") + " not found in catalog");
74 AcceptRequest(result);
77 catch (JSONException e){
78 RejectRequest(result, e.getMessage());
85 /** Method to emulate response from an Ansible
86 Server when presented with a GET request
87 Returns an ansibl object result. The response code is always the http code 200 (i.e connection successful)
88 payload is json string as would be sent back by Ansible Server
91 public AnsibleResult Get(String AgentUrl){
94 Pattern pattern = Pattern.compile(".*?\\?Id=(.*?)&Type.*");
95 Matcher matcher = pattern.matcher(AgentUrl);
99 Id = matcher.group(1);
102 AnsibleResult get_result = new AnsibleResult();
104 JSONObject response = new JSONObject();
105 response.put("StatusCode", 200);
106 response.put("StatusMessage", "FINISHED");
108 JSONObject results = new JSONObject();
110 JSONObject vm_results = new JSONObject();
111 vm_results.put("StatusCode", 200);
112 vm_results.put("StatusMessage", "SUCCESS");
113 vm_results.put("Id", Id);
114 results.put("192.168.1.10", vm_results);
117 response.put("Results", results);
119 get_result.setStatusCode(200);
120 get_result.setStatusMessage(response.toString());
127 private void RejectRequest(AnsibleResult result, String Message){
128 result.setStatusCode(200);
129 JSONObject response = new JSONObject();
130 response.put("StatusCode", AnsibleResultCodes.REJECTED.getValue());
131 response.put("StatusMessage", Message);
132 result.setStatusMessage(response.toString());
136 private void AcceptRequest(AnsibleResult result){
137 result.setStatusCode(200);
138 JSONObject response = new JSONObject();
139 response.put("StatusCode", AnsibleResultCodes.PENDING.getValue());
140 response.put("StatusMessage", "PENDING");
141 result.setStatusMessage(response.toString());