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