62b811d84dcb7a8556138ed8c4330355cd727f7e
[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  * Modifications Copyright (C) 2019 Nordix Foundation.
5  * ================================================================================
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  * SPDX-License-Identifier: Apache-2.0
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.pdpx.main.startstop;
23
24 import java.util.Arrays;
25 import java.util.Properties;
26 import lombok.Getter;
27 import org.onap.policy.common.endpoints.utils.ParameterUtils;
28 import org.onap.policy.pdpx.main.PolicyXacmlPdpException;
29 import org.onap.policy.pdpx.main.parameters.XacmlPdpParameterGroup;
30 import org.onap.policy.pdpx.main.parameters.XacmlPdpParameterHandler;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33
34
35 /**
36  * This class initiates ONAP Policy Framework policy xacml pdp.
37  *
38  */
39 public class Main {
40     private static final Logger LOGGER = LoggerFactory.getLogger(Main.class);
41
42     // The policy xacml pdp Activator that activates the policy xacml pdp service
43     private XacmlPdpActivator activator;
44
45     @Getter
46     private String argumentMessage = null;
47
48     /**
49      * Instantiates the policy xacml pdp service.
50      *
51      * @param args the command line arguments
52      * @throws PolicyXacmlPdpException if an error occurs
53      */
54     public Main(final String[] args) throws PolicyXacmlPdpException {
55         final String argumentString = Arrays.toString(args);
56         LOGGER.info("Starting policy xacml pdp service with arguments - {}", argumentString);
57
58         // Check the arguments
59         final XacmlPdpCommandLineArguments arguments = new XacmlPdpCommandLineArguments();
60
61         // The arguments return a string if there is a message to print and we should exit
62         argumentMessage = arguments.parse(args);
63         if (argumentMessage != null) {
64             LOGGER.info(argumentMessage);
65             return;
66         }
67
68         // Validate that the arguments are sane
69         arguments.validate();
70
71         // Read the parameters
72         XacmlPdpParameterGroup parameterGroup = new XacmlPdpParameterHandler().getParameters(arguments);
73
74         // Read the properties
75         Properties props = ParameterUtils.getTopicProperties(parameterGroup.getTopicParameterGroup());
76
77         // Now, create the activator for the policy xacml pdp service
78         activator = new XacmlPdpActivator(parameterGroup, props);
79         XacmlPdpActivator.setCurrent(activator);
80
81         // Start the activator
82         activator.start();
83
84         // Add a shutdown hook to shut everything down in an orderly manner
85         Runtime.getRuntime().addShutdownHook(new Thread(this::shutdown));
86         LOGGER.info("Started policy xacml pdp service");
87     }
88
89     /**
90      * Shut down Execution.
91      *
92      * @throws PolicyXacmlPdpException on shutdown errors
93      */
94     public synchronized void shutdown() {
95         // clear the xacml pdp activator
96         if (activator != null && activator.isAlive()) {
97             activator.stop();
98             activator = null;
99         }
100     }
101
102     /**
103      * The main method.
104      *
105      * @param args the arguments
106      */
107     public static void main(final String[] args) {
108         try {
109             new Main(args);
110         } catch (RuntimeException | PolicyXacmlPdpException e) {
111             LOGGER.error("start of policy xacml pdp service failed", e);
112         }
113     }
114 }