diff options
Diffstat (limited to 'main/src')
5 files changed, 135 insertions, 41 deletions
diff --git a/main/src/main/java/org/onap/policy/pap/main/startstop/Main.java b/main/src/main/java/org/onap/policy/pap/main/startstop/Main.java index a351e6f7..b8cd71e1 100644 --- a/main/src/main/java/org/onap/policy/pap/main/startstop/Main.java +++ b/main/src/main/java/org/onap/policy/pap/main/startstop/Main.java @@ -26,7 +26,6 @@ import java.util.Arrays; import org.onap.policy.common.utils.resources.MessageConstants; import org.onap.policy.common.utils.services.Registry; import org.onap.policy.pap.main.PapConstants; -import org.onap.policy.pap.main.PolicyPapException; import org.onap.policy.pap.main.PolicyPapRuntimeException; import org.onap.policy.pap.main.parameters.PapParameterGroup; import org.onap.policy.pap.main.parameters.PapParameterHandler; @@ -51,11 +50,11 @@ public class Main { * @param args the command line arguments */ public Main(final String[] args) { - final String argumentString = Arrays.toString(args); + final var argumentString = Arrays.toString(args); LOGGER.info("Starting policy pap service with arguments - {}", argumentString); // Check the arguments - final PapCommandLineArguments arguments = new PapCommandLineArguments(); + final var arguments = new PapCommandLineArguments(); try { // The arguments return a string if there is a message to print and we should exit final String argumentMessage = arguments.parse(args); @@ -70,7 +69,8 @@ public class Main { parameterGroup = new PapParameterHandler().getParameters(arguments); // Initialize database - new PapDatabaseInitializer().initializePapDatabase(parameterGroup.getDatabaseProviderParameters()); + new PapDatabaseInitializer().initializePapDatabase( + parameterGroup.getDatabaseProviderParameters(), arguments.getGroupFilePath()); // Now, create the activator for the policy pap service activator = new PapActivator(parameterGroup); @@ -94,7 +94,7 @@ public class Main { // Add a shutdown hook to shut everything down in an orderly manner Runtime.getRuntime().addShutdownHook(new PolicyPapShutdownHookClass()); - String successMsg = String.format(MessageConstants.START_SUCCESS_MSG, MessageConstants.POLICY_PAP); + var successMsg = String.format(MessageConstants.START_SUCCESS_MSG, MessageConstants.POLICY_PAP); LOGGER.info(successMsg); } @@ -110,9 +110,8 @@ public class Main { /** * Shut down Execution. * - * @throws PolicyPapException on shutdown errors */ - public void shutdown() throws PolicyPapException { + public void shutdown() { // clear the parameterGroup variable parameterGroup = null; @@ -141,7 +140,7 @@ public class Main { // Shutdown the policy pap service and wait for everything to stop activator.stop(); } catch (final RuntimeException e) { - LOGGER.warn("error occured during shut down of the policy pap service", e); + LOGGER.warn("error occurred during shut down of the policy pap service", e); } } } diff --git a/main/src/main/java/org/onap/policy/pap/main/startstop/PapCommandLineArguments.java b/main/src/main/java/org/onap/policy/pap/main/startstop/PapCommandLineArguments.java index 78c2cc70..6d9c2f03 100644 --- a/main/src/main/java/org/onap/policy/pap/main/startstop/PapCommandLineArguments.java +++ b/main/src/main/java/org/onap/policy/pap/main/startstop/PapCommandLineArguments.java @@ -1,7 +1,7 @@ /*- * ============LICENSE_START======================================================= * Copyright (C) 2019 Nordix Foundation. - * Modifications Copyright (C) 2019 AT&T Intellectual Property. + * Modifications Copyright (C) 2019, 2021 AT&T Intellectual Property. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -24,7 +24,6 @@ package org.onap.policy.pap.main.startstop; import java.io.File; import java.io.PrintWriter; import java.io.StringWriter; -import java.net.URL; import java.util.Arrays; import org.apache.commons.cli.CommandLine; import org.apache.commons.cli.DefaultParser; @@ -49,6 +48,7 @@ public class PapCommandLineArguments { private final Options options; private String configurationFilePath = null; private String propertyFilePath = null; + private String groupFilePath = null; /** * Construct the options for the CLI editor. @@ -86,6 +86,15 @@ public class PapCommandLineArguments { .required(false) .type(String.class) .build()); + options.addOption(Option.builder("g") + .longOpt("groups-file") + .desc("the full path to the groups file to use, " + + "the groups file contains the group configuration") + .hasArg() + .argName("GROUP_FILE") + .required(false) + .type(String.class) + .build()); //@formatter:on } @@ -117,8 +126,9 @@ public class PapCommandLineArguments { // Clear all our arguments setConfigurationFilePath(null); setPropertyFilePath(null); + setGroupFilePath(null); - CommandLine commandLine = null; + CommandLine commandLine; try { commandLine = new DefaultParser().parse(options, args); } catch (final ParseException e) { @@ -132,10 +142,6 @@ public class PapCommandLineArguments { throw new PolicyPapException("too many command line arguments specified : " + Arrays.toString(args)); } - if (remainingArgs.length == 1) { - configurationFilePath = remainingArgs[0]; - } - if (commandLine.hasOption('h')) { return help(Main.class.getName()); } @@ -152,6 +158,10 @@ public class PapCommandLineArguments { setPropertyFilePath(commandLine.getOptionValue('p')); } + if (commandLine.hasOption('g')) { + setGroupFilePath(commandLine.getOptionValue('g')); + } + return null; } @@ -162,6 +172,9 @@ public class PapCommandLineArguments { */ public void validate() throws PolicyPapException { validateReadableFile("policy pap configuration", configurationFilePath); + if (groupFilePath != null) { + validateReadableFile("policy pap groups", groupFilePath); + } } /** @@ -180,9 +193,9 @@ public class PapCommandLineArguments { * @return the help string */ public String help(final String mainClassName) { - final HelpFormatter helpFormatter = new HelpFormatter(); - final StringWriter stringWriter = new StringWriter(); - final PrintWriter printWriter = new PrintWriter(stringWriter); + final var helpFormatter = new HelpFormatter(); + final var stringWriter = new StringWriter(); + final var printWriter = new PrintWriter(stringWriter); helpFormatter.printHelp(printWriter, HELP_LINE_LENGTH, mainClassName + " [options...]", "options", options, 0, 0, ""); @@ -265,6 +278,24 @@ public class PapCommandLineArguments { } /** + * Gets the group file path or the default file group resource if not present. + * + * @return the group file path + */ + public String getGroupFilePath() { + return groupFilePath; + } + + /** + * Sets the group file path. + * + * @param groupFilePath the property file path + */ + public void setGroupFilePath(final String groupFilePath) { + this.groupFilePath = groupFilePath; + } + + /** * Validate readable file. * * @param fileTag the file tag @@ -277,12 +308,12 @@ public class PapCommandLineArguments { } // The file name refers to a resource on the local file system - final URL fileUrl = ResourceUtils.getUrl4Resource(fileName); + final var fileUrl = ResourceUtils.getUrl4Resource(fileName); if (fileUrl == null) { throw new PolicyPapException(fileTag + FILE_MESSAGE_PREAMBLE + fileName + "\" does not exist"); } - final File theFile = new File(fileUrl.getPath()); + final var theFile = new File(fileUrl.getPath()); if (!theFile.exists()) { throw new PolicyPapException(fileTag + FILE_MESSAGE_PREAMBLE + fileName + "\" does not exist"); } @@ -290,7 +321,7 @@ public class PapCommandLineArguments { throw new PolicyPapException(fileTag + FILE_MESSAGE_PREAMBLE + fileName + "\" is not a normal file"); } if (!theFile.canRead()) { - throw new PolicyPapException(fileTag + FILE_MESSAGE_PREAMBLE + fileName + "\" is ureadable"); + throw new PolicyPapException(fileTag + FILE_MESSAGE_PREAMBLE + fileName + "\" is unreadable"); } } } diff --git a/main/src/main/java/org/onap/policy/pap/main/startstop/PapDatabaseInitializer.java b/main/src/main/java/org/onap/policy/pap/main/startstop/PapDatabaseInitializer.java index affe1e9f..2d9fd47b 100644 --- a/main/src/main/java/org/onap/policy/pap/main/startstop/PapDatabaseInitializer.java +++ b/main/src/main/java/org/onap/policy/pap/main/startstop/PapDatabaseInitializer.java @@ -1,7 +1,7 @@ /*- * ============LICENSE_START======================================================= * Copyright (C) 2019 Nordix Foundation. - * Modifications Copyright (C) 2019 AT&T Intellectual Property. + * Modifications Copyright (C) 2019, 2021 AT&T Intellectual Property. * ================================================================================ * 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,7 @@ package org.onap.policy.pap.main.startstop; import java.util.List; +import java.util.Optional; import org.onap.policy.common.parameters.ValidationResult; import org.onap.policy.common.utils.coder.CoderException; import org.onap.policy.common.utils.coder.StandardCoder; @@ -29,7 +30,6 @@ import org.onap.policy.common.utils.resources.ResourceUtils; import org.onap.policy.models.base.PfModelException; import org.onap.policy.models.pdp.concepts.PdpGroup; import org.onap.policy.models.pdp.concepts.PdpGroups; -import org.onap.policy.models.provider.PolicyModelsProvider; import org.onap.policy.models.provider.PolicyModelsProviderFactory; import org.onap.policy.models.provider.PolicyModelsProviderParameters; import org.onap.policy.pap.main.PolicyPapException; @@ -44,9 +44,10 @@ import org.slf4j.LoggerFactory; public class PapDatabaseInitializer { private static final Logger LOGGER = LoggerFactory.getLogger(PapDatabaseInitializer.class); + private static final String DEFAULT_GROUP_RESOURCE = "PapDb.json"; - private StandardCoder standardCoder; - private PolicyModelsProviderFactory factory; + private final StandardCoder standardCoder; + private final PolicyModelsProviderFactory factory; /** * Constructs the object. @@ -57,18 +58,21 @@ public class PapDatabaseInitializer { } /** - * Initializes database. + * Initializes database with group information. * * @param policyModelsProviderParameters the database parameters * @throws PolicyPapException in case of errors. */ - public void initializePapDatabase(final PolicyModelsProviderParameters policyModelsProviderParameters) - throws PolicyPapException { + public void initializePapDatabase( + final PolicyModelsProviderParameters policyModelsProviderParameters, + final String groupsFile) throws PolicyPapException { - try (PolicyModelsProvider databaseProvider = + String groupsResource = Optional.ofNullable(groupsFile).orElse(DEFAULT_GROUP_RESOURCE); + + try (var databaseProvider = factory.createPolicyModelsProvider(policyModelsProviderParameters)) { - final String originalJson = ResourceUtils.getResourceAsString("PapDb.json"); - final PdpGroups pdpGroupsToCreate = standardCoder.decode(originalJson, PdpGroups.class); + final var originalJson = ResourceUtils.getResourceAsString(groupsResource); + final var pdpGroupsToCreate = standardCoder.decode(originalJson, PdpGroups.class); final List<PdpGroup> pdpGroupsFromDb = databaseProvider.getPdpGroups( pdpGroupsToCreate.getGroups().get(0).getName()); if (pdpGroupsFromDb.isEmpty()) { @@ -77,9 +81,10 @@ public class PapDatabaseInitializer { throw new PolicyPapException(result.getResult()); } databaseProvider.createPdpGroups(pdpGroupsToCreate.getGroups()); - LOGGER.debug("Created initial pdpGroup in DB - {}", pdpGroupsToCreate); + LOGGER.info("Created initial pdpGroup in DB - {} from {}", pdpGroupsToCreate, groupsResource); } else { - LOGGER.debug("Initial pdpGroup already exists in DB, skipping create - {}", pdpGroupsFromDb); + LOGGER.info("Initial pdpGroup already exists in DB, skipping create - {} from {}", + pdpGroupsFromDb, groupsResource); } } catch (final PfModelException | CoderException exp) { throw new PolicyPapException(exp); diff --git a/main/src/test/java/org/onap/policy/pap/main/startstop/TestMain.java b/main/src/test/java/org/onap/policy/pap/main/startstop/TestMain.java index e49982bf..bf3a2a4e 100644 --- a/main/src/test/java/org/onap/policy/pap/main/startstop/TestMain.java +++ b/main/src/test/java/org/onap/policy/pap/main/startstop/TestMain.java @@ -1,7 +1,7 @@ /* * ============LICENSE_START======================================================= * Copyright (C) 2019-2020 Nordix Foundation. - * Modifications Copyright (C) 2019 AT&T Intellectual Property. + * Modifications Copyright (C) 2019, 2021 AT&T Intellectual Property. * Modifications Copyright (C) 2020 Bell Canada. All rights reserved. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); @@ -35,7 +35,6 @@ import org.onap.policy.common.endpoints.http.server.HttpServletServerFactoryInst import org.onap.policy.common.utils.resources.MessageConstants; import org.onap.policy.common.utils.services.Registry; import org.onap.policy.pap.main.PapConstants; -import org.onap.policy.pap.main.PolicyPapException; import org.onap.policy.pap.main.PolicyPapRuntimeException; import org.onap.policy.pap.main.parameters.CommonTestData; @@ -59,10 +58,9 @@ public class TestMain { /** * Shuts "main" down. * - * @throws Exception if an error occurs */ @After - public void tearDown() throws Exception { + public void tearDown() { // shut down activator PapActivator activator = Registry.getOrDefault(PapConstants.REG_PAP_ACTIVATOR, PapActivator.class, null); if (activator != null && activator.isAlive()) { @@ -70,20 +68,45 @@ public class TestMain { } } - @Test - public void testMain() throws PolicyPapException { - final String[] papConfigParameters = {"-c", "parameters/PapConfigParameters.json"}; + private void testMainBody(String[] papConfigParameters) { main = new Main(papConfigParameters); assertTrue(main.getParameters().isValid()); assertEquals(CommonTestData.PAP_GROUP_NAME, main.getParameters().getName()); // ensure items were added to the registry assertNotNull(Registry.get(PapConstants.REG_PAP_ACTIVATOR, PapActivator.class)); - main.shutdown(); } @Test + public void testMain() { + final String[] papConfigParameters = {"-c", "parameters/PapConfigParameters.json"}; + testMainBody(papConfigParameters); + } + + @Test + public void testMainCustomGroup() { + final String[] papConfigParameters = { + "-c", + "parameters/PapConfigParameters.json", + "-g", + "parameters/PapDbGroup1.json" + }; + testMainBody(papConfigParameters); + } + + @Test + public void testMainPapDb() { + final String[] papConfigParameters = { + "-c", + "parameters/PapConfigParameters.json", + "-g", + "PapDb.json" + }; + testMainBody(papConfigParameters); + } + + @Test public void testMain_NoArguments() { final String[] papConfigParameters = {}; assertThatThrownBy(() -> new Main(papConfigParameters)).isInstanceOf(PolicyPapRuntimeException.class) diff --git a/main/src/test/resources/parameters/PapDbGroup1.json b/main/src/test/resources/parameters/PapDbGroup1.json new file mode 100644 index 00000000..9a967448 --- /dev/null +++ b/main/src/test/resources/parameters/PapDbGroup1.json @@ -0,0 +1,36 @@ +{ + "groups": [ + { + "name": "group1", + "version": "1.0.0", + "description": "group 1", + "pdpGroupState": "ACTIVE", + "pdpSubgroups": [ + { + "pdpType": "T1", + "supportedPolicyTypes": [ + { + "name": "t1", + "version": "1.0.0" + } + ], + "currentInstanceCount": 0, + "desiredInstanceCount": 1, + "policies": [] + }, + { + "pdpType": "T2", + "supportedPolicyTypes": [ + { + "name": "t2", + "version": "1.0.0" + } + ], + "currentInstanceCount": 0, + "desiredInstanceCount": 1, + "policies": [] + } + ] + } + ] +} |