[POLICY-30] optional loadable eelf feature
[policy/drools-pdp.git] / policy-management / src / main / java / org / openecomp / policy / drools / system / Main.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * policy-management
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20
21 package org.openecomp.policy.drools.system;
22
23 import java.io.File;
24 import java.nio.file.Path;
25 import java.nio.file.Paths;
26 import java.util.Properties;
27
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;
32
33 /**
34  * Programmatic entry point to the management layer
35  */
36 public class Main {
37
38         /**
39          * logback configuration file system property
40          */
41         public static final String LOGBACK_CONFIGURATION_FILE_SYSTEM_PROPERTY = "logback.configurationFile";
42         
43         /**
44          * logback configuration file system property
45          */
46         public static final String LOGBACK_CONFIGURATION_FILE_DEFAULT = "config/logback.xml";
47         
48         /**
49          * constructor (hides public default one)
50          */
51         private Main() {}       
52         
53         /**
54          * main
55          * 
56          * @param args program arguments
57          */
58         public static void main(String args[]) {
59                 
60                 /* logging defaults */
61                 
62                 if (System.getProperty(LOGBACK_CONFIGURATION_FILE_SYSTEM_PROPERTY) == null)
63                         System.setProperty(LOGBACK_CONFIGURATION_FILE_SYSTEM_PROPERTY, LOGBACK_CONFIGURATION_FILE_DEFAULT);
64                 
65                 /* 0. boot */
66                 
67                 PolicyEngine.manager.boot(args);
68                 
69                 Logger logger = LoggerFactory.getLogger(Main.class);
70                 
71                 File configDir = new File(SystemPersistence.CONFIG_DIR_NAME);
72                 
73                 if (!configDir.isDirectory()) {
74                         throw new IllegalArgumentException
75                                                 ("config directory: " + configDir.getAbsolutePath() + 
76                                                  " not found");
77                 }
78                 
79                 /* 1. Configure the Engine */
80
81                 try {
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);
87                 }
88                 
89                 /* 2. Start the Engine with the basic services only (no Policy Controllers) */
90                 
91                 try {
92                         boolean success = PolicyEngine.manager.start();
93                         if (!success) {
94                                 logger.warn("Main: {} has been partially started", PolicyEngine.manager);               
95                         }
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);
100                         System.exit(1);
101                 }
102                 
103                 /* 3. Create and start the controllers */
104                 
105                 File[] controllerFiles = configDir.listFiles();
106                 for (File config : controllerFiles) {
107
108                         if (config.getName().endsWith(SystemPersistence.PROPERTIES_FILE_CONTROLLER_SUFFIX)) {
109                                 int idxSuffix = 
110                                                 config.getName().indexOf(SystemPersistence.PROPERTIES_FILE_CONTROLLER_SUFFIX);
111                                 int lastIdxSuffix = 
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);
118                                 }
119
120                                 String name = 
121                                                 config.getName().substring(0, lastIdxSuffix);
122                                 try {
123                                         Properties properties = PropertyUtil.getProperties(config);
124                                         PolicyController controller = PolicyEngine.manager.createPolicyController(name, properties);
125                                         controller.start();
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);
131                                 }
132                         }
133                 }
134         }
135 }