Xacml PDP Register/Unregister Changes
[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.net.UnknownHostException;
25 import java.util.Arrays;
26 import java.util.Properties;
27 import org.onap.policy.common.endpoints.event.comm.client.TopicSinkClientException;
28 import org.onap.policy.pdpx.main.PolicyXacmlPdpException;
29 import org.onap.policy.pdpx.main.comm.XacmlPdpPapRegistration;
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     // The parameters read in from JSON
47     private XacmlPdpParameterGroup parameterGroup;
48
49     // The argument message for some args that return a message
50     private String argumentMessage = null;
51
52     /**
53      * Instantiates the policy xacml pdp service.
54      *
55      * @param args the command line arguments
56      */
57     public Main(final String[] args) {
58         final String argumentString = Arrays.toString(args);
59         LOGGER.info("Starting policy xacml pdp service with arguments - {}", argumentString);
60
61         // Check the arguments
62         final XacmlPdpCommandLineArguments arguments = new XacmlPdpCommandLineArguments();
63         try {
64             // The arguments return a string if there is a message to print and we should exit
65             argumentMessage = arguments.parse(args);
66             if (argumentMessage != null) {
67                 LOGGER.info(argumentMessage);
68                 return;
69             }
70
71             // Validate that the arguments are sane
72             arguments.validate();
73         } catch (final PolicyXacmlPdpException e) {
74             LOGGER.error("start of policy xacml pdp service failed", e);
75             return;
76         }
77
78         // Read the parameters
79         try {
80             parameterGroup = new XacmlPdpParameterHandler().getParameters(arguments);
81         } catch (final Exception e) {
82             LOGGER.error("start of policy xacml pdp service failed", e);
83             return;
84         }
85
86         // Read the properties
87         Properties props = new Properties();
88         try {
89             String propFile = arguments.getFullPropertyFilePath();
90             try (FileInputStream stream = new FileInputStream(propFile)) {
91                 props.load(stream);
92             }
93         } catch (final Exception e) {
94             LOGGER.error("start of xacml pdp service failed", e);
95             return;
96         }
97
98         // Now, create the activator for the policy xacml pdp service
99         activator = new XacmlPdpActivator(parameterGroup, props);
100
101         // Start the activator
102         try {
103             activator.start();
104         } catch (final RuntimeException e) {
105             LOGGER.error("start of policy xacml pdp service failed, used parameters are " + Arrays.toString(args), e);
106             return;
107         }
108
109         // Add a shutdown hook to shut everything down in an orderly manner
110         Runtime.getRuntime().addShutdownHook(new PolicyXacmlPdpShutdownHookClass());
111         LOGGER.info("Started policy xacml pdp service");
112     }
113
114     /**
115      * Get the parameters specified in JSON.
116      *
117      * @return the parameters
118      */
119     public XacmlPdpParameterGroup getParameters() {
120         return parameterGroup;
121     }
122
123     /**
124      * Get the argumentMessage string.
125      *
126      * @return the argumentMessage
127      */
128     public String getArgumentMessage() {
129         return argumentMessage;
130     }
131
132     /**
133      * Shut down Execution.
134      *
135      * @throws PolicyXacmlPdpException on shutdown errors
136      */
137     public void shutdown() {
138         // clear the parameterGroup variable
139         parameterGroup = null;
140
141         // clear the xacml pdp activator
142         if (activator != null) {
143             activator.stop();
144         }
145     }
146
147     /**
148      * The Class PolicyXacmlPdpShutdownHookClass terminates the policy xacml pdp service when its run
149      * method is called.
150      */
151     private class PolicyXacmlPdpShutdownHookClass extends Thread {
152         /*
153          * (non-Javadoc)
154          *
155          * @see java.lang.Runnable#run()
156          */
157         @Override
158         public void run() {
159             // Shutdown the policy xacml pdp service and wait for everything to stop
160             activator.stop();
161         }
162     }
163
164     /**
165      * The main method.
166      *
167      * @param args the arguments
168      */
169     public static void main(final String[] args) {
170         new Main(args);
171     }
172 }