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