diff options
Diffstat (limited to 'site-manager/src/main/java/org/onap/policy/common/sitemanager/utils')
7 files changed, 577 insertions, 0 deletions
diff --git a/site-manager/src/main/java/org/onap/policy/common/sitemanager/utils/CommandLineHelper.java b/site-manager/src/main/java/org/onap/policy/common/sitemanager/utils/CommandLineHelper.java new file mode 100644 index 00000000..58db7bc8 --- /dev/null +++ b/site-manager/src/main/java/org/onap/policy/common/sitemanager/utils/CommandLineHelper.java @@ -0,0 +1,116 @@ +/*- + * ============LICENSE_START======================================================= + * site-manager + * ================================================================================ + * Copyright (C) 2018 Ericsson. 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.sitemanager.utils; + +import java.util.List; +import org.apache.commons.cli.CommandLine; +import org.apache.commons.cli.CommandLineParser; +import org.apache.commons.cli.DefaultParser; +import org.apache.commons.cli.Option; +import org.apache.commons.cli.Options; +import org.apache.commons.cli.ParseException; +import org.onap.policy.common.sitemanager.exception.IllegalCommandLineArgumentException; + +public class CommandLineHelper { + + private static final String HELP_QUESTION_MARK_ARGUMENT_NAME = "?"; + + private static final String HELP_ARGUMENT_NAME = "h"; + + private static final String RESOURCE_ARGUMENT_NAME = "r"; + + private static final String SITE_ARGUMENT_NAME = "s"; + + private final CommandLine commandLine; + + private static final CommandLineParser PARSER = new DefaultParser(); + + private final Printable printable; + + public CommandLineHelper(final String[] args, final Printable printable) { + this.commandLine = getCommandLine(getOptions(), args); + this.printable = printable; + } + + private Options getOptions() { + final Options options = new Options(); + options.addOption(Option.builder(SITE_ARGUMENT_NAME).hasArg(true).desc("specify site").build()); + options.addOption(Option.builder(RESOURCE_ARGUMENT_NAME).hasArg(true).desc("specify resource name").build()); + options.addOption(Option.builder(HELP_ARGUMENT_NAME).hasArg(false).desc("display help").build()); + options.addOption(Option.builder(HELP_QUESTION_MARK_ARGUMENT_NAME).hasArg(false).desc("display help").build()); + + return options; + } + + public String getSite() { + return commandLine.getOptionValue(SITE_ARGUMENT_NAME); + } + + public String getResourceName() { + return commandLine.getOptionValue(RESOURCE_ARGUMENT_NAME); + } + + private CommandLine getCommandLine(final Options options, final String[] args) { + try { + return PARSER.parse(options, args); + } catch (final ParseException | NullPointerException exception) { + throw new IllegalCommandLineArgumentException(exception.getMessage(), exception); + } + } + + public boolean isValid() { + // fetch options, and remaining arguments + final String sOption = commandLine.getOptionValue(SITE_ARGUMENT_NAME); + final String rOption = commandLine.getOptionValue(RESOURCE_ARGUMENT_NAME); + final List<String> argList = commandLine.getArgList(); + + // a number of commands require either the '-r' option or '-s' option + final boolean optionLetterSpecified = (rOption != null || sOption != null); + + if (argList.isEmpty()) { + printable.println(ErrorMessages.NO_COMMAND_SPECIFIED); + return false; + } + // a number of commands require either the '-r' option or '-s' option + final String arg0 = argList.get(0); + final ExtraCommandLineArgument argument = ExtraCommandLineArgument.getExtraCommandLineArgument(arg0); + + if (!argument.isValid(argList, printable, optionLetterSpecified)) { + return false; + } + + if (sOption != null && rOption != null) { + printable.println(arg0 + ErrorMessages.R_AND_S_OPTIONS_ARE_MUTUALLY_EXCLUSIVE); + return false; + } + + return true; + } + + public boolean isHelpArgumentSet() { + return commandLine.hasOption(HELP_ARGUMENT_NAME) || commandLine.hasOption(HELP_QUESTION_MARK_ARGUMENT_NAME); + } + + public List<String> getArgList() { + return commandLine.getArgList(); + } + +} diff --git a/site-manager/src/main/java/org/onap/policy/common/sitemanager/utils/Constants.java b/site-manager/src/main/java/org/onap/policy/common/sitemanager/utils/Constants.java new file mode 100644 index 00000000..0f2d9a49 --- /dev/null +++ b/site-manager/src/main/java/org/onap/policy/common/sitemanager/utils/Constants.java @@ -0,0 +1,54 @@ +/*- + * ============LICENSE_START======================================================= + * site-manager + * ================================================================================ + * Copyright (C) 2018 Ericsson. 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.sitemanager.utils; + +public final class Constants { + + public static final String SITE_NAME = "site"; + + public static final String WHERE_R_SITE_NAME = " WHERE r.site = :"; + + public static final String WHERE_R_RESOURCE_NAME = " WHERE r.resourceName = :"; + + public static final String RESOURCE_NAME = "resourceName"; + + public static final String WHERE_S_RESOURCE_NAME = " WHERE s.resourceName = :"; + + public static final String RESOURCE_REGISTRATION_QUERY = "SELECT r FROM ResourceRegistrationEntity r"; + + public static final String STATE_MANAGEMENT_QUERY = "SELECT s FROM StateManagementEntity s"; + + public static final String JDBC_PASSWORD_PROPERTY_NAME = "javax.persistence.jdbc.password"; + + public static final String JDBC_USER_PROPERTY_NAME = "javax.persistence.jdbc.user"; + + public static final String JDBC_URL_PROPERTY_NAME = "javax.persistence.jdbc.url"; + + public static final String JDBC_DRIVER_PROPERTY_NAME = "javax.persistence.jdbc.driver"; + + public static final String OPERATIONAL_PERSISTENCE_UNIT = "operationalPU"; + + public static final String SITE_MANAGER_PROPERTIES_PROPERTY_NAME = "siteManager.properties"; + + private Constants() { + super(); + } +} diff --git a/site-manager/src/main/java/org/onap/policy/common/sitemanager/utils/ErrorMessages.java b/site-manager/src/main/java/org/onap/policy/common/sitemanager/utils/ErrorMessages.java new file mode 100644 index 00000000..81145462 --- /dev/null +++ b/site-manager/src/main/java/org/onap/policy/common/sitemanager/utils/ErrorMessages.java @@ -0,0 +1,76 @@ +/*- + * ============LICENSE_START======================================================= + * site-manager + * ================================================================================ + * Copyright (C) 2018 Ericsson. 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.sitemanager.utils; + +public class ErrorMessages { + + public static final String EXTRA_ARGUMENTS = "Extra arguments"; + + public static final String EITHER_S_OR_R_OPTION_IS_NEEDED = "Either '-s' or '-r' option is needed"; + + public static final String R_AND_S_OPTIONS_ARE_MUTUALLY_EXCLUSIVE = ": 'r' and 's' options are mutually exclusive"; + + public static final String UNKNOWN_COMMAND = ": Unknown command"; + + public static final String UNLOCK_EITHER_S_OR_R_OPTION_IS_NEEDED = "unlock: " + EITHER_S_OR_R_OPTION_IS_NEEDED; + + public static final String UNLOCK_EXTRA_ARGUMENTS = "unlock: " + EXTRA_ARGUMENTS; + + public static final String LOCK_EITHER_S_OR_R_OPTION_IS_NEEDED = "lock: " + EITHER_S_OR_R_OPTION_IS_NEEDED; + + public static final String LOCK_EXTRA_ARGUMENTS = "lock: " + EXTRA_ARGUMENTS; + + public static final String SET_ADMIN_STATE_EITHER_S_OR_R_OPTION_IS_NEEDED = + "setAdminState: " + EITHER_S_OR_R_OPTION_IS_NEEDED; + + public static final String SET_ADMIN_STATE_EXTRA_ARGUMENTS = "setAdminState: " + EXTRA_ARGUMENTS; + + public static final String SET_ADMIN_STATE_MISSING_NEW_STATE_VALUE = "setAdminState: Missing <new-state> value"; + + public static final String SHOW_EXTRA_ARGUMENTS = "show: " + EXTRA_ARGUMENTS; + + public static final String NO_COMMAND_SPECIFIED = "No command specified"; + + public static final String HELP_STRING = "Usage:\n" + " siteManager show [ -s <site> | -r <resourceName> ] :\n" + + " display node information\n" + " siteManager setAdminState { -s <site> | -r <resourceName> }" + + " <new-state> :\n" + " update admin state on selected nodes\n" + + " siteManager lock { -s <site> | -r <resourceName> } :\n" + " lock selected nodes\n" + + " siteManager unlock { -s <site> | -r <resourceName> } :\n" + " unlock selected nodes\n"; + + public static final String SITE_MANAGER_PROPERY_FILE_MISSING_PROPERTY = + "The following properties need to be specified:\n\n" + " javax.persistence.jdbc.driver -" + + " typically 'org.mariadb.jdbc.Driver'\n" + + " javax.persistence.jdbc.url - URL referring to the database,\n" + + " which typically has the form:" + " 'jdbc:mariadb://<host>:<port>/<db>'\n" + + " ('<db>' is probably 'xacml' in this case)\n" + + " javax.persistence.jdbc.user - the user id for accessing the" + " database\n" + + " javax.persistence.jdbc.password - password for accessing the" + " database\n"; + + public static final String SITE_MANAGER_PROPERY_FILE_NOT_DEFINED_MESSAGE = + "'siteManager' needs to be passed the system property\n" + + "'siteManager.properties', which is the file name of a\n" + + "properties file containing database access information\n\n"; + + private ErrorMessages() { + super(); + } + +} diff --git a/site-manager/src/main/java/org/onap/policy/common/sitemanager/utils/ExtraCommandLineArgument.java b/site-manager/src/main/java/org/onap/policy/common/sitemanager/utils/ExtraCommandLineArgument.java new file mode 100644 index 00000000..a28deece --- /dev/null +++ b/site-manager/src/main/java/org/onap/policy/common/sitemanager/utils/ExtraCommandLineArgument.java @@ -0,0 +1,138 @@ +/*- + * ============LICENSE_START======================================================= + * site-manager + * ================================================================================ + * Copyright (C) 2018 Ericsson. 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.sitemanager.utils; + +import java.util.List; + +public enum ExtraCommandLineArgument { + SHOW("show") { + + @Override + public boolean isValid(final List<String> argList, final Printable printable) { + if (argList.size() != 1) { + printable.println(ErrorMessages.SHOW_EXTRA_ARGUMENTS); + return false; + } + return true; + } + + @Override + public boolean isValid(final List<String> argList, final Printable printable, + final boolean optionLetterSpecified) { + return isValid(argList, printable); + } + }, + SET_ADMIN_STATE("setAdminState") { + @Override + public boolean isValid(final List<String> argList, final Printable printable, + final boolean optionLetterSpecified) { + boolean isValid = true; + switch (argList.size()) { + case 1: + printable.println(ErrorMessages.SET_ADMIN_STATE_MISSING_NEW_STATE_VALUE); + isValid = false; + break; + case 2: + isValid = true; + break; + default: + printable.println(ErrorMessages.SET_ADMIN_STATE_EXTRA_ARGUMENTS); + isValid = false; + } + if (!optionLetterSpecified) { + printable.println(ErrorMessages.SET_ADMIN_STATE_EITHER_S_OR_R_OPTION_IS_NEEDED); + return false; + } + + return isValid; + } + + }, + LOCK("lock") { + + @Override + public boolean isValid(final List<String> argList, final Printable printable, + final boolean optionLetterSpecified) { + boolean isValid = true; + if (argList.size() != 1) { + printable.println(ErrorMessages.LOCK_EXTRA_ARGUMENTS); + isValid = false; + } + if (!optionLetterSpecified) { + printable.println(ErrorMessages.LOCK_EITHER_S_OR_R_OPTION_IS_NEEDED); + isValid = false; + } + return isValid; + } + }, + UNLOCK("unlock") { + + @Override + public boolean isValid(final List<String> argList, final Printable printable, + final boolean optionLetterSpecified) { + boolean isValid = true; + if (argList.size() != 1) { + printable.println(ErrorMessages.UNLOCK_EXTRA_ARGUMENTS); + isValid = false; + } + if (!optionLetterSpecified) { + printable.println(ErrorMessages.UNLOCK_EITHER_S_OR_R_OPTION_IS_NEEDED); + isValid = false; + } + return isValid; + } + }, + INVALID("") { + @Override + public boolean isValid(final List<String> argList, final Printable printable, + final boolean optionLetterSpecified) { + printable.println(argList.get(0) + ErrorMessages.UNKNOWN_COMMAND); + return false; + } + }; + + private final String value; + + private ExtraCommandLineArgument(final String value) { + this.value = value; + } + + public String getValue() { + return value; + } + + public static ExtraCommandLineArgument getExtraCommandLineArgument(final String value) { + for (final ExtraCommandLineArgument argument : ExtraCommandLineArgument.values()) { + if (argument.getValue().equals(value)) { + return argument; + } + } + return ExtraCommandLineArgument.INVALID; + } + + public boolean isValid(final List<String> argList, final Printable printable) { + return isValid(argList, printable, false); + } + + public abstract boolean isValid(final List<String> argList, final Printable printable, + final boolean optionLetterSpecified); + +} diff --git a/site-manager/src/main/java/org/onap/policy/common/sitemanager/utils/JmxOpProcessor.java b/site-manager/src/main/java/org/onap/policy/common/sitemanager/utils/JmxOpProcessor.java new file mode 100644 index 00000000..2e354d3b --- /dev/null +++ b/site-manager/src/main/java/org/onap/policy/common/sitemanager/utils/JmxOpProcessor.java @@ -0,0 +1,71 @@ +/*- + * ============LICENSE_START======================================================= + * site-manager + * ================================================================================ + * Copyright (C) 2018 Ericsson. 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.sitemanager.utils; + +import java.io.IOException; +import javax.management.JMX; +import javax.management.MalformedObjectNameException; +import javax.management.ObjectName; +import javax.management.remote.JMXConnector; +import javax.management.remote.JMXConnectorFactory; +import javax.management.remote.JMXServiceURL; +import org.onap.policy.common.im.IntegrityMonitorException; +import org.onap.policy.common.im.jmx.ComponentAdminMBean; +import org.onap.policy.common.im.jpa.ResourceRegistrationEntity; + +public class JmxOpProcessor { + + private JmxOpProcessor() { + super(); + } + + /** + * Process a 'lock' or 'unlock' operation on a single 'ResourceRegistrationEntity' + * + * @param action this is the string "lock" or "unlock" + * @param resourceRegistrationEntity this is the ResourceRegistrationEntity to lock or unlock + */ + public static void jmxOp(final String action, final ResourceRegistrationEntity resourceRegistrationEntity, + final Printable printable) { + final String resourceName = resourceRegistrationEntity.getResourceName(); + final String jmxUrl = resourceRegistrationEntity.getResourceUrl(); + if (jmxUrl == null) { + printable.println(action + ": no resource URL for '" + resourceName + "'"); + return; + } + + try (final JMXConnector connector = JMXConnectorFactory.connect(new JMXServiceURL(jmxUrl))) { + final ComponentAdminMBean admin = JMX.newMXBeanProxy(connector.getMBeanServerConnection(), + new ObjectName("ONAP_POLICY_COMP:name=" + resourceName), ComponentAdminMBean.class); + + if ("lock".equals(action)) { + admin.lock(); + } else if ("unlock".equals(action)) { + admin.unlock(); + } else { + printable.println("Unknown action: " + action); + } + } catch (final IOException | MalformedObjectNameException | IntegrityMonitorException exception) { + printable.println(action + " failed for '" + resourceName + "': " + exception); + } + } + +} diff --git a/site-manager/src/main/java/org/onap/policy/common/sitemanager/utils/PersistenceUnitPropertiesProvider.java b/site-manager/src/main/java/org/onap/policy/common/sitemanager/utils/PersistenceUnitPropertiesProvider.java new file mode 100644 index 00000000..5d7abe7d --- /dev/null +++ b/site-manager/src/main/java/org/onap/policy/common/sitemanager/utils/PersistenceUnitPropertiesProvider.java @@ -0,0 +1,94 @@ +/*- + * ============LICENSE_START======================================================= + * site-manager + * ================================================================================ + * Copyright (C) 2018 Ericsson. 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.sitemanager.utils; + +import static org.onap.policy.common.sitemanager.utils.Constants.JDBC_DRIVER_PROPERTY_NAME; +import static org.onap.policy.common.sitemanager.utils.Constants.JDBC_PASSWORD_PROPERTY_NAME; +import static org.onap.policy.common.sitemanager.utils.Constants.JDBC_URL_PROPERTY_NAME; +import static org.onap.policy.common.sitemanager.utils.Constants.JDBC_USER_PROPERTY_NAME; +import static org.onap.policy.common.sitemanager.utils.ErrorMessages.SITE_MANAGER_PROPERY_FILE_MISSING_PROPERTY; +import static org.onap.policy.common.sitemanager.utils.ErrorMessages.SITE_MANAGER_PROPERY_FILE_NOT_DEFINED_MESSAGE; +import java.io.BufferedReader; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.Properties; +import org.onap.policy.common.sitemanager.exception.MissingPropertyException; +import org.onap.policy.common.sitemanager.exception.PropertyFileProcessingException; + +public class PersistenceUnitPropertiesProvider { + + private PersistenceUnitPropertiesProvider() { + super(); + } + + public static Properties getProperties(final String propertiesFileName, final Printable printable) { + if (propertiesFileName == null) { + printable.println(SITE_MANAGER_PROPERY_FILE_NOT_DEFINED_MESSAGE); + printable.println(SITE_MANAGER_PROPERY_FILE_MISSING_PROPERTY); + throw new PropertyFileProcessingException("Property file name is null :" + propertiesFileName); + } + + final Path filePath = Paths.get(propertiesFileName); + final Properties properties = getProperties(filePath, printable); + + // verify that we have all of the properties needed + if (isNotValid(properties, JDBC_DRIVER_PROPERTY_NAME, JDBC_URL_PROPERTY_NAME, JDBC_USER_PROPERTY_NAME, + JDBC_PASSWORD_PROPERTY_NAME)) { + // one or more missing properties + printable.println(SITE_MANAGER_PROPERY_FILE_MISSING_PROPERTY); + throw new MissingPropertyException("missing mandatory attributes"); + } + return properties; + } + + private static boolean isNotValid(final Properties properties, final String... values) { + if (values == null || values.length == 0) { + return true; + } + for (final String val : values) { + if (properties.get(val) == null) { + return true; + } + } + return false; + } + + private static Properties getProperties(final Path filePath, final Printable printable) { + if (!filePath.toFile().exists()) { + printable.println(SITE_MANAGER_PROPERY_FILE_NOT_DEFINED_MESSAGE); + printable.println(SITE_MANAGER_PROPERY_FILE_MISSING_PROPERTY); + throw new PropertyFileProcessingException("Property file not found"); + } + + try (final BufferedReader bufferedReader = Files.newBufferedReader(filePath);) { + final Properties properties = new Properties(); + properties.load(bufferedReader); + return properties; + } catch (final IOException exception) { + printable.println("Exception loading properties: " + exception); + printable.println(ErrorMessages.SITE_MANAGER_PROPERY_FILE_NOT_DEFINED_MESSAGE); + printable.println(ErrorMessages.SITE_MANAGER_PROPERY_FILE_MISSING_PROPERTY); + throw new PropertyFileProcessingException("Exception loading properties: ", exception); + } + } +} diff --git a/site-manager/src/main/java/org/onap/policy/common/sitemanager/utils/Printable.java b/site-manager/src/main/java/org/onap/policy/common/sitemanager/utils/Printable.java new file mode 100644 index 00000000..757268a7 --- /dev/null +++ b/site-manager/src/main/java/org/onap/policy/common/sitemanager/utils/Printable.java @@ -0,0 +1,28 @@ +/*- + * ============LICENSE_START======================================================= + * site-manager + * ================================================================================ + * Copyright (C) 2018 Ericsson. 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.sitemanager.utils; + +@FunctionalInterface +public interface Printable { + + void println(final String value); + +} |