d3247525a6a9f9936f11f9724cb39a51a324ccd3
[ccsdk/sli/adaptors.git] / saltstack-adapter / saltstack-adapter-provider / src / main / java / org / onap / ccsdk / sli / adaptors / saltstack / model / SaltstackServerEmulator.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 Saltstack Server that is compliant with the APP-C Saltstack 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.ccsdk.sli.adaptors.saltstack.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 SaltstackServerEmulator {
44
45     private final EELFLogger logger = EELFManager.getInstance().getLogger(SaltstackServerEmulator.class);
46
47     private static final String SALTSTATE_NAME = "SaltStateName";
48     private static final String STATUS_CODE = "StatusCode";
49     private static final String STATUS_MESSAGE = "StatusMessage";
50
51     private String saltStateName = "test_saltState.yaml";
52
53     /**
54      * Method that emulates the response from an Saltstack Server
55      * when presented with a request to execute a saltState
56      * Returns an saltstack object result. The response code is always the ssh code 200 (i.e connection successful)
57      * payload is json string as would be sent back by Saltstack Server
58      **/
59     //TODO: This class is to be altered completely based on the SALTSTACK server communicaiton.
60     public SaltstackResult Connect(String agentUrl, String payload) {
61         SaltstackResult result = new SaltstackResult();
62
63         try {
64             // Request must be a JSON object
65
66             JSONObject message = new JSONObject(payload);
67             if (message.isNull("Id")) {
68                 rejectRequest(result, "Must provide a valid Id");
69             } else if (message.isNull(SALTSTATE_NAME)) {
70                 rejectRequest(result, "Must provide a saltState Name");
71             } else if (!message.getString(SALTSTATE_NAME).equals(saltStateName)) {
72                 rejectRequest(result, "SaltState " + message.getString(SALTSTATE_NAME) + "  not found in catalog");
73             } else {
74                 acceptRequest(result);
75             }
76         } catch (JSONException e) {
77             logger.error("JSONException caught", e);
78             rejectRequest(result, e.getMessage());
79         }
80         return result;
81     }
82
83     /**
84      * Method to emulate response from an Saltstack
85      * Server when presented with a GET request
86      * Returns an ansibl object result. The response code is always the ssh code 200 (i.e connection successful)
87      * payload is json string as would be sent back by Saltstack Server
88      *
89      **/
90     public SaltstackResult Execute(String agentUrl) {
91
92         Pattern pattern = Pattern.compile(".*?\\?Id=(.*?)&Type.*");
93         Matcher matcher = pattern.matcher(agentUrl);
94         String id = StringUtils.EMPTY;
95         String vmAddress = "192.168.1.10";
96
97         if (matcher.find()) {
98             id = matcher.group(1);
99         }
100
101         SaltstackResult getResult = new SaltstackResult();
102
103         JSONObject response = new JSONObject();
104         response.put(STATUS_CODE, 200);
105         response.put(STATUS_MESSAGE, "FINISHED");
106
107         JSONObject results = new JSONObject();
108
109         JSONObject vmResults = new JSONObject();
110         vmResults.put(STATUS_CODE, 200);
111         vmResults.put(STATUS_MESSAGE, "SUCCESS");
112         vmResults.put("Id", id);
113         results.put(vmAddress, vmResults);
114
115         response.put("Results", results);
116
117         getResult.setStatusCode(200);
118         getResult.setStatusMessage(response.toString());
119
120         return getResult;
121     }
122
123     private void rejectRequest(SaltstackResult result, String Message) {
124         result.setStatusCode(200);
125         JSONObject response = new JSONObject();
126         response.put(STATUS_CODE, SaltstackResultCodes.REJECTED.getValue());
127         response.put(STATUS_MESSAGE, Message);
128         result.setStatusMessage(response.toString());
129     }
130
131     private void acceptRequest(SaltstackResult result) {
132         result.setStatusCode(200);
133         JSONObject response = new JSONObject();
134         response.put(STATUS_CODE, SaltstackResultCodes.PENDING.getValue());
135         response.put(STATUS_MESSAGE, "PENDING");
136         result.setStatusMessage(response.toString());
137     }
138 }