support external configuration of pdp groups 74/121574/6
authorjhh <jorge.hernandez-herrero@att.com>
Thu, 27 May 2021 19:41:11 +0000 (14:41 -0500)
committerjhh <jorge.hernandez-herrero@att.com>
Thu, 27 May 2021 22:19:46 +0000 (17:19 -0500)
- modify policy-pap.sh to optionally provision
  a custom group if such a file is present.

Issue-ID: POLICY-3331
Signed-off-by: jhh <jorge.hernandez-herrero@att.com>
Change-Id: Ib53bc14ee6b9471ab48f5a792b4283db3ed53b93
Signed-off-by: jhh <jorge.hernandez-herrero@att.com>
main/src/main/java/org/onap/policy/pap/main/startstop/Main.java
main/src/main/java/org/onap/policy/pap/main/startstop/PapCommandLineArguments.java
main/src/main/java/org/onap/policy/pap/main/startstop/PapDatabaseInitializer.java
main/src/test/java/org/onap/policy/pap/main/startstop/TestMain.java
main/src/test/resources/parameters/PapDbGroup1.json [new file with mode: 0644]
packages/policy-pap-docker/src/main/docker/policy-pap.sh

index fe9b736..5918ed1 100644 (file)
@@ -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);
             }
         }
     }
index 6020c84..afc8fbc 100644 (file)
@@ -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);
+        }
     }
 
     /**
index a266d69..5c28e7a 100644 (file)
@@ -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<PdpGroup> 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);
index e49982b..12f740b 100644 (file)
@@ -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 (file)
index 0000000..9a96744
--- /dev/null
@@ -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": []
+        }
+      ]
+    }
+  ]
+}
index 2b49aff..469d95e 100644 (file)
@@ -53,4 +53,20 @@ if [ -f "${POLICY_HOME}/etc/mounted/logback.xml" ]; then
     cp -f "${POLICY_HOME}"/etc/mounted/logback.xml "${POLICY_HOME}"/etc/
 fi
 
-$JAVA_HOME/bin/java -cp "${POLICY_HOME}/etc:${POLICY_HOME}/lib/*" -Dlogback.configurationFile="${POLICY_HOME}/etc/logback.xml" -Djavax.net.ssl.keyStore="${KEYSTORE}" -Djavax.net.ssl.keyStorePassword="${KEYSTORE_PASSWD}" -Djavax.net.ssl.trustStore="${TRUSTSTORE}" -Djavax.net.ssl.trustStorePassword="${TRUSTSTORE_PASSWD}" org.onap.policy.pap.main.startstop.Main -c "${CONFIG_FILE}"
+# provide and external PDP group configuration if a groups.json
+# file is present in the data directory. If none is present,
+# the PAP will use the PapDb.json resource in the classpath
+# to load a default group.
+
+if [ -f "${POLICY_HOME}/etc/mounted/groups.json" ]; then
+    CUSTOM_GROUPS="-g ${POLICY_HOME}/etc/mounted/groups.json"
+fi
+
+$JAVA_HOME/bin/java -cp "${POLICY_HOME}/etc:${POLICY_HOME}/lib/*" \
+    -Dlogback.configurationFile="${POLICY_HOME}/etc/logback.xml" \
+    -Djavax.net.ssl.keyStore="${KEYSTORE}" \
+    -Djavax.net.ssl.keyStorePassword="${KEYSTORE_PASSWD}" \
+    -Djavax.net.ssl.trustStore="${TRUSTSTORE}" \
+    -Djavax.net.ssl.trustStorePassword="${TRUSTSTORE_PASSWD}" \
+    org.onap.policy.pap.main.startstop.Main \
+    -c "${CONFIG_FILE}" "${CUSTOM_GROUPS}"