2 * ============LICENSE_START=======================================================
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
13 * http://www.apache.org/licenses/LICENSE-2.0
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.
21 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22 * ============LICENSE_END=========================================================
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
33 package org.onap.ccsdk.sli.adaptors.saltstack.model;
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;
44 public class SaltstackServerEmulator {
46 private final EELFLogger logger = EELFManager.getInstance().getLogger(SaltstackServerEmulator.class);
48 private static final String SALTSTATE_NAME = "SaltStateName";
49 private static final String STATUS_CODE = "StatusCode";
50 private static final String STATUS_MESSAGE = "StatusMessage";
52 private String saltStateName = "test_saltState.yaml";
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
60 public SaltstackResult MockReqExec(Map<String, String> params) {
61 SaltstackResult result = new SaltstackResult();
64 if (params.get("Test") == "fail") {
65 result = rejectRequest(result, "Must provide a valid Id");
67 result = acceptRequest(result);
69 } catch (Exception e) {
70 logger.error("JSONException caught", e);
71 rejectRequest(result, e.getMessage());
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
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();
87 // Request must be a JSON object
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");
97 acceptRequest(result);
99 } catch (JSONException e) {
100 logger.error("JSONException caught", e);
101 rejectRequest(result, e.getMessage());
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
113 public SaltstackResult Execute(String agentUrl) {
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";
120 if (matcher.find()) {
121 id = matcher.group(1);
124 SaltstackResult getResult = new SaltstackResult();
126 JSONObject response = new JSONObject();
127 response.put(STATUS_CODE, 200);
128 response.put(STATUS_MESSAGE, "FINISHED");
130 JSONObject results = new JSONObject();
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);
138 response.put("Results", results);
140 getResult.setStatusCode(200);
141 getResult.setStatusMessage(response.toString());
146 private SaltstackResult rejectRequest(SaltstackResult result, String Message) {
147 result.setStatusCode(SaltstackResultCodes.REJECTED.getValue());
148 result.setStatusMessage("Rejected");
152 private SaltstackResult acceptRequest(SaltstackResult result) {
153 result.setStatusCode(SaltstackResultCodes.SUCCESS.getValue());
154 result.setStatusMessage("Success");