blob: 3f1e4c57e12164cd545854b1427fcf8cb8dde18b (
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
|
/**
* Copyright 2020 Nokia.
*
* 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.vtp.execution;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonParseException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.util.Arrays;
import java.util.Comparator;
import java.util.Optional;
public class VTPExecutionResultsSupplier {
public static final Logger logger = LoggerFactory.getLogger(VTPExecutionResultsSupplier.class);
private static final String SUB_PATH_TO_EXECUTION_OUTPUT = "/output";
private static final Gson gson = new Gson();
protected String pathToExecutions;
VTPExecutionResultsSupplier(String pathToExecutions) {
this.pathToExecutions = pathToExecutions;
}
public JsonElement getExecutionOutputsFromFile(String executionId) {
File directoryWithExecutionFiles = new File(pathToExecutions);
return getExecutionFilesForGivenRequest(executionId, directoryWithExecutionFiles)
.map(this::getOutputOfLatestExecutionFile)
.orElse(createNoOutputFileErrorMessageInJsonFormat());
}
private Optional<File[]> getExecutionFilesForGivenRequest(String requestId, File directoryWithExecutionsData) {
return Optional.ofNullable(
directoryWithExecutionsData.listFiles((dir, name) -> name.startsWith(requestId))
);
}
private JsonElement getOutputOfLatestExecutionFile(File[] directoriesWithExecutionsData) {
return Arrays.stream(directoriesWithExecutionsData)
.max(Comparator.comparing(File::lastModified))
.map(file -> new File(file.getAbsolutePath() + SUB_PATH_TO_EXECUTION_OUTPUT))
.filter(File::exists)
.map(this::loadOutputJsonFromFile)
.orElse(createNoOutputFileErrorMessageInJsonFormat());
}
private JsonArray loadOutputJsonFromFile(File file) {
JsonArray outputJson;
try {
String executionResult = Files.readString(file.toPath());
outputJson = gson.fromJson(executionResult, JsonArray.class);
} catch (IOException | JsonParseException e) {
logger.error(e.getMessage(),e);
String errorMessage = "" +
"[{ \"error\": \"fail to load execution result\",\"reason\":\"" + e.getMessage() + "\"}]";
outputJson = gson.fromJson(errorMessage, JsonArray.class);
}
return outputJson;
}
private JsonArray createNoOutputFileErrorMessageInJsonFormat() {
return gson.fromJson("[{ \"error\": \"unable to find execution results\"}]", JsonArray.class);
}
}
|