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