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;
 
  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;
 
  43 public class SaltstackServerEmulator {
 
  45     private final EELFLogger logger = EELFManager.getInstance().getLogger(SaltstackServerEmulator.class);
 
  47     private static final String SALTSTATE_NAME = "SaltStateName";
 
  48     private static final String STATUS_CODE = "StatusCode";
 
  49     private static final String STATUS_MESSAGE = "StatusMessage";
 
  51     private String saltStateName = "test_saltState.yaml";
 
  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
 
  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();
 
  64             // Request must be a JSON object
 
  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");
 
  74                 acceptRequest(result);
 
  76         } catch (JSONException e) {
 
  77             logger.error("JSONException caught", e);
 
  78             rejectRequest(result, e.getMessage());
 
  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
 
  90     public SaltstackResult Execute(String agentUrl) {
 
  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";
 
  98             id = matcher.group(1);
 
 101         SaltstackResult getResult = new SaltstackResult();
 
 103         JSONObject response = new JSONObject();
 
 104         response.put(STATUS_CODE, 200);
 
 105         response.put(STATUS_MESSAGE, "FINISHED");
 
 107         JSONObject results = new JSONObject();
 
 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);
 
 115         response.put("Results", results);
 
 117         getResult.setStatusCode(200);
 
 118         getResult.setStatusMessage(response.toString());
 
 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());
 
 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());