Changes to PAP infrastructure to support PDP
[policy/pap.git] / main / src / main / java / org / onap / policy / pap / main / startstop / Main.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2019 Nordix Foundation.
4  *  Modifications Copyright (C) 2019 AT&T Intellectual Property.
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.pap.main.startstop;
23
24 import java.io.FileInputStream;
25 import java.util.Arrays;
26 import java.util.Properties;
27 import org.onap.policy.common.utils.services.Registry;
28 import org.onap.policy.pap.main.PapConstants;
29 import org.onap.policy.pap.main.PolicyPapException;
30 import org.onap.policy.pap.main.parameters.PapParameterGroup;
31 import org.onap.policy.pap.main.parameters.PapParameterHandler;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34
35 /**
36  * This class initiates ONAP Policy Framework PAP component.
37  *
38  * @author Ram Krishna Verma (ram.krishna.verma@est.tech)
39  */
40 public class Main {
41
42     private static final Logger LOGGER = LoggerFactory.getLogger(Main.class);
43
44     private PapActivator activator;
45     private PapParameterGroup parameterGroup;
46
47     /**
48      * Instantiates the policy pap service.
49      *
50      * @param args the command line arguments
51      */
52     public Main(final String[] args) {
53         final String argumentString = Arrays.toString(args);
54         LOGGER.info("Starting policy pap service with arguments - {}", argumentString);
55
56         // Check the arguments
57         final PapCommandLineArguments arguments = new PapCommandLineArguments();
58         try {
59             // The arguments return a string if there is a message to print and we should exit
60             final String argumentMessage = arguments.parse(args);
61             if (argumentMessage != null) {
62                 LOGGER.info(argumentMessage);
63                 return;
64             }
65             // Validate that the arguments are sane
66             arguments.validate();
67         } catch (final PolicyPapException e) {
68             LOGGER.error("start of policy pap service failed", e);
69             return;
70         }
71
72         // Read the parameters
73         try {
74             parameterGroup = new PapParameterHandler().getParameters(arguments);
75         } catch (final Exception e) {
76             LOGGER.error("start of policy pap service failed", e);
77             return;
78         }
79
80         // Read the properties
81         Properties props = new Properties();
82         try {
83             String propFile = arguments.getFullPropertyFilePath();
84             try (FileInputStream stream = new FileInputStream(propFile)) {
85                 props.load(stream);
86             }
87         } catch (final Exception e) {
88             LOGGER.error("start of policy pap service failed", e);
89             return;
90         }
91
92         // Now, create the activator for the policy pap service
93         activator = new PapActivator(parameterGroup, props);
94         Registry.register(PapConstants.REG_PAP_ACTIVATOR, activator);
95
96         // Start the activator
97         try {
98             activator.start();
99         } catch (final RuntimeException e) {
100             LOGGER.error("start of policy pap service failed, used parameters are {}", Arrays.toString(args), e);
101             Registry.unregister(PapConstants.REG_PAP_ACTIVATOR);
102             return;
103         }
104
105         // Add a shutdown hook to shut everything down in an orderly manner
106         Runtime.getRuntime().addShutdownHook(new PolicyPapShutdownHookClass());
107         LOGGER.info("Started policy pap service");
108     }
109
110     /**
111      * Get the parameters specified in JSON.
112      *
113      * @return the parameters
114      */
115     public PapParameterGroup getParameters() {
116         return parameterGroup;
117     }
118
119     /**
120      * Shut down Execution.
121      *
122      * @throws PolicyPapException on shutdown errors
123      */
124     public void shutdown() throws PolicyPapException {
125         // clear the parameterGroup variable
126         parameterGroup = null;
127
128         // clear the pap activator
129         if (activator != null) {
130             activator.stop();
131         }
132     }
133
134     /**
135      * The Class PolicyPapShutdownHookClass terminates the policy pap service when its run method is called.
136      */
137     private class PolicyPapShutdownHookClass extends Thread {
138         /*
139          * (non-Javadoc)
140          *
141          * @see java.lang.Runnable#run()
142          */
143         @Override
144         public void run() {
145             try {
146                 // Shutdown the policy pap service and wait for everything to stop
147                 activator.stop();
148             } catch (final RuntimeException e) {
149                 LOGGER.warn("error occured during shut down of the policy pap service", e);
150             }
151         }
152     }
153
154     /**
155      * The main method.
156      *
157      * @param args the arguments
158      */
159     public static void main(final String[] args) {
160         new Main(args);
161     }
162 }