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;
36 import java.util.regex.Pattern;
37 import java.util.regex.Matcher;
39 import org.json.JSONObject;
40 import org.json.JSONException;
41 import org.onap.appc.adapter.ansible.model.AnsibleResult;
43 public class AnsibleServerEmulator {
46 private String playbookName = "test_playbook.yaml";
47 private String TestId;
50 * Method that emulates the response from an Ansible Server
51 when presented with a request to execute a playbook
52 Returns an ansible object result. The response code is always the http code 200 (i.e connection successful)
53 payload is json string as would be sent back by Ansible Server
56 public AnsibleResult Post(String AgentUrl, String payload){
57 AnsibleResult result = new AnsibleResult() ;
60 // Request must be a JSON object
62 JSONObject message = new JSONObject(payload);
63 if (message.isNull("Id")){
64 RejectRequest(result, "Must provide a valid Id");
66 else if(message.isNull("PlaybookName")){
67 RejectRequest(result, "Must provide a playbook Name");
69 else if(!message.getString("PlaybookName").equals(playbookName)){
70 RejectRequest(result, "Playbook " + message.getString("PlaybookName") + " not found in catalog");
73 AcceptRequest(result);
76 catch (JSONException e){
77 RejectRequest(result, e.getMessage());
84 /** Method to emulate response from an Ansible
85 Server when presented with a GET request
86 Returns an ansibl object result. The response code is always the http code 200 (i.e connection successful)
87 payload is json string as would be sent back by Ansible Server
90 public AnsibleResult Get(String AgentUrl){
93 Pattern pattern = Pattern.compile(".*?\\?Id=(.*?)&Type.*");
94 Matcher matcher = pattern.matcher(AgentUrl);
98 Id = matcher.group(1);
101 AnsibleResult get_result = new AnsibleResult();
103 JSONObject response = new JSONObject();
104 response.put("StatusCode", 200);
105 response.put("StatusMessage", "FINISHED");
107 JSONObject results = new JSONObject();
109 JSONObject vm_results = new JSONObject();
110 vm_results.put("StatusCode", 200);
111 vm_results.put("StatusMessage", "SUCCESS");
112 vm_results.put("Id", Id);
113 results.put("192.168.1.10", vm_results);
116 response.put("Results", results);
118 get_result.setStatusCode(200);
119 get_result.setStatusMessage(response.toString());
126 private void RejectRequest(AnsibleResult result, String Message){
127 result.setStatusCode(200);
128 JSONObject response = new JSONObject();
129 response.put("StatusCode", AnsibleResultCodes.REJECTED.getValue());
130 response.put("StatusMessage", Message);
131 result.setStatusMessage(response.toString());
135 private void AcceptRequest(AnsibleResult result){
136 result.setStatusCode(200);
137 JSONObject response = new JSONObject();
138 response.put("StatusCode", AnsibleResultCodes.PENDING.getValue());
139 response.put("StatusMessage", "PENDING");
140 result.setStatusMessage(response.toString());