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