a10a0aeb209c91f509856f5159460e1e1356fdce
[ccsdk/sli.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : SLI
4  * ================================================================================
5  * Copyright (C) 2021 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
20  * ============LICENSE_END=========================================================
21  */
22
23 package org.onap.ccsdk.sli.adaptors.ansible.model;
24
25 import com.att.eelf.configuration.EELFLogger;
26 import com.att.eelf.configuration.EELFManager;
27 import java.util.regex.Matcher;
28 import java.util.regex.Pattern;
29 import org.apache.commons.lang.StringUtils;
30 import org.json.JSONException;
31 import org.json.JSONObject;
32
33 import static org.onap.ccsdk.sli.adaptors.ansible.AnsibleAdapterConstants.PLAYBOOK_NAME;
34 import static org.onap.ccsdk.sli.adaptors.ansible.AnsibleAdapterConstants.STATUS_CODE;
35 import static org.onap.ccsdk.sli.adaptors.ansible.AnsibleAdapterConstants.STATUS_MESSAGE;
36
37 public class AnsibleServerEmulator {
38
39     private final EELFLogger logger = EELFManager.getInstance().getLogger(AnsibleServerEmulator.class);
40
41     /**
42      * Method that emulates the response from an Ansible Server
43      * when presented with a request to execute a playbook
44      * Returns an ansible object result. The response code is always the http code 200 (i.e connection successful)
45      * payload is json string as would be sent back by Ansible Server
46      **/
47     public AnsibleResult post(String payload) {
48         AnsibleResult result = new AnsibleResult();
49
50         try {
51             // Request must be a JSON object
52
53             JSONObject message = new JSONObject(payload);
54             String playbookName = "test_playbook.yaml";
55             if (message.isNull("Id")) {
56                 rejectRequest(result, "Must provide a valid Id");
57             } else if (message.isNull(PLAYBOOK_NAME)) {
58                 rejectRequest(result, "Must provide a playbook Name");
59             } else if (!message.getString(PLAYBOOK_NAME).equals(playbookName)) {
60                 rejectRequest(result, "Playbook " + message.getString(PLAYBOOK_NAME) + "  not found in catalog");
61             } else {
62                 acceptRequest(result);
63             }
64         } catch (JSONException e) {
65             logger.error("JSONException caught", e);
66             rejectRequest(result, e.getMessage());
67         }
68         return result;
69     }
70
71     /**
72      * Method to emulate response from an Ansible
73      * Server when presented with a GET request
74      * Returns an ansibl object result. The response code is always the http code 200 (i.e connection successful)
75      * payload is json string as would be sent back by Ansible Server
76      **/
77     public AnsibleResult get(String agentUrl) {
78
79         Pattern pattern = Pattern.compile(".*?\\?Id=(.*?)&Type.*");
80         Matcher matcher = pattern.matcher(agentUrl);
81         String id = StringUtils.EMPTY;
82         String vmAddress = "192.168.1.10";
83
84         if (matcher.find()) {
85             id = matcher.group(1);
86         }
87
88         AnsibleResult getResult = new AnsibleResult();
89
90         JSONObject response = new JSONObject();
91         response.put(STATUS_CODE, 200);
92         response.put(STATUS_MESSAGE, "FINISHED");
93
94         JSONObject results = new JSONObject();
95
96         JSONObject vmResults = new JSONObject();
97         vmResults.put(STATUS_CODE, 200);
98         vmResults.put(STATUS_MESSAGE, "SUCCESS");
99         vmResults.put("Id", id);
100         results.put(vmAddress, vmResults);
101
102         response.put("Results", results);
103
104         getResult.setStatusCode(200);
105         getResult.setStatusMessage(response.toString());
106
107         return getResult;
108     }
109
110     private void rejectRequest(AnsibleResult result, String Message) {
111         result.setStatusCode(200);
112         JSONObject response = new JSONObject();
113         response.put(STATUS_CODE, AnsibleResultCodes.REJECTED.getValue());
114         response.put(STATUS_MESSAGE, Message);
115         result.setStatusMessage(response.toString());
116     }
117
118     private void acceptRequest(AnsibleResult result) {
119         result.setStatusCode(200);
120         JSONObject response = new JSONObject();
121         response.put(STATUS_CODE, AnsibleResultCodes.PENDING.getValue());
122         response.put(STATUS_MESSAGE, "PENDING");
123         result.setStatusMessage(response.toString());
124     }
125
126 }