e743794ece4cc20985697ce140dddeaac744d542
[policy/engine.git] / BRMSGateway / src / main / java / org / onap / policy / brms / api / BrmsGateway.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP Policy Engine
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.onap.policy.brms.api;
22
23 import org.onap.policy.api.NotificationScheme;
24 import org.onap.policy.api.PolicyEngine;
25 import org.onap.policy.api.PolicyException;
26 import org.onap.policy.common.logging.flexlogger.FlexLogger;
27 import org.onap.policy.common.logging.flexlogger.Logger;
28 import org.onap.policy.xacml.api.XACMLErrorConstants;
29
30 /**
31  * BRMSGateway: This application acts as the Gateway interface between the PDP XACML and PDP Drools.
32  * The listens for BRMS based policies and pushes them to the specified Policy Repository, from
33  * where the PDP Drools reads the Rule Jar.
34  * 
35  * @version 0.1
36  */
37 public class BrmsGateway {
38
39     private static final Logger logger = FlexLogger.getLogger(BrmsGateway.class);
40     private static final String CONFIGFILE = "config.properties";
41
42     private static PolicyEngine policyEngine = null;
43
44     private BrmsGateway() {
45         // Default private constructor
46     }
47
48     /**
49      * Main method.
50      * @param args The path to the configuration file is the only allowed optional argument
51      * @throws Exception on BRMS Gateway errors
52      */
53     public static void main(final String[] args) throws Exception {
54         // The configuration file containing the configuration for the BRMS Gateway
55         String configFile = CONFIGFILE;
56
57         // Check if a configuration file has been specified as a parameter
58         if (args.length == 1) {
59             configFile = args[0];
60         }
61         else if (args.length > 1) {
62             String errorString = "usage: " + BrmsGateway.class.getCanonicalName() + " [configFile]";
63             logger.error(errorString);
64             throw new PolicyException(errorString);
65         }
66
67         // Initialize Handler.
68         logger.info("Initializing BRMS Handler");
69         BrmsHandler brmsHandler = null;
70         try {
71             brmsHandler = new BrmsHandler(configFile);
72         } catch (final PolicyException e) {
73             String errorString = "Check your property file: " + e.getMessage();
74             logger.error(errorString);
75             throw new PolicyException(errorString);
76         }
77
78         // Set Handler with Auto Notification and initialize policyEngine
79         try {
80             logger.info("Initializing policyEngine with Auto Notifications");
81             policyEngine = new PolicyEngine(CONFIGFILE, NotificationScheme.AUTO_ALL_NOTIFICATIONS, brmsHandler);
82         } catch (final Exception e) {
83             logger.error(XACMLErrorConstants.ERROR_UNKNOWN + "Error while Initializing Policy Engine " + e.getMessage(),
84                             e);
85         }
86
87         // Keep Running....
88         final Runnable runnable = () -> {
89             while (true) {
90                 try {
91                     Thread.sleep(30000);
92                 } catch (final InterruptedException e) {
93                     logger.error(XACMLErrorConstants.ERROR_SYSTEM_ERROR + "Thread Exception " + e.getMessage());
94                     Thread.currentThread().interrupt();
95                 }
96             }
97         };
98         final Thread thread = new Thread(runnable);
99         thread.start();
100     }
101
102     public static PolicyEngine getPolicyEngine() {
103         return policyEngine;
104     }
105 }