Code hardening for Ansible Bundle
[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.regex.Matcher;
36 import java.util.regex.Pattern;
37 import org.apache.commons.lang.StringUtils;
38 import org.json.JSONException;
39 import org.json.JSONObject;
40 import com.att.eelf.configuration.EELFLogger;
41 import com.att.eelf.configuration.EELFManager;
42
43 public class AnsibleServerEmulator {
44
45     private final EELFLogger logger = EELFManager.getInstance().getLogger(AnsibleServerEmulator.class);
46
47     private static final String PLAYBOOK_NAME = "PlaybookName";
48     private static final String STATUS_CODE = "StatusCode";
49     private static final String STATUS_MESSAGE = "StatusMessage";
50
51     private String playbookName = "test_playbook.yaml";
52
53     /**
54      * Method that emulates the response from an Ansible Server
55      * when presented with a request to execute a playbook
56      * Returns an ansible object result. The response code is always the http code 200 (i.e connection successful)
57      * payload is json string as would be sent back by Ansible Server
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             } else if (message.isNull(PLAYBOOK_NAME)) {
69                 rejectRequest(result, "Must provide a playbook Name");
70             } else if (!message.getString(PLAYBOOK_NAME).equals(playbookName)) {
71                 rejectRequest(result, "Playbook " + message.getString(PLAYBOOK_NAME) + "  not found in catalog");
72             } else {
73                 acceptRequest(result);
74             }
75         } catch (JSONException e) {
76             logger.error("JSONException caught", e);
77             rejectRequest(result, e.getMessage());
78         }
79         return result;
80     }
81
82     /**
83      * Method to emulate response from an Ansible
84      * Server when presented with a GET request
85      * Returns an ansibl object result. The response code is always the http code 200 (i.e connection successful)
86      * payload is json string as would be sent back by Ansible Server
87      *
88      **/
89     public AnsibleResult Get(String agentUrl) {
90
91         Pattern pattern = Pattern.compile(".*?\\?Id=(.*?)&Type.*");
92         Matcher matcher = pattern.matcher(agentUrl);
93         String id = StringUtils.EMPTY;
94         String vmAddress = "192.168.1.10";
95
96         if (matcher.find()) {
97             id = matcher.group(1);
98         }
99
100         AnsibleResult getResult = new AnsibleResult();
101
102         JSONObject response = new JSONObject();
103         response.put(STATUS_CODE, 200);
104         response.put(STATUS_MESSAGE, "FINISHED");
105
106         JSONObject results = new JSONObject();
107
108         JSONObject vmResults = new JSONObject();
109         vmResults.put(STATUS_CODE, 200);
110         vmResults.put(STATUS_MESSAGE, "SUCCESS");
111         vmResults.put("Id", id);
112         results.put(vmAddress, vmResults);
113
114         response.put("Results", results);
115
116         getResult.setStatusCode(200);
117         getResult.setStatusMessage(response.toString());
118
119         return getResult;
120     }
121
122     private void rejectRequest(AnsibleResult result, String Message) {
123         result.setStatusCode(200);
124         JSONObject response = new JSONObject();
125         response.put(STATUS_CODE, AnsibleResultCodes.REJECTED.getValue());
126         response.put(STATUS_MESSAGE, Message);
127         result.setStatusMessage(response.toString());
128     }
129
130     private void acceptRequest(AnsibleResult result) {
131         result.setStatusCode(200);
132         JSONObject response = new JSONObject();
133         response.put(STATUS_CODE, AnsibleResultCodes.PENDING.getValue());
134         response.put(STATUS_MESSAGE, "PENDING");
135         result.setStatusMessage(response.toString());
136     }
137 }