More bug fix and refactoring
[dmaap/datarouter.git] / datarouter-prov / src / main / java / org / onap / dmaap / datarouter / provisioning / ProvRunner.java
1 /*******************************************************************************
2  * ============LICENSE_START==================================================
3  * * org.onap.dmaap
4  * * ===========================================================================
5  * * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
6  * * ===========================================================================
7  * * Licensed under the Apache License, Version 2.0 (the "License");
8  * * you may not use this file except in compliance with the License.
9  * * You may obtain a copy of the License at
10  * *
11  *  *      http://www.apache.org/licenses/LICENSE-2.0
12  * *
13  *  * Unless required by applicable law or agreed to in writing, software
14  * * distributed under the License is distributed on an "AS IS" BASIS,
15  * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * * See the License for the specific language governing permissions and
17  * * limitations under the License.
18  * * ============LICENSE_END====================================================
19  * *
20  * * ECOMP is a trademark and service mark of AT&T Intellectual Property.
21  * *
22  ******************************************************************************/
23
24
25 package org.onap.dmaap.datarouter.provisioning;
26
27 import static java.lang.System.exit;
28 import static java.lang.System.getProperty;
29
30 import com.att.eelf.configuration.EELFLogger;
31 import com.att.eelf.configuration.EELFManager;
32 import java.io.File;
33 import java.io.FileInputStream;
34 import java.io.IOException;
35 import java.util.Properties;
36 import java.util.Timer;
37 import org.eclipse.jetty.server.Server;
38 import org.onap.dmaap.datarouter.provisioning.utils.AafPropsUtils;
39 import org.onap.dmaap.datarouter.provisioning.utils.LogfileLoader;
40 import org.onap.dmaap.datarouter.provisioning.utils.Poker;
41 import org.onap.dmaap.datarouter.provisioning.utils.ProvDbUtils;
42 import org.onap.dmaap.datarouter.provisioning.utils.PurgeLogDirTask;
43 import org.onap.dmaap.datarouter.provisioning.utils.SynchronizerTask;
44
45 /**
46  * <p>
47  * A main class which may be used to start the provisioning server with an "embedded" Jetty server. Configuration is
48  * done via the properties file <i>provserver.properties</i>, which should be in the CLASSPATH. The provisioning server
49  * may also be packaged with a web.xml and started as a traditional webapp.
50  * </p>
51  * <p>
52  * Most of the work of the provisioning server is carried out within the eight servlets (configured below) that are used
53  * to handle each of the eight types of requests the server may receive. In addition, there are background threads
54  * started to perform other tasks:
55  * </p>
56  * <ul>
57  * <li>One background Thread runs the {@link LogfileLoader} in order to process incoming logfiles.
58  * This Thread is created as a side effect of the first successful POST to the /internal/logs/ servlet.</li>
59  * <li>One background Thread runs the {@link SynchronizerTask} which is used to periodically
60  * synchronize the database between active and standby servers.</li>
61  * <li>One background Thread runs the {@link Poker} which is used to notify the nodes whenever
62  * provisioning data changes.</li>
63  * <li>One task is run once a day to run {@link PurgeLogDirTask} which purges older logs from the
64  * /opt/app/datartr/logs directory.</li>
65  * </ul>
66  * <p>
67  * The provisioning server is stopped by issuing a GET to the URL http://127.0.0.1/internal/halt using <i>curl</i> or
68  * some other such tool.
69  * </p>
70  *
71  * @author Robert Eby
72  * @version $Id: Main.java,v 1.12 2014/03/12 19:45:41 eby Exp $
73  */
74 public class ProvRunner {
75
76     public static final EELFLogger intlogger = EELFManager.getInstance()
77                                                        .getLogger("org.onap.dmaap.datarouter.provisioning.internal");
78
79     private static Server provServer;
80     private static AafPropsUtils aafPropsUtils;
81     private static Properties provProperties;
82
83     /**
84      * Starts the Data Router Provisioning server.
85      *
86      * @param args not used
87      */
88     public static void main(String[] args) {
89         // Check DB is accessible and contains the expected tables
90         if (!ProvDbUtils.getInstance().initProvDB()) {
91             intlogger.error("Data Router Provisioning database init failure. Exiting.");
92             exit(1);
93         }
94         // Set up AAF properties
95         try {
96             aafPropsUtils = new AafPropsUtils(new File(getProvProperties().getProperty(
97                 "org.onap.dmaap.datarouter.provserver.aafprops.path",
98                 "/opt/app/osaaf/local/org.onap.dmaap-dr.props")));
99         } catch (IOException e) {
100             intlogger.error("NODE0314 Failed to load AAF props. Exiting", e);
101             exit(1);
102         }
103         // Daemon to clean up the log directory on a daily basis
104         Timer rolex = new Timer();
105         rolex.scheduleAtFixedRate(new PurgeLogDirTask(), 0, 86400000L);    // run once per day
106
107         try {
108             // Create and start the Jetty server
109             provServer = ProvServer.getServerInstance();
110             intlogger.info("PROV0000 **** DMaaP Data Router Provisioning Server starting....");
111             provServer.start();
112             provServer.dumpStdErr();
113             provServer.join();
114             intlogger.info("PROV0000 **** DMaaP Data Router Provisioning Server started: " + provServer.getState());
115         } catch (Exception e) {
116             intlogger.error(
117                 "PROV0010 **** DMaaP Data Router Provisioning Server failed to start. Exiting: " + e.getMessage(), e);
118             exit(1);
119         }
120         // Start LogfileLoader
121         LogfileLoader.getLoader();
122     }
123
124     /**
125      * Stop the Jetty server.
126      */
127     static void shutdown() {
128         new Thread(() -> {
129             try {
130                 provServer.stop();
131                 Thread.sleep(5000L);
132                 exit(0);
133             } catch (Exception e) {
134                 intlogger.error("Exception in Main.shutdown(): " + e.getMessage(), e);
135             }
136         });
137     }
138
139     public static Properties getProvProperties() {
140         if (provProperties == null) {
141             try {
142                 provProperties = new Properties();
143                 provProperties.load(new FileInputStream(getProperty(
144                     "org.onap.dmaap.datarouter.provserver.properties",
145                     "/opt/app/datartr/etc/provserver.properties")));
146             } catch (IOException e) {
147                 intlogger.error("Failed to load PROV properties: " + e.getMessage(), e);
148                 exit(1);
149             }
150         }
151         return provProperties;
152     }
153
154     public static AafPropsUtils getAafPropsUtils() {
155         return aafPropsUtils;
156     }
157 }