65e0a31e5753c4ccbf9a1f16a805c40ed83b3a96
[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.util.Arrays;
24 import org.onap.policy.pdpx.main.PolicyXacmlPdpException;
25 import org.onap.policy.pdpx.main.parameters.XacmlPdpParameterGroup;
26 import org.onap.policy.pdpx.main.parameters.XacmlPdpParameterHandler;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29
30
31 /**
32  * This class initiates ONAP Policy Framework policy xacml pdp.
33  *
34  */
35 public class Main {
36     private static final Logger LOGGER = LoggerFactory.getLogger(Main.class);
37
38     // The policy xacml pdp Activator that activates the policy xacml pdp service
39     private XacmlPdpActivator activator;
40
41     // The parameters read in from JSON
42     private XacmlPdpParameterGroup parameterGroup;
43
44     // The argument message for some args that return a message
45     private String argumentMessage = null;
46
47     /**
48      * Instantiates the policy xacml pdp service.
49      *
50      * @param args the command line arguments
51      */
52     public Main(final String[] args) {
53         final String argumentString = Arrays.toString(args);
54         LOGGER.info("Starting policy xacml pdp service with arguments - {}", argumentString);
55
56         // Check the arguments
57         final XacmlPdpCommandLineArguments arguments = new XacmlPdpCommandLineArguments();
58         try {
59             // The arguments return a string if there is a message to print and we should exit
60             argumentMessage = arguments.parse(args);
61             if (argumentMessage != null) {
62                 LOGGER.info(argumentMessage);
63                 return;
64             }
65
66             // Validate that the arguments are sane
67             arguments.validate();
68         } catch (final PolicyXacmlPdpException e) {
69             LOGGER.error("start of policy xacml pdp service failed", e);
70             return;
71         }
72
73         // Read the parameters
74         try {
75             parameterGroup = new XacmlPdpParameterHandler().getParameters(arguments);
76         } catch (final Exception e) {
77             LOGGER.error("start of policy xacml pdp service failed", e);
78             return;
79         }
80
81         // Now, create the activator for the policy xacml pdp service
82         activator = new XacmlPdpActivator(parameterGroup);
83
84         // Start the activator
85         try {
86             activator.initialize();
87         } catch (final PolicyXacmlPdpException e) {
88             LOGGER.error("start of policy xacml pdp service failed, used parameters are " + Arrays.toString(args), e);
89             return;
90         }
91
92         // Add a shutdown hook to shut everything down in an orderly manner
93         Runtime.getRuntime().addShutdownHook(new PolicyXacmlPdpShutdownHookClass());
94         LOGGER.info("Started policy xacml pdp service");
95     }
96
97     /**
98      * Get the parameters specified in JSON.
99      *
100      * @return the parameters
101      */
102     public XacmlPdpParameterGroup getParameters() {
103         return parameterGroup;
104     }
105
106     /**
107      * Get the argumentMessage string.
108      *
109      * @return the argumentMessage
110      */
111     public String getArgumentMessage() {
112         return argumentMessage;
113     }
114
115     /**
116      * Shut down Execution.
117      *
118      * @throws PolicyXacmlPdpException on shutdown errors
119      */
120     public void shutdown() throws PolicyXacmlPdpException {
121         // clear the parameterGroup variable
122         parameterGroup = null;
123
124         // clear the xacml pdp activator
125         if (activator != null) {
126             activator.terminate();
127         }
128     }
129
130     /**
131      * The Class PolicyXacmlPdpShutdownHookClass terminates the policy xacml pdp service when its run
132      * method is called.
133      */
134     private class PolicyXacmlPdpShutdownHookClass extends Thread {
135         /*
136          * (non-Javadoc)
137          *
138          * @see java.lang.Runnable#run()
139          */
140         @Override
141         public void run() {
142             try {
143                 // Shutdown the policy xacml pdp service and wait for everything to stop
144                 activator.terminate();
145             } catch (final PolicyXacmlPdpException e) {
146                 LOGGER.warn("error occured during shut down of the policy xacml pdp service", e);
147             }
148         }
149     }
150
151     /**
152      * The main method.
153      *
154      * @param args the arguments
155      */
156     public static void main(final String[] args) {
157         new Main(args);
158     }
159 }