Saltstack now aligned with APPC
[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 : CCSDK
4  * ================================================================================
5  * Copyright (C) 2018 Samsung Electronics. All rights reserved.
6  * ================================================================================
7  *
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  *
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
38 import java.io.ByteArrayOutputStream;
39 import java.io.FileNotFoundException;
40 import java.io.IOException;
41 import java.nio.file.Files;
42 import java.nio.file.Path;
43 import java.nio.file.Paths;
44 import java.util.Map;
45
46 public class SaltstackServerEmulator {
47
48     private static final String SALTSTATE_FILE_NAME = "fileName";
49     private final EELFLogger logger = EELFManager.getInstance().getLogger(SaltstackServerEmulator.class);
50
51     /**
52      * Method that emulates the response from an Saltstack Server
53      * when presented with a request to execute a saltState
54      * Returns an saltstack object result. The response code is always the ssh code 200 (i.e connection successful)
55      * payload is json string as would be sent back by Saltstack Server
56      **/
57     public SaltstackResult mockReqExec(Map<String, String> params) {
58         SaltstackResult result = new SaltstackResult();
59
60         try {
61             if (params.get("Test") == "fail") {
62                 result = rejectRequest(result, "Mocked: Fail");
63             } else {
64                 String fileName = params.get(SALTSTATE_FILE_NAME);
65                 if (fileName == null) {
66                     throw new FileNotFoundException("No response file found");
67                 }
68                 result = acceptRequest(result, fileName);
69             }
70         } catch (Exception e) {
71             logger.error("Exception caught", e);
72             rejectRequest(result, e.getMessage());
73         }
74         return result;
75     }
76
77     private SaltstackResult rejectRequest(SaltstackResult result, String Message) {
78         result.setStatusCode(SaltstackResultCodes.REJECTED.getValue());
79         result.setStatusMessage("Rejected");
80         return result;
81     }
82
83     private SaltstackResult acceptRequest(SaltstackResult result, String fileName) throws IOException {
84         result.setStatusCode(SaltstackResultCodes.SUCCESS.getValue());
85         result.setStatusMessage("Success");
86         Path path = Paths.get(fileName);
87         byte[] data = Files.readAllBytes(path);
88         ByteArrayOutputStream byteOut = new ByteArrayOutputStream(data.length);
89         byteOut.write(data, 0, data.length);
90         result.setOutputMessage(byteOut);
91         return result;
92     }
93 }