summaryrefslogtreecommitdiffstats
path: root/vnf-sdk-function-test/src/main/java/org/openo/vnfsdk/functest/TaskExecution.java
blob: 8e3300e8d8588c2fcced1527d7b30dd491ac84a0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
/*
 * Copyright 2017 Huawei Technologies Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.openo.vnfsdk.functest;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Map;
import java.util.Map.Entry;
import java.util.UUID;

import org.apache.commons.lang3.SystemUtils;
import org.openo.vnfsdk.functest.constants.ApplicationConstants;
import org.openo.vnfsdk.functest.externalservice.entity.Environment;
import org.openo.vnfsdk.functest.externalservice.entity.EnvironmentMap;
import org.openo.vnfsdk.functest.externalservice.entity.OperationStatus;
import org.openo.vnfsdk.functest.externalservice.entity.OperationStatus.operResultCode;
import org.openo.vnfsdk.functest.externalservice.entity.OperationStatusHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.fasterxml.jackson.databind.ObjectMapper;

public class TaskExecution {

    private static final Logger LOGGER = LoggerFactory.getLogger(TaskExecution.class);

    public void executeScript(String dirPath, UUID uniqueKey) {

        String nl = File.separator;
        String curDir = System.getProperty(ApplicationConstants.USER_DIR);
        String confDir = curDir + nl + ApplicationConstants.CONF + nl + ApplicationConstants.ROBOT + nl;

        // Read the MetaData from the VNF package
        ObjectMapper mapper = new ObjectMapper();

        Map<String, String> mapValues = null;
        try {
            mapValues =
                    mapper.readValue(new FileInputStream(confDir + ApplicationConstants.ROBOTMETADATA_JSON), Map.class);
        } catch(IOException e) {

            LOGGER.error(ApplicationConstants.JSON_METADATA_FILE_FAILED, e);
            return;
        }

        // Form the variables for the upload, transfer and execute command
        String scriptDirName = new File(dirPath).getName();
        mapValues.put("SCRIPT_DIR", dirPath);

        String remoteScriptDir = mapValues.get("DIR_REMOTE") + scriptDirName;
        String remoteScriptResult = remoteScriptDir + "/" + "output ";
        mapValues.put("DIR_REMOTE_RESULT", remoteScriptResult);

        String dirResult = mapValues.get(ApplicationConstants.DIR_RESULT) + uniqueKey;
        mapValues.put(ApplicationConstants.DIR_RESULT, dirResult);

        String remoteScriptFile = remoteScriptDir + "/" + mapValues.get("MAIN_SCRIPT");
        String remoteArgs = "--argumentfile " + remoteScriptDir + "/" + "config.args ";
        String remoteCommand =
                ApplicationConstants.ROBOT_SPACE + "-d " + remoteScriptResult + remoteArgs + remoteScriptFile;
        mapValues.put("REMOTE_COMMAND", "\"" + remoteCommand + "\"");

        String robotvariables = "";
        for(Entry<String, String> values : mapValues.entrySet()) {

            robotvariables = robotvariables + " -v " + values.getKey() + ":" + values.getValue() + " ";
        }

        // Execute the command
        String argumentFilePath = confDir + "config.args ";
        String robotScript = confDir + "RemoteConnection.robot";

        Process process = null;
        InputStream inputStream = null;
        int ch;
        try {
            String command = "robot --argumentfile " + argumentFilePath + robotvariables + " " + robotScript;
            LOGGER.info("Command execute to execute the script:" + command);
            process = Runtime.getRuntime().exec(new String[] {getShellCommand(), getShellArg(), command});
            if(process != null) {
                process.waitFor();
                inputStream = process.getInputStream();
            }
            while((ch = inputStream.read()) != -1) {
                LOGGER.info(ApplicationConstants.CHARACTER + Integer.toString(ch));
            }

        } catch(Exception e) {
            LOGGER.error(ApplicationConstants.TASKEXE_EXESCRIPT_EXCEPTION, e);
        }
    }

    public void executeRobotScript(UUID envId, UUID executeId) {

        String nl = File.separator;
        String curDir = System.getProperty(ApplicationConstants.USER_DIR);
        String confDir = curDir + nl + ApplicationConstants.CONF + nl + ApplicationConstants.ROBOT + nl;

        // Read the MetaData from the VNF package
        ObjectMapper mapper = new ObjectMapper();
        Map<String, String> mapValues = null;
        try {
            mapValues =
                    mapper.readValue(new FileInputStream(confDir + ApplicationConstants.ROBOTMETADATA_JSON), Map.class);
        } catch(IOException e) {

            LOGGER.error(ApplicationConstants.JSON_METADATA_FILE_FAILED, e);
            return;
        }

        // Get environment of given UUID
        Environment functestEnv = EnvironmentMap.getInstance().getEnv(envId);

        String remoteDir = functestEnv.getPath() + mapValues.get("SCRIPT_NAME");
        String remoteScriptFile = remoteDir + "/" + mapValues.get("MAIN_SCRIPT");
        String remoteScriptResult = remoteDir + "/" + "output ";
        String dirResult = mapValues.get(ApplicationConstants.DIR_RESULT) + executeId;

        // set the argument parameters
        String remoteArgs = "";
        remoteArgs = remoteArgs + " -v " + "NODE_IP" + ":" + functestEnv.getRemoteIp() + " ";
        remoteArgs = remoteArgs + " -v " + "NODE_USERNAME" + ":" + functestEnv.getUserName() + " ";
        remoteArgs = remoteArgs + " -v " + "NODE_PASSWORD" + ":" + functestEnv.getPassword() + " ";

        String remoteCommand =
                ApplicationConstants.ROBOT_SPACE + "-d " + remoteScriptResult + remoteArgs + remoteScriptFile;

        // set the parameters required by the execute script
        remoteCommand = "\"" + remoteCommand + "\"";
        remoteArgs = remoteArgs + " -v " + "REMOTE_COMMAND" + ":" + remoteCommand + " ";

        remoteArgs = remoteArgs + " -v " + ApplicationConstants.DIR_RESULT + ":" + dirResult + " ";
        remoteArgs = remoteArgs + " -v " + "DIR_REMOTE_RESULT" + ":" + remoteScriptResult + " ";

        // Execute script directory
        String robotScript = confDir + "execute.robot";

        Process process = null;
        InputStream inputStream = null;
        int ch;
        try {
            String command = ApplicationConstants.ROBOT + remoteArgs + robotScript;
            LOGGER.info("Command execute to execute the script:" + command);
            process = Runtime.getRuntime().exec(new String[] {getShellCommand(), getShellArg(), command});
            if(process != null) {
                process.waitFor();
                inputStream = process.getInputStream();
            }

            while((ch = inputStream.read()) != -1) {
                LOGGER.info(ApplicationConstants.CHARACTER + Integer.toString(ch));
            }
        } catch(Exception e) {
            LOGGER.error(ApplicationConstants.TASKEXE_EXESCRIPT_EXCEPTION, e);
        }

        OperationStatus operstatus = new OperationStatus();
        operstatus.setoResultCode(operResultCode.SUCCESS);
        operstatus.setOperResultMessage("Execute function test finished");
        operstatus.setOperFinished(true);
        OperationStatusHandler.getInstance().setOperStatusMap(executeId, operstatus);
    }

    public void uploadScript(String dirPath, UUID uuidEnv, UUID uuidUpload) {

        String nl = File.separator;
        String curDir = System.getProperty(ApplicationConstants.USER_DIR);
        String confDir = curDir + nl + ApplicationConstants.CONF + nl + ApplicationConstants.ROBOT + nl;

        // Read the MetaData from the VNF package
        ObjectMapper mapper = new ObjectMapper();

        Map<String, String> mapValues = null;
        try {

            mapValues =
                    mapper.readValue(new FileInputStream(confDir + ApplicationConstants.ROBOTMETADATA_JSON), Map.class);
        } catch(Exception e) {

            LOGGER.error(ApplicationConstants.JSON_METADATA_FILE_FAILED, e);
            return;
        }

        // Form the variables for the upload, transfer and execute command
        mapValues.put("SCRIPT_DIR", dirPath);

        String robotvariables = "";
        for(Entry<String, String> values : mapValues.entrySet()) {

            robotvariables = robotvariables + " -v " + values.getKey() + ":" + values.getValue() + " ";
        }

        // Append the Func test environment variables
        Environment functestEnv = EnvironmentMap.getInstance().getEnv(uuidEnv);
        robotvariables = robotvariables + " -v " + "NODE_IP" + ":" + functestEnv.getRemoteIp() + " ";
        robotvariables = robotvariables + " -v " + "NODE_USERNAME" + ":" + functestEnv.getUserName() + " ";
        robotvariables = robotvariables + " -v " + "NODE_PASSWORD" + ":" + functestEnv.getPassword() + " ";
        robotvariables = robotvariables + " -v " + "DIR_REMOTE" + ":" + functestEnv.getPath() + " ";

        // Execute the command
        String robotScript = confDir + "upload.robot";

        Process process = null;
        InputStream inputStream = null;
        int ch;
        try {
            String command = ApplicationConstants.ROBOT_SPACE + robotvariables + robotScript;
            LOGGER.info("Command execute to upload the script:" + command);
            process = Runtime.getRuntime().exec(new String[] {getShellCommand(), getShellArg(), command});
            if(process != null) {
                process.waitFor();
                inputStream = process.getInputStream();
            }

            while((ch = inputStream.read()) != -1) {
                LOGGER.info(ApplicationConstants.CHARACTER + Integer.toString(ch));
            }

        } catch(Exception e) {
            LOGGER.error(ApplicationConstants.TASKEXE_EXESCRIPT_EXCEPTION, e);
        }

        OperationStatus operstatus = new OperationStatus();
        operstatus.setoResultCode(operResultCode.SUCCESS);
        operstatus.setOperResultMessage("");
        operstatus.setOperFinished(true);
        OperationStatusHandler.getInstance().setOperStatusMap(uuidUpload, operstatus);

    }

    private String getShellCommand() {

        String shellcommand = ApplicationConstants.SHELL_COMMAND;
        if(SystemUtils.IS_OS_LINUX) {
            shellcommand = ApplicationConstants.SHELL_COMMAND_BASH;
        }

        return shellcommand;
    }

    private String getShellArg() {

        String commandArg = "/c";
        if(SystemUtils.IS_OS_LINUX) {
            commandArg = "-c";
        }

        return commandArg;
    }

}