diff options
author | 2020-12-11 13:56:54 +0100 | |
---|---|---|
committer | 2020-12-14 10:02:45 +0100 | |
commit | bb56d1bcc20d7321f923ab3fae9c6a15c1e92a40 (patch) | |
tree | cef75f9a0adf52cb7ba02df9f6c05b7f4fec4dfc /pmdictionaryvalidation/src/main/java | |
parent | a2068b518a880600b18ce1ca29540fa680fa86f2 (diff) |
Add cli to pmdictionary validation
- Extend pmdictionary validation library by cli
Change-Id: Id24bca84f985b2859eff1eeb5bcf0c443269a947
Issue-ID: VNFSDK-715
Signed-off-by: Zebek Bogumil <bogumil.zebek@nokia.com>
Diffstat (limited to 'pmdictionaryvalidation/src/main/java')
12 files changed, 548 insertions, 0 deletions
diff --git a/pmdictionaryvalidation/src/main/java/org/onap/validation/cli/Main.java b/pmdictionaryvalidation/src/main/java/org/onap/validation/cli/Main.java new file mode 100644 index 0000000..5ab2e69 --- /dev/null +++ b/pmdictionaryvalidation/src/main/java/org/onap/validation/cli/Main.java @@ -0,0 +1,48 @@ +/* + * 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.validation.cli; + +import org.onap.validation.cli.command.validate.ResponseStorage; +import org.onap.validation.cli.command.validate.ToJsonConverter; +import org.onap.validation.cli.command.validate.ValidatePmDictionaryYamlCommand; +import org.onap.validation.cli.core.Cli; +import org.onap.validation.cli.core.Console; +import org.onap.validation.yaml.util.Args; + +import java.util.List; + +public class Main { + + public static void main(String[] args) { + final Console console = new Console(); + final ResponseStorage storage = new ResponseStorage(); + + System.exit( + run(args, console, storage) + ); + } + + static int run(String[] args, Console console, ResponseStorage responseStorage) { + + Cli<String> cli = new Cli<>(console, responseStorage); + + return cli.run( + new Args(List.of(args)), + new ValidatePmDictionaryYamlCommand(new ToJsonConverter()) + ); + } +} diff --git a/pmdictionaryvalidation/src/main/java/org/onap/validation/cli/command/validate/OutputFilePathGenerator.java b/pmdictionaryvalidation/src/main/java/org/onap/validation/cli/command/validate/OutputFilePathGenerator.java new file mode 100644 index 0000000..2ed7285 --- /dev/null +++ b/pmdictionaryvalidation/src/main/java/org/onap/validation/cli/command/validate/OutputFilePathGenerator.java @@ -0,0 +1,41 @@ +/* + *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.validation.cli.command.validate; + +import java.nio.file.Path; + +public class OutputFilePathGenerator { + + public static final int FILE_NAME_ABBREVIATION_INDEX = 0; + public static final String BY_PERIOD_REGEX = "\\."; + public static final String POST_FIX = "-validation-results.json"; + + public Path responsePathFor(Path filePath) { + final Path parent = filePath.getParent(); + final String fileNameAbbreviation = getFileNameAbbreviation(filePath); + return Path.of(parent.toString(), createFileName(fileNameAbbreviation)); + } + + private String createFileName(String fileNameAbbreviation) { + return fileNameAbbreviation + POST_FIX; + } + + private String getFileNameAbbreviation(Path filePath) { + final Path fileName = filePath.getFileName(); + return fileName.toString().split(BY_PERIOD_REGEX)[FILE_NAME_ABBREVIATION_INDEX]; + } +} diff --git a/pmdictionaryvalidation/src/main/java/org/onap/validation/cli/command/validate/ResponseModel.java b/pmdictionaryvalidation/src/main/java/org/onap/validation/cli/command/validate/ResponseModel.java new file mode 100644 index 0000000..033031d --- /dev/null +++ b/pmdictionaryvalidation/src/main/java/org/onap/validation/cli/command/validate/ResponseModel.java @@ -0,0 +1,51 @@ +/* + *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.validation.cli.command.validate; + +import org.onap.validation.yaml.error.YamlDocumentValidationError; + +import java.util.Collections; +import java.util.List; + +public class ResponseModel { + + private final String file; + private final ResponseStatus status; + private final List<YamlDocumentValidationError> errors; + + public ResponseModel(String file, ResponseStatus status, List<YamlDocumentValidationError> errors) { + this.file = file; + this.status = status; + this.errors = errors; + } + + public String getFile() { + return file; + } + + public List<YamlDocumentValidationError> getErrors() { + return Collections.unmodifiableList(errors); + } + + public ResponseStatus getStatus() { + return status; + } + + public enum ResponseStatus { + PASS, FAILED + } +} diff --git a/pmdictionaryvalidation/src/main/java/org/onap/validation/cli/command/validate/ResponseStorage.java b/pmdictionaryvalidation/src/main/java/org/onap/validation/cli/command/validate/ResponseStorage.java new file mode 100644 index 0000000..3daed16 --- /dev/null +++ b/pmdictionaryvalidation/src/main/java/org/onap/validation/cli/command/validate/ResponseStorage.java @@ -0,0 +1,30 @@ +/* + *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.validation.cli.command.validate; + +import java.io.IOException; +import java.io.PrintWriter; +import java.nio.file.Path; + +public class ResponseStorage{ + + public <T> void store(Path filePath, T response) throws IOException { + try (PrintWriter out = new PrintWriter(filePath.toFile())) { + out.println(response); + } + } +} diff --git a/pmdictionaryvalidation/src/main/java/org/onap/validation/cli/command/validate/ToJsonConverter.java b/pmdictionaryvalidation/src/main/java/org/onap/validation/cli/command/validate/ToJsonConverter.java new file mode 100644 index 0000000..168daf8 --- /dev/null +++ b/pmdictionaryvalidation/src/main/java/org/onap/validation/cli/command/validate/ToJsonConverter.java @@ -0,0 +1,28 @@ +/* + *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.validation.cli.command.validate; + +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; + + +public class ToJsonConverter { + public String convert(ResponseModel responseModel) { + Gson gson = new GsonBuilder().setPrettyPrinting().create(); + return gson.toJson(responseModel); + } +} diff --git a/pmdictionaryvalidation/src/main/java/org/onap/validation/cli/command/validate/ValidatePmDictionaryYamlCommand.java b/pmdictionaryvalidation/src/main/java/org/onap/validation/cli/command/validate/ValidatePmDictionaryYamlCommand.java new file mode 100644 index 0000000..ceb46d3 --- /dev/null +++ b/pmdictionaryvalidation/src/main/java/org/onap/validation/cli/command/validate/ValidatePmDictionaryYamlCommand.java @@ -0,0 +1,92 @@ +/* + *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.validation.cli.command.validate; + +import org.onap.validation.cli.core.Command; +import org.onap.validation.cli.core.CommandException; +import org.onap.validation.cli.core.CommandResponse; +import org.onap.validation.yaml.YamlContentValidator; +import org.onap.validation.yaml.error.YamlDocumentValidationError; +import org.onap.validation.yaml.exception.YamlProcessingException; +import org.onap.validation.yaml.util.Args; + +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.List; + +public class ValidatePmDictionaryYamlCommand implements Command<String> { + + public static final int PATH_TO_FILE_INDEX = 0; + private final YamlContentValidator yamlContentValidator = new YamlContentValidator(); + + private final ToJsonConverter toJsonConverter; + private final OutputFilePathGenerator filePathGenerator = new OutputFilePathGenerator(); + + public ValidatePmDictionaryYamlCommand(ToJsonConverter toJsonConverter) { + this.toJsonConverter = toJsonConverter; + } + + @Override + public CommandResponse<String> execute(Args args) throws CommandException { + final String pathToFile = resolvePathToFile(args); + + try { + return validate(pathToFile); + } catch (YamlProcessingException e) { + throw new CommandException("Provided yaml file has invalid structure!", e); + } + } + + @Override + public String getName() { + return "Validate PM Dictionary yaml"; + } + + @Override + public Path getOutputFilePath(Args args) throws CommandException { + final String pathToFile = resolvePathToFile(args); + return this.filePathGenerator.responsePathFor(Path.of(pathToFile)); + } + + private CommandResponse<String> validate(String pathToFile) throws YamlProcessingException { + final List<YamlDocumentValidationError> errors = yamlContentValidator.validate(pathToFile); + return new CommandResponse<>( + toJsonConverter.convert(new ResponseModel(pathToFile, resolveResponseStatus(errors), errors)) + ,resolveCommandStatus(errors) + ); + } + + private ResponseModel.ResponseStatus resolveResponseStatus(List<YamlDocumentValidationError> errors) { + return errors.isEmpty() ? ResponseModel.ResponseStatus.PASS : ResponseModel.ResponseStatus.FAILED; + } + + private CommandResponse.CommandStatus resolveCommandStatus(List<YamlDocumentValidationError> errors) { + return errors.isEmpty() ? CommandResponse.CommandStatus.PASS : CommandResponse.CommandStatus.FAILED; + } + + private String resolvePathToFile(Args args) throws CommandException { + try { + final String pathToFile = args.getArg(PATH_TO_FILE_INDEX); + if (!Files.exists(Path.of(pathToFile))) { + throw new CommandException(String.format("File '%s' does not exist!", pathToFile)); + } + return pathToFile; + } catch (IllegalArgumentException ex) { + throw new CommandException("Command argument is missing: provide a path to file", ex); + } + } +} diff --git a/pmdictionaryvalidation/src/main/java/org/onap/validation/cli/core/Cli.java b/pmdictionaryvalidation/src/main/java/org/onap/validation/cli/core/Cli.java new file mode 100644 index 0000000..bf2abad --- /dev/null +++ b/pmdictionaryvalidation/src/main/java/org/onap/validation/cli/core/Cli.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.validation.cli.core; + +import org.onap.validation.cli.command.validate.ResponseStorage; +import org.onap.validation.cli.core.CommandResponse.CommandStatus; +import org.onap.validation.yaml.util.Args; + +import java.nio.file.Path; + +public class Cli<T> { + + public static final int INTERNAL_ERROR_STATUS_CODE = 2; + public static final int FAILED_STATUS_CODE = 1; + public static final int PASS_STATUS_CODE = 0; + + public static final String APPLICATION_INTERNAL_ERROR_MSG = "\n# Application fails with internal error."; + public static final String APPLICATION_EXIT_SUCCESSFULLY_MSG = "\n# Application exits successfully."; + + private final Console console; + private final ResponseStorage responseStorage; + + public Cli(Console console, ResponseStorage responseStorage) { + this.console = console; + this.responseStorage = responseStorage; + } + + public int run(Args args, Command<T> command) { + try { + final CommandResponse<T> commandResponse = processCommand(args, command); + + logResultInfo(commandResponse.getResult()); + storeResult(args, command, commandResponse.getResult()); + + this.console.info(APPLICATION_EXIT_SUCCESSFULLY_MSG); + + return resolveCliStatusCodeFor(commandResponse); + } catch (Exception e) { + logException(e); + return INTERNAL_ERROR_STATUS_CODE; + } + } + + private int resolveCliStatusCodeFor(CommandResponse<T> commandResponse) { + return commandResponse.getCommandStatus() == CommandStatus.PASS ? PASS_STATUS_CODE : FAILED_STATUS_CODE; + } + + private void logException(Exception e) { + this.console.error("# Command error: "); + this.console.error(e); + this.console.error(APPLICATION_INTERNAL_ERROR_MSG); + } + + private void storeResult(Args args, Command<T> command, T result) throws CommandException, java.io.IOException { + final Path pathToFileWithResponse = command.getOutputFilePath(args); + this.responseStorage.store(pathToFileWithResponse, result); + this.console.info(String.format("%n# Result was stored in a file: '%s'", pathToFileWithResponse)); + } + + private void logResultInfo(T result) { + this.console.info("\n# Operation result:\n"); + this.console.info(result.toString()); + } + + private CommandResponse<T> processCommand(Args args, Command<T> command) throws CommandException { + this.console.info(String.format("# Executing a '%s' operation ...", command.getName())); + final CommandResponse<T> commandResponse = command.execute(args); + this.console.info("# ... Done."); + return commandResponse; + } +} diff --git a/pmdictionaryvalidation/src/main/java/org/onap/validation/cli/core/Command.java b/pmdictionaryvalidation/src/main/java/org/onap/validation/cli/core/Command.java new file mode 100644 index 0000000..1da724f --- /dev/null +++ b/pmdictionaryvalidation/src/main/java/org/onap/validation/cli/core/Command.java @@ -0,0 +1,29 @@ +/* + *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.validation.cli.core; + +import org.onap.validation.yaml.util.Args; + +import java.nio.file.Path; + +public interface Command<T> { + CommandResponse<T> execute(Args args) throws CommandException; + + String getName(); + + Path getOutputFilePath(Args args) throws CommandException; +} diff --git a/pmdictionaryvalidation/src/main/java/org/onap/validation/cli/core/CommandException.java b/pmdictionaryvalidation/src/main/java/org/onap/validation/cli/core/CommandException.java new file mode 100644 index 0000000..2c72ff8 --- /dev/null +++ b/pmdictionaryvalidation/src/main/java/org/onap/validation/cli/core/CommandException.java @@ -0,0 +1,27 @@ +/* + *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.validation.cli.core; + +public class CommandException extends Exception { + public CommandException(String msg) { + super(msg); + } + + public CommandException(String msg, Exception ex) { + super(msg, ex); + } +} diff --git a/pmdictionaryvalidation/src/main/java/org/onap/validation/cli/core/CommandResponse.java b/pmdictionaryvalidation/src/main/java/org/onap/validation/cli/core/CommandResponse.java new file mode 100644 index 0000000..e9f70ca --- /dev/null +++ b/pmdictionaryvalidation/src/main/java/org/onap/validation/cli/core/CommandResponse.java @@ -0,0 +1,40 @@ +/* + *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.validation.cli.core; + +public class CommandResponse<T> { + + private final T result; + private final CommandStatus commandStatus; + + public CommandResponse(T result, CommandStatus commandStatus) { + this.result = result; + this.commandStatus = commandStatus; + } + + public T getResult() { + return result; + } + + public CommandStatus getCommandStatus() { + return commandStatus; + } + + public enum CommandStatus { + PASS, FAILED + } +} diff --git a/pmdictionaryvalidation/src/main/java/org/onap/validation/cli/core/Console.java b/pmdictionaryvalidation/src/main/java/org/onap/validation/cli/core/Console.java new file mode 100644 index 0000000..4c20437 --- /dev/null +++ b/pmdictionaryvalidation/src/main/java/org/onap/validation/cli/core/Console.java @@ -0,0 +1,41 @@ +/* + *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.validation.cli.core; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class Console { + + public static final Logger logger = LoggerFactory.getLogger(Console.class); + + public void info(String msg) { + System.out.println(msg); + logger.info(msg); + } + + public void error(Exception ex) { + System.err.println(ex.getMessage() + ", more information in log file."); + logger.error("Internal error", ex); + } + + public void error(String msg) { + System.err.println(msg); + logger.error(msg); + } + +} diff --git a/pmdictionaryvalidation/src/main/java/org/onap/validation/yaml/util/Args.java b/pmdictionaryvalidation/src/main/java/org/onap/validation/yaml/util/Args.java new file mode 100644 index 0000000..9ae44c8 --- /dev/null +++ b/pmdictionaryvalidation/src/main/java/org/onap/validation/yaml/util/Args.java @@ -0,0 +1,36 @@ +/* + *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.validation.yaml.util; + +import java.util.ArrayList; +import java.util.List; + +public class Args { + + private final List<String> data; + + public Args(List<String> data) { + this.data = new ArrayList<>(data); + } + + public String getArg(int index) { + if (this.data.size() <= index) { + throw new IllegalArgumentException(String.format("Argument with index %d is not available!", index)); + } + return this.data.get(index); + } +} |