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;
* @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);
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);
// 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);
}
/**
* Shut down Execution.
*
- * @throws PolicyPapException on shutdown errors
*/
- public void shutdown() throws PolicyPapException {
+ public void shutdown() {
// clear the parameterGroup variable
parameterGroup = null;
// 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);
}
}
}
/*-
* ============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.
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;
private final Options options;
private String configurationFilePath = null;
private String propertyFilePath = null;
+ private String groupFilePath = null;
/**
* Construct the options for the CLI editor.
.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
}
// 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) {
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());
}
setPropertyFilePath(commandLine.getOptionValue('p'));
}
+ if (commandLine.hasOption('g')) {
+ setGroupFilePath(commandLine.getOptionValue('g'));
+ }
+
return null;
}
*/
public void validate() throws PolicyPapException {
validateReadableFile("policy pap configuration", configurationFilePath);
+ if (groupFilePath != null) {
+ validateReadableFile("policy pap groups", groupFilePath);
+ }
}
/**
* @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, "");
return propertyFilePath != null && propertyFilePath.length() > 0;
}
+ /**
+ * 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.
*
}
// 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");
}
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");
}
}
}
/*-
* ============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.
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;
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;
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.
}
/**
- * 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()) {
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);
/*
* ============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");
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;
/**
* 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()) {
}
}
- @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 = {};
--- /dev/null
+{
+ "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": []
+ }
+ ]
+ }
+ ]
+}
#
# ============LICENSE_START=======================================================
# Copyright (C) 2019-2020 Nordix Foundation.
-# Modifications Copyright (C) 2019-2020 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.
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}"