From 9e627229dad4b56fd626f0b2813eb5b63e9a27a2 Mon Sep 17 00:00:00 2001 From: jhh Date: Thu, 27 May 2021 14:41:11 -0500 Subject: support external configuration of pdp groups - modify policy-pap.sh to optionally provision a custom group if such a file is present. Issue-ID: POLICY-3331 Signed-off-by: jhh Change-Id: Ib53bc14ee6b9471ab48f5a792b4283db3ed53b93 Signed-off-by: jhh --- .../org/onap/policy/pap/main/startstop/Main.java | 10 +++--- .../main/startstop/PapCommandLineArguments.java | 39 +++++++++++++++------- .../pap/main/startstop/PapDatabaseInitializer.java | 20 ++++++----- .../onap/policy/pap/main/startstop/TestMain.java | 37 ++++++++++++++++---- .../src/test/resources/parameters/PapDbGroup1.json | 36 ++++++++++++++++++++ 5 files changed, 109 insertions(+), 33 deletions(-) create mode 100644 main/src/test/resources/parameters/PapDbGroup1.json (limited to 'main') 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 fe9b736a..5918ed12 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; @@ -70,7 +69,9 @@ public class Main { parameterGroup = new PapParameterHandler().getParameters(arguments); // Initialize database - new PapDatabaseInitializer().initializePapDatabase(parameterGroup.getDatabaseProviderParameters()); + new PapDatabaseInitializer().initializePapDatabase( + parameterGroup.getDatabaseProviderParameters(), + arguments.getPdpGroupsConfiguration()); // Now, create the activator for the policy pap service activator = new PapActivator(parameterGroup); @@ -110,9 +111,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 +141,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 6020c847..afc8fbca 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-2021 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. @@ -34,23 +34,38 @@ import org.onap.policy.pap.main.PolicyPapRuntimeException; */ public class PapCommandLineArguments extends CommandLineArgumentsHandler { + protected static final String GROUP_FILE_OPTION = "g"; + protected static final String GROUP_FILE_LONG_OPTION = "groups-file"; + public static final String GROUP_FILE_ARG_NAME = "GROUP_FILE"; + + protected static final String DEFAULT_GROUP_RESOURCE = "PapDb.json"; + /** * Construct the options for the CLI editor. */ public PapCommandLineArguments() { - super(Main.class.getName(), MessageConstants.POLICY_PAP, customOption()); + super(Main.class.getName(), MessageConstants.POLICY_PAP, customOptionG()); } - /** - * Builds the extra property-file option to be declared on constructor. - * - * @return property-file option - */ - private static Option customOption() { - return Option.builder("p").longOpt("property-file") - .desc("the full path to the topic property file to use, " - + "the property file contains the policy pap topic properties") - .hasArg().argName("PROP_FILE").required(false).type(String.class).build(); + private static Option customOptionG() { + return Option.builder(GROUP_FILE_OPTION).longOpt(GROUP_FILE_LONG_OPTION) + .desc("the full path to the groups file to use, " + + "the groups file contains the group configuration added to the DB") + .hasArg().argName(GROUP_FILE_ARG_NAME).required(false).type(String.class).build(); + } + + protected String getPdpGroupsConfiguration() { + return this.getCommandLine() + .getOptionValue(GROUP_FILE_OPTION, DEFAULT_GROUP_RESOURCE); + } + + @Override + public void validate() throws CommandLineException { + super.validate(); + String groupConfig = getPdpGroupsConfiguration(); + if (!groupConfig.equals(DEFAULT_GROUP_RESOURCE)) { + validateReadableFile(MessageConstants.POLICY_PAP, groupConfig); + } } /** 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 a266d695..5c28e7a6 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 @@ -44,8 +44,8 @@ public class PapDatabaseInitializer { private static final Logger LOGGER = LoggerFactory.getLogger(PapDatabaseInitializer.class); - private StandardCoder standardCoder; - private PolicyModelsProviderFactory factory; + private final StandardCoder standardCoder; + private final PolicyModelsProviderFactory factory; /** * Constructs the object. @@ -56,17 +56,18 @@ 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, + String groupsJson) throws PolicyPapException { try (var databaseProvider = - factory.createPolicyModelsProvider(policyModelsProviderParameters)) { - final var originalJson = ResourceUtils.getResourceAsString("PapDb.json"); + factory.createPolicyModelsProvider(policyModelsProviderParameters)) { + final var originalJson = ResourceUtils.getResourceAsString(groupsJson); final var pdpGroupsToCreate = standardCoder.decode(originalJson, PdpGroups.class); final List pdpGroupsFromDb = databaseProvider.getPdpGroups( pdpGroupsToCreate.getGroups().get(0).getName()); @@ -76,9 +77,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, groupsJson); } else { - LOGGER.debug("Initial pdpGroup already exists in DB, skipping create - {}", pdpGroupsFromDb); + LOGGER.info("Initial pdpGroup already exists in DB, skipping create - {} from {}", + pdpGroupsFromDb, groupsJson); } } 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..12f740b4 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 @@ -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,19 +68,44 @@ 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 = {}; 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": [] + } + ] + } + ] +} -- cgit 1.2.3-korg