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