aboutsummaryrefslogtreecommitdiffstats
path: root/profiles/command/src/main/java/org/onap/cli/fw/cmd/cmd/OpenCommandShellCmd.java
blob: dd2c26cd6c2289c39679b746602b0b7033a4f5c2 (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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
/*
 * Copyright 2018 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.onap.cli.fw.cmd.cmd;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.apache.commons.io.FileUtils;
import org.onap.cli.fw.cmd.OnapCommand;
import org.onap.cli.fw.cmd.conf.OnapCommandCmdConstants;
import org.onap.cli.fw.cmd.error.OnapCommandCmdFailure;
import org.onap.cli.fw.cmd.schema.OnapCommandSchemaCmdLoader;
import org.onap.cli.fw.conf.OnapCommandConfig;
import org.onap.cli.fw.error.OnapCommandException;
import org.onap.cli.fw.error.OnapCommandExecutionFailed;
import org.onap.cli.fw.error.OnapCommandResultEmpty;
import org.onap.cli.fw.error.OnapCommandResultMapProcessingFailed;
import org.onap.cli.fw.input.OnapCommandParameter;
import org.onap.cli.fw.output.OnapCommandResultAttribute;
import org.onap.cli.fw.output.OnapCommandResultType;
import org.onap.cli.fw.registrar.OnapCommandRegistrar;
import org.onap.cli.fw.schema.OnapCommandSchema;
import org.onap.cli.fw.store.OnapCommandExecutionStore;
import org.onap.cli.fw.utils.OnapCommandUtils;
import org.onap.cli.fw.utils.ProcessRunner;

import com.jayway.jsonpath.JsonPath;
import com.jayway.jsonpath.PathNotFoundException;

import net.minidev.json.JSONArray;

/**
 * Hello world.
 */
@OnapCommandSchema(type = "cmd")
public class OpenCommandShellCmd extends OnapCommand {

    public OpenCommandShellCmd() {
        super.addDefaultSchemas(OnapCommandCmdConstants.DEFAULT_PARAMETER_CMD_FILE_NAME);
    }

    private Map<String, String> resultMap = new HashMap<>();

    private List<String> command;

    private Map<String, String> envs = new HashMap<>();

    private String wd = null;

    private List<Integer> successStatusCodes = new ArrayList<>();

    private List<Integer> passCodes = new ArrayList<>();

    private String output = "$stdout";

    private String error = "$stderr";

    public List<Integer> getSuccessStatusCodes() {
        return successStatusCodes;
    }

    public void setSuccessStatusCodes(List<Integer> successStatusCodes) {
        this.successStatusCodes = successStatusCodes;
    }

    public String getWd() {
        return wd;
    }

    public void setWd(String wd) {
        this.wd = wd;
    }

    public Map<String, String> getEnvs() {
        return envs;
    }

    public void setEnvs(Map<String, String> envs) {
        this.envs = envs;
    }


    public List<String> getCommand() {
        return command;
    }

    public void setCommand(List<String> command) {
        this.command = command;
    }

    public Map<String, String> getResultMap() {
        return resultMap;
    }

    public void setResultMap(Map<String, String> resultMap) {
        this.resultMap = resultMap;
    }

    private String getStdoutPath() {
        String storePath = this.getExecutionContext().getStorePath();
        storePath = storePath + File.separator + "stdout";
        return storePath;
    }

    private String getStderrPath() {
        String storePath = this.getExecutionContext().getStorePath();
        storePath = storePath + File.separator + "stderr";
        return storePath;
    }

    private String getOutputAttributeFilePath(String attrName, boolean temp) {
        String storePath = (!temp) ? this.getExecutionContext().getStorePath() : OnapCommandConfig.getPropertyValue("cli.tmp.dir");
        String randomId = (this.getExecutionContext() != null) ? this.getExecutionContext().getExecutionId() : ("" + System.currentTimeMillis());
        storePath = storePath + File.separator + randomId + "_" + attrName;
        return storePath;
    }

    @Override
    protected List<String> initializeProfileSchema(Map<String, ?> schemaMap, boolean validate) throws OnapCommandException {
        return OnapCommandSchemaCmdLoader.parseCmdSchema(this, schemaMap, validate);
    }

    @Override
    protected void run() throws OnapCommandException {
        //Read the input arguments
        Map<String, OnapCommandParameter> paramMap = this.getParametersMap();

        //process command section
        Map<String, String> tmpFiles = new HashMap<>();
        List<String> commandLine = new ArrayList<>();
        for (String cmdTkn: this.getCommand()) {
            tmpFiles.putAll(this.formTmpFiles(cmdTkn));
            String commandLine1 = OnapCommandUtils.replaceLineForSpecialValues(
                    cmdTkn, tmpFiles);
            commandLine1 = OnapCommandUtils.replaceLineFromInputParameters(commandLine1, paramMap);

            commandLine.add(commandLine1);
        }

        long timeout = Long.parseLong(this.getParametersMap().get(OnapCommandCmdConstants.TIMEOUT).getValue().toString());

        //Process command
        String []cmd = commandLine.toArray(new String []{});
        String cwd = this.getWd();
        List <String> envs = new ArrayList<>();

        //add current process environments to sub process
        for (Map.Entry<String, String> env: System.getenv().entrySet()) { //NOSONAR
            envs.add(env.getKey() + "=" + env.getValue());
        }

        //add oclip specific environment variables
        if (this.getExecutionContext() != null) {
            envs.add("OPEN_CLI_REQUEST_ID=" + this.getExecutionContext().getRequestId());
            if (this.getExecutionContext().getProfile() != null) {
                envs.add("OPEN_CLI_PROFILE=" + this.getExecutionContext().getProfile());
            }
            if (OnapCommandRegistrar.getRegistrar().getHost() != null) {
                envs.add("OPEN_CLI_RPC_HOST=" + OnapCommandRegistrar.getRegistrar().getHost());
                envs.add("OPEN_CLI_RPC_PORT=" + OnapCommandRegistrar.getRegistrar().getPort());
            }
        }

        for (String env: this.getEnvs().keySet()) {
            envs.add(env + "=" + this.getEnvs().get(env));
        }

        ProcessRunner pr = new ProcessRunner(
                cmd,
                (!envs.isEmpty()) ? envs.toArray(new String []{}) : null,
                cwd);
        FileOutputStream stdoutStream = null;
        FileOutputStream stderrStream = null;
        String outputValue = "";

        try { //NOSONAR
            pr.setTimeout(timeout);

            if (this.getExecutionContext() != null) {

                stdoutStream = new FileOutputStream(this.getStdoutPath());
                stderrStream = new FileOutputStream(this.getStderrPath());

                pr.setStdout(stdoutStream);
                pr.setStderr(stderrStream);

                OnapCommandExecutionStore.getStore().storeExectutionDebug(this.getExecutionContext(), pr.toString());
            }

            this.getResult().setDebugInfo(pr.toString());

            pr.run();
        } catch (Exception e) {
            throw new OnapCommandExecutionFailed(this.getName(), e);
        } finally {
            if (stdoutStream != null) {
                try {
                    stdoutStream.close();
                } catch (IOException e) {
                    //never occurs  // NOSONAR
                }
            }
            if (stderrStream != null) {
                try {
                    stderrStream.close();
                } catch (IOException e) {
                    //never occurs  // NOSONAR
                }
            }
        }

        if (!this.successStatusCodes.contains(pr.getExitCode())) {
            throw new OnapCommandExecutionFailed(this.getName(), pr.getError(), pr.getExitCode());
        }

        if (this.output.equals("$stdout")) {
            if (this.getExecutionContext() != null) {
                try (FileInputStream is = new FileInputStream(this.getStdoutPath())){
                    outputValue = pr.streamToString(is);
                } catch (IOException e) {
                    //never occurs  // NOSONAR
                }
            } else
                outputValue = pr.getOutput();

        } else if (this.output.equals("$stderr")) {
            if (this.getExecutionContext() != null) {
                try (FileInputStream is = new FileInputStream(this.getStderrPath())) {
                    outputValue = pr.streamToString(is);
                } catch (IOException e) {
                    //never occurs  // NOSONAR
                }
            } else
                outputValue = pr.getError();

        } else {
            //remove ${tmp: and closing }
            String tmpName = this.output.substring(7, this.output.length()-1);
            String tmpFile = tmpFiles.get("tmp:" + tmpName);
            if (tmpFile != null) {
                try (FileInputStream is = new FileInputStream(tmpFile)) {
                    outputValue = pr.streamToString(is);
                } catch (IOException e) {
                    //never occurs  // NOSONAR
                }
            }
        }

        this.getResult().setDebugInfo(pr.toString() + "\n" + outputValue);
        this.getResult().setOutput(outputValue);

        //populate results
        if (!this.getResult().getType().name().equalsIgnoreCase(OnapCommandResultType.TEXT.name()))
            for (Entry<String, String> resultMapEntry : this.getResultMap().entrySet()) {
                String attrName = resultMapEntry.getKey();
                OnapCommandResultAttribute attr = this.getResult().getRecordsMap().get(attrName);

                String value = OnapCommandUtils.replaceLineFromInputParameters(resultMapEntry.getValue(), paramMap);
                value = OnapCommandUtils.replaceLineForSpecialValues(value);
                attr.setValues(this.replaceLineFromOutputResults(value, outputValue));
            }

        //check for pass/failure
        this.getResult().setPassed(!(!this.passCodes.isEmpty() && !this.passCodes.contains(pr.getExitCode())));
   }

    public String getOutput() {
        return output;
    }

    public void setOutput(String output) {
        this.output = output;
    }

    private Map<String, String> formTmpFiles(String line){

        Map<String, String> result = new HashMap<>();

        if (!line.contains("$s{tmp")) {
            return result;
        }

        int currentIdx = 0;
        while (currentIdx < line.length()) {
            int idxS = line.indexOf("$s{tmp:", currentIdx); //check for output stream
            if (idxS == -1) {
                break;
            }

            int idxE = line.indexOf('}', idxS);
            String tmpName = line.substring(idxS + 7, idxE);
            tmpName = tmpName.trim();
            String[] tmpTkns = tmpName.split(":");
            String tmpFileName;
            String paramName;
            if (tmpTkns.length == 2) {
                tmpFileName = tmpTkns[0];
                paramName = tmpTkns[1];
            } else {
                tmpFileName = tmpTkns[0];
                paramName = null;
            }

            String tmpFilePath = this.getOutputAttributeFilePath(tmpFileName, true);
            if (paramName != null) {
                //Write the value of input params into file before passing to command
                try {
                    FileUtils.touch(new File(tmpFilePath));
                    FileUtils.writeStringToFile(new File(tmpFilePath),
                            this.getParametersMap().get(paramName).getValue().toString());
                } catch (IOException e) {
                    // NO SONAR
                }
            }

            result.put("tmp:" + tmpFileName, tmpFilePath); //used in output parsing
            result.put("tmp:" + tmpName, tmpFilePath); //used in line replacement

            currentIdx = idxE + 1;
        }
        return result;
    }

   private ArrayList<String> replaceLineFromOutputResults(String line, String output)
            throws OnapCommandException {


        ArrayList<String> result = new ArrayList<>();
        if (!line.contains("$o{")) {
            result.add(line);
            return result;
        }

        /**
         * In case of empty output [] or {}
         **/
        if (output.length() <= 2) {
            return result;
        }

        int currentIdx = 0;

        // Process  jsonpath macros
        List<Object> values = new ArrayList<>();
        StringBuilder processedPattern = new StringBuilder();
        currentIdx = 0;
        int maxRows = 1; // in normal case, only one row will be there
        while (currentIdx < line.length()) {
            int idxS = line.indexOf("$o{", currentIdx); //check for output stream
            if (idxS == -1) {
                idxS = line.indexOf("$e{", currentIdx); //check for error stream
                if (idxS == -1) {
                    processedPattern.append(line.substring(currentIdx));
                    break;
                }
            }
            int idxE = line.indexOf('}', idxS);
            String jsonPath = line.substring(idxS + 3, idxE);
            jsonPath = jsonPath.trim();
            Object value = new Object();
            try {
                // Instead of parsing, just assign the json as it is
                if (!jsonPath.equals("$"))
                    value = JsonPath.read(output, jsonPath);
                else
                    value = output;
            } catch (PathNotFoundException e1) { // NOSONAR
                //set to blank for those entries which are missing from the output json
                value = "";
            } catch (Exception e) {
                throw new OnapCommandCmdFailure("Invalid json format in command output");
            }

            if (value instanceof JSONArray) {
                JSONArray arr = (JSONArray) value;
                if (arr.size() > maxRows) {
                    maxRows = arr.size();
                }
            }
            processedPattern.append(line.substring(currentIdx, idxS) + "%s");
            values.add(value);
            currentIdx = idxE + 1;
        }

        if (processedPattern.toString().isEmpty()) {
            result.add(line);
            return result;
        } else {
            for (int i = 0; i < maxRows; i++) {
                currentIdx = 0;
                StringBuilder bodyProcessedLine = new StringBuilder();
                int positionalIdx = 0; // %s positional idx
                while (currentIdx < processedPattern.length()) {
                    int idxS = processedPattern.indexOf("%s", currentIdx);
                    if (idxS == -1) {
                        bodyProcessedLine.append(processedPattern.substring(currentIdx));
                        break;
                    }

                    int idxEnd = idxS + 2; // %s

                    try {
                        Object val = values.get(positionalIdx);
                        String valStr = String.valueOf(val);

                        if (val instanceof JSONArray) {
                            JSONArray aJson = (JSONArray) val;

                            if (!aJson.isEmpty()) {
                                valStr = aJson.get(i).toString();
                            } else {
                                throw new OnapCommandResultEmpty();
                            }
                        }

                        bodyProcessedLine.append(processedPattern.substring(currentIdx, idxS) + valStr);
                        currentIdx = idxEnd;
                        positionalIdx++;
                    } catch (OnapCommandResultEmpty e) {
                        throw e;
                    } catch (Exception e) {
                        throw new OnapCommandResultMapProcessingFailed(line, e);
                    }
                }
                result.add(bodyProcessedLine.toString());
            }

            return result;
        }
    }

public String getError() {
    return error;
}

public void setError(String error) {
    this.error = error;
}

public List<Integer> getPassCodes() {
    return passCodes;
}

public void setPassCodes(List<Integer> passCodes) {
    this.passCodes = passCodes;
}

}