Use new addTopic() method in xacml-pdp
[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 lombok.Getter;
26 import org.onap.policy.pdpx.main.PolicyXacmlPdpException;
27 import org.onap.policy.pdpx.main.parameters.XacmlPdpParameterGroup;
28 import org.onap.policy.pdpx.main.parameters.XacmlPdpParameterHandler;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31
32
33 /**
34  * This class initiates ONAP Policy Framework policy xacml pdp.
35  *
36  */
37 public class Main {
38     private static final Logger LOGGER = LoggerFactory.getLogger(Main.class);
39
40     // The policy xacml pdp Activator that activates the policy xacml pdp service
41     private XacmlPdpActivator activator;
42
43     @Getter
44     private String argumentMessage = null;
45
46     /**
47      * Instantiates the policy xacml pdp service.
48      *
49      * @param args the command line arguments
50      * @throws PolicyXacmlPdpException if an error occurs
51      */
52     public Main(final String[] args) throws PolicyXacmlPdpException {
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
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
69         // Read the parameters
70         XacmlPdpParameterGroup parameterGroup = new XacmlPdpParameterHandler().getParameters(arguments);
71
72         // Now, create the activator for the policy xacml pdp service
73         activator = new XacmlPdpActivator(parameterGroup);
74         XacmlPdpActivator.setCurrent(activator);
75
76         // Start the activator
77         activator.start();
78
79         // Add a shutdown hook to shut everything down in an orderly manner
80         Runtime.getRuntime().addShutdownHook(new Thread(this::shutdown));
81         LOGGER.info("Started policy xacml pdp service");
82     }
83
84     /**
85      * Shut down Execution.
86      *
87      * @throws PolicyXacmlPdpException on shutdown errors
88      */
89     public synchronized void shutdown() {
90         // clear the xacml pdp activator
91         if (activator != null && activator.isAlive()) {
92             activator.stop();
93             activator = null;
94         }
95     }
96
97     /**
98      * The main method.
99      *
100      * @param args the arguments
101      */
102     public static void main(final String[] args) {
103         try {
104             new Main(args);
105         } catch (RuntimeException | PolicyXacmlPdpException e) {
106             LOGGER.error("start of policy xacml pdp service failed", e);
107         }
108     }
109 }