First part of onap rename
[appc.git] / appc-adapters / appc-ansible-adapter / appc-ansible-adapter-bundle / src / main / java / org / onap / appc / adapter / ansible / model / AnsibleServerEmulator.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
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
12  * 
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  * 
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.
20  * 
21  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22  * ============LICENSE_END=========================================================
23  */
24
25
26
27
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
31 */
32
33 package org.onap.appc.adapter.ansible.model;
34
35 import java.util.*;
36 import java.util.regex.Pattern;
37 import java.util.regex.Matcher;
38 import com.google.common.base.Strings;
39
40 import org.json.JSONObject;
41 import org.json.JSONArray;
42 import org.json.JSONException;
43 import org.onap.appc.exceptions.APPCException;
44 import org.onap.appc.adapter.ansible.model.AnsibleResult;
45
46 public class AnsibleServerEmulator {
47
48     
49     private String playbookName = "test_playbook.yaml";
50     private String TestId;
51
52     /**
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
57     **/
58     
59     public AnsibleResult Post(String AgentUrl, String payload){
60         AnsibleResult result = new AnsibleResult() ;
61         
62         try{
63             // Request must be a JSON object
64             
65             JSONObject message = new JSONObject(payload);
66             if (message.isNull("Id")){
67                 RejectRequest(result, "Must provide a valid Id");
68             }
69             else if(message.isNull("PlaybookName")){
70                 RejectRequest(result, "Must provide a playbook Name");
71             }
72             else if(!message.getString("PlaybookName").equals(playbookName)){
73                 RejectRequest(result, "Playbook " + message.getString("PlaybookName") + "  not found in catalog");
74             }
75             else{
76                 AcceptRequest(result);
77             }
78         }
79         catch (JSONException e){
80             RejectRequest(result, e.getMessage());
81         }
82
83         return result;
84     }
85
86
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
91
92     **/
93     public AnsibleResult Get(String AgentUrl){
94
95         // Extract id
96         Pattern pattern = Pattern.compile(".*?\\?Id=(.*?)&Type.*");
97         Matcher matcher = pattern.matcher(AgentUrl);
98         String Id = "";
99         
100         if (matcher.find()){
101             Id = matcher.group(1);
102         }
103
104         AnsibleResult get_result = new AnsibleResult();
105
106         JSONObject response = new JSONObject();
107         response.put("StatusCode", 200);
108         response.put("StatusMessage", "FINISHED");
109
110         JSONObject results = new JSONObject();
111
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);
117         
118
119         response.put("Results", results);
120
121         get_result.setStatusCode(200);
122         get_result.setStatusMessage(response.toString());
123
124         return get_result;
125         
126     }
127
128     
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());
135         
136     }
137
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());
144
145     }
146
147 };
148