6ee917c8d67103cd8acd5fe77b7e9dcbff3f8a5c
[appc.git] / appc-adapters / appc-ansible-adapter / appc-ansible-adapter-bundle / src / main / java / org / openecomp / appc / adapter / ansible / model / AnsibleServerEmulator.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * APPC
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
11  * 
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  * 
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.
21  */
22
23
24
25
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
29 */
30
31 package org.openecomp.appc.adapter.ansible.model;
32
33 import java.util.*;
34 import java.util.regex.Pattern;
35 import java.util.regex.Matcher;
36 import com.google.common.base.Strings;
37
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;
43
44 public class AnsibleServerEmulator {
45
46     
47     private String playbookName = "test_playbook.yaml";
48     private String TestId;
49
50     /**
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
55     **/
56     
57     public AnsibleResult Post(String AgentUrl, String payload){
58         AnsibleResult result = new AnsibleResult() ;
59         
60         try{
61             // Request must be a JSON object
62             
63             JSONObject message = new JSONObject(payload);
64             if (message.isNull("Id")){
65                 RejectRequest(result, "Must provide a valid Id");
66             }
67             else if(message.isNull("PlaybookName")){
68                 RejectRequest(result, "Must provide a playbook Name");
69             }
70             else if(!message.getString("PlaybookName").equals(playbookName)){
71                 RejectRequest(result, "Playbook " + message.getString("PlaybookName") + "  not found in catalog");
72             }
73             else{
74                 AcceptRequest(result);
75             }
76         }
77         catch (JSONException e){
78             RejectRequest(result, e.getMessage());
79         }
80
81         return result;
82     }
83
84
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
89
90     **/
91     public AnsibleResult Get(String AgentUrl){
92
93         // Extract id
94         Pattern pattern = Pattern.compile(".*?\\?Id=(.*?)&Type.*");
95         Matcher matcher = pattern.matcher(AgentUrl);
96         String Id = "";
97         
98         if (matcher.find()){
99             Id = matcher.group(1);
100         }
101
102         AnsibleResult get_result = new AnsibleResult();
103
104         JSONObject response = new JSONObject();
105         response.put("StatusCode", 200);
106         response.put("StatusMessage", "FINISHED");
107
108         JSONObject results = new JSONObject();
109
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);
115         
116
117         response.put("Results", results);
118
119         get_result.setStatusCode(200);
120         get_result.setStatusMessage(response.toString());
121
122         return get_result;
123         
124     }
125
126     
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());
133         
134     }
135
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());
142
143     }
144
145 };
146