saltstack reqExecSls implemented as adaptor
[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 com.att.eelf.configuration.EELFLogger;
36 import com.att.eelf.configuration.EELFManager;
37 import org.apache.commons.lang.StringUtils;
38 import org.json.JSONObject;
39
40 import java.util.Map;
41 import java.util.regex.Matcher;
42 import java.util.regex.Pattern;
43
44 public class SaltstackServerEmulator {
45
46     private static final String SALTSTATE_FILE_NAME = "fileName";
47     private static final String STATUS_CODE = "StatusCode";
48     private static final String STATUS_MESSAGE = "StatusMessage";
49     private final EELFLogger logger = EELFManager.getInstance().getLogger(SaltstackServerEmulator.class);
50     private String saltStateName = "test_saltState.yaml";
51
52     /**
53      * Method that emulates the response from an Saltstack Server
54      * when presented with a request to execute a saltState
55      * Returns an saltstack object result. The response code is always the ssh code 200 (i.e connection successful)
56      * payload is json string as would be sent back by Saltstack Server
57      **/
58     public SaltstackResult MockReqExec(Map<String, String> params) {
59         SaltstackResult result = new SaltstackResult();
60
61         try {
62             if (params.get("Test") == "fail") {
63                 result = rejectRequest(result, "Mocked: Fail");
64             } else {
65                 String fileName = params.get(SALTSTATE_FILE_NAME);
66                 if (fileName == null)
67                     result = acceptRequest(result, "");
68                 else
69                     result = acceptRequest(result, fileName);
70             }
71         } catch (Exception e) {
72             logger.error("JSONException caught", e);
73             rejectRequest(result, e.getMessage());
74         }
75         return result;
76     }
77
78     /**
79      * Method to emulate response from an Saltstack
80      * Server when presented with a GET request
81      * Returns an saltstack object result. The response code is always the ssh code 200 (i.e connection successful)
82      * payload is json string as would be sent back by Saltstack Server
83      **/
84     public SaltstackResult Execute(String agentUrl) {
85
86         Pattern pattern = Pattern.compile(".*?\\?Id=(.*?)&Type.*");
87         Matcher matcher = pattern.matcher(agentUrl);
88         String id = StringUtils.EMPTY;
89         String vmAddress = "192.168.1.10";
90
91         if (matcher.find()) {
92             id = matcher.group(1);
93         }
94
95         SaltstackResult getResult = new SaltstackResult();
96
97         JSONObject response = new JSONObject();
98         response.put(STATUS_CODE, 200);
99         response.put(STATUS_MESSAGE, "FINISHED");
100
101         JSONObject results = new JSONObject();
102
103         JSONObject vmResults = new JSONObject();
104         vmResults.put(STATUS_CODE, 200);
105         vmResults.put(STATUS_MESSAGE, "SUCCESS");
106         vmResults.put("Id", id);
107         results.put(vmAddress, vmResults);
108
109         response.put("Results", results);
110
111         getResult.setStatusCode(200);
112         getResult.setStatusMessage(response.toString());
113
114         return getResult;
115     }
116
117     private SaltstackResult rejectRequest(SaltstackResult result, String Message) {
118         result.setStatusCode(SaltstackResultCodes.REJECTED.getValue());
119         result.setStatusMessage("Rejected");
120         return result;
121     }
122
123     private SaltstackResult acceptRequest(SaltstackResult result, String fileName) {
124         result.setStatusCode(SaltstackResultCodes.SUCCESS.getValue());
125         result.setStatusMessage("Success");
126         result.setOutputFileName(fileName);
127         return result;
128     }
129 }