Fix sonar issues for APPC
[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
39 import org.json.JSONObject;
40 import org.json.JSONException;
41 import org.onap.appc.adapter.ansible.model.AnsibleResult;
42
43 public class AnsibleServerEmulator {
44
45     
46     private String playbookName = "test_playbook.yaml";
47     private String TestId;
48
49     /**
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
54     **/
55     
56     public AnsibleResult Post(String AgentUrl, String payload){
57         AnsibleResult result = new AnsibleResult() ;
58         
59         try{
60             // Request must be a JSON object
61             
62             JSONObject message = new JSONObject(payload);
63             if (message.isNull("Id")){
64                 RejectRequest(result, "Must provide a valid Id");
65             }
66             else if(message.isNull("PlaybookName")){
67                 RejectRequest(result, "Must provide a playbook Name");
68             }
69             else if(!message.getString("PlaybookName").equals(playbookName)){
70                 RejectRequest(result, "Playbook " + message.getString("PlaybookName") + "  not found in catalog");
71             }
72             else{
73                 AcceptRequest(result);
74             }
75         }
76         catch (JSONException e){
77             RejectRequest(result, e.getMessage());
78         }
79
80         return result;
81     }
82
83
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
88
89     **/
90     public AnsibleResult Get(String AgentUrl){
91
92         // Extract id
93         Pattern pattern = Pattern.compile(".*?\\?Id=(.*?)&Type.*");
94         Matcher matcher = pattern.matcher(AgentUrl);
95         String Id = "";
96         
97         if (matcher.find()){
98             Id = matcher.group(1);
99         }
100
101         AnsibleResult get_result = new AnsibleResult();
102
103         JSONObject response = new JSONObject();
104         response.put("StatusCode", 200);
105         response.put("StatusMessage", "FINISHED");
106
107         JSONObject results = new JSONObject();
108
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);
114         
115
116         response.put("Results", results);
117
118         get_result.setStatusCode(200);
119         get_result.setStatusMessage(response.toString());
120
121         return get_result;
122         
123     }
124
125     
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());
132         
133     }
134
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());
141
142     }
143
144 };
145