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