Test decision from main entry
[policy/xacml-pdp.git] / main / src / main / java / org / onap / policy / pdpx / main / startstop / Main.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
4  * ================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.pdpx.main.startstop;
22
23 import java.io.FileInputStream;
24 import java.util.Arrays;
25 import java.util.Properties;
26
27 import org.onap.policy.pdpx.main.PolicyXacmlPdpException;
28 import org.onap.policy.pdpx.main.parameters.XacmlPdpParameterGroup;
29 import org.onap.policy.pdpx.main.parameters.XacmlPdpParameterHandler;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32
33
34 /**
35  * This class initiates ONAP Policy Framework policy xacml pdp.
36  *
37  */
38 public class Main {
39     private static final Logger LOGGER = LoggerFactory.getLogger(Main.class);
40
41     // The policy xacml pdp Activator that activates the policy xacml pdp service
42     private XacmlPdpActivator activator;
43
44     // The parameters read in from JSON
45     private XacmlPdpParameterGroup parameterGroup;
46
47     // The argument message for some args that return a message
48     private String argumentMessage = null;
49
50     /**
51      * Instantiates the policy xacml pdp service.
52      *
53      * @param args the command line arguments
54      */
55     public Main(final String[] args) {
56         final String argumentString = Arrays.toString(args);
57         LOGGER.info("Starting policy xacml pdp service with arguments - {}", argumentString);
58
59         // Check the arguments
60         final XacmlPdpCommandLineArguments arguments = new XacmlPdpCommandLineArguments();
61         try {
62             // The arguments return a string if there is a message to print and we should exit
63             argumentMessage = arguments.parse(args);
64             if (argumentMessage != null) {
65                 LOGGER.info(argumentMessage);
66                 return;
67             }
68
69             // Validate that the arguments are sane
70             arguments.validate();
71         } catch (final PolicyXacmlPdpException e) {
72             LOGGER.error("start of policy xacml pdp service failed", e);
73             return;
74         }
75
76         // Read the parameters
77         try {
78             parameterGroup = new XacmlPdpParameterHandler().getParameters(arguments);
79         } catch (final Exception e) {
80             LOGGER.error("start of policy xacml pdp service failed", e);
81             return;
82         }
83
84         // Read the properties
85         Properties props = new Properties();
86         try {
87             String propFile = arguments.getFullPropertyFilePath();
88             try (FileInputStream stream = new FileInputStream(propFile)) {
89                 props.load(stream);
90             }
91         } catch (final Exception e) {
92             LOGGER.error("start of xacml pdp service failed", e);
93             return;
94         }
95
96         // Now, create the activator for the policy xacml pdp service
97         activator = new XacmlPdpActivator(parameterGroup, props);
98
99         // Start the activator
100         try {
101             activator.start();
102         } catch (final RuntimeException e) {
103             LOGGER.error("start of policy xacml pdp service failed, used parameters are " + Arrays.toString(args), e);
104             return;
105         }
106
107         // Add a shutdown hook to shut everything down in an orderly manner
108         Runtime.getRuntime().addShutdownHook(new PolicyXacmlPdpShutdownHookClass());
109         LOGGER.info("Started policy xacml pdp service");
110     }
111
112     /**
113      * Get the parameters specified in JSON.
114      *
115      * @return the parameters
116      */
117     public XacmlPdpParameterGroup getParameters() {
118         return parameterGroup;
119     }
120
121     /**
122      * Get the argumentMessage string.
123      *
124      * @return the argumentMessage
125      */
126     public String getArgumentMessage() {
127         return argumentMessage;
128     }
129
130     /**
131      * Shut down Execution.
132      *
133      * @throws PolicyXacmlPdpException on shutdown errors
134      */
135     public void shutdown() {
136         // clear the parameterGroup variable
137         parameterGroup = null;
138
139         // clear the xacml pdp activator
140         if (activator != null) {
141             activator.stop();
142         }
143     }
144
145     /**
146      * The Class PolicyXacmlPdpShutdownHookClass terminates the policy xacml pdp service when its run
147      * method is called.
148      */
149     private class PolicyXacmlPdpShutdownHookClass extends Thread {
150         /*
151          * (non-Javadoc)
152          *
153          * @see java.lang.Runnable#run()
154          */
155         @Override
156         public void run() {
157             // Shutdown the policy xacml pdp service and wait for everything to stop
158             activator.stop();
159         }
160     }
161
162     /**
163      * The main method.
164      *
165      * @param args the arguments
166      */
167     public static void main(final String[] args) {
168         new Main(args);
169     }
170 }