Changed to unmaintained
[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-2018 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  * ============LICENSE_END=========================================================
22  */
23
24
25
26 /*
27  * Class to emulate responses from the Ansible Server that is compliant with the APP-C Ansible Server
28  * Interface. Used for jUnit tests to verify code is working. In tests it can be used
29  * as a replacement for methods from ConnectionBuilder class
30  */
31
32 package org.onap.appc.adapter.ansible.model;
33
34 import java.util.regex.Matcher;
35 import java.util.regex.Pattern;
36 import org.apache.commons.lang.StringUtils;
37 import org.json.JSONException;
38 import org.json.JSONObject;
39 import com.att.eelf.configuration.EELFLogger;
40 import com.att.eelf.configuration.EELFManager;
41
42 public class AnsibleServerEmulator {
43
44     private final EELFLogger logger = EELFManager.getInstance().getLogger(AnsibleServerEmulator.class);
45
46     private static final String PLAYBOOK_NAME = "PlaybookName";
47     private static final String STATUS_CODE = "StatusCode";
48     private static final String STATUS_MESSAGE = "StatusMessage";
49
50     private String playbookName = "test_playbook.yaml";
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     public AnsibleResult post(String agentUrl, String payload) {
59         AnsibleResult result = new AnsibleResult();
60
61         try {
62             // Request must be a JSON object
63
64             JSONObject message = new JSONObject(payload);
65             if (message.isNull("Id")) {
66                 rejectRequest(result, "Must provide a valid Id");
67             } else if (message.isNull(PLAYBOOK_NAME)) {
68                 rejectRequest(result, "Must provide a playbook Name");
69             } else if (!message.getString(PLAYBOOK_NAME).equals(playbookName)) {
70                 rejectRequest(result, "Playbook " + message.getString(PLAYBOOK_NAME) + "  not found in catalog");
71             } else {
72                 acceptRequest(result);
73             }
74         } catch (JSONException e) {
75             logger.error("JSONException caught", e);
76             rejectRequest(result, e.getMessage());
77         }
78         return result;
79     }
80
81     /**
82      * Method to emulate response from an Ansible
83      * Server when presented with a GET request
84      * Returns an ansibl object result. The response code is always the http code 200 (i.e connection successful)
85      * payload is json string as would be sent back by Ansible Server
86      *
87      **/
88     public AnsibleResult get(String agentUrl) {
89
90         Pattern pattern = Pattern.compile(".*?\\?Id=(.*?)&Type.*");
91         Matcher matcher = pattern.matcher(agentUrl);
92         String id = StringUtils.EMPTY;
93         String vmAddress = "192.168.1.10";
94
95         if (matcher.find()) {
96             id = matcher.group(1);
97         }
98
99         AnsibleResult getResult = new AnsibleResult();
100
101         JSONObject response = new JSONObject();
102         response.put(STATUS_CODE, 200);
103         response.put(STATUS_MESSAGE, "FINISHED");
104
105         JSONObject results = new JSONObject();
106
107         JSONObject vmResults = new JSONObject();
108         vmResults.put(STATUS_CODE, 200);
109         vmResults.put(STATUS_MESSAGE, "SUCCESS");
110         vmResults.put("Id", id);
111         results.put(vmAddress, vmResults);
112
113         response.put("Results", results);
114
115         getResult.setStatusCode(200);
116         getResult.setStatusMessage(response.toString());
117
118         return getResult;
119     }
120
121     private void rejectRequest(AnsibleResult result, String message) {
122         result.setStatusCode(200);
123         JSONObject response = new JSONObject();
124         response.put(STATUS_CODE, AnsibleResultCodes.REJECTED.getValue());
125         response.put(STATUS_MESSAGE, message);
126         result.setStatusMessage(response.toString());
127     }
128
129     private void acceptRequest(AnsibleResult result) {
130         result.setStatusCode(200);
131         JSONObject response = new JSONObject();
132         response.put(STATUS_CODE, AnsibleResultCodes.PENDING.getValue());
133         response.put(STATUS_MESSAGE, "PENDING");
134         result.setStatusMessage(response.toString());
135     }
136 }