020bfcbf1f235352dcc3a97c12aadc5cf1701a95
[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.io.FileInputStream;
24 import java.util.Arrays;
25 import java.util.Properties;
26 import lombok.Getter;
27 import org.onap.policy.pdpx.main.PolicyXacmlPdpException;
28 import org.onap.policy.pdpx.main.parameters.XacmlPdpParameterGroup;
29 import org.onap.policy.pdpx.main.parameters.XacmlPdpParameterHandler;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32
33
34 /**
35  * This class initiates ONAP Policy Framework policy xacml pdp.
36  *
37  */
38 public class Main {
39     private static final Logger LOGGER = LoggerFactory.getLogger(Main.class);
40
41     // The policy xacml pdp Activator that activates the policy xacml pdp service
42     private XacmlPdpActivator activator;
43
44     @Getter
45     private String argumentMessage = null;
46
47     /**
48      * Instantiates the policy xacml pdp service.
49      *
50      * @param args the command line arguments
51      * @throws PolicyXacmlPdpException if an error occurs
52      */
53     public Main(final String[] args) throws PolicyXacmlPdpException {
54         final String argumentString = Arrays.toString(args);
55         LOGGER.info("Starting policy xacml pdp service with arguments - {}", argumentString);
56
57         // Check the arguments
58         final XacmlPdpCommandLineArguments arguments = new XacmlPdpCommandLineArguments();
59
60         // The arguments return a string if there is a message to print and we should exit
61         argumentMessage = arguments.parse(args);
62         if (argumentMessage != null) {
63             LOGGER.info(argumentMessage);
64             return;
65         }
66
67         // Validate that the arguments are sane
68         arguments.validate();
69
70         // Read the parameters
71         XacmlPdpParameterGroup parameterGroup = new XacmlPdpParameterHandler().getParameters(arguments);
72
73         // Read the properties
74         Properties props = new Properties();
75         try {
76             String propFile = arguments.getFullPropertyFilePath();
77             try (FileInputStream stream = new FileInputStream(propFile)) {
78                 props.load(stream);
79             }
80         } catch (final Exception e) {
81             throw new PolicyXacmlPdpException("cannot load property file", e);
82         }
83
84         // Now, create the activator for the policy xacml pdp service
85         activator = new XacmlPdpActivator(parameterGroup, props);
86         XacmlPdpActivator.setCurrent(activator);
87
88         // Start the activator
89         activator.start();
90
91         // Add a shutdown hook to shut everything down in an orderly manner
92         Runtime.getRuntime().addShutdownHook(new Thread(this::shutdown));
93         LOGGER.info("Started policy xacml pdp service");
94     }
95
96     /**
97      * Shut down Execution.
98      *
99      * @throws PolicyXacmlPdpException on shutdown errors
100      */
101     public synchronized void shutdown() {
102         // clear the xacml pdp activator
103         if (activator != null && activator.isAlive()) {
104             activator.stop();
105             activator = null;
106         }
107     }
108
109     /**
110      * The main method.
111      *
112      * @param args the arguments
113      */
114     public static void main(final String[] args) {
115         try {
116             new Main(args);
117         } catch (RuntimeException | PolicyXacmlPdpException e) {
118             LOGGER.error("start of policy xacml pdp service failed", e);
119         }
120     }
121 }