Merge "Add period after inheritDoc for Sonar"
[policy/apex-pdp.git] / services / services-engine / src / main / java / org / onap / policy / apex / service / engine / main / ApexMain.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2016-2018 Ericsson. All rights reserved.
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.apex.service.engine.main;
22
23 import java.util.Arrays;
24 import java.util.Base64;
25 import java.util.Map.Entry;
26
27 import org.onap.policy.apex.model.basicmodel.concepts.ApexException;
28 import org.onap.policy.apex.service.parameters.ApexParameterHandler;
29 import org.onap.policy.apex.service.parameters.ApexParameters;
30 import org.onap.policy.apex.service.parameters.eventhandler.EventHandlerParameters;
31 import org.slf4j.ext.XLogger;
32 import org.slf4j.ext.XLoggerFactory;
33
34 /**
35  * This class initiates Apex as a complete service from the command line.
36  *
37  * @author Liam Fallon (liam.fallon@ericsson.com)
38  */
39 public class ApexMain {
40     private static final XLogger LOGGER = XLoggerFactory.getXLogger(ApexMain.class);
41
42     // The Apex Activator that activates the Apex engine
43     private ApexActivator activator;
44
45     // The parameters read in from JSON
46     private ApexParameters parameters;
47
48     /**
49      * Instantiates the Apex service.
50      *
51      * @param args the commaind line arguments
52      */
53     public ApexMain(final String[] args) {
54         LOGGER.entry("Starting Apex service with parameters " + Arrays.toString(args) + " . . .");
55
56         // Check the arguments
57         final ApexCommandLineArguments arguments = new ApexCommandLineArguments();
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.info(argumentMessage);
63                 return;
64             }
65
66             // Validate that the arguments are sane
67             arguments.validate();
68         } catch (final ApexException e) {
69             LOGGER.error("start of Apex service failed", e);
70             return;
71         }
72
73         // Read the parameters
74         try {
75             parameters = new ApexParameterHandler().getParameters(arguments);
76         } catch (final Exception e) {
77             LOGGER.error("start of Apex service failed", e);
78             return;
79         }
80
81         // Set incoming Java properties
82         setJavaProperties(parameters);
83
84         // Set the name of the event handler parameters for producers and consumers
85         for (final Entry<String, EventHandlerParameters> ehParameterEntry : parameters.getEventOutputParameters()
86             .entrySet()) {
87             if (!ehParameterEntry.getValue().checkSetName()) {
88                 ehParameterEntry.getValue().setName(ehParameterEntry.getKey());
89             }
90         }
91         for (final Entry<String, EventHandlerParameters> ehParameterEntry : parameters.getEventInputParameters()
92             .entrySet()) {
93             if (!ehParameterEntry.getValue().checkSetName()) {
94                 ehParameterEntry.getValue().setName(ehParameterEntry.getKey());
95             }
96         }
97
98         // Now, create the activator for the Apex service
99         activator = new ApexActivator(parameters);
100
101         // Start the activator
102         try {
103             activator.initialize();
104         } catch (final ApexActivatorException e) {
105             LOGGER.error("start of Apex service failed, used parameters are " + Arrays.toString(args), e);
106             return;
107         }
108
109         // Add a shutdown hook to shut everything down in an orderly manner
110         Runtime.getRuntime().addShutdownHook(new ApexMainShutdownHookClass());
111         LOGGER.exit("Started Apex");
112     }
113
114     /**
115      * Get the parameters specified in JSON.
116      *
117      * @return the parameters
118      */
119     public ApexParameters getParameters() {
120         return parameters;
121     }
122
123     /**
124      * Shut down Execution.
125      *
126      * @throws ApexException on shutdown errors
127      */
128     public void shutdown() throws ApexException {
129         if (activator != null) {
130             activator.terminate();
131         }
132     }
133
134     /**
135      * The Class ApexMainShutdownHookClass terminates the Apex engine for the Apex service when its run method is
136      * called.
137      */
138     private class ApexMainShutdownHookClass extends Thread {
139         /*
140          * (non-Javadoc)
141          *
142          * @see java.lang.Runnable#run()
143          */
144         @Override
145         public void run() {
146             try {
147                 // Shutdown the Apex engine and wait for everything to stop
148                 activator.terminate();
149             } catch (final ApexException e) {
150                 LOGGER.warn("error occured during shut down of the Apex service", e);
151             }
152         }
153     }
154
155     /**
156      * Set the Java properties specified in the parameters.
157      *
158      * @param parameters The incoming parameters
159      */
160     private void setJavaProperties(final ApexParameters parameters) {
161         if (!parameters.checkJavaPropertiesSet()) {
162             return;
163         }
164
165         // Set each Java property
166         for (String[] javaProperty : parameters.getJavaProperties()) {
167             String javaPropertyName = javaProperty[0];
168             String javaPropertyValue = javaProperty[1];
169
170             // Passwords are encoded using base64, better than sending passwords in the clear
171             if (javaPropertyName.toLowerCase().contains("password")) {
172                 javaPropertyValue = new String(Base64.getDecoder().decode(javaPropertyValue.getBytes()));
173             }
174
175             // Set the Java property
176             System.setProperty(javaPropertyName, javaPropertyValue);
177         }
178     }
179
180     /**
181      * The main method.
182      *
183      * @param args the arguments
184      */
185     public static void main(final String[] args) {
186         new ApexMain(args);
187     }
188 }