2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.openecomp.policy.drools.system;
24 import java.nio.file.Path;
25 import java.nio.file.Paths;
26 import java.util.Properties;
28 import org.openecomp.policy.drools.persistence.SystemPersistence;
29 import org.openecomp.policy.drools.utils.PropertyUtil;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
34 * Programmatic entry point to the management layer
39 * logback configuration file system property
41 public static final String LOGBACK_CONFIGURATION_FILE_SYSTEM_PROPERTY = "logback.configurationFile";
44 * logback configuration file system property
46 public static final String LOGBACK_CONFIGURATION_FILE_DEFAULT = "config/logback.xml";
49 * constructor (hides public default one)
56 * @param args program arguments
58 public static void main(String args[]) {
60 /* logging defaults */
62 if (System.getProperty(LOGBACK_CONFIGURATION_FILE_SYSTEM_PROPERTY) == null)
63 System.setProperty(LOGBACK_CONFIGURATION_FILE_SYSTEM_PROPERTY, LOGBACK_CONFIGURATION_FILE_DEFAULT);
67 PolicyEngine.manager.boot(args);
69 Logger logger = LoggerFactory.getLogger(Main.class);
71 File configDir = new File(SystemPersistence.CONFIG_DIR_NAME);
73 if (!configDir.isDirectory()) {
74 throw new IllegalArgumentException
75 ("config directory: " + configDir.getAbsolutePath() +
79 /* 1. Configure the Engine */
82 Path policyEnginePath = Paths.get(configDir.toPath().toString(), SystemPersistence.PROPERTIES_FILE_ENGINE);
83 Properties properties = PropertyUtil.getProperties(policyEnginePath.toFile());
84 PolicyEngine.manager.configure(properties);
85 } catch (Exception e) {
86 logger.warn("Main: cannot initialize {} because of {}", PolicyEngine.manager, e.getMessage(), e);
89 /* 2. Start the Engine with the basic services only (no Policy Controllers) */
92 boolean success = PolicyEngine.manager.start();
94 logger.warn("Main: {} has been partially started", PolicyEngine.manager);
96 } catch (IllegalStateException e) {
97 logger.warn("Main: cannot start {} (bad state) because of {}", PolicyEngine.manager, e.getMessage(), e);
98 } catch (Exception e) {
99 logger.warn("Main: cannot start {} because of {}", PolicyEngine.manager, e.getMessage(), e);
103 /* 3. Create and start the controllers */
105 File[] controllerFiles = configDir.listFiles();
106 for (File config : controllerFiles) {
108 if (config.getName().endsWith(SystemPersistence.PROPERTIES_FILE_CONTROLLER_SUFFIX)) {
110 config.getName().indexOf(SystemPersistence.PROPERTIES_FILE_CONTROLLER_SUFFIX);
112 config.getName().lastIndexOf(SystemPersistence.PROPERTIES_FILE_CONTROLLER_SUFFIX);
113 if (idxSuffix != lastIdxSuffix) {
114 throw new IllegalArgumentException
115 ("Improper naming of controller properties file: " +
116 "Expected <controller-name>" +
117 SystemPersistence.PROPERTIES_FILE_CONTROLLER_SUFFIX);
121 config.getName().substring(0, lastIdxSuffix);
123 Properties properties = PropertyUtil.getProperties(config);
124 PolicyController controller = PolicyEngine.manager.createPolicyController(name, properties);
126 } catch (Exception e) {
127 logger.error("Main: cannot instantiate policy-controller {} because of {}", name, e.getMessage(), e);
128 } catch (LinkageError e) {
129 logger.warn("Main: cannot instantiate policy-controller {} (linkage) because of {}",
130 name, e.getMessage(), e);