Adding pdp simulator for testing purposes
[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  * ================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.models.sim.pdp;
22
23 import java.io.FileInputStream;
24 import java.util.Arrays;
25 import java.util.Properties;
26
27
28 import org.onap.policy.common.utils.services.Registry;
29 import org.onap.policy.models.sim.pdp.exception.PdpSimulatorException;
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     private PdpSimulatorParameterGroup parameterGroup;
48
49     /**
50      * Instantiates the PdpSimulator.
51      *
52      * @param args the command line arguments
53      */
54     public PdpSimulatorMain(final String[] args) {
55         LOGGER.info("In PdpSimulator with parameters ", Arrays.toString(args));
56
57         // Check the arguments
58         final PdpSimulatorCommandLineArguments arguments = new PdpSimulatorCommandLineArguments();
59         try {
60             // The arguments return a string if there is a message to print and we should exit
61             final String argumentMessage = arguments.parse(args);
62             if (argumentMessage != null) {
63                 LOGGER.debug(argumentMessage);
64                 return;
65             }
66             // Validate that the arguments are sane
67             arguments.validate();
68         } catch (final PdpSimulatorException e) {
69             LOGGER.error(PDP_SIMULATOR_FAIL_MSG, e);
70             return;
71         }
72
73         // Read the parameters
74         try {
75             parameterGroup = new PdpSimulatorParameterHandler().getParameters(arguments);
76         } catch (final Exception e) {
77             LOGGER.error(PDP_SIMULATOR_FAIL_MSG, e);
78             return;
79         }
80
81         // Read the properties
82         final Properties topicProperties = new Properties();
83         try {
84             final String propFile = arguments.getFullPropertyFilePath();
85             try (FileInputStream stream = new FileInputStream(propFile)) {
86                 topicProperties.load(stream);
87             }
88         } catch (final Exception e) {
89             LOGGER.error(PDP_SIMULATOR_FAIL_MSG, e);
90             return;
91         }
92
93         // create the activator
94         activator = new PdpSimulatorActivator(parameterGroup, topicProperties);
95         Registry.register(PdpSimulatorConstants.REG_PDP_SIMULATOR_ACTIVATOR, activator);
96         // Start the activator
97         try {
98             activator.initialize();
99         } catch (final PdpSimulatorException e) {
100             LOGGER.error("start of PdpSimulator failed, used parameters are {}", Arrays.toString(args), e);
101             Registry.unregister(PdpSimulatorConstants.REG_PDP_SIMULATOR_ACTIVATOR);
102             return;
103         }
104
105         // Add a shutdown hook to shut everything down in an orderly manner
106         Runtime.getRuntime().addShutdownHook(new PdpSimulatorShutdownHookClass());
107
108         LOGGER.info("Started PdpSimulator service");
109     }
110
111     /**
112      * Get the parameters specified in JSON.
113      *
114      * @return parameterGroup the parameters
115      */
116     public PdpSimulatorParameterGroup getParameters() {
117         return parameterGroup;
118     }
119
120
121     /**
122      * Shut down Execution.
123      *
124      * @throws PdpSimulatorException on shutdown errors
125      */
126     public void shutdown() throws PdpSimulatorException {
127         // clear the parameterGroup variable
128         parameterGroup = null;
129
130         // clear the pdp simulator activator
131         if (activator != null && activator.isAlive()) {
132             activator.terminate();
133         }
134     }
135
136     /**
137      * The Class PdpSimulatorShutdownHookClass terminates the pdp simulator when its run method is called.
138      */
139     private class PdpSimulatorShutdownHookClass extends Thread {
140         /*
141          * (non-Javadoc)
142          *
143          * @see java.lang.Runnable#run()
144          */
145         @Override
146         public void run() {
147             try {
148                 // Shutdown the pdp simulator service and wait for everything to stop
149                 if (activator != null && activator.isAlive()) {
150                     activator.terminate();
151                 }
152             } catch (final PdpSimulatorException e) {
153                 LOGGER.warn("error occured during shut down of the pdp simulator service", e);
154             }
155         }
156     }
157
158     /**
159      * The main method.
160      *
161      * @param args the arguments
162      *
163      */
164     public static void main(final String[] args) {
165         new PdpSimulatorMain(args);
166     }
167 }