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