1441ba37fae92d60c3c1127a3caf86a937b3253f
[policy/models.git] / models-sim / policy-models-sim-pdp / src / main / java / org / onap / policy / models / sim / pdp / PdpSimulatorMain.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.pdp;
23
24 import java.util.Arrays;
25 import lombok.Getter;
26 import org.onap.policy.common.utils.cmd.CommandLineException;
27 import org.onap.policy.common.utils.services.Registry;
28 import org.onap.policy.models.sim.pdp.exception.PdpSimulatorException;
29 import org.onap.policy.models.sim.pdp.exception.PdpSimulatorRunTimeException;
30 import org.onap.policy.models.sim.pdp.parameters.PdpSimulatorParameterGroup;
31 import org.onap.policy.models.sim.pdp.parameters.PdpSimulatorParameterHandler;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34
35 /**
36  * This class initiates PdpSimulator.
37  *
38  * @author Ajith Sreekumar (ajith.sreekumar@est.tech)
39  */
40 public class PdpSimulatorMain {
41
42     private static final String PDP_SIMULATOR_FAIL_MSG = "start of pdp simulator failed";
43
44     private static final Logger LOGGER = LoggerFactory.getLogger(PdpSimulatorMain.class);
45
46     private PdpSimulatorActivator activator;
47     @Getter
48     private PdpSimulatorParameterGroup parameters;
49
50     /**
51      * Instantiates the PdpSimulator.
52      *
53      * @param args the command line arguments
54      */
55     public PdpSimulatorMain(final String[] args) {
56         if (LOGGER.isInfoEnabled()) {
57             LOGGER.info("In PdpSimulator with parameters {}", Arrays.toString(args));
58         }
59
60         // Check the arguments
61         final var arguments = new PdpSimulatorCommandLineArguments();
62         try {
63             // The arguments return a string if there is a message to print and we should exit
64             final String argumentMessage = arguments.parse(args);
65             if (argumentMessage != null) {
66                 LOGGER.debug(argumentMessage);
67                 return;
68             }
69             // Validate that the arguments are sane
70             arguments.validate();
71         } catch (final PdpSimulatorRunTimeException | CommandLineException e) {
72             LOGGER.error(PDP_SIMULATOR_FAIL_MSG, e);
73             return;
74         }
75
76         // Read the parameters
77         try {
78             parameters = new PdpSimulatorParameterHandler().getParameters(arguments);
79         } catch (final Exception e) {
80             LOGGER.error(PDP_SIMULATOR_FAIL_MSG, e);
81             return;
82         }
83
84         // create the activator
85         activator = new PdpSimulatorActivator(parameters);
86         Registry.register(PdpSimulatorConstants.REG_PDP_SIMULATOR_ACTIVATOR, activator);
87         // Start the activator
88         try {
89             activator.initialize();
90         } catch (final PdpSimulatorException e) {
91             LOGGER.error("start of PdpSimulator failed, used parameters are {}", Arrays.toString(args), e);
92             Registry.unregister(PdpSimulatorConstants.REG_PDP_SIMULATOR_ACTIVATOR);
93             return;
94         }
95
96         // Add a shutdown hook to shut everything down in an orderly manner
97         Runtime.getRuntime().addShutdownHook(new PdpSimulatorShutdownHookClass());
98
99         LOGGER.info("Started PdpSimulator service");
100     }
101
102
103     /**
104      * Shut down Execution.
105      *
106      * @throws PdpSimulatorException on shutdown errors
107      */
108     public void shutdown() throws PdpSimulatorException {
109         // clear the parameterGroup variable
110         parameters = null;
111
112         // clear the pdp simulator activator
113         if (activator != null && activator.isAlive()) {
114             activator.terminate();
115         }
116     }
117
118     /**
119      * The Class PdpSimulatorShutdownHookClass terminates the pdp simulator when its run method is called.
120      */
121     private class PdpSimulatorShutdownHookClass 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 pdp simulator service and wait for everything to stop
131                 if (activator != null && activator.isAlive()) {
132                     activator.terminate();
133                 }
134             } catch (final PdpSimulatorException e) {
135                 LOGGER.warn("error occured during shut down of the pdp simulator service", e);
136             }
137         }
138     }
139
140     /**
141      * The main method. Arguments are validated in the constructor thus adding the NOSONAR.
142      *
143      * @param args the arguments
144      *
145      */
146     public static void main(final String[] args) { // NOSONAR
147         /*
148          * The arguments are validated by the constructor, thus sonar is disabled.
149          */
150
151         new PdpSimulatorMain(args);
152     }
153 }