Merge "Handle numRecords default setting when built as 0."
[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-2021 Nordix Foundation.
4  *  Modifications Copyright (C) 2019, 2021 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 lombok.Getter;
26 import org.onap.policy.common.utils.cmd.CommandLineException;
27 import org.onap.policy.models.sim.dmaap.DmaapSimException;
28 import org.onap.policy.models.sim.dmaap.DmaapSimRuntimeException;
29 import org.onap.policy.models.sim.dmaap.parameters.DmaapSimParameterGroup;
30 import org.onap.policy.models.sim.dmaap.parameters.DmaapSimParameterHandler;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33
34 /**
35  * This class initiates the DMaaP simulator component.
36  */
37 public class Main {
38
39     private static final Logger LOGGER = LoggerFactory.getLogger(Main.class);
40
41     private DmaapSimActivator activator;
42     @Getter
43     private DmaapSimParameterGroup parameters;
44
45     /**
46      * Instantiates the DMaap Simulator service.
47      *
48      * @param args the command line arguments
49      */
50     public Main(final String[] args) {
51         final var argumentString = Arrays.toString(args);
52         LOGGER.info("Starting DMaaP simulator service with arguments - {}", argumentString);
53
54         // Check the arguments
55         final var arguments = new DmaapSimCommandLineArguments();
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 DmaapSimRuntimeException | CommandLineException e) {
66             LOGGER.error("start of DMaaP simulator service failed", e);
67             return;
68         }
69
70         // Read the parameters
71         try {
72             parameters = new DmaapSimParameterHandler().getParameters(arguments);
73         } catch (final Exception e) {
74             LOGGER.error("start of DMaaP simulator service failed", e);
75             return;
76         }
77
78         // Now, create the activator for the DMaaP Simulator service
79         activator = new DmaapSimActivator(parameters);
80
81         // Start the activator
82         try {
83             activator.start();
84         } catch (final RuntimeException e) {
85             LOGGER.error("start of DMaaP simulator service failed, used parameters are {}", Arrays.toString(args), e);
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      * Shut down Execution.
96      *
97      * @throws DmaapSimException on shutdown errors
98      */
99     public void shutdown() throws DmaapSimException {
100         // clear the parameterGroup variable
101         parameters = null;
102
103         // clear the DMaaP simulator activator
104         if (activator != null && activator.isAlive()) {
105             activator.stop();
106         }
107     }
108
109     /**
110      * The Class DmaapSimShutdownHookClass terminates the DMaaP simulator service when its run method is called.
111      */
112     private class DmaapSimShutdownHookClass extends Thread {
113         /*
114          * (non-Javadoc)
115          *
116          * @see java.lang.Runnable#run()
117          */
118         @Override
119         public void run() {
120             try {
121                 // Shutdown the DMaaP simulator service and wait for everything to stop
122                 shutdown();
123
124             } catch (final RuntimeException | DmaapSimException e) {
125                 LOGGER.warn("error occured during shut down of the DMaaP simulator service", e);
126             }
127         }
128     }
129
130     /**
131      * The main method.
132      *
133      * @param args the arguments
134      */
135     public static void main(final String[] args) {      // NOSONAR
136         /*
137          * The arguments are validated by the constructor, thus sonar is disabled.
138          */
139
140         new Main(args);
141     }
142 }