add rest serve and distribution framework
[multicloud/framework.git] / artifactbroker / main / src / main / java / org / onap / policy / distribution / main / startstop / Main.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2018 Ericsson. All rights reserved.
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.distribution.main.startstop;
22
23 import java.util.Arrays;
24
25 import org.onap.policy.common.logging.flexlogger.FlexLogger;
26 import org.onap.policy.common.logging.flexlogger.Logger;
27 import org.onap.policy.distribution.main.PolicyDistributionException;
28 import org.onap.policy.distribution.main.parameters.DistributionParameterGroup;
29 import org.onap.policy.distribution.main.parameters.DistributionParameterHandler;
30
31 /**
32  * This class initiates ONAP Policy Framework policy distribution.
33  *
34  * @author Liam Fallon (liam.fallon@ericsson.com)
35  */
36 public class Main {
37     private static final Logger LOGGER = FlexLogger.getLogger(Main.class);
38
39     // The policy distribution Activator that activates the policy distribution service
40     private DistributionActivator activator;
41
42     // The parameters read in from JSON
43     private DistributionParameterGroup parameterGroup;
44
45     /**
46      * Instantiates the policy distribution 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 distribution service with arguments - " + argumentString);
53
54         // Check the arguments
55         final DistributionCommandLineArguments arguments = new DistributionCommandLineArguments();
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
64             // Validate that the arguments are sane
65             arguments.validate();
66         } catch (final PolicyDistributionException e) {
67             LOGGER.error("start of policy distribution service failed", e);
68             return;
69         }
70
71         // Read the parameters
72         try {
73             parameterGroup = new DistributionParameterHandler().getParameters(arguments);
74         } catch (final Exception e) {
75             LOGGER.error("start of policy distribution service failed", e);
76             return;
77         }
78
79         // Now, create the activator for the policy distribution service
80         activator = new DistributionActivator(parameterGroup);
81
82         // Start the activator
83         try {
84             activator.initialize();
85         } catch (final PolicyDistributionException e) {
86             LOGGER.error("start of policy distribution service failed, used parameters are " + Arrays.toString(args),
87                     e);
88             return;
89         }
90
91         // Add a shutdown hook to shut everything down in an orderly manner
92         Runtime.getRuntime().addShutdownHook(new PolicyDistributionShutdownHookClass());
93         LOGGER.info("Started policy distribution service");
94     }
95
96     /**
97      * Get the parameters specified in JSON.
98      *
99      * @return the parameters
100      */
101     public DistributionParameterGroup getParameters() {
102         return parameterGroup;
103     }
104
105     /**
106      * Shut down Execution.
107      *
108      * @throws PolicyDistributionException on shutdown errors
109      */
110     public void shutdown() throws PolicyDistributionException {
111         // clear the parameterGroup variable
112         parameterGroup = null;
113
114         // clear the distribution activator
115         if (activator != null) {
116             activator.terminate();
117         }
118     }
119
120     /**
121      * The Class PolicyDistributionShutdownHookClass terminates the policy distribution service when its run method is
122      * called.
123      */
124     private class PolicyDistributionShutdownHookClass extends Thread {
125         /*
126          * (non-Javadoc)
127          *
128          * @see java.lang.Runnable#run()
129          */
130         @Override
131         public void run() {
132             try {
133                 // Shutdown the policy distribution service and wait for everything to stop
134                 activator.terminate();
135             } catch (final PolicyDistributionException e) {
136                 LOGGER.warn("error occured during shut down of the policy distribution service", e);
137             }
138         }
139     }
140
141     /**
142      * The main method.
143      *
144      * @param args the arguments
145      */
146     public static void main(final String[] args) {
147         new Main(args);
148     }
149 }