Allow xacml-pdp to use kafka
[policy/xacml-pdp.git] / main / src / main / java / org / onap / policy / pdpx / main / startstop / Main.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * Copyright (C) 2019-2021 AT&T Intellectual Property. All rights reserved.
4  * Modifications Copyright (C) 2019 Nordix Foundation.
5  * Modifications Copyright (C) 2020 Bell Canada. 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  *
19  * SPDX-License-Identifier: Apache-2.0
20  * ============LICENSE_END=========================================================
21  */
22
23 package org.onap.policy.pdpx.main.startstop;
24
25 import java.util.Arrays;
26 import lombok.Getter;
27 import org.onap.policy.common.utils.resources.MessageConstants;
28 import org.onap.policy.pdpx.main.PolicyXacmlPdpException;
29 import org.onap.policy.pdpx.main.PolicyXacmlPdpRuntimeException;
30 import org.onap.policy.pdpx.main.parameters.XacmlPdpParameterGroup;
31 import org.onap.policy.pdpx.main.parameters.XacmlPdpParameterHandler;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34
35
36 /**
37  * This class initiates ONAP Policy Framework policy xacml pdp.
38  *
39  */
40 public class Main {
41     private static final Logger LOGGER = LoggerFactory.getLogger(Main.class);
42
43     // The policy xacml pdp Activator that activates the policy xacml pdp service
44     private XacmlPdpActivator activator;
45
46     @Getter
47     private String argumentMessage;
48
49     /**
50      * Instantiates the policy xacml pdp service.
51      *
52      * @param args the command line arguments
53      * @throws PolicyXacmlPdpException if an error occurs
54      */
55     public Main(final String[] args) throws PolicyXacmlPdpException {
56         final var argumentString = Arrays.toString(args);
57         LOGGER.info("Starting policy xacml pdp service with arguments - {}", argumentString);
58
59         // Check the arguments
60         final var arguments = new XacmlPdpCommandLineArguments();
61
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
72         // Read the parameters
73         XacmlPdpParameterGroup parameterGroup = new XacmlPdpParameterHandler().getParameters(arguments);
74
75         // Now, create the activator for the policy xacml pdp service
76         activator = new XacmlPdpActivator(parameterGroup);
77         XacmlPdpActivator.setCurrent(activator);
78
79         // Start the activator
80         activator.start();
81
82         // Add a shutdown hook to shut everything down in an orderly manner
83         Runtime.getRuntime().addShutdownHook(new Thread(this::shutdown));
84         var successMsg = String.format(MessageConstants.START_SUCCESS_MSG, MessageConstants.POLICY_XACML_PDP);
85         LOGGER.info(successMsg);
86     }
87
88     /**
89      * Shut down Execution.
90      *
91      */
92     public synchronized void shutdown() {
93         // clear the xacml pdp activator
94         if (activator != null && activator.isAlive()) {
95             activator.stop();
96             activator = null;
97         }
98     }
99
100     /**
101      * The main method. The NOSONAR is for security hotspot for checking
102      * command line arguments. Since we use XacmlPdpCommandLineArguments
103      * and validate the arguments the NOSONAR is ok to clear it.
104      *
105      * @param args the arguments
106      */
107     public static void main(final String[] args) { //NOSONAR
108         try {
109             new Main(args);
110         } catch (RuntimeException | PolicyXacmlPdpException e) {
111             throw new PolicyXacmlPdpRuntimeException(
112                 String.format(MessageConstants.START_FAILURE_MSG, MessageConstants.POLICY_XACML_PDP), e);
113         }
114     }
115 }