reqExec API implemented for saltstack
[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.Map;
36 import java.util.regex.Matcher;
37 import java.util.regex.Pattern;
38 import org.apache.commons.lang.StringUtils;
39 import org.json.JSONException;
40 import org.json.JSONObject;
41 import com.att.eelf.configuration.EELFLogger;
42 import com.att.eelf.configuration.EELFManager;
43
44 public class SaltstackServerEmulator {
45
46     private final EELFLogger logger = EELFManager.getInstance().getLogger(SaltstackServerEmulator.class);
47
48     private static final String SALTSTATE_NAME = "SaltStateName";
49     private static final String STATUS_CODE = "StatusCode";
50     private static final String STATUS_MESSAGE = "StatusMessage";
51
52     private String saltStateName = "test_saltState.yaml";
53
54     /**
55      * Method that emulates the response from an Saltstack Server
56      * when presented with a request to execute a saltState
57      * Returns an saltstack object result. The response code is always the ssh code 200 (i.e connection successful)
58      * payload is json string as would be sent back by Saltstack Server
59      **/
60     public SaltstackResult MockReqExec(Map<String, String> params) {
61         SaltstackResult result = new SaltstackResult();
62
63         try {
64             if (params.get("Test") == "fail") {
65                 result = rejectRequest(result, "Must provide a valid Id");
66             } else {
67                 result = acceptRequest(result);
68             }
69         } catch (Exception e) {
70             logger.error("JSONException caught", e);
71             rejectRequest(result, e.getMessage());
72         }
73         return result;
74     }
75
76     /**
77      * Method that emulates the response from an Saltstack Server
78      * when presented with a request to execute a saltState
79      * Returns an saltstack object result. The response code is always the ssh code 200 (i.e connection successful)
80      * payload is json string as would be sent back by Saltstack Server
81      **/
82     //TODO: This class is to be altered completely based on the SALTSTACK server communicaiton.
83     public SaltstackResult Connect(Map<String, String> params) {
84         SaltstackResult result = new SaltstackResult();
85
86         try {
87             // Request must be a JSON object
88
89             JSONObject message = new JSONObject();
90             if (message.isNull("Id")) {
91                 rejectRequest(result, "Must provide a valid Id");
92             } else if (message.isNull(SALTSTATE_NAME)) {
93                 rejectRequest(result, "Must provide a saltState Name");
94             } else if (!message.getString(SALTSTATE_NAME).equals(saltStateName)) {
95                 rejectRequest(result, "SaltState " + message.getString(SALTSTATE_NAME) + "  not found in catalog");
96             } else {
97                 acceptRequest(result);
98             }
99         } catch (JSONException e) {
100             logger.error("JSONException caught", e);
101             rejectRequest(result, e.getMessage());
102         }
103         return result;
104     }
105
106     /**
107      * Method to emulate response from an Saltstack
108      * Server when presented with a GET request
109      * Returns an saltstack object result. The response code is always the ssh code 200 (i.e connection successful)
110      * payload is json string as would be sent back by Saltstack Server
111      *
112      **/
113     public SaltstackResult Execute(String agentUrl) {
114
115         Pattern pattern = Pattern.compile(".*?\\?Id=(.*?)&Type.*");
116         Matcher matcher = pattern.matcher(agentUrl);
117         String id = StringUtils.EMPTY;
118         String vmAddress = "192.168.1.10";
119
120         if (matcher.find()) {
121             id = matcher.group(1);
122         }
123
124         SaltstackResult getResult = new SaltstackResult();
125
126         JSONObject response = new JSONObject();
127         response.put(STATUS_CODE, 200);
128         response.put(STATUS_MESSAGE, "FINISHED");
129
130         JSONObject results = new JSONObject();
131
132         JSONObject vmResults = new JSONObject();
133         vmResults.put(STATUS_CODE, 200);
134         vmResults.put(STATUS_MESSAGE, "SUCCESS");
135         vmResults.put("Id", id);
136         results.put(vmAddress, vmResults);
137
138         response.put("Results", results);
139
140         getResult.setStatusCode(200);
141         getResult.setStatusMessage(response.toString());
142
143         return getResult;
144     }
145
146     private SaltstackResult rejectRequest(SaltstackResult result, String Message) {
147         result.setStatusCode(SaltstackResultCodes.REJECTED.getValue());
148         result.setStatusMessage("Rejected");
149         return result;
150     }
151
152     private SaltstackResult acceptRequest(SaltstackResult result) {
153         result.setStatusCode(SaltstackResultCodes.SUCCESS.getValue());
154         result.setStatusMessage("Success");
155         return result;
156     }
157 }