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