diff options
Diffstat (limited to 'policy-management/src/main/java/org/onap/policy/drools/system/Main.java')
-rw-r--r-- | policy-management/src/main/java/org/onap/policy/drools/system/Main.java | 281 |
1 files changed, 159 insertions, 122 deletions
diff --git a/policy-management/src/main/java/org/onap/policy/drools/system/Main.java b/policy-management/src/main/java/org/onap/policy/drools/system/Main.java index c8956839..79268b2c 100644 --- a/policy-management/src/main/java/org/onap/policy/drools/system/Main.java +++ b/policy-management/src/main/java/org/onap/policy/drools/system/Main.java @@ -32,130 +32,167 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; /** - * Programmatic entry point to the management layer + * Programmatic entry point to the management layer. */ public class Main { - /** - * logback configuration file system property - */ - public static final String LOGBACK_CONFIGURATION_FILE_SYSTEM_PROPERTY = - "logback.configurationFile"; - - /** - * logback configuration file system property - */ - public static final String LOGBACK_CONFIGURATION_FILE_DEFAULT = "config/logback.xml"; - - /** - * constructor (hides public default one) - */ - private Main() {} - - /** - * main - * - * @param args program arguments - */ - public static void main(String[] args) { - - /* logging defaults */ - - if (System.getProperty(LOGBACK_CONFIGURATION_FILE_SYSTEM_PROPERTY) == null) { - if (Files.exists(Paths.get(LOGBACK_CONFIGURATION_FILE_DEFAULT))) { - System.setProperty(LOGBACK_CONFIGURATION_FILE_SYSTEM_PROPERTY, - LOGBACK_CONFIGURATION_FILE_DEFAULT); - } else { - LoggerUtil.setLevel(LoggerUtil.ROOT_LOGGER, ch.qos.logback.classic.Level.INFO.toString()); - } + /** logback configuration file system property. */ + public static final String LOGBACK_CONFIGURATION_FILE_SYSTEM_PROPERTY = + "logback.configurationFile"; + + /** logback configuration file system property. */ + public static final String LOGBACK_CONFIGURATION_FILE_DEFAULT = "config/logback.xml"; + + /** constructor (hides public default one). */ + private Main() {} + + /** + * main. + * + * @param args program arguments. + */ + public static void main(String[] args) { + + /* logging defaults */ + + if (System.getProperty(LOGBACK_CONFIGURATION_FILE_SYSTEM_PROPERTY) == null) { + if (Files.exists(Paths.get(LOGBACK_CONFIGURATION_FILE_DEFAULT))) { + System.setProperty( + LOGBACK_CONFIGURATION_FILE_SYSTEM_PROPERTY, LOGBACK_CONFIGURATION_FILE_DEFAULT); + } else { + LoggerUtil.setLevel(LoggerUtil.ROOT_LOGGER, ch.qos.logback.classic.Level.INFO.toString()); + } + } + + /* make sure the default configuration directory is properly set up */ + + SystemPersistence.manager.setConfigurationDir(null); + + /* logging defaults */ + + if (System.getProperty(LOGBACK_CONFIGURATION_FILE_SYSTEM_PROPERTY) == null) { + if (Files.exists(Paths.get(LOGBACK_CONFIGURATION_FILE_DEFAULT))) { + System.setProperty( + LOGBACK_CONFIGURATION_FILE_SYSTEM_PROPERTY, LOGBACK_CONFIGURATION_FILE_DEFAULT); + } else { + LoggerUtil.setLevel(LoggerUtil.ROOT_LOGGER, ch.qos.logback.classic.Level.INFO.toString()); + } + } + + /* 0. boot */ + + PolicyEngine.manager.boot(args); + + /* start logger */ + + final Logger logger = LoggerFactory.getLogger(Main.class); + + /* 1.a. Configure the Engine */ + + Properties engineProperties = SystemPersistence.manager.getEngineProperties(); + if (engineProperties == null) { + engineProperties = PolicyEngine.manager.defaultTelemetryConfig(); + } + + PolicyEngine.manager.configure(engineProperties); + + /* 1.b. Load Installation Environment(s) */ + + for (final Properties env : SystemPersistence.manager.getEnvironmentProperties()) { + PolicyEngine.manager.setEnvironment(env); + } + + /* 2. Start the Engine with the basic services only (no Policy Controllers) */ + + MDCTransaction trans = + MDCTransaction.newTransaction(null, null) + .setServiceName(Main.class.getSimpleName()) + .setTargetEntity("engine") + .setTargetServiceName("start"); + try { + final boolean success = PolicyEngine.manager.start(); + if (!success) { + trans.setStatusCode(false).setResponseDescription("partial start").flush(); + logger.warn( + LoggerUtil.TRANSACTION_LOG_MARKER, + "Main: {} has been partially started", + PolicyEngine.manager); + } else { + trans.setStatusCode(true).transaction(); + } + } catch (final IllegalStateException e) { + trans + .setStatusCode(false) + .setResponseCode(e.getClass().getSimpleName()) + .setResponseDescription(e.getMessage()) + .flush(); + logger.warn( + LoggerUtil.TRANSACTION_LOG_MARKER, + "Main: cannot start {} (bad state) because of {}", + PolicyEngine.manager, + e.getMessage(), + e); + } catch (final Exception e) { + trans + .setStatusCode(false) + .setResponseCode(e.getClass().getSimpleName()) + .setResponseDescription(e.getMessage()) + .flush(); + logger.warn( + LoggerUtil.TRANSACTION_LOG_MARKER, + "Main: cannot start {} because of {}", + PolicyEngine.manager, + e.getMessage(), + e); + System.exit(1); + } + + /* 3. Create and start the controllers */ + + for (final Properties controllerProperties : + SystemPersistence.manager.getControllerProperties()) { + final String controllerName = + controllerProperties.getProperty(DroolsProperties.PROPERTY_CONTROLLER_NAME); + try { + trans = + MDCTransaction.newTransaction(null, null) + .setServiceName(Main.class.getSimpleName()) + .setTargetEntity("controller:" + controllerName) + .setTargetServiceName("start"); + + final PolicyController controller = + PolicyEngine.manager.createPolicyController(controllerName, controllerProperties); + controller.start(); + + trans + .setStatusCode(true) + .setResponseDescription(controller.getDrools().getCanonicalSessionNames().toString()) + .transaction(); + } catch (final Exception e) { + trans + .setStatusCode(false) + .setResponseCode(e.getClass().getSimpleName()) + .setResponseDescription(e.getMessage()) + .flush(); + logger.error( + LoggerUtil.TRANSACTION_LOG_MARKER, + "Main: cannot instantiate policy-controller {} because of {}", + controllerName, + e.getMessage(), + e); + } catch (final LinkageError e) { + trans + .setStatusCode(false) + .setResponseCode(e.getClass().getSimpleName()) + .setResponseDescription(e.getMessage()) + .flush(); + logger.warn( + LoggerUtil.TRANSACTION_LOG_MARKER, + "Main: cannot instantiate policy-controller {} (linkage) because of {}", + controllerName, + e.getMessage(), + e); + } + } } - - /* make sure the default configuration directory is properly set up */ - - SystemPersistence.manager.setConfigurationDir(null); - - /* logging defaults */ - - if (System.getProperty(LOGBACK_CONFIGURATION_FILE_SYSTEM_PROPERTY) == null) { - if (Files.exists(Paths.get(LOGBACK_CONFIGURATION_FILE_DEFAULT))) { - System.setProperty(LOGBACK_CONFIGURATION_FILE_SYSTEM_PROPERTY, - LOGBACK_CONFIGURATION_FILE_DEFAULT); - } else { - LoggerUtil.setLevel(LoggerUtil.ROOT_LOGGER, ch.qos.logback.classic.Level.INFO.toString()); - } - } - - /* 0. boot */ - - PolicyEngine.manager.boot(args); - - /* start logger */ - - final Logger logger = LoggerFactory.getLogger(Main.class); - - /* 1.a. Configure the Engine */ - - Properties engineProperties = SystemPersistence.manager.getEngineProperties(); - if (engineProperties == null) - engineProperties = PolicyEngine.manager.defaultTelemetryConfig(); - - PolicyEngine.manager.configure(engineProperties); - - /* 1.b. Load Installation Environment(s) */ - - for (final Properties env : SystemPersistence.manager.getEnvironmentProperties()) { - PolicyEngine.manager.setEnvironment(env); - } - - /* 2. Start the Engine with the basic services only (no Policy Controllers) */ - - MDCTransaction trans = - MDCTransaction.newTransaction(null, null).setServiceName(Main.class.getSimpleName()). - setTargetEntity("engine").setTargetServiceName("start"); - try { - final boolean success = PolicyEngine.manager.start(); - if (!success) { - trans.setStatusCode(false).setResponseDescription("partial start").flush(); - logger.warn(LoggerUtil.TRANSACTION_LOG_MARKER, "Main: {} has been partially started", PolicyEngine.manager); - } else { - trans.setStatusCode(true).transaction(); - } - } catch (final IllegalStateException e) { - trans.setStatusCode(false).setResponseCode(e.getClass().getSimpleName()).setResponseDescription(e.getMessage()).flush(); - logger.warn(LoggerUtil.TRANSACTION_LOG_MARKER, "Main: cannot start {} (bad state) because of {}", PolicyEngine.manager, e.getMessage(), e); - } catch (final Exception e) { - trans.setStatusCode(false).setResponseCode(e.getClass().getSimpleName()).setResponseDescription(e.getMessage()).flush(); - logger.warn(LoggerUtil.TRANSACTION_LOG_MARKER, "Main: cannot start {} because of {}", PolicyEngine.manager, e.getMessage(), e); - System.exit(1); - } - - /* 3. Create and start the controllers */ - - for (final Properties controllerProperties : SystemPersistence.manager - .getControllerProperties()) { - final String controllerName = - controllerProperties.getProperty(DroolsProperties.PROPERTY_CONTROLLER_NAME); - try { - trans = - MDCTransaction.newTransaction(null, null).setServiceName(Main.class.getSimpleName()). - setTargetEntity("controller:" + controllerName).setTargetServiceName("start"); - - final PolicyController controller = - PolicyEngine.manager.createPolicyController(controllerName, controllerProperties); - controller.start(); - - trans.setStatusCode(true). - setResponseDescription(controller.getDrools().getCanonicalSessionNames().toString()). - transaction(); - } catch (final Exception e) { - trans.setStatusCode(false).setResponseCode(e.getClass().getSimpleName()).setResponseDescription(e.getMessage()).flush(); - logger.error(LoggerUtil.TRANSACTION_LOG_MARKER, "Main: cannot instantiate policy-controller {} because of {}", - controllerName, e.getMessage(), e); - } catch (final LinkageError e) { - trans.setStatusCode(false).setResponseCode(e.getClass().getSimpleName()).setResponseDescription(e.getMessage()).flush(); - logger.warn(LoggerUtil.TRANSACTION_LOG_MARKER, "Main: cannot instantiate policy-controller {} (linkage) because of {}", - controllerName, e.getMessage(), e); - } - } - } } |