support external configuration of pdp groups
[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, 2021 AT&T Intellectual Property.
5  *  Modifications Copyright (C) 2020 Bell Canada. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  * SPDX-License-Identifier: Apache-2.0
20  * ============LICENSE_END=========================================================
21  */
22
23 package org.onap.policy.pap.main.startstop;
24
25 import java.util.Arrays;
26 import org.onap.policy.common.utils.resources.MessageConstants;
27 import org.onap.policy.common.utils.services.Registry;
28 import org.onap.policy.pap.main.PapConstants;
29 import org.onap.policy.pap.main.PolicyPapRuntimeException;
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 var argumentString = Arrays.toString(args);
54         LOGGER.info("Starting policy pap service with arguments - {}", argumentString);
55
56         // Check the arguments
57         final var 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
68             // Read the parameters
69             parameterGroup = new PapParameterHandler().getParameters(arguments);
70
71             // Initialize database
72             new PapDatabaseInitializer().initializePapDatabase(
73                     parameterGroup.getDatabaseProviderParameters(),
74                     arguments.getPdpGroupsConfiguration());
75
76             // Now, create the activator for the policy pap service
77             activator = new PapActivator(parameterGroup);
78             Registry.register(PapConstants.REG_PAP_ACTIVATOR, activator);
79
80             // Start the activator
81             activator.start();
82         } catch (Exception exp) { // NOSONAR
83             /*
84              * Disabled sonar on the above line, because we want to capture the stack
85              * trace via the logger while still reporting the exception message on stdout
86              * when the JVM exits.
87              */
88             LOGGER.error("failed to start Main", exp);
89             if (null != activator) {
90                 Registry.unregister(PapConstants.REG_PAP_ACTIVATOR);
91             }
92             throw new PolicyPapRuntimeException(
93                 String.format(MessageConstants.START_FAILURE_MSG, MessageConstants.POLICY_PAP), exp);
94         }
95
96         // Add a shutdown hook to shut everything down in an orderly manner
97         Runtime.getRuntime().addShutdownHook(new PolicyPapShutdownHookClass());
98         var successMsg = String.format(MessageConstants.START_SUCCESS_MSG, MessageConstants.POLICY_PAP);
99         LOGGER.info(successMsg);
100     }
101
102     /**
103      * Get the parameters specified in JSON.
104      *
105      * @return the parameters
106      */
107     public PapParameterGroup getParameters() {
108         return parameterGroup;
109     }
110
111     /**
112      * Shut down Execution.
113      *
114      */
115     public void shutdown() {
116         // clear the parameterGroup variable
117         parameterGroup = null;
118
119         // clear the pap activator
120         if (activator != null) {
121             activator.stop();
122         }
123     }
124
125     /**
126      * The Class PolicyPapShutdownHookClass terminates the policy pap service when its run method is called.
127      */
128     private class PolicyPapShutdownHookClass extends Thread {
129         /*
130          * (non-Javadoc)
131          *
132          * @see java.lang.Runnable#run()
133          */
134         @Override
135         public void run() {
136             if (!activator.isAlive()) {
137                 return;
138             }
139
140             try {
141                 // Shutdown the policy pap service and wait for everything to stop
142                 activator.stop();
143             } catch (final RuntimeException e) {
144                 LOGGER.warn("error occurred during shut down of the policy pap service", e);
145             }
146         }
147     }
148
149     /**
150      * The main method.
151      *
152      * @param args the arguments
153      */
154     public static void main(final String[] args) {      // NOSONAR
155         /*
156          * NOTE: arguments are validated by the constructor, thus sonar is disabled.
157          */
158
159         new Main(args);
160     }
161 }