aboutsummaryrefslogtreecommitdiffstats
path: root/framework/src/main/java/org/onap/cli/fw/store/OnapCommandExecutionStore.java
blob: e72327feb3c1bc7ac08ba622443539b087648727 (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
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
/*
 * Copyright 2019 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.store;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.TimeZone;

import org.apache.commons.io.FileUtils;
import org.onap.cli.fw.conf.OnapCommandConfig;
import org.onap.cli.fw.conf.OnapCommandConstants;
import org.onap.cli.fw.error.OnapCommandExecutionFailed;
import org.onap.cli.fw.error.OnapCommandExecutionNotFound;
import org.onap.cli.fw.utils.ProcessRunner;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class OnapCommandExecutionStore {
    private static Logger log = LoggerFactory.getLogger(OnapCommandExecutionStore.class);

    private static boolean storeReady = false; // NOSONAR
    private static final String REQUEST_ID = "requestId";
    private static final String EXECUTION_ID = "executionId";
    private static final String INPUT = "input";
    private static final String STDOUT = "stdout";
    private static final String STDERR = "stderr";
    private static final String DEBUG = "debug";
    private static final String IN_PROGRESS = "in-progress";
    private static final String OUTPUT = "output";
    private static final String ERROR = "error";
    private static final String COMPLETED = "completed";
    private static final String FAILED = "failed";
    private static final String EXECUTIONID = "execution-id";
    private static final String REQUESTID = "request-id";

    private SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS", Locale.US);

    private static final String SEPARATOR = "__";

    private enum SearchMode {
        FIND,
        FILE //for developer mode


    }

    private static SearchMode searchMode = SearchMode.FILE;
    static {
        String mode = OnapCommandConfig.getPropertyValue(OnapCommandConstants.OPEN_CLI_EXECUTION_SEARCH_MODE);
        if (mode.equalsIgnoreCase(SearchMode.FIND.name()))
            searchMode = SearchMode.FIND;
    }

    public static class ExecutionStoreContext {
        private String requestId;
        private String executionId;
        private String profile;
        private String storePath;
        public String getExecutionId() {
            return executionId;
        }
        public ExecutionStoreContext setExecutionId(String executionId) {
            this.executionId = executionId;
            return this;
        }
        public String getStorePath() {
            return storePath;
        }
        public ExecutionStoreContext setStorePath(String storePath) {
            this.storePath = storePath;
            return this;
        }
        public String getRequestId() {
            return requestId;
        }
        public ExecutionStoreContext setRequestId(String requestId) {
            this.requestId = requestId;
             return this;
        }
        public String getProfile() {
            return profile;
        }
        public void setProfile(String profile) {
            this.profile = profile;
        }
    }

    public static class Execution {
        private String id;
        private String requestId;
        private String status;
        private String startTime;
        private String endTime;
        private String input;
        private String output;
        private String profile;
        private String command;
        private String product;
        private String service;

        public String getInput() {
            return input;
        }
        public void setInput(String input) {
            this.input = input;
        }
        public String getOutput() {
            return output;
        }
        public void setOutput(String output) {
            this.output = output;
        }
        public String getProfile() {
            return profile;
        }
        public void setProfile(String profile) {
            this.profile = profile;
        }
        public String getCommand() {
            return command;
        }
        public void setCommand(String command) {
            this.command = command;
        }
        public String getProduct() {
            return product;
        }
        public void setProduct(String product) {
            this.product = product;
        }
        public String getService() {
            return service;
        }
        public void setService(String service) {
            this.service = service;
        }
        public String getId() {
            return id;
        }
        public void setId(String id) {
            this.id = id;
        }
        public String getEndTime() {
            return endTime;
        }
        public void setEndTime(String endTime) {
            this.endTime = endTime;
        }
        public void setStartTime(String timeOfExecution) {
            this.startTime = timeOfExecution;
        }
        public String getStartTime() {
            return startTime;
        }
        public String getStatus() {
            return status;
        }
        public void setStatus(String status) {
            this.status = status;
        }
        public String getRequestId() {
            return requestId;
        }
        public void setRequestId(String requestId) {
            this.requestId = requestId;
        }
    }

    static {
        try {
            FileUtils.forceMkdir(new File(getBasePath()));
            storeReady = true;
        } catch (IOException e) {
            log.error("Failed to create the data store results");
        }
    }

    private static OnapCommandExecutionStore store = null;

    private OnapCommandExecutionStore() {
        this.dateFormatter.setTimeZone(TimeZone.getTimeZone("UTC"));
    }

    public static OnapCommandExecutionStore getStore() {
        if (store == null) {
            store = new OnapCommandExecutionStore();
        }

        return store;
    }

    private static String getBasePath() {
        return OnapCommandConfig.getPropertyValue(OnapCommandConstants.OPEN_CLI_DATA_DIR) +
                File.separator + "executions";
    }

    public ExecutionStoreContext storeExectutionStart(
            String requestId, String product, String service, String cmd, String profile, String input) {

        ExecutionStoreContext context = new ExecutionStoreContext();
        context.setRequestId(requestId);

        String executionId = requestId + "-" + System.currentTimeMillis();
        context.setExecutionId(executionId);

        String storePath = getBasePath() + File.separator + executionId + SEPARATOR + product +
                SEPARATOR + service +
                SEPARATOR + cmd +
                SEPARATOR + (profile != null ? profile : "" );

        try {
            File dir = new File(storePath);
            FileUtils.forceMkdir(dir);
            context.setStorePath(dir.getAbsolutePath());

            if (product != null)
                FileUtils.writeStringToFile(new File(context.getStorePath() + File.separator + OnapCommandConstants.INFO_PRODUCT), product);
            if (service != null)
                FileUtils.writeStringToFile(new File(context.getStorePath() + File.separator + OnapCommandConstants.INFO_SERVICE), service);
            if (cmd != null)
                FileUtils.writeStringToFile(new File(context.getStorePath() + File.separator + OnapCommandConstants.RPC_CMD), cmd);

            FileUtils.writeStringToFile(new File(context.getStorePath() + File.separator + REQUEST_ID), requestId);

            FileUtils.writeStringToFile(new File(context.getStorePath() + File.separator + EXECUTION_ID), executionId);

            if (input != null)
                FileUtils.writeStringToFile(new File(context.getStorePath() + File.separator + INPUT), input);
            if (profile != null) {
                context.setProfile(profile);
                FileUtils.writeStringToFile(new File(context.getStorePath() + File.separator + OnapCommandConstants.RPC_PROFILE), profile);
            }

            FileUtils.touch(new File(context.getStorePath() + File.separator + STDOUT));
            FileUtils.touch(new File(context.getStorePath() + File.separator + STDERR));
            FileUtils.touch(new File(context.getStorePath() + File.separator + DEBUG));

            FileUtils.touch(new File(context.getStorePath() + File.separator + IN_PROGRESS));
        } catch (IOException e) {
            log.error("Failed to store the execution start details {}", storePath);
        }

        return context;
    }

    public void storeExectutionEnd(
            ExecutionStoreContext context,
            String output, String error, String debug, boolean passed) {

        try {
            if (output != null)
                FileUtils.writeStringToFile(new File(context.getStorePath() + File.separator + OUTPUT), output);
            if (error != null)
                FileUtils.writeStringToFile(new File(context.getStorePath() + File.separator + ERROR), error);
            if (debug != null)
                FileUtils.writeStringToFile(new File(context.getStorePath() + File.separator + DEBUG), debug);
            if (passed)
                FileUtils.touch(new File(context.getStorePath() + File.separator + COMPLETED));
            else
                FileUtils.touch(new File(context.getStorePath() + File.separator + FAILED));
            Path path= Paths.get(context.getStorePath() + File.separator + IN_PROGRESS);
            deleteFile(context, path);
        } catch (IOException e) {
            log.error("Failed to store the execution end details {}", context.storePath);
        }
    }

    private void deleteFile(ExecutionStoreContext context, Path path){
        try {
            Files.delete(path);
        } catch (IOException e) {
            String contextPath = context.getStorePath() + File.separator + IN_PROGRESS;
            log.error("Failed to delete {}", contextPath);
        }
    }

    public void storeExectutionProgress(
            ExecutionStoreContext context,
            String output, String error, String debug) {

        try {
            if (output != null)
                FileUtils.writeStringToFile(new File(context.getStorePath() + File.separator + OUTPUT), output);
            if (error != null)
                FileUtils.writeStringToFile(new File(context.getStorePath() + File.separator + ERROR), error);
            if (debug != null)
                FileUtils.writeStringToFile(new File(context.getStorePath() + File.separator + DEBUG), debug);
        } catch (IOException e) {
            log.error("Failed to store the execution end details {}", context.storePath);
        }
    }

    public void storeExectutionDebug(
            ExecutionStoreContext context,
            String debug) {

        try {
            if (debug != null) {
                FileUtils.writeStringToFile(new File(context.getStorePath() + File.separator + DEBUG), debug);
            }
        } catch (IOException e) {
            log.error("Failed to store the execution debug details {}", context.storePath);
        }
    }

    public void storeExectutionOutput(
            ExecutionStoreContext context,
            String output) {

        try {
            if (output != null) {
                FileUtils.writeStringToFile(new File(context.getStorePath() + File.separator + OUTPUT), output);
            }
        } catch (IOException e) {
            log.error("Failed to store the execution output details {}", context.storePath);
        }
    }
    public List<OnapCommandExecutionStore.Execution> listExecutions(Map<String, String> search) throws OnapCommandExecutionFailed {
        List <OnapCommandExecutionStore.Execution> list = new ArrayList<>();

        try {
            List <String> dirs = new ArrayList<>();
            if (System.getProperty("os.name").toLowerCase().startsWith("windows") || searchMode.equals(SearchMode.FILE)) {
                for (File f: new File(getBasePath()).listFiles()) {
                    if(search.containsKey(EXECUTIONID)) {
                        if (f.getName().startsWith(search.get(EXECUTIONID)))
                                dirs.add(f.getAbsolutePath());

                        continue;
                    }

                    if(search.containsKey(REQUESTID)) {
                        if (f.getName().startsWith(search.get(REQUESTID)))
                                dirs.add(f.getAbsolutePath());

                    }

                    else
                        dirs.add(f.getAbsolutePath());
                }
            } else {
                //find results -type d -newermt '2019-02-11 10:00:00' ! -newermt '2019-02-11 15:10:00' -name "*__*__profile-list*"
                //find 'results' -type d -newermt '2019-02-11T10:00:00.000' ! -newermt '2019-02-11T15:10:00.000' -name "*__*__profile*"

                StringBuilder searchString = new StringBuilder("find " + new File(getBasePath()).getAbsolutePath() + " -type d ");

                String startTime = search.get("startTime");
                if (startTime != null) {
                    searchString.append(" -newermt " + startTime);
                }

                String endTime = search.get("endTime");
                if (endTime != null) {
                    searchString.append(" ! -newermt " + endTime);
                }

                searchString.append(" -name \"");

                if(search.containsKey(EXECUTIONID)) {
                    searchString.append(search.get(EXECUTIONID));
                } else if(search.containsKey(REQUESTID)) {
                    searchString.append(search.get(REQUESTID) + "*");
                } else {
                    searchString.append("*");
                }

                for (String term: Arrays.asList("product", "service", "command", "profile")) {
                    searchString.append("__");
                    if (search.get(term) != null && !search.get(term).isEmpty()) {
                        searchString.append(search.get(term));
                    } else {
                        searchString.append("*");
                    }
                }
                if (!searchString.toString().endsWith("*"))
                    searchString.append("*");

                searchString.append("\"");

                ProcessRunner pr = new ProcessRunner(new String [] {searchString.toString()}, null, ".");
                pr.setTimeout(10000);
                pr.overrideToUnix();
                pr.run();
                if (pr.getExitCode() != 0) {
                    throw new OnapCommandExecutionFailed("System failed to search the executions with error " + pr.getError());
                }

                if (!pr.getOutput().trim().isEmpty())
                    dirs = Arrays.asList(pr.getOutput().split("\\r?\\n"));
            }

            for (String dir: dirs) {
                list.add(this.makeExecution(dir));
            }
        } catch (Exception e) {// NOSONAR
            throw new OnapCommandExecutionFailed(e, "Failed to search the executions");
        }

        return list;
    }

    private Execution makeExecution(String executionStorePath) throws IOException {
        OnapCommandExecutionStore.Execution exectuion = new OnapCommandExecutionStore.Execution();
        if (new File(executionStorePath + File.separator + REQUEST_ID).exists())
            exectuion.setRequestId(FileUtils.readFileToString(new File(executionStorePath + File.separator + REQUEST_ID)));
        if (new File(executionStorePath + File.separator + EXECUTION_ID).exists())
            exectuion.setId(FileUtils.readFileToString(new File(executionStorePath + File.separator + EXECUTION_ID)));
        exectuion.setProduct(FileUtils.readFileToString(new File(executionStorePath + File.separator + OnapCommandConstants.INFO_PRODUCT)));
        exectuion.setService(FileUtils.readFileToString(new File(executionStorePath + File.separator + OnapCommandConstants.INFO_SERVICE)));
        exectuion.setCommand(FileUtils.readFileToString(new File(executionStorePath + File.separator + OnapCommandConstants.RPC_CMD)));
        if (new File(executionStorePath + File.separator + OnapCommandConstants.RPC_PROFILE).exists())
            exectuion.setProfile(FileUtils.readFileToString(new File(executionStorePath + File.separator + OnapCommandConstants.RPC_PROFILE)));

        exectuion.setInput(FileUtils.readFileToString(new File(executionStorePath + File.separator + INPUT)));
        exectuion.setStartTime(dateFormatter.format(new File(executionStorePath + File.separator + INPUT).lastModified()));

        if (new File(executionStorePath + File.separator + IN_PROGRESS).exists()) {
            exectuion.setStatus(IN_PROGRESS);
        } else if (new File(executionStorePath + File.separator + COMPLETED).exists()) {
            exectuion.setStatus(COMPLETED);
            if (new File(executionStorePath + File.separator + OUTPUT).exists()) {
                exectuion.setOutput(FileUtils.readFileToString(new File(executionStorePath + File.separator + OUTPUT)));
                exectuion.setEndTime(dateFormatter.format(new File(executionStorePath + File.separator + OUTPUT).lastModified()));
            }
        } else if (new File(executionStorePath + File.separator + FAILED).exists()) {
            exectuion.setStatus(FAILED);
            if (new File(executionStorePath + File.separator + ERROR).exists()) {
                exectuion.setOutput(FileUtils.readFileToString(new File(executionStorePath + File.separator + ERROR)));
                exectuion.setEndTime(dateFormatter.format(new File(executionStorePath + File.separator + ERROR).lastModified()));
            }
        }

        return exectuion;
    }

    private File getExecutionDir(String executionId) throws OnapCommandExecutionNotFound {
        File []f =  new File(getBasePath()).listFiles((dir, name) -> {
            return name.startsWith(executionId);
        });

        if (f.length == 0) {
            throw new OnapCommandExecutionNotFound(executionId);
        }

        return f[0];
    }

    public String showExecutionOut(String executionId) throws OnapCommandExecutionNotFound {
        try {
            return FileUtils.readFileToString(new File (this.getExecutionDir(executionId).getAbsolutePath() + File.separator + "stdout"));
        } catch (IOException e) {
            return "";
        }
    }

    public String showExecutionErr(String executionId) throws OnapCommandExecutionNotFound {
        try {
            return FileUtils.readFileToString(new File (this.getExecutionDir(executionId).getAbsolutePath() + File.separator + "stderr"));
        } catch (IOException e) {
            return "";
        }
    }

    public String showExecutionDebug(String executionId) throws OnapCommandExecutionNotFound {
        try {
            return FileUtils.readFileToString(new File (this.getExecutionDir(executionId).getAbsolutePath() + File.separator + DEBUG));
        } catch (IOException e) {
            return "";
        }
    }
    public Execution getExecution(String executionId) throws OnapCommandExecutionNotFound, OnapCommandExecutionFailed {
        try {
            return this.makeExecution(this.getExecutionDir(executionId).getAbsolutePath());
        } catch (IOException e) {
            throw new OnapCommandExecutionFailed(e, "Failed to retrieve the execution");
        }
    }
}