diff options
Diffstat (limited to 'utils/src/main/java/org/onap/policy/common/utils')
31 files changed, 1021 insertions, 365 deletions
diff --git a/utils/src/main/java/org/onap/policy/common/utils/cmd/CommandLineArgumentsHandler.java b/utils/src/main/java/org/onap/policy/common/utils/cmd/CommandLineArgumentsHandler.java new file mode 100644 index 00000000..970cebfe --- /dev/null +++ b/utils/src/main/java/org/onap/policy/common/utils/cmd/CommandLineArgumentsHandler.java @@ -0,0 +1,273 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2021 Nordix Foundation. + * Modifications Copyright (C) 2021 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * 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. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.common.utils.cmd; + +import java.io.PrintWriter; +import java.io.StringWriter; +import java.net.URISyntaxException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.Arrays; +import lombok.Getter; +import lombok.Setter; +import org.apache.commons.cli.CommandLine; +import org.apache.commons.cli.DefaultParser; +import org.apache.commons.cli.HelpFormatter; +import org.apache.commons.cli.Option; +import org.apache.commons.cli.Options; +import org.apache.commons.cli.ParseException; +import org.apache.commons.lang3.StringUtils; +import org.onap.policy.common.utils.resources.ResourceUtils; + +/** + * Class for command line common processing. + * + * @author Adheli Tavares (adheli.tavares@est.tech) + * + */ +public class CommandLineArgumentsHandler { + private static final String FILE_MESSAGE_PREAMBLE = " file \""; + private static final int HELP_LINE_LENGTH = 120; + + private final Options options; + + private final String helpClassName; + private final String component; + + @Getter + @Setter + private String configurationFilePath = null; + + @Getter + @Setter + private String propertyFilePath = null; + + @Getter + private CommandLine commandLine = null; + + /** + * Construct the options with default values for the CLI editor. + */ + protected CommandLineArgumentsHandler(String helpClassName, String component) { + this.helpClassName = helpClassName; + this.component = component; + //@formatter:off + options = new Options(); + options.addOption(Option.builder("h") + .longOpt("help") + .desc("outputs the usage of this command") + .required(false) + .type(Boolean.class) + .build()); + options.addOption(Option.builder("v") + .longOpt("version") + .desc("outputs the version of " + this.component) + .required(false) + .type(Boolean.class) + .build()); + options.addOption(Option.builder("c") + .longOpt("config-file") + .desc(String.format("the full path to the configuration file to use, " + + "the configuration file must be a Json file containing the %s parameters", this.component)) + .hasArg().argName("CONFIG_FILE") + .required(false) + .type(String.class) + .build()); + //@formatter:on + } + + /** + * Construct the options for the CLI editor with extra options. + */ + public CommandLineArgumentsHandler(String helpClassName, String component, Option... customOptions) { + this(helpClassName, component); + if (customOptions != null) { + for (Option option : customOptions) { + options.addOption(option); + } + } + } + + /** + * Construct the options with brand-new options for the CLI editor. + */ + public CommandLineArgumentsHandler(String helpClassName, String component, Options options) { + this.options = options; + this.helpClassName = helpClassName; + this.component = component; + } + + /** + * Parse the command line options. + * + * @param args The command line arguments + * @return a string with a message for help and version, or null if there is no message + * @throws CommandLineException on command argument errors + */ + public String parse(final String[] args) throws CommandLineException { + // Clear all our arguments + setConfigurationFilePath(null); + setPropertyFilePath(null); + + try { + commandLine = new DefaultParser().parse(options, args); + } catch (final ParseException | NullPointerException e) { + throw new CommandLineException("invalid command line arguments specified", e); + } + + // Arguments left over after Commons CLI does its stuff + final String[] remainingArgs = removeEmptyValues(commandLine.getArgs()); + + if (remainingArgs.length > 0) { + throw new CommandLineException("too many command line arguments specified: " + Arrays.toString(args)); + } + + if (commandLine.hasOption('h')) { + return help(); + } + + if (commandLine.hasOption('v')) { + return version(); + } + + if (commandLine.hasOption('c')) { + setConfigurationFilePath(commandLine.getOptionValue('c')); + } + + if (commandLine.hasOption('p')) { + setPropertyFilePath(commandLine.getOptionValue('p')); + } + + return null; + } + + /** + * Validate the command line options. + * + * @throws CommandLineException on command argument validation errors + */ + public void validate() throws CommandLineException { + validateReadableFile(this.component + " configuration", configurationFilePath); + } + + /** + * Print version information for policy distribution. + * + * @return the version string + */ + public String version() { + return ResourceUtils.getResourceAsString("version.txt"); + } + + /** + * Print help information for policy distribution. + * + * @return the help string + */ + public String help() { + final var helpFormatter = new HelpFormatter(); + final var stringWriter = new StringWriter(); + final var printWriter = new PrintWriter(stringWriter); + final String cmdLineSyntax = this.helpClassName + " [options...]"; + + helpFormatter.printHelp(printWriter, HELP_LINE_LENGTH, cmdLineSyntax, "options", options, 0, 0, ""); + + return stringWriter.toString(); + } + + /** + * Gets the full expanded configuration file path. + * + * @return the configuration file path + */ + public String getFullConfigurationFilePath() { + return ResourceUtils.getFilePath4Resource(getConfigurationFilePath()); + } + + /** + * Check set configuration file path. + * + * @return true, if check set configuration file path + */ + public boolean checkSetConfigurationFilePath() { + return StringUtils.isNotBlank(getConfigurationFilePath()); + } + + /** + * Gets the full expanded property file path. + * + * @return the property file path + */ + public String getFullPropertyFilePath() { + return ResourceUtils.getFilePath4Resource(getPropertyFilePath()); + } + + /** + * Check set property file path. + * + * @return true, if check set property file path + */ + public boolean checkSetPropertyFilePath() { + return StringUtils.isNotBlank(getPropertyFilePath()); + } + + /** + * Validate readable file. + * + * @param fileTag the file tag + * @param fileName the file name + * @throws CommandLineException on the file name passed as a parameter + */ + protected void validateReadableFile(final String fileTag, final String fileName) throws CommandLineException { + if (StringUtils.isBlank(fileName)) { + throw new CommandLineException(fileTag + " file was not specified as an argument"); + } + + // The file name refers to a resource on the local file system + final var fileUrl = ResourceUtils.getUrl4Resource(fileName); + if (fileUrl == null) { + throw new CommandLineException(fileTag + FILE_MESSAGE_PREAMBLE + fileName + "\" does not exist"); + } + + try { + var path = Path.of(fileUrl.toURI()); + if (!Files.isRegularFile(path)) { + throw new CommandLineException(fileTag + FILE_MESSAGE_PREAMBLE + fileName + "\" is not a normal file"); + } + if (!Files.isReadable(path)) { + throw new CommandLineException(fileTag + FILE_MESSAGE_PREAMBLE + fileName + "\" is unreadable"); + } + } catch (URISyntaxException e) { + throw new CommandLineException("Error parsing " + fileUrl.toString(), e); + } + + } + + /** + * Checks if args has any null or empty value after parsing. + * + * @param args remaining args from CLI parse. + */ + private String[] removeEmptyValues(String[] args) { + return Arrays.stream(args).filter(StringUtils::isNotBlank).toArray(String[]::new); + } +} diff --git a/utils/src/main/java/org/onap/policy/common/utils/cmd/CommandLineException.java b/utils/src/main/java/org/onap/policy/common/utils/cmd/CommandLineException.java new file mode 100644 index 00000000..9cdf3d37 --- /dev/null +++ b/utils/src/main/java/org/onap/policy/common/utils/cmd/CommandLineException.java @@ -0,0 +1,57 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2021 Nordix Foundation. + * ================================================================================ + * 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. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.common.utils.cmd; + +import java.io.Serial; + +/** + * Exception used for CommandLineArguments class. + * + * @author Adheli Tavares (adheli.tavares@est.tech) + * + */ +public class CommandLineException extends Exception { + + /** + * Generated serialVersionUID. + */ + @Serial + private static final long serialVersionUID = -1200607308084606425L; + + /** + * Instantiates a new exception with a message. + * + * @param message the message + */ + public CommandLineException(final String message) { + super(message); + } + + /** + * Instantiates a new exception with a message and a caused by exception. + * + * @param message the message + * @param exp the exception that caused this exception to be thrown + */ + public CommandLineException(final String message, final Exception exp) { + super(message, exp); + } +} diff --git a/utils/src/main/java/org/onap/policy/common/utils/coder/PropertyCoder.java b/utils/src/main/java/org/onap/policy/common/utils/coder/PropertyCoder.java index 3036d353..daacf479 100644 --- a/utils/src/main/java/org/onap/policy/common/utils/coder/PropertyCoder.java +++ b/utils/src/main/java/org/onap/policy/common/utils/coder/PropertyCoder.java @@ -2,7 +2,7 @@ * ============LICENSE_START======================================================= * ONAP PAP * ================================================================================ - * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2019, 2021 AT&T Intellectual Property. All rights reserved. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -55,12 +55,12 @@ public class PropertyCoder { * @return a class T object */ public <T> T decode(String json, String keyProperty, Class<T> clazz) { - JsonElement jsonElement = GSON.fromJson(json, JsonElement.class); + var jsonElement = GSON.fromJson(json, JsonElement.class); return new MyDecoder(jsonElement, keyProperty).decrypt(jsonElement, clazz); } public <T> T decode(Reader reader, String keyProperty, Class<T> clazz) { - JsonElement jsonElement = GSON.fromJson(reader, JsonElement.class); + var jsonElement = GSON.fromJson(reader, JsonElement.class); return new MyDecoder(jsonElement, keyProperty).decrypt(jsonElement, clazz); } @@ -71,9 +71,9 @@ public class PropertyCoder { if (!jsonElement.isJsonObject()) { return; } - JsonObject jsonObject = jsonElement.getAsJsonObject(); + var jsonObject = jsonElement.getAsJsonObject(); // Use keyProperty from input to retrieve secretKey - String secretKey = jsonObject.get(keyProperty).getAsString(); + var secretKey = jsonObject.get(keyProperty).getAsString(); if (!StringUtils.isBlank(secretKey)) { crypto = new CryptoUtils(secretKey); } @@ -97,7 +97,7 @@ public class PropertyCoder { if (!jsonElement.getAsJsonPrimitive().isString()) { return jsonElement; } - String value = jsonElement.getAsString(); + var value = jsonElement.getAsString(); if (!value.startsWith("enc:")) { return jsonElement; } @@ -111,7 +111,7 @@ public class PropertyCoder { if (crypto == null) { return jsonArray; } - JsonArray newArray = new JsonArray(); + var newArray = new JsonArray(); for (JsonElement element: jsonArray) { newArray.add(decrypt(element)); } @@ -122,14 +122,14 @@ public class PropertyCoder { if (crypto == null) { return jsonObject; } - JsonObject newObject = new JsonObject(); + var newObject = new JsonObject(); Set<Entry<String, JsonElement>> entrySet = jsonObject.entrySet(); for (Map.Entry<String, JsonElement> entry : entrySet) { String key = entry.getKey(); - JsonElement jsonElement = decrypt(entry.getValue()); + var jsonElement = decrypt(entry.getValue()); newObject.add(key, jsonElement); } return newObject; } } -}
\ No newline at end of file +} diff --git a/utils/src/main/java/org/onap/policy/common/utils/coder/StandardCoder.java b/utils/src/main/java/org/onap/policy/common/utils/coder/StandardCoder.java index 7f5e3b85..d6135afd 100644 --- a/utils/src/main/java/org/onap/policy/common/utils/coder/StandardCoder.java +++ b/utils/src/main/java/org/onap/policy/common/utils/coder/StandardCoder.java @@ -2,7 +2,7 @@ * ============LICENSE_START======================================================= * ONAP * ================================================================================ - * Copyright (C) 2019-2020 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2019-2021 AT&T Intellectual Property. All rights reserved. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -40,12 +40,15 @@ import java.io.Writer; import java.nio.charset.StandardCharsets; import java.util.List; import java.util.Map; +import lombok.AccessLevel; +import lombok.AllArgsConstructor; import org.onap.policy.common.gson.DoubleConverter; import org.onap.policy.common.gson.GsonMessageBodyHandler; /** * JSON encoder and decoder using the "standard" mechanism, which is currently gson. */ +@AllArgsConstructor(access = AccessLevel.PROTECTED) public class StandardCoder implements Coder { /** @@ -83,14 +86,6 @@ public class StandardCoder implements Coder { this(GSON_STD, GSON_STD_PRETTY); } - /** - * Constructs the object. - */ - protected StandardCoder(Gson gson, Gson gsonPretty) { - this.gson = gson; - this.gsonPretty = gsonPretty; - } - @Override public <S, T> T convert(S source, Class<T> clazz) throws CoderException { if (source == null) { @@ -157,7 +152,7 @@ public class StandardCoder implements Coder { @Override public void encode(OutputStream target, Object object) throws CoderException { try { - Writer wtr = makeWriter(target); + var wtr = makeWriter(target); toJson(wtr, object); // flush, but don't close @@ -170,7 +165,7 @@ public class StandardCoder implements Coder { @Override public void encode(File target, Object object) throws CoderException { - try (Writer wtr = makeWriter(target)) { + try (var wtr = makeWriter(target)) { toJson(wtr, object); // no need to flush or close here @@ -212,7 +207,7 @@ public class StandardCoder implements Coder { @Override public <T> T decode(File source, Class<T> clazz) throws CoderException { - try (Reader input = makeReader(source)) { + try (var input = makeReader(source)) { return fromJson(input, clazz); } catch (RuntimeException | IOException e) { @@ -377,6 +372,7 @@ public class StandardCoder implements Coder { /** * Adapter for standard objects. */ + @AllArgsConstructor protected static class StandardTypeAdapter extends TypeAdapter<StandardCoderObject> { /** @@ -384,13 +380,6 @@ public class StandardCoder implements Coder { */ private static TypeAdapter<JsonElement> elementAdapter = new Gson().getAdapter(JsonElement.class); - /** - * Constructs the object. - */ - public StandardTypeAdapter() { - super(); - } - @Override public void write(JsonWriter out, StandardCoderObject value) throws IOException { elementAdapter.write(out, value.getData()); diff --git a/utils/src/main/java/org/onap/policy/common/utils/coder/StandardCoderObject.java b/utils/src/main/java/org/onap/policy/common/utils/coder/StandardCoderObject.java index 5d682638..55f7f9d7 100644 --- a/utils/src/main/java/org/onap/policy/common/utils/coder/StandardCoderObject.java +++ b/utils/src/main/java/org/onap/policy/common/utils/coder/StandardCoderObject.java @@ -2,7 +2,7 @@ * ============LICENSE_START======================================================= * ONAP * ================================================================================ - * Copyright (C) 2019-2020 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2019-2021 AT&T Intellectual Property. All rights reserved. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -20,15 +20,18 @@ package org.onap.policy.common.utils.coder; -import com.google.gson.JsonArray; import com.google.gson.JsonElement; import java.io.Serializable; +import lombok.AccessLevel; +import lombok.AllArgsConstructor; +import lombok.Getter; /** * Object type used by the {@link StandardCoder}. Different serialization tools have * different "standard objects". For instance, GSON uses {@link JsonElement}. This class * wraps that object so that it can be used without exposing the object, itself. */ +@AllArgsConstructor(access = AccessLevel.PROTECTED) public class StandardCoderObject implements Serializable { private static final long serialVersionUID = 1L; @@ -39,6 +42,7 @@ public class StandardCoderObject implements Serializable { * this should not be transient, but since it isn't serializable, we're stuck with it * until there's time to address the issue */ + @Getter(AccessLevel.PROTECTED) private final transient JsonElement data; /** @@ -49,24 +53,6 @@ public class StandardCoderObject implements Serializable { } /** - * Constructs the object. - * - * @param data data wrapped by this object. - */ - protected StandardCoderObject(JsonElement data) { - this.data = data; - } - - /** - * Gets the data wrapped by this. - * - * @return the data wrapped by this - */ - protected JsonElement getData() { - return data; - } - - /** * Gets a field's value from this object, traversing the object hierarchy. * * @param fields field hierarchy. These may be strings, identifying fields within the @@ -130,7 +116,7 @@ public class StandardCoderObject implements Serializable { return null; } - JsonArray array = element.getAsJsonArray(); + var array = element.getAsJsonArray(); if (index >= array.size()) { return null; diff --git a/utils/src/main/java/org/onap/policy/common/utils/coder/StandardValCoder.java b/utils/src/main/java/org/onap/policy/common/utils/coder/StandardValCoder.java index 647a6155..4deeba14 100644 --- a/utils/src/main/java/org/onap/policy/common/utils/coder/StandardValCoder.java +++ b/utils/src/main/java/org/onap/policy/common/utils/coder/StandardValCoder.java @@ -1,6 +1,6 @@ /*-- * ============LICENSE_START======================================================= - * Copyright (C) 2020 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2020-2021 AT&T Intellectual Property. All rights reserved. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -72,7 +72,7 @@ public class StandardValCoder extends StandardCoder { @Override protected String toJson(@NonNull Object object) { - StringWriter output = new StringWriter(); + var output = new StringWriter(); toJson(output, object); return output.toString(); } @@ -89,7 +89,7 @@ public class StandardValCoder extends StandardCoder { @Override protected <T> T fromJson(String json, Class<T> clazz) { - StringReader reader = new StringReader(json); + var reader = new StringReader(json); return convertFromDouble(clazz, gson.fromJson(validatorApi.createJsonReader(validator, reader), clazz)); } diff --git a/utils/src/main/java/org/onap/policy/common/utils/coder/StandardYamlCoder.java b/utils/src/main/java/org/onap/policy/common/utils/coder/StandardYamlCoder.java index c4375968..d94ddca4 100644 --- a/utils/src/main/java/org/onap/policy/common/utils/coder/StandardYamlCoder.java +++ b/utils/src/main/java/org/onap/policy/common/utils/coder/StandardYamlCoder.java @@ -34,7 +34,12 @@ public class StandardYamlCoder extends StandardCoder { * Constructs the object. */ public StandardYamlCoder() { - translator = new YamlJsonTranslator(gson); + translator = new YamlJsonTranslator(gson) { + @Override + protected <T> T convertFromDouble(Class<T> clazz, T value) { + return StandardYamlCoder.this.convertFromDouble(clazz, value); + } + }; } @Override diff --git a/utils/src/main/java/org/onap/policy/common/utils/coder/YamlJsonTranslator.java b/utils/src/main/java/org/onap/policy/common/utils/coder/YamlJsonTranslator.java index 906c9fdd..077246bf 100644 --- a/utils/src/main/java/org/onap/policy/common/utils/coder/YamlJsonTranslator.java +++ b/utils/src/main/java/org/onap/policy/common/utils/coder/YamlJsonTranslator.java @@ -2,7 +2,7 @@ * ============LICENSE_START======================================================= * ONAP * ================================================================================ - * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2019-2021 AT&T Intellectual Property. All rights reserved. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -21,6 +21,7 @@ package org.onap.policy.common.utils.coder; import com.google.gson.Gson; +import com.google.gson.GsonBuilder; import com.google.gson.JsonArray; import com.google.gson.JsonElement; import com.google.gson.JsonNull; @@ -31,9 +32,12 @@ import java.io.Reader; import java.io.StringReader; import java.io.StringWriter; import java.io.Writer; +import java.time.Instant; import java.util.ArrayList; import java.util.List; import java.util.Map.Entry; +import lombok.AllArgsConstructor; +import org.onap.policy.common.gson.InstantTypeAdapter; import org.yaml.snakeyaml.DumperOptions; import org.yaml.snakeyaml.Yaml; import org.yaml.snakeyaml.emitter.Emitter; @@ -50,7 +54,13 @@ import org.yaml.snakeyaml.serializer.Serializer; /** * YAML-JSON translator. The methods may throw either of the runtime exceptions, * YAMLException or JsonSyntaxException. + * <p/> + * Note: if the invoker wishes Double to be converted to Integer/Long when type + * Object.class is requested, then a Gson object must be used that will perform the + * translation. In addition, the {@link #convertFromDouble(Class, Object)} method should + * be overridden with an appropriate conversion method. */ +@AllArgsConstructor public class YamlJsonTranslator { /** @@ -62,16 +72,9 @@ public class YamlJsonTranslator { * Constructs the object. */ public YamlJsonTranslator() { - this(new Gson()); - } - - /** - * Constructs the object. - * - * @param gson the Gson object to be used to serialize and de-serialize - */ - public YamlJsonTranslator(Gson gson) { - this.gson = gson; + GsonBuilder builder = new GsonBuilder(); + builder.registerTypeAdapter(Instant.class, new InstantTypeAdapter()); + gson = builder.create(); } /** @@ -81,7 +84,7 @@ public class YamlJsonTranslator { * @return YAML representing the original object */ public String toYaml(Object object) { - StringWriter output = new StringWriter(); + var output = new StringWriter(); toYaml(output, object); return output.toString(); } @@ -93,8 +96,8 @@ public class YamlJsonTranslator { * @param object POJO to be translated */ public void toYaml(Writer target, Object object) { - DumperOptions dumper = new DumperOptions(); - Serializer serializer = new Serializer(new Emitter(target, dumper), new Resolver(), dumper, null); + var dumper = new DumperOptions(); + var serializer = new Serializer(new Emitter(target, dumper), new Resolver(), dumper, null); try { serializer.open(); @@ -135,7 +138,7 @@ public class YamlJsonTranslator { * @return a POJO representing the YAML read from the reader */ public <T> T fromYaml(Reader source, Class<T> clazz) { - Node node = new Yaml().compose(source); + var node = new Yaml().compose(source); return fromJson(makeJson(node), clazz); } @@ -147,7 +150,22 @@ public class YamlJsonTranslator { * @return a POJO representing the original element */ protected <T> T fromJson(JsonElement jel, Class<T> clazz) { - return gson.fromJson(jel, clazz); + return convertFromDouble(clazz, gson.fromJson(jel, clazz)); + } + + /** + * Converts a value from Double to Integer/Long, walking the value's contents if it's + * a List/Map. Only applies if the specified class refers to the Object class. + * Otherwise, it leaves the value unchanged. + * <p/> + * The default method simply returns the original value. + * + * @param clazz class of object to be decoded + * @param value value to be converted + * @return the converted value + */ + protected <T> T convertFromDouble(Class<T> clazz, T value) { + return value; } /** @@ -261,7 +279,7 @@ public class YamlJsonTranslator { protected JsonArray makeJsonArray(SequenceNode node) { List<Node> nodes = node.getValue(); - JsonArray array = new JsonArray(nodes.size()); + var array = new JsonArray(nodes.size()); nodes.forEach(subnode -> array.add(makeJson(subnode))); return array; @@ -274,10 +292,10 @@ public class YamlJsonTranslator { * @return a gson element corresponding to the node */ protected JsonObject makeJsonObject(MappingNode node) { - JsonObject obj = new JsonObject(); + var obj = new JsonObject(); for (NodeTuple tuple : node.getValue()) { - Node key = tuple.getKeyNode(); + var key = tuple.getKeyNode(); String skey = ((ScalarNode) key).getValue(); obj.add(skey, makeJson(tuple.getValueNode())); @@ -294,7 +312,7 @@ public class YamlJsonTranslator { */ protected JsonElement makeJsonPrim(ScalarNode node) { try { - Tag tag = node.getTag(); + var tag = node.getTag(); if (tag == Tag.INT) { return new JsonPrimitive(Long.valueOf(node.getValue())); diff --git a/utils/src/main/java/org/onap/policy/common/utils/jpa/EntityMgrCloser.java b/utils/src/main/java/org/onap/policy/common/utils/jpa/EntityMgrCloser.java index 3532002e..f1798516 100644 --- a/utils/src/main/java/org/onap/policy/common/utils/jpa/EntityMgrCloser.java +++ b/utils/src/main/java/org/onap/policy/common/utils/jpa/EntityMgrCloser.java @@ -2,14 +2,15 @@ * ============LICENSE_START======================================================= * Common Utils * ================================================================================ - * Copyright (C) 2018 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2018, 2021 AT&T Intellectual Property. All rights reserved. + * Modifications Copyright (C) 2023 Nordix Foundation. * ================================================================================ * 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. @@ -20,40 +21,26 @@ package org.onap.policy.common.utils.jpa; -import javax.persistence.EntityManager; +import jakarta.persistence.EntityManager; +import lombok.AllArgsConstructor; +import lombok.Getter; /** * Wrapper for an <i>EntityManager</i>, providing auto-close functionality. This is useful in * try-with-resources statements. */ +@AllArgsConstructor public class EntityMgrCloser implements AutoCloseable { /** * The wrapped manager. */ - private final EntityManager em; - - /** - * Construct an instance with the EntityManager. - * - * @param em manager to be auto-closed - */ - public EntityMgrCloser(EntityManager em) { - this.em = em; - } - - /** - * Gets the EntityManager wrapped within this object. - * - * @return the associated EntityManager - */ - public EntityManager getManager() { - return em; - } + @Getter + private final EntityManager manager; @Override public void close() { - em.close(); + manager.close(); } } diff --git a/utils/src/main/java/org/onap/policy/common/utils/jpa/EntityMgrFactoryCloser.java b/utils/src/main/java/org/onap/policy/common/utils/jpa/EntityMgrFactoryCloser.java index b046cc55..5f3a6ea8 100644 --- a/utils/src/main/java/org/onap/policy/common/utils/jpa/EntityMgrFactoryCloser.java +++ b/utils/src/main/java/org/onap/policy/common/utils/jpa/EntityMgrFactoryCloser.java @@ -2,14 +2,15 @@ * ============LICENSE_START======================================================= * Common Utils * ================================================================================ - * Copyright (C) 2018 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2018, 2021 AT&T Intellectual Property. All rights reserved. + * Modifications Copyright (C) 2023 Nordix Foundation. * ================================================================================ * 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. @@ -20,40 +21,26 @@ package org.onap.policy.common.utils.jpa; -import javax.persistence.EntityManagerFactory; +import jakarta.persistence.EntityManagerFactory; +import lombok.AllArgsConstructor; +import lombok.Getter; /** * Wrapper for an <i>EntityManagerFactory</i>, providing auto-close functionality. This is useful in * try-with-resources statements. */ +@AllArgsConstructor public class EntityMgrFactoryCloser implements AutoCloseable { /** * The wrapped factory. */ - private final EntityManagerFactory emf; - - /** - * Construct an instance with the given EntityManagerFactory. - * - * @param emf manager to be auto-closed - */ - public EntityMgrFactoryCloser(EntityManagerFactory emf) { - this.emf = emf; - } - - /** - * Gets the EntityManagerFactory wrapped within this object. - * - * @return the associated EntityManagerFactory - */ - public EntityManagerFactory getFactory() { - return emf; - } + @Getter + private final EntityManagerFactory factory; @Override public void close() { - emf.close(); + factory.close(); } } diff --git a/utils/src/main/java/org/onap/policy/common/utils/jpa/EntityTransCloser.java b/utils/src/main/java/org/onap/policy/common/utils/jpa/EntityTransCloser.java index 3552a6fa..131e87a4 100644 --- a/utils/src/main/java/org/onap/policy/common/utils/jpa/EntityTransCloser.java +++ b/utils/src/main/java/org/onap/policy/common/utils/jpa/EntityTransCloser.java @@ -2,14 +2,15 @@ * ============LICENSE_START======================================================= * Common Utils * ================================================================================ - * Copyright (C) 2018 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2018, 2021 AT&T Intellectual Property. All rights reserved. + * Modifications Copyright (C) 2023 Nordix Foundation. * ================================================================================ * 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. @@ -20,7 +21,8 @@ package org.onap.policy.common.utils.jpa; -import javax.persistence.EntityTransaction; +import jakarta.persistence.EntityTransaction; +import lombok.Getter; /** * Wrapper for an <i>EntityTransaction</i> that is auto-rolled back when closed. This is useful in @@ -31,46 +33,37 @@ public class EntityTransCloser implements AutoCloseable { /** * Transaction to be rolled back. */ - private final EntityTransaction trans; + @Getter + private final EntityTransaction transaction; /** * Begins a transaction. - * + * * @param et transaction to wrap/begin */ public EntityTransCloser(EntityTransaction et) { - trans = et; - trans.begin(); - } - - /** - * Gets the wrapped transaction. - * - * @return the transaction - */ - public EntityTransaction getTransation() { - return trans; + transaction = et; + transaction.begin(); } /** * Commits the transaction. */ public void commit() { - trans.commit(); + transaction.commit(); } /** * Rolls back the transaction. */ public void rollback() { - trans.rollback(); + transaction.rollback(); } @Override public void close() { - if (trans.isActive()) { - trans.rollback(); + if (transaction.isActive()) { + transaction.rollback(); } } - } diff --git a/utils/src/main/java/org/onap/policy/common/utils/logging/LoggerMarkerFilter.java b/utils/src/main/java/org/onap/policy/common/utils/logging/LoggerMarkerFilter.java new file mode 100644 index 00000000..2c9830dc --- /dev/null +++ b/utils/src/main/java/org/onap/policy/common/utils/logging/LoggerMarkerFilter.java @@ -0,0 +1,98 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP POLICY + * ================================================================================ + * Copyright (C) 2021 AT&T Intellectual Property. All right reserved. + * Modifications Copyright (C) 2023 Nordix Foundation. + * ================================================================================ + * 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. + * ============LICENSE_END============================================ + * =================================================================== + * + */ + +package org.onap.policy.common.utils.logging; + +import ch.qos.logback.classic.spi.ILoggingEvent; +import ch.qos.logback.core.filter.AbstractMatcherFilter; +import ch.qos.logback.core.spi.FilterReply; +import org.slf4j.Marker; + +public abstract class LoggerMarkerFilter extends AbstractMatcherFilter<ILoggingEvent> { + + protected final Marker marker; + + protected LoggerMarkerFilter(Marker marker) { + this.marker = marker; + } + + @Override + public FilterReply decide(ILoggingEvent event) { + + if (this.marker == null || !isStarted()) { + return FilterReply.DENY; + } + + if (event == null || event.getMarkerList() == null) { + return FilterReply.DENY; + } + + if (event.getMarkerList().stream().anyMatch(mk -> mk.equals(marker))) { + return FilterReply.ACCEPT; + } else { + return FilterReply.DENY; + } + } + + /** + * Metric Logger Marker Filter. + */ + public static class MetricLoggerMarkerFilter extends LoggerMarkerFilter { + + public MetricLoggerMarkerFilter() { + super(LoggerUtils.METRIC_LOG_MARKER); + } + + } + + /** + * Security Logger Marker Filter. + */ + public static class SecurityLoggerMarkerFilter extends LoggerMarkerFilter { + + public SecurityLoggerMarkerFilter() { + super(LoggerUtils.SECURITY_LOG_MARKER); + } + } + + /** + * Audit Logger Marker Filter. + */ + public static class AuditLoggerMarkerFilter extends LoggerMarkerFilter { + + public AuditLoggerMarkerFilter() { + super(LoggerUtils.AUDIT_LOG_MARKER); + } + } + + /** + * Transaction Logger Marker Filter. + */ + public static class TransactionLoggerMarkerFilter extends LoggerMarkerFilter { + + public TransactionLoggerMarkerFilter() { + super(LoggerUtils.TRANSACTION_LOG_MARKER); + } + } + +} diff --git a/utils/src/main/java/org/onap/policy/common/utils/logging/LoggerUtils.java b/utils/src/main/java/org/onap/policy/common/utils/logging/LoggerUtils.java new file mode 100644 index 00000000..89575eb2 --- /dev/null +++ b/utils/src/main/java/org/onap/policy/common/utils/logging/LoggerUtils.java @@ -0,0 +1,105 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP POLICY + * ================================================================================ + * Copyright (C) 2021 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * 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. + * ============LICENSE_END============================================ + * =================================================================== + * + */ + +package org.onap.policy.common.utils.logging; + +import ch.qos.logback.classic.LoggerContext; +import lombok.AccessLevel; +import lombok.NoArgsConstructor; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.slf4j.Marker; +import org.slf4j.MarkerFactory; + +@NoArgsConstructor(access = AccessLevel.PRIVATE) +public final class LoggerUtils { + + private static final Logger LOGGER = LoggerFactory.getLogger(LoggerUtils.class); + + /** + * ROOT logger. + */ + public static final String ROOT_LOGGER = "ROOT"; + /** + * Metric logger. + */ + public static final String METRIC_LOG_MARKER_NAME = "metric"; + /** + * Audit Log Marker Name. + */ + public static final String AUDIT_LOG_MARKER_NAME = "audit"; + /** + * Security Log Marker Name. + */ + public static final String SECURITY_LOG_MARKER_NAME = "security"; + /** + * Transaction Log Marker Name. + */ + public static final String TRANSACTION_LOG_MARKER_NAME = "transaction"; + /** + * Marks a logging record for metric. + */ + public static final Marker METRIC_LOG_MARKER = MarkerFactory.getMarker(METRIC_LOG_MARKER_NAME); + /** + * Marks a logging record for security. + */ + public static final Marker SECURITY_LOG_MARKER = MarkerFactory.getMarker(SECURITY_LOG_MARKER_NAME); + /** + * Marks a logging record for audit. + */ + public static final Marker AUDIT_LOG_MARKER = MarkerFactory.getMarker(AUDIT_LOG_MARKER_NAME); + /** + * Marks a logging record as an end-to-end transaction. + */ + public static final Marker TRANSACTION_LOG_MARKER = MarkerFactory.getMarker(TRANSACTION_LOG_MARKER_NAME); + + + /** + * Set the log level of a logger. + * + * @param loggerName logger name + * @param loggerLevel logger level + */ + public static String setLevel(String loggerName, String loggerLevel) { + if (!(LoggerFactory.getILoggerFactory() instanceof LoggerContext)) { + throw new IllegalStateException("The SLF4J logger factory is not configured for logback"); + } + + final LoggerContext context = (LoggerContext) LoggerFactory.getILoggerFactory(); + final var logger = context.getLogger(loggerName); + if (logger == null) { + throw new IllegalArgumentException("no logger " + loggerName); + } + + LOGGER.warn("setting {} logger to level {}", loggerName, loggerLevel); + + // use the current log level if the string provided cannot be converted to a valid Level. + + // NOSONAR: this method is currently used by the telemetry api (which should be authenticated). + // It is no more or no less dangerous than an admin changing the logback level on the fly. + // This is a controlled admin function that should not cause any risks when the system + // is configured properly. + logger.setLevel(ch.qos.logback.classic.Level.toLevel(loggerLevel, logger.getLevel())); // NOSONAR + + return logger.getLevel().toString(); + } +} diff --git a/utils/src/main/java/org/onap/policy/common/utils/network/NetworkUtil.java b/utils/src/main/java/org/onap/policy/common/utils/network/NetworkUtil.java index a2fb5a8b..6698d7cf 100644 --- a/utils/src/main/java/org/onap/policy/common/utils/network/NetworkUtil.java +++ b/utils/src/main/java/org/onap/policy/common/utils/network/NetworkUtil.java @@ -2,7 +2,7 @@ * ============LICENSE_START======================================================= * ONAP * ================================================================================ - * Copyright (C) 2017-2020 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2017-2021 AT&T Intellectual Property. All rights reserved. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -26,7 +26,10 @@ import java.net.InetSocketAddress; import java.net.ServerSocket; import java.net.Socket; import java.net.UnknownHostException; +import java.util.UUID; import javax.net.ssl.TrustManager; +import lombok.AccessLevel; +import lombok.NoArgsConstructor; import org.apache.commons.net.util.TrustManagerUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -34,7 +37,8 @@ import org.slf4j.LoggerFactory; /** * Network Utilities. */ -public class NetworkUtil { +@NoArgsConstructor(access = AccessLevel.PRIVATE) +public final class NetworkUtil { public static final Logger logger = LoggerFactory.getLogger(NetworkUtil.class.getName()); @@ -49,10 +53,6 @@ public class NetworkUtil { */ private static final TrustManager[] ALWAYS_TRUST_MANAGER = { TrustManagerUtils.getAcceptAllTrustManager() }; - private NetworkUtil() { - // Empty constructor - } - /** * Allocates an available port on which a server may listen. * @@ -113,7 +113,7 @@ public class NetworkUtil { */ public static boolean isTcpPortOpen(String host, int port, int retries, long interval) throws InterruptedException { - int retry = 0; + var retry = 0; while (retry < retries) { /* * As with the server socket, this is only used to see if the port is open, @@ -171,4 +171,15 @@ public class NetworkUtil { return "127.0.0.1"; } + + /** + * Generates a globally unique name, typically for use in PDP messages, to uniquely + * identify a PDP (or PAP), regardless on what cluster it resides. + * + * @param prefix text to be prepended to the generated value + * @return a globally unique name + */ + public static String genUniqueName(String prefix) { + return prefix + "-" + UUID.randomUUID(); + } } diff --git a/utils/src/main/java/org/onap/policy/common/utils/properties/BeanConfigurator.java b/utils/src/main/java/org/onap/policy/common/utils/properties/BeanConfigurator.java index 2ef91911..f8c52091 100644 --- a/utils/src/main/java/org/onap/policy/common/utils/properties/BeanConfigurator.java +++ b/utils/src/main/java/org/onap/policy/common/utils/properties/BeanConfigurator.java @@ -2,7 +2,7 @@ * ============LICENSE_START======================================================= * ONAP - Common Modules * ================================================================================ - * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2019, 2021 AT&T Intellectual Property. All rights reserved. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -20,6 +20,7 @@ package org.onap.policy.common.utils.properties; +import com.google.re2j.Pattern; import java.lang.reflect.Field; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; @@ -45,6 +46,7 @@ import org.onap.policy.common.utils.properties.exception.PropertyMissingExceptio * <i>accept</i> includes the "empty" option. */ public class BeanConfigurator { + private static final Pattern COMMA_PAT = Pattern.compile(","); /** * The "empty" option that may appear within the {@link Property}'s <i>accept</i> @@ -414,7 +416,7 @@ public class BeanConfigurator { * @return {@code true} if the <i>accept</i> attribute includes "empty" */ protected boolean isEmptyOk(Property prop) { - for (String option : prop.accept().split(",")) { + for (String option : COMMA_PAT.split(prop.accept())) { if (ACCEPT_EMPTY.equals(option)) { return true; } @@ -495,7 +497,7 @@ public class BeanConfigurator { * @throws PropertyAccessException if a "get" method cannot be identified */ private Method getGetter(Field field, Property prop) throws PropertyAccessException { - String capnm = StringUtils.capitalize(field.getName()); + var capnm = StringUtils.capitalize(field.getName()); try { return getGetter(field, "get" + capnm); diff --git a/utils/src/main/java/org/onap/policy/common/utils/properties/PropertyObjectUtils.java b/utils/src/main/java/org/onap/policy/common/utils/properties/PropertyObjectUtils.java index 996f1b87..2b6e514f 100644 --- a/utils/src/main/java/org/onap/policy/common/utils/properties/PropertyObjectUtils.java +++ b/utils/src/main/java/org/onap/policy/common/utils/properties/PropertyObjectUtils.java @@ -1,6 +1,6 @@ /*- * ============LICENSE_START======================================================= - * Copyright (C) 2019-2020 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2019-2021 AT&T Intellectual Property. All rights reserved. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -18,28 +18,27 @@ package org.onap.policy.common.utils.properties; +import com.google.re2j.Pattern; import java.util.ArrayList; import java.util.Iterator; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; import java.util.Properties; -import java.util.regex.Matcher; -import java.util.regex.Pattern; +import lombok.AccessLevel; +import lombok.NoArgsConstructor; import org.slf4j.Logger; import org.slf4j.LoggerFactory; /** * Utilities for generating POJOs from Properties. */ -public class PropertyObjectUtils { +@NoArgsConstructor(access = AccessLevel.PRIVATE) +public final class PropertyObjectUtils { public static final Logger logger = LoggerFactory.getLogger(PropertyObjectUtils.class); private static final Pattern NAME_PAT = Pattern.compile("\\[(\\d{1,3})\\]$"); - - private PropertyObjectUtils() { - // do nothing - } + private static final Pattern DOT_PAT = Pattern.compile("[.]"); /** * Converts a set of properties to a Map. Supports json-path style property names with @@ -58,7 +57,7 @@ public class PropertyObjectUtils { for (String name : properties.stringPropertyNames()) { if (name.startsWith(dottedPrefix)) { - String[] components = name.substring(pfxlen).split("[.]"); + String[] components = DOT_PAT.split(name.substring(pfxlen)); setProperty(map, components, properties.getProperty(name)); } } @@ -79,13 +78,13 @@ public class PropertyObjectUtils { final int lastComp = names.length - 1; // process all but the final component - for (int comp = 0; comp < lastComp; ++comp) { + for (var comp = 0; comp < lastComp; ++comp) { node = getNode(node, names[comp]); } // process the final component String name = names[lastComp]; - Matcher matcher = NAME_PAT.matcher(name); + var matcher = NAME_PAT.matcher(name); if (!matcher.find()) { // no subscript @@ -95,7 +94,7 @@ public class PropertyObjectUtils { // subscripted List<Object> array = getArray(node, name.substring(0, matcher.start())); - int index = Integer.parseInt(matcher.group(1)); + var index = Integer.parseInt(matcher.group(1)); expand(array, index); array.set(index, value); } @@ -109,7 +108,7 @@ public class PropertyObjectUtils { */ @SuppressWarnings("unchecked") private static Map<String, Object> getNode(Map<String, Object> map, String name) { - Matcher matcher = NAME_PAT.matcher(name); + var matcher = NAME_PAT.matcher(name); if (!matcher.find()) { // no subscript @@ -118,7 +117,7 @@ public class PropertyObjectUtils { // subscripted List<Object> array = getArray(map, name.substring(0, matcher.start())); - int index = Integer.parseInt(matcher.group(1)); + var index = Integer.parseInt(matcher.group(1)); expand(array, index); Object item = array.get(index); diff --git a/utils/src/main/java/org/onap/policy/common/utils/properties/SpecProperties.java b/utils/src/main/java/org/onap/policy/common/utils/properties/SpecProperties.java index ec7157d3..2ea94959 100644 --- a/utils/src/main/java/org/onap/policy/common/utils/properties/SpecProperties.java +++ b/utils/src/main/java/org/onap/policy/common/utils/properties/SpecProperties.java @@ -2,7 +2,7 @@ * ============LICENSE_START======================================================= * ONAP * ================================================================================ - * Copyright (C) 2018, 2020 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2018, 2020-2021 AT&T Intellectual Property. All rights reserved. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -21,10 +21,13 @@ package org.onap.policy.common.utils.properties; import java.util.Properties; +import lombok.AccessLevel; +import lombok.Getter; /** * Properties with an optional specialization (e.g., session name, controller name). */ +@Getter(AccessLevel.PROTECTED) public class SpecProperties extends Properties { private static final long serialVersionUID = 1L; @@ -89,7 +92,7 @@ public class SpecProperties extends Properties { return super.getProperty(key); } - String suffix = key.substring(prefix.length()); + var suffix = key.substring(prefix.length()); String val = super.getProperty(specPrefix + suffix); if (val != null) { @@ -99,14 +102,6 @@ public class SpecProperties extends Properties { return super.getProperty(key); } - protected String getPrefix() { - return prefix; - } - - protected String getSpecPrefix() { - return specPrefix; - } - @Override public final synchronized int hashCode() { throw new UnsupportedOperationException("SpecProperties cannot be hashed"); diff --git a/utils/src/main/java/org/onap/policy/common/utils/properties/exception/PropertyException.java b/utils/src/main/java/org/onap/policy/common/utils/properties/exception/PropertyException.java index 44edd428..3c03f38d 100644 --- a/utils/src/main/java/org/onap/policy/common/utils/properties/exception/PropertyException.java +++ b/utils/src/main/java/org/onap/policy/common/utils/properties/exception/PropertyException.java @@ -2,14 +2,14 @@ * ============LICENSE_START======================================================= * ONAP Policy Engine - Common Modules * ================================================================================ - * Copyright (C) 2018 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2018, 2021 AT&T Intellectual Property. All rights reserved. * ================================================================================ * 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. @@ -20,9 +20,12 @@ package org.onap.policy.common.utils.properties.exception; +import lombok.Getter; + /** * Exception associated with a Property. */ +@Getter public class PropertyException extends Exception { private static final long serialVersionUID = 1L; @@ -38,7 +41,7 @@ public class PropertyException extends Exception { /** * Constructor. - * + * * @param propName name of the property causing the exception, or {@code null} * @param fieldName name of the field causing the exception, or {@code null} */ @@ -51,7 +54,7 @@ public class PropertyException extends Exception { /** * Constructor. - * + * * @param propnm name of the property causing the exception, or {@code null} * @param fieldName name of the field causing the exception, or {@code null} * @param message error message @@ -65,7 +68,7 @@ public class PropertyException extends Exception { /** * Constructor. - * + * * @param propnm name of the property causing the exception, or {@code null} * @param fieldName name of the field causing the exception, or {@code null} * @param cause cause of the exception @@ -79,7 +82,7 @@ public class PropertyException extends Exception { /** * Constructor. - * + * * @param propnm name of the property causing the exception, or {@code null} * @param fieldName name of the field causing the exception, or {@code null} * @param message error message @@ -93,28 +96,8 @@ public class PropertyException extends Exception { } /** - * Get the property name. - * - * @return name of the property for which the exception was thrown, or {@code null} if - * no name was provided - */ - public String getPropertyName() { - return propertyName; - } - - /** - * Get the field name. - * - * @return name of the field for which the exception was thrown, or {@code null} if no - * field was provided - */ - public String getFieldName() { - return fieldName; - } - - /** * Make the message. - * + * * @param propnm name of the property causing the exception, or {@code null} * @param fieldName name of the field causing the exception, or {@code null} * @param message error message, never {@code null} @@ -126,13 +109,13 @@ public class PropertyException extends Exception { /** * Make the message. - * + * * @param propnm name of the property causing the exception, or {@code null} * @param fieldName name of the field causing the exception, or {@code null} * @return an error message composed of the two items */ private static String makeMessage(String propnm, String fieldName) { - StringBuilder bldr = new StringBuilder(50); + var bldr = new StringBuilder(50); if (propnm == null) { bldr.append("property exception"); diff --git a/utils/src/main/java/org/onap/policy/common/utils/resources/DirectoryUtils.java b/utils/src/main/java/org/onap/policy/common/utils/resources/DirectoryUtils.java new file mode 100644 index 00000000..35a13138 --- /dev/null +++ b/utils/src/main/java/org/onap/policy/common/utils/resources/DirectoryUtils.java @@ -0,0 +1,63 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP + * ================================================================================ + * Copyright (C) 2021 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * 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. + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.common.utils.resources; + +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import lombok.AccessLevel; +import lombok.NoArgsConstructor; +import org.apache.commons.io.FileUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Utilities for manipulating directories. + */ +@NoArgsConstructor(access = AccessLevel.PRIVATE) +public final class DirectoryUtils { + private static final Logger logger = LoggerFactory.getLogger(DirectoryUtils.class); + + /** + * Creates a directory file, only accessible by the owner. + * + * @param prefix file name prefix + * @return a new, temporary directory + * @throws IOException if an error occurs + */ + public static Path createTempDirectory(String prefix) throws IOException { + /* + * Disabling sonar, as the code below sets the permissions, just as sonar + * suggests it be fixed. + */ + var path = Files.createTempDirectory(prefix); // NOSONAR + logger.info("created temporary directory, {}", path); + + var file = path.toFile(); + + TextFileUtils.setDefaultPermissions(file); + + // ensure nothing has been written to it yet + FileUtils.cleanDirectory(file); + + return path; + } +} diff --git a/utils/src/main/java/org/onap/policy/common/utils/resources/MessageConstants.java b/utils/src/main/java/org/onap/policy/common/utils/resources/MessageConstants.java index a31a77d2..ef46fdf5 100644 --- a/utils/src/main/java/org/onap/policy/common/utils/resources/MessageConstants.java +++ b/utils/src/main/java/org/onap/policy/common/utils/resources/MessageConstants.java @@ -1,6 +1,7 @@ /*- * ============LICENSE_START======================================================= * Copyright (C) 2020 Bell Canada. All rights reserved. + * Modifications Copyright (C) 2021 AT&T Intellectual Property. All rights reserved. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -20,10 +21,14 @@ package org.onap.policy.common.utils.resources; +import lombok.AccessLevel; +import lombok.NoArgsConstructor; + /** * Common messages to be used by all components. */ -public class MessageConstants { +@NoArgsConstructor(access = AccessLevel.PRIVATE) +public final class MessageConstants { public static final String POLICY_API = "policy-api"; public static final String POLICY_PAP = "policy-pap"; @@ -31,11 +36,8 @@ public class MessageConstants { public static final String POLICY_DROOLS_PDP = "policy-drools-pdp"; public static final String POLICY_XACML_PDP = "policy-xacml-pdp"; public static final String POLICY_DISTRIBUTION = "policy-distribution"; + public static final String POLICY_CLAMP = "policy-clamp"; public static final String START_SUCCESS_MSG = "Started %s service successfully."; public static final String START_FAILURE_MSG = "Start of %s service failed."; - - private MessageConstants() { - super(); - } } diff --git a/utils/src/main/java/org/onap/policy/common/utils/resources/PrometheusUtils.java b/utils/src/main/java/org/onap/policy/common/utils/resources/PrometheusUtils.java new file mode 100644 index 00000000..bac65d4e --- /dev/null +++ b/utils/src/main/java/org/onap/policy/common/utils/resources/PrometheusUtils.java @@ -0,0 +1,107 @@ +/* + * ============LICENSE_START======================================================= + * ONAP + * ================================================================================ + * Copyright (C) 2022 AT&T Intellectual Property. All rights reserved. + * Modifications Copyright (C) 2022 Bell Canada. All rights reserved. + * ================================================================================ + * 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. + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.common.utils.resources; + +import lombok.AccessLevel; +import lombok.Getter; +import lombok.NoArgsConstructor; + +/** + * Prometheus constants and utilities. + */ + +@NoArgsConstructor(access = AccessLevel.PRIVATE) +public class PrometheusUtils { + + /** + * Policy Deployments Metric Name. + */ + public static final String POLICY_DEPLOYMENTS_METRIC = "policy_deployments"; + + /** + * Policy Deployments Metric Help Message. + */ + public static final String POLICY_DEPLOYMENT_HELP = "The total number of policy deployments."; + + /** + * Policy Execution Metric Name. + */ + public static final String POLICY_EXECUTION_METRIC = "policy_executions"; + + /** + * Policy Execution Metric Help Message. + */ + public static final String POLICY_EXECUTION_HELP = "The total number of TOSCA policy executions."; + + /** + * Policy Execution Latency in Seconds Metric Name. + * This metric name is not to be used as a counter. + */ + public static final String POLICY_EXECUTIONS_LATENCY_SECONDS_METRIC = "policy_executions_latency_seconds"; + + /** + * Policy Execution Latency in Seconds Metric Help message. + */ + public static final String POLICY_EXECUTIONS_LATENCY_SECONDS_HELP = + "The latency in seconds of TOSCA policy executions."; + + /** + * Metric label for arbitrary operations (eg. deploy, undeploy, execute). + */ + public static final String OPERATION_METRIC_LABEL = "operation"; + + /** + * Deploy operation value. + */ + public static final String DEPLOY_OPERATION = "deploy"; + + /** + * Undeploy operation value. + */ + public static final String UNDEPLOY_OPERATION = "undeploy"; + + /** + * Metric label for states (ie. PASSIVE, ACTIVE). + */ + public static final String STATE_METRIC_LABEL = "state"; + + /** + * Metric label for status of an operation (ie. SUCCESS or FAILURE). + */ + public static final String STATUS_METRIC_LABEL = "status"; + + /** + * Prometheus namespace values mapping to the supported PDP types. + */ + public enum PdpType { + PDPD("pdpd"), + PDPA("pdpa"), + PDPX("pdpx"); + + @Getter + private final String namespace; + + PdpType(String namespace) { + this.namespace = namespace; + } + } +}
\ No newline at end of file diff --git a/utils/src/main/java/org/onap/policy/common/utils/resources/ResourceUtils.java b/utils/src/main/java/org/onap/policy/common/utils/resources/ResourceUtils.java index 2acc67ac..3ee062f1 100644 --- a/utils/src/main/java/org/onap/policy/common/utils/resources/ResourceUtils.java +++ b/utils/src/main/java/org/onap/policy/common/utils/resources/ResourceUtils.java @@ -1,8 +1,8 @@ /*- * ============LICENSE_START======================================================= * Copyright (C) 2018 Ericsson. All rights reserved. - * Modifications Copyright (C) 2020 Nordix Foundation. - * Modifications Copyright (C) 2020 AT&T Intellectual Property. All rights reserved. + * Modifications Copyright (C) 2020, 2023 Nordix Foundation. + * Modifications Copyright (C) 2020-2021 AT&T Intellectual Property. All rights reserved. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -22,17 +22,22 @@ package org.onap.policy.common.utils.resources; -import java.io.ByteArrayOutputStream; +import com.google.re2j.Pattern; import java.io.File; import java.io.IOException; import java.io.InputStream; import java.net.URL; +import java.nio.charset.StandardCharsets; import java.util.Collections; import java.util.Enumeration; +import java.util.Objects; import java.util.Set; import java.util.TreeSet; import java.util.jar.JarEntry; import java.util.jar.JarFile; +import lombok.AccessLevel; +import lombok.NoArgsConstructor; +import org.apache.commons.io.IOUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -40,25 +45,18 @@ import org.slf4j.LoggerFactory; * This is common utility class with static methods for handling Java resources on the class path. It is an abstract * class to prevent any direct instantiation and private constructor to prevent extending this class. */ -public abstract class ResourceUtils { +@NoArgsConstructor(access = AccessLevel.PRIVATE) +public final class ResourceUtils { // Get a reference to the logger private static final Logger LOGGER = LoggerFactory.getLogger(ResourceUtils.class); - // The length of byte buffers used to read resources into strings - private static final int BYTE_BUFFER_LENGH = 1024; + private static final Pattern SLASH_PAT = Pattern.compile("/"); // Resource types private static final String FILE_PROTOCOL = "file"; private static final String JAR_PROTOCOL = "jar"; /** - * Private constructor used to prevent sub class instantiation. - */ - private ResourceUtils() { - // Prevent construction of this class - } - - /** * Method to resolve a resource; the local file system is checked first and then the class path is checked. * * @param resourceName The resource name @@ -66,7 +64,7 @@ public abstract class ResourceUtils { */ public static URL getUrl4Resource(final String resourceName) { // Check the local fine system first - final URL urlToResource = getLocalFile(resourceName); + final var urlToResource = getLocalFile(resourceName); // Check if this is a local file if (urlToResource != null) { @@ -86,25 +84,16 @@ public abstract class ResourceUtils { */ public static String getResourceAsString(final String resourceName) { // Get the resource as a stream, we'll convert it to a string then - final InputStream resourceStream = getResourceAsStream(resourceName); - if (resourceStream == null) { - return null; - } - - // Read the stream contents in to an output stream - final ByteArrayOutputStream resourceOutputStreamBuffer = new ByteArrayOutputStream(); - final byte[] resourceBuffer = new byte[BYTE_BUFFER_LENGH]; - int length; - try { - while ((length = resourceStream.read(resourceBuffer)) != -1) { - resourceOutputStreamBuffer.write(resourceBuffer, 0, length); + // Read the stream contents, closing when done + try (var resourceStream = getResourceAsStream(resourceName)) { + if (resourceStream == null) { + return null; } + return IOUtils.toString(resourceStream, StandardCharsets.UTF_8); } catch (final IOException e) { LOGGER.debug("error reading resource stream {}", resourceName, e); return null; } - - return resourceOutputStreamBuffer.toString(); } /** @@ -116,12 +105,12 @@ public abstract class ResourceUtils { */ public static InputStream getResourceAsStream(final String resourceName) { // Find a URL to the resource first - final URL urlToResource = getUrl4Resource(resourceName); + final var urlToResource = getUrl4Resource(resourceName); // Check if the resource exists if (urlToResource == null) { // No resource found - LOGGER.debug("cound not find resource \"{}\" : ", resourceName); + LOGGER.debug("could not find resource \"{}\" : ", resourceName); return null; } @@ -143,11 +132,11 @@ public abstract class ResourceUtils { */ public static URL getUrlResource(final String resourceName) { try { - final ClassLoader classLoader = ResourceUtils.class.getClassLoader(); + final var classLoader = ResourceUtils.class.getClassLoader(); - final String[] fileParts = resourceName.split("/"); + final String[] fileParts = SLASH_PAT.split(resourceName); // Read the resource - URL url = classLoader.getResource(resourceName); + var url = classLoader.getResource(resourceName); // Check if the resource is defined if (url != null) { @@ -178,8 +167,8 @@ public abstract class ResourceUtils { public static URL getLocalFile(final String resourceName) { try { // Input might already be in URL format - final URL ret = new URL(resourceName); - final File f = new File(ret.toURI()); + final var ret = new URL(resourceName); + final var f = new File(ret.toURI()); if (f.exists()) { return ret; } @@ -188,10 +177,10 @@ public abstract class ResourceUtils { } try { - final File f = new File(resourceName); + final var f = new File(resourceName); // Check if the file exists if (f.exists()) { - final URL urlret = f.toURI().toURL(); + final var urlret = f.toURI().toURL(); LOGGER.debug("resource \"{}\" was found on the local file system", f.toURI().toURL()); return urlret; } else { @@ -215,7 +204,7 @@ public abstract class ResourceUtils { return null; } - URL modelFileUrl = getUrl4Resource(resource); + var modelFileUrl = getUrl4Resource(resource); if (modelFileUrl != null) { return modelFileUrl.getPath(); } else { @@ -227,11 +216,11 @@ public abstract class ResourceUtils { * Read the list of entries in a resource directory. * * @param resourceDirectoryName the name of the resource directory - * @return the list of entries + * @return a set of entries */ public static Set<String> getDirectoryContents(final String resourceDirectoryName) { // Find the location of the resource, is it in a Jar or on the local file system? - URL directoryUrl = ResourceUtils.getUrl4Resource(resourceDirectoryName); + var directoryUrl = ResourceUtils.getUrl4Resource(resourceDirectoryName); if (directoryUrl == null) { LOGGER.debug("resource \"{}\" was not found", resourceDirectoryName); @@ -255,11 +244,11 @@ public abstract class ResourceUtils { * * @param localResourceDirectoryUrl the local resource file URL * @param resourceDirectoryName the name of the resource directory - * @return a list of the directory contents + * @return a set of the directory contents */ public static Set<String> getDirectoryContentsLocal(final URL localResourceDirectoryUrl, final String resourceDirectoryName) { - File localDirectory = new File(localResourceDirectoryUrl.getFile()); + var localDirectory = new File(localResourceDirectoryUrl.getFile()); if (!localDirectory.isDirectory()) { LOGGER.debug("resource \"{}\" is not a directory", resourceDirectoryName); @@ -267,7 +256,7 @@ public abstract class ResourceUtils { } Set<String> localDirectorySet = new TreeSet<>(); - for (File localDirectoryEntry : localDirectory.listFiles()) { + for (File localDirectoryEntry : Objects.requireNonNull(localDirectory.listFiles())) { if (localDirectoryEntry.isDirectory()) { localDirectorySet .add(resourceDirectoryName + File.separator + localDirectoryEntry.getName() + File.separator); @@ -284,19 +273,19 @@ public abstract class ResourceUtils { * * @param jarResourceDirectoryUrl the name of the resource directory in the jar * @param resourceDirectoryName the name of the resource directory - * @return a list of the directory contents + * @return a set of the directory contents */ public static Set<String> getDirectoryContentsJar(final URL jarResourceDirectoryUrl, final String resourceDirectoryName) { String dirNameWithSlash = resourceDirectoryName + "/"; int minLength = dirNameWithSlash.length() + 1; - File jarResourceDirectory = new File(jarResourceDirectoryUrl.getPath()); + var jarResourceDirectory = new File(jarResourceDirectoryUrl.getPath()); String jarFileName = jarResourceDirectory.getParent().replaceFirst("^file:", "").replaceFirst("!.*$", ""); Set<String> localDirectorySet = new TreeSet<>(); - try (JarFile jarFile = new JarFile(jarFileName)) { - Enumeration<JarEntry> entries = jarFile.entries(); + try (var jarFile = new JarFile(jarFileName)) { + Enumeration<JarEntry> entries = jarFile.entries(); // NOSONAR while (entries.hasMoreElements()) { /* diff --git a/utils/src/main/java/org/onap/policy/common/utils/resources/TextFileUtils.java b/utils/src/main/java/org/onap/policy/common/utils/resources/TextFileUtils.java index 2810c7be..7701eae9 100644 --- a/utils/src/main/java/org/onap/policy/common/utils/resources/TextFileUtils.java +++ b/utils/src/main/java/org/onap/policy/common/utils/resources/TextFileUtils.java @@ -1,7 +1,7 @@ /*- * ============LICENSE_START======================================================= * Copyright (C) 2019 Nordix Foundation. - * Modifications Copyright (C) 2020 AT&T Intellectual Property. All rights reserved. + * Modifications Copyright (C) 2020-2021 AT&T Intellectual Property. All rights reserved. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -28,6 +28,11 @@ import java.io.InputStreamReader; import java.io.Reader; import java.nio.charset.StandardCharsets; import java.nio.file.Files; +import lombok.AccessLevel; +import lombok.NoArgsConstructor; +import org.apache.commons.io.IOUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; /** * The Class TextFileUtils is class that provides useful functions for handling text files. Functions to read and write @@ -35,12 +40,9 @@ import java.nio.file.Files; * * @author Liam Fallon (liam.fallon@est.tech) */ -public abstract class TextFileUtils { - private static final int READER_CHAR_BUFFER_SIZE_4096 = 4096; - - private TextFileUtils() { - // This class cannot be initialized - } +@NoArgsConstructor(access = AccessLevel.PRIVATE) +public final class TextFileUtils { + private static final Logger logger = LoggerFactory.getLogger(TextFileUtils.class); /** * Method to return the contents of a text file as a string. @@ -50,7 +52,7 @@ public abstract class TextFileUtils { * @throws IOException on errors reading text from the file */ public static String getTextFileAsString(final String textFilePath) throws IOException { - final File textFile = new File(textFilePath); + final var textFile = new File(textFilePath); return Files.readString(textFile.toPath()); } @@ -62,7 +64,7 @@ public abstract class TextFileUtils { * @throws IOException on errors reading text from the file */ public static void putStringAsTextFile(final String outString, final String textFilePath) throws IOException { - final File textFile = new File(textFilePath); + final var textFile = new File(textFilePath); if (!textFile.getParentFile().exists()) { textFile.getParentFile().mkdirs(); } @@ -100,15 +102,37 @@ public abstract class TextFileUtils { * @throws IOException on errors reading text from the file */ public static String getReaderAsString(final Reader textReader) throws IOException { - final StringBuilder builder = new StringBuilder(); - int charsRead = -1; - final char[] chars = new char[READER_CHAR_BUFFER_SIZE_4096]; - do { - charsRead = textReader.read(chars); - if (charsRead > 0) { - builder.append(chars, 0, charsRead); - } - } while (charsRead > 0); - return builder.toString(); + return IOUtils.toString(textReader); + } + + /** + * Creates a temporary file, only accessible by the owner. + * + * @param prefix file name prefix + * @param suffix file name suffix + * @return a new, temporary file + * @throws IOException if an error occurs + */ + public static File createTempFile(String prefix, String suffix) throws IOException { + /* + * Disabling sonar, because setDefaultPermissions() will set the permissions of + * the file. + */ + var file = File.createTempFile(prefix, suffix); // NOSONAR + + setDefaultPermissions(file); + + return file; + } + + /** + * Sets permissions on a file or directory so that only the owner can access it. + * + * @param file file or directory on which permissions are to be set + */ + public static void setDefaultPermissions(File file) { + if (!file.setReadable(true, true) || !file.setWritable(true, true) || !file.setExecutable(true, true)) { + logger.warn("cannot set permissions for {}", file); + } } } diff --git a/utils/src/main/java/org/onap/policy/common/utils/security/CryptoUtils.java b/utils/src/main/java/org/onap/policy/common/utils/security/CryptoUtils.java index af5b3d49..a974f1e5 100644 --- a/utils/src/main/java/org/onap/policy/common/utils/security/CryptoUtils.java +++ b/utils/src/main/java/org/onap/policy/common/utils/security/CryptoUtils.java @@ -2,7 +2,8 @@ * ============LICENSE_START======================================================= * ONAP * ================================================================================ - * Copyright (C) 2019-2020 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2019-2021 AT&T Intellectual Property. All rights reserved. + * Modifications Copyright (C) 2023 Nordix Foundation. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -20,12 +21,12 @@ package org.onap.policy.common.utils.security; +import jakarta.xml.bind.DatatypeConverter; import java.nio.charset.StandardCharsets; import java.util.Random; import javax.crypto.Cipher; -import javax.crypto.spec.IvParameterSpec; +import javax.crypto.spec.GCMParameterSpec; import javax.crypto.spec.SecretKeySpec; -import javax.xml.bind.DatatypeConverter; import org.apache.commons.lang3.ArrayUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -44,7 +45,9 @@ public class CryptoUtils implements CryptoCoder { /** * Detailed definition of encryption algorithm. */ - private static final String ALGORITHM_DETAILS = ALGORITHM + "/CBC/PKCS5PADDING"; + private static final String ALGORITHM_DETAILS = ALGORITHM + "/GCM/NoPadding"; + + private static final int TAG_SIZE_IN_BITS = 128; private static final int IV_BLOCK_SIZE_IN_BITS = 128; @@ -69,7 +72,7 @@ public class CryptoUtils implements CryptoCoder { /** * CryptoUtils - encryption tool constructor. * @param secretKeySpec - * AES supports 128, 192 or 256-bit long key size, it can be plain text or generated with key generator + * AES supports 128, 192 or 256-bit long key size, it can be plain text or generated with key generator */ public CryptoUtils(SecretKeySpec secretKeySpec) { this.secretKeySpec = secretKeySpec; @@ -87,7 +90,7 @@ public class CryptoUtils implements CryptoCoder { * <p>Final result is to put in properties file is: IV + Outcome of openssl command * * @param value - * The plain text string + * The plain text string * @return The encrypted String */ @Override @@ -98,13 +101,13 @@ public class CryptoUtils implements CryptoCoder { /** * Encrypt a value based on the Policy Encryption Key. * @param value - * The plain text string + * The plain text string * @param secretKey - * The secret key + * The secret key * @return The encrypted String */ public static String encrypt(String value, String secretKey) { - SecretKeySpec keySpec = readSecretKeySpec(secretKey); + var keySpec = readSecretKeySpec(secretKey); return encryptValue(value, keySpec); } @@ -117,10 +120,10 @@ public class CryptoUtils implements CryptoCoder { return value; } try { - Cipher cipher = Cipher.getInstance(ALGORITHM_DETAILS); - byte[] iv = new byte[IV_BLOCK_SIZE_IN_BYTES]; + var cipher = Cipher.getInstance(ALGORITHM_DETAILS); + var iv = new byte[IV_BLOCK_SIZE_IN_BYTES]; RANDOM.nextBytes(iv); - IvParameterSpec ivspec = new IvParameterSpec(iv); + var ivspec = new GCMParameterSpec(TAG_SIZE_IN_BITS, iv); cipher.init(Cipher.ENCRYPT_MODE, keySpec, ivspec); return "enc:" + DatatypeConverter.printBase64Binary( @@ -137,7 +140,7 @@ public class CryptoUtils implements CryptoCoder { * -K PrivateHexKey -iv 16BytesIVFromEncryptedString * * @param value - * The encrypted string that must be decrypted using the Policy Encryption Key + * The encrypted string that must be decrypted using the Policy Encryption Key * @return The String decrypted if string begin with 'enc:' */ @Override @@ -149,13 +152,13 @@ public class CryptoUtils implements CryptoCoder { * Decrypt a value based on the Policy Encryption Key if string begin with 'enc:'. * * @param value - * The encrypted string that must be decrypted using the Policy Encryption Key + * The encrypted string that must be decrypted using the Policy Encryption Key * @param secretKey - * The secret key + * The secret key * @return The String decrypted if string begin with 'enc:' */ public static String decrypt(String value, String secretKey) { - SecretKeySpec keySpec = readSecretKeySpec(secretKey); + var keySpec = readSecretKeySpec(secretKey); if (keySpec != null) { return decryptValue(value, keySpec); } else { @@ -171,11 +174,11 @@ public class CryptoUtils implements CryptoCoder { throw new IllegalArgumentException("Invalid size on input value"); } try { - String pureValue = value.substring(4); + var pureValue = value.substring(4); byte[] encryptedValue = DatatypeConverter.parseBase64Binary(pureValue); - Cipher cipher = Cipher.getInstance(ALGORITHM_DETAILS); - IvParameterSpec ivspec = new IvParameterSpec( + var cipher = Cipher.getInstance(ALGORITHM_DETAILS); + var ivspec = new GCMParameterSpec(TAG_SIZE_IN_BITS, ArrayUtils.subarray(encryptedValue, 0, IV_BLOCK_SIZE_IN_BYTES)); byte[] realData = ArrayUtils.subarray(encryptedValue, IV_BLOCK_SIZE_IN_BYTES, encryptedValue.length); @@ -225,7 +228,7 @@ public class CryptoUtils implements CryptoCoder { * Check if string is encrypted by verify if string prefix with 'enc:'. * * @param value - * The encrypted string or plain text value + * The encrypted string or plain text value * @return boolean value indicate if string prefix with enc: or not */ public static boolean isEncrypted(String value) { diff --git a/utils/src/main/java/org/onap/policy/common/utils/services/FeatureApiUtils.java b/utils/src/main/java/org/onap/policy/common/utils/services/FeatureApiUtils.java index 332e0f67..042ee937 100644 --- a/utils/src/main/java/org/onap/policy/common/utils/services/FeatureApiUtils.java +++ b/utils/src/main/java/org/onap/policy/common/utils/services/FeatureApiUtils.java @@ -2,7 +2,7 @@ * ============LICENSE_START======================================================= * ONAP * ================================================================================ - * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2019, 2021 AT&T Intellectual Property. All rights reserved. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -23,15 +23,14 @@ package org.onap.policy.common.utils.services; import java.util.List; import java.util.function.BiConsumer; import java.util.function.Predicate; +import lombok.AccessLevel; +import lombok.NoArgsConstructor; /** * Utilities for use with "feature APIs". */ -public class FeatureApiUtils { - - private FeatureApiUtils() { - // do nothing - } +@NoArgsConstructor(access = AccessLevel.PRIVATE) +public final class FeatureApiUtils { /** * Applies a function on each feature provider, stopping as soon as one returns true. diff --git a/utils/src/main/java/org/onap/policy/common/utils/services/Registry.java b/utils/src/main/java/org/onap/policy/common/utils/services/Registry.java index c3eabe8e..8765ba8b 100644 --- a/utils/src/main/java/org/onap/policy/common/utils/services/Registry.java +++ b/utils/src/main/java/org/onap/policy/common/utils/services/Registry.java @@ -2,7 +2,7 @@ * ============LICENSE_START======================================================= * ONAP * ================================================================================ - * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2019, 2021 AT&T Intellectual Property. All rights reserved. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -22,6 +22,8 @@ package org.onap.policy.common.utils.services; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; +import lombok.AccessLevel; +import lombok.NoArgsConstructor; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -29,6 +31,7 @@ import org.slf4j.LoggerFactory; * This is a simple object registry, similar in spirit to JNDI, but suitable for use in a * stand-alone JVM. */ +@NoArgsConstructor(access = AccessLevel.PRIVATE) public class Registry { private static final Logger logger = LoggerFactory.getLogger(Registry.class); @@ -40,13 +43,6 @@ public class Registry { private Map<String, Object> name2object = new ConcurrentHashMap<>(); /** - * Constructs the object. - */ - private Registry() { - super(); - } - - /** * Registers an object. * * @param name name by which the object is known diff --git a/utils/src/main/java/org/onap/policy/common/utils/services/ServiceManager.java b/utils/src/main/java/org/onap/policy/common/utils/services/ServiceManager.java index 78fe853b..08994912 100644 --- a/utils/src/main/java/org/onap/policy/common/utils/services/ServiceManager.java +++ b/utils/src/main/java/org/onap/policy/common/utils/services/ServiceManager.java @@ -2,7 +2,7 @@ * ============LICENSE_START======================================================= * ONAP PAP * ================================================================================ - * Copyright (C) 2019-2020 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2019-2021 AT&T Intellectual Property. All rights reserved. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -23,6 +23,9 @@ package org.onap.policy.common.utils.services; import java.util.Deque; import java.util.Iterator; import java.util.LinkedList; +import java.util.concurrent.atomic.AtomicBoolean; +import lombok.AllArgsConstructor; +import lombok.Getter; import org.onap.policy.common.capabilities.Startable; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -37,6 +40,7 @@ public class ServiceManager implements Startable { /** * Manager name. */ + @Getter private final String name; /** @@ -47,7 +51,7 @@ public class ServiceManager implements Startable { /** * {@code True} if the services are currently running, {@code false} otherwise. */ - private boolean running; + private final AtomicBoolean running = new AtomicBoolean(false); /** * Constructs the object, with a default name. @@ -65,10 +69,6 @@ public class ServiceManager implements Startable { this.name = name; } - public String getName() { - return name; - } - /** * Adds a pair of service actions to the manager. * @@ -78,7 +78,7 @@ public class ServiceManager implements Startable { * @return this manager */ public synchronized ServiceManager addAction(String stepName, RunnableWithEx starter, RunnableWithEx stopper) { - if (running) { + if (isAlive()) { throw new IllegalStateException(name + " is already running; cannot add " + stepName); } @@ -95,7 +95,7 @@ public class ServiceManager implements Startable { * @return this manager */ public synchronized ServiceManager addService(String stepName, Startable service) { - if (running) { + if (isAlive()) { throw new IllegalStateException(name + " is already running; cannot add " + stepName); } @@ -104,13 +104,13 @@ public class ServiceManager implements Startable { } @Override - public synchronized boolean isAlive() { - return running; + public boolean isAlive() { + return running.get(); } @Override public synchronized boolean start() { - if (running) { + if (isAlive()) { throw new IllegalStateException(name + " is already running"); } @@ -135,7 +135,7 @@ public class ServiceManager implements Startable { if (ex == null) { logger.info("{} started", name); - running = true; + running.set(true); return true; } @@ -152,11 +152,11 @@ public class ServiceManager implements Startable { @Override public synchronized boolean stop() { - if (!running) { + if (!isAlive()) { throw new IllegalStateException(name + " is not running"); } - running = false; + running.set(false); rewind(items); return true; @@ -204,21 +204,16 @@ public class ServiceManager implements Startable { /** * Service information. */ + @AllArgsConstructor private static class Service { private String stepName; private RunnableWithEx starter; private RunnableWithEx stopper; - - public Service(String stepName, RunnableWithEx starter, RunnableWithEx stopper) { - this.stepName = stepName; - this.starter = starter; - this.stopper = stopper; - } } /* * Cannot use a plain Runnable, because it can't throw exceptions. Could use a - * Callable, instead, but then all of the lambda expressions become rather messy, thus + * Callable, instead, but then all the lambda expressions become rather messy, thus * we'll stick with RunnableWithEx, and just disable the sonar warning. */ @FunctionalInterface diff --git a/utils/src/main/java/org/onap/policy/common/utils/time/CurrentTime.java b/utils/src/main/java/org/onap/policy/common/utils/time/CurrentTime.java index 857b69b6..95f4d69a 100644 --- a/utils/src/main/java/org/onap/policy/common/utils/time/CurrentTime.java +++ b/utils/src/main/java/org/onap/policy/common/utils/time/CurrentTime.java @@ -2,14 +2,14 @@ * ============LICENSE_START======================================================= * Common Utils * ================================================================================ - * Copyright (C) 2018 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2018, 2021 AT&T Intellectual Property. All rights reserved. * ================================================================================ * 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. @@ -21,24 +21,18 @@ package org.onap.policy.common.utils.time; import java.util.Date; +import lombok.NoArgsConstructor; /** * Methods to access the current time. Classes can use objects of this type to get current * time information, while allowing the objects to be overridden by junit tests. */ +@NoArgsConstructor public class CurrentTime { /** - * Constructor. - * - */ - public CurrentTime() { - super(); - } - - /** * Get the millisecond time. - * + * * @return the current time, in milliseconds */ public long getMillis() { @@ -47,7 +41,7 @@ public class CurrentTime { /** * Get the current date. - * + * * @return the current Date */ public Date getDate() { @@ -56,7 +50,7 @@ public class CurrentTime { /** * Sleeps for a period of time. - * + * * @param sleepMs amount of time to sleep, in milliseconds * @throws InterruptedException can be interrupted */ diff --git a/utils/src/main/java/org/onap/policy/common/utils/validation/Assertions.java b/utils/src/main/java/org/onap/policy/common/utils/validation/Assertions.java index 047989e7..8e474204 100644 --- a/utils/src/main/java/org/onap/policy/common/utils/validation/Assertions.java +++ b/utils/src/main/java/org/onap/policy/common/utils/validation/Assertions.java @@ -2,7 +2,7 @@ * ============LICENSE_START======================================================= * Copyright (C) 2016-2018 Ericsson. All rights reserved. * Modifications Copyright (C) 2019 Nordix Foundation. - * Modifications Copyright (C) 2019 AT&T Intellectual Property. All rights reserved. + * Modifications Copyright (C) 2019, 2021 AT&T Intellectual Property. All rights reserved. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -22,6 +22,8 @@ package org.onap.policy.common.utils.validation; +import lombok.AccessLevel; +import lombok.NoArgsConstructor; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -29,17 +31,12 @@ import org.slf4j.LoggerFactory; * The Class Assertions is a static class that is used as a shorthand for assertions in the source code. * It throws runtime exceptions on assertion fails. */ +@NoArgsConstructor(access = AccessLevel.PRIVATE) public final class Assertions { // Logger for this class private static final Logger LOGGER = LoggerFactory.getLogger(Assertions.class); /** - * Private constructor used to prevent sub class instantiation. - */ - private Assertions() { - } - - /** * Gets the validation message for a string parameter. * * @param parameterName the string parameter name diff --git a/utils/src/main/java/org/onap/policy/common/utils/validation/ParameterValidationUtils.java b/utils/src/main/java/org/onap/policy/common/utils/validation/ParameterValidationUtils.java index f15d936b..0723242d 100644 --- a/utils/src/main/java/org/onap/policy/common/utils/validation/ParameterValidationUtils.java +++ b/utils/src/main/java/org/onap/policy/common/utils/validation/ParameterValidationUtils.java @@ -1,7 +1,7 @@ /*- * ============LICENSE_START======================================================= * Copyright (C) 2018 Ericsson. All rights reserved. - * Modifications Copyright (C) 2019 AT&T Intellectual Property. All rights reserved. + * Modifications Copyright (C) 2019, 2021 AT&T Intellectual Property. All rights reserved. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -21,17 +21,17 @@ package org.onap.policy.common.utils.validation; +import lombok.AccessLevel; +import lombok.NoArgsConstructor; + /** * Class to provide utility methods for common parameter validations. * * @author Ram Krishna Verma (ram.krishna.verma@ericsson.com) */ +@NoArgsConstructor(access = AccessLevel.PRIVATE) public class ParameterValidationUtils { - private ParameterValidationUtils() { - - } - /** * Validates the given string input. * diff --git a/utils/src/main/java/org/onap/policy/common/utils/validation/Version.java b/utils/src/main/java/org/onap/policy/common/utils/validation/Version.java index 527113dd..46e006bd 100644 --- a/utils/src/main/java/org/onap/policy/common/utils/validation/Version.java +++ b/utils/src/main/java/org/onap/policy/common/utils/validation/Version.java @@ -2,7 +2,7 @@ * ============LICENSE_START======================================================= * ONAP COMMON * ================================================================================ - * Copyright (C) 2019-2020 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2019-2021 AT&T Intellectual Property. All rights reserved. * Modifications Copyright (C) 2019 Nordix Foundation. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); @@ -21,7 +21,6 @@ package org.onap.policy.common.utils.validation; -import com.google.re2j.Matcher; import com.google.re2j.Pattern; import lombok.Data; import lombok.NoArgsConstructor; @@ -56,7 +55,7 @@ public class Version implements Comparable<Version> { * @param versionString the version string */ public Version(@NonNull final String versionString) { - Version newVersion = makeVersion("String", "constructor", versionString); + var newVersion = makeVersion("String", "constructor", versionString); if (newVersion != null) { this.major = newVersion.major; @@ -79,7 +78,7 @@ public class Version implements Comparable<Version> { * that does not match the major.minor.patch form) */ public static Version makeVersion(String type, String name, String versionText) { - Matcher matcher = VERSION_PAT.matcher(versionText); + var matcher = VERSION_PAT.matcher(versionText); if (!matcher.matches()) { logger.info("invalid version for {} {}: {}", type, name, versionText); return null; @@ -113,7 +112,7 @@ public class Version implements Comparable<Version> { @Override public int compareTo(Version other) { - int result = Integer.compare(major, other.major); + var result = Integer.compare(major, other.major); if (result != 0) { return result; } |