diff options
author | Kanagaraj Manickam <kanagaraj.manickam@huawei.com> | 2020-09-10 06:34:16 +0000 |
---|---|---|
committer | Gerrit Code Review <gerrit@onap.org> | 2020-09-10 06:34:16 +0000 |
commit | 19f97800c3fbcc9a1f845b9a8e9fee6f3f6400f7 (patch) | |
tree | fedb1c193dba58866ebdfd22ebf716e97938b3a3 /vnfmarket-be/vnf-sdk-marketplace/src/main | |
parent | c25bb0be5f2d0358f094fd5ce294ca046882c981 (diff) | |
parent | 5fd768b457c0ec2eaf75963681fe7fcd1e69ae4e (diff) |
Merge "Add list of errors in responce for GET request."
Diffstat (limited to 'vnfmarket-be/vnf-sdk-marketplace/src/main')
2 files changed, 96 insertions, 2 deletions
diff --git a/vnfmarket-be/vnf-sdk-marketplace/src/main/java/org/onap/vtp/execution/VTPExecutionResource.java b/vnfmarket-be/vnf-sdk-marketplace/src/main/java/org/onap/vtp/execution/VTPExecutionResource.java index 3bd522ff..2b06fe8f 100644 --- a/vnfmarket-be/vnf-sdk-marketplace/src/main/java/org/onap/vtp/execution/VTPExecutionResource.java +++ b/vnfmarket-be/vnf-sdk-marketplace/src/main/java/org/onap/vtp/execution/VTPExecutionResource.java @@ -1,5 +1,6 @@ /** * Copyright 2018 Huawei Technologies Co., Ltd. + * 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. @@ -87,8 +88,9 @@ public class VTPExecutionResource extends VTPResource{ private static final String PRODUCT_ARG="--product"; private static final String OPEN_CLI="open-cli"; private static final String FORMAT="--format"; + private static final Gson gson = new Gson(); - private static Gson gson = new Gson(); + protected static String pathToExecutions = "/opt/vtp/data/executions/"; public VTPTestExecutionList executeHandler(VTPTestExecutionList executions, String requestId) throws VTPException { if (requestId == null) { @@ -295,8 +297,13 @@ public class VTPExecutionResource extends VTPResource{ if (n.get(END_TIME) != null) exec.setEndTime(n.get(END_TIME).getAsString()); - if (n.get(EXECUTION_ID) != null) + if (n.get(EXECUTION_ID) != null) { exec.setExecutionId(n.get(EXECUTION_ID).getAsString()); + exec.setResults( + new VTPExecutionResultsSupplier(pathToExecutions) + .getExecutionOutputsFromFile(exec.getExecutionId()) + ); + } if (n.get(REQUEST_ID) != null) exec.setRequestId(n.get(REQUEST_ID).getAsString()); @@ -316,6 +323,7 @@ public class VTPExecutionResource extends VTPResource{ if (n.get(STATUS) != null) exec.setStatus(n.get(STATUS).getAsString()); + list.getExecutions().add(exec); } @@ -325,6 +333,7 @@ public class VTPExecutionResource extends VTPResource{ return list; } + @Path("/executions") @GET @ApiOperation(tags = "VTP Execution", value = " List test executions", response = VTPTestExecution.class, responseContainer = "List") diff --git a/vnfmarket-be/vnf-sdk-marketplace/src/main/java/org/onap/vtp/execution/VTPExecutionResultsSupplier.java b/vnfmarket-be/vnf-sdk-marketplace/src/main/java/org/onap/vtp/execution/VTPExecutionResultsSupplier.java new file mode 100644 index 00000000..3f1e4c57 --- /dev/null +++ b/vnfmarket-be/vnf-sdk-marketplace/src/main/java/org/onap/vtp/execution/VTPExecutionResultsSupplier.java @@ -0,0 +1,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); + } +} |