Remove topic.properties and incorporate into overall config file
[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 import java.util.Properties;
26 import org.onap.policy.common.endpoints.utils.ParameterUtils;
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 String START_FAILED = "start of policy pap service failed";
43
44     private static final Logger LOGGER = LoggerFactory.getLogger(Main.class);
45
46     private PapActivator activator;
47     private PapParameterGroup parameterGroup;
48
49     /**
50      * Instantiates the policy pap service.
51      *
52      * @param args the command line arguments
53      */
54     public Main(final String[] args) {
55         final String argumentString = Arrays.toString(args);
56         LOGGER.info("Starting policy pap service with arguments - {}", argumentString);
57
58         // Check the arguments
59         final PapCommandLineArguments arguments = new PapCommandLineArguments();
60         try {
61             // The arguments return a string if there is a message to print and we should exit
62             final String argumentMessage = arguments.parse(args);
63             if (argumentMessage != null) {
64                 LOGGER.info(argumentMessage);
65                 return;
66             }
67             // Validate that the arguments are sane
68             arguments.validate();
69         } catch (final PolicyPapException e) {
70             LOGGER.error(START_FAILED, e);
71             return;
72         }
73
74         // Read the parameters
75         try {
76             parameterGroup = new PapParameterHandler().getParameters(arguments);
77         } catch (final Exception e) {
78             LOGGER.error(START_FAILED, e);
79             return;
80         }
81
82         // Read the properties
83         Properties props = ParameterUtils.getTopicProperties(parameterGroup.getTopicParameterGroup());
84
85         // Initialize database
86         try {
87             new PapDatabaseInitializer().initializePapDatabase(parameterGroup.getDatabaseProviderParameters());
88         } catch (final PolicyPapException exp) {
89             LOGGER.error(START_FAILED + ", used parameters are {}", Arrays.toString(args), exp);
90             return;
91         }
92
93         // Now, create the activator for the policy pap service
94         activator = new PapActivator(parameterGroup, props);
95         Registry.register(PapConstants.REG_PAP_ACTIVATOR, activator);
96
97         // Start the activator
98         try {
99             activator.start();
100         } catch (final RuntimeException e) {
101             LOGGER.error("start of policy pap service failed, used parameters are {}", Arrays.toString(args), e);
102             Registry.unregister(PapConstants.REG_PAP_ACTIVATOR);
103             return;
104         }
105
106         // Add a shutdown hook to shut everything down in an orderly manner
107         Runtime.getRuntime().addShutdownHook(new PolicyPapShutdownHookClass());
108         LOGGER.info("Started policy pap service");
109     }
110
111     /**
112      * Get the parameters specified in JSON.
113      *
114      * @return the parameters
115      */
116     public PapParameterGroup getParameters() {
117         return parameterGroup;
118     }
119
120     /**
121      * Shut down Execution.
122      *
123      * @throws PolicyPapException on shutdown errors
124      */
125     public void shutdown() throws PolicyPapException {
126         // clear the parameterGroup variable
127         parameterGroup = null;
128
129         // clear the pap activator
130         if (activator != null) {
131             activator.stop();
132         }
133     }
134
135     /**
136      * The Class PolicyPapShutdownHookClass terminates the policy pap service when its run method is called.
137      */
138     private class PolicyPapShutdownHookClass extends Thread {
139         /*
140          * (non-Javadoc)
141          *
142          * @see java.lang.Runnable#run()
143          */
144         @Override
145         public void run() {
146             if (!activator.isAlive()) {
147                 return;
148             }
149
150             try {
151                 // Shutdown the policy pap service and wait for everything to stop
152                 activator.stop();
153             } catch (final RuntimeException e) {
154                 LOGGER.warn("error occured during shut down of the policy pap service", e);
155             }
156         }
157     }
158
159     /**
160      * The main method.
161      *
162      * @param args the arguments
163      */
164     public static void main(final String[] args) {
165         new Main(args);
166     }
167 }