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