update link to upper-constraints.txt
[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.FileInputStream;
33 import java.io.IOException;
34 import java.util.Properties;
35 import java.util.Timer;
36 import org.eclipse.jetty.server.Server;
37 import org.onap.dmaap.datarouter.provisioning.utils.LogfileLoader;
38 import org.onap.dmaap.datarouter.provisioning.utils.Poker;
39 import org.onap.dmaap.datarouter.provisioning.utils.ProvDbUtils;
40 import org.onap.dmaap.datarouter.provisioning.utils.ProvTlsManager;
41 import org.onap.dmaap.datarouter.provisioning.utils.PurgeLogDirTask;
42 import org.onap.dmaap.datarouter.provisioning.utils.SynchronizerTask;
43
44 /**
45  * <p>
46  * A main class which may be used to start the provisioning server with an "embedded" Jetty server. Configuration is
47  * done via the properties file <i>provserver.properties</i>, which should be in the CLASSPATH. The provisioning server
48  * may also be packaged with a web.xml and started as a traditional webapp.
49  * </p>
50  * <p>
51  * Most of the work of the provisioning server is carried out within the eight servlets (configured below) that are used
52  * to handle each of the eight types of requests the server may receive. In addition, there are background threads
53  * started to perform other tasks:
54  * </p>
55  * <ul>
56  * <li>One background Thread runs the {@link LogfileLoader} in order to process incoming logfiles.
57  * This Thread is created as a side effect of the first successful POST to the /internal/logs/ servlet.</li>
58  * <li>One background Thread runs the {@link SynchronizerTask} which is used to periodically
59  * synchronize the database between active and standby servers.</li>
60  * <li>One background Thread runs the {@link Poker} which is used to notify the nodes whenever
61  * provisioning data changes.</li>
62  * <li>One task is run once a day to run {@link PurgeLogDirTask} which purges older logs from the
63  * /opt/app/datartr/logs directory.</li>
64  * </ul>
65  * <p>
66  * The provisioning server is stopped by issuing a GET to the URL http://127.0.0.1/internal/halt using <i>curl</i> or
67  * some other such tool.
68  * </p>
69  *
70  * @author Robert Eby
71  * @version $Id: Main.java,v 1.12 2014/03/12 19:45:41 eby Exp $
72  */
73 public class ProvRunner {
74
75     public static final EELFLogger intlogger = EELFManager.getInstance().getLogger("org.onap.dmaap.datarouter.provisioning.internal");
76
77     private static Server provServer;
78     private static ProvTlsManager provTlsManager;
79     private static Properties provProperties;
80     private static Boolean tlsEnabled;
81
82     /**
83      * Starts the Data Router Provisioning server.
84      *
85      * @param args not used
86      */
87     public static void main(String[] args) {
88         // Check DB is accessible and contains the expected tables
89         if (!ProvDbUtils.getInstance().initProvDB()) {
90             intlogger.error("Data Router Provisioning database init failure. Exiting.");
91             exit(1);
92         }
93         if (Boolean.TRUE.equals(getTlsEnabled())) {
94             // Set up TLS Manager
95             try {
96                 provTlsManager = new ProvTlsManager(ProvRunner.getProvProperties(), true);
97             } catch (Exception e) {
98                 intlogger.error("NODE0314 Failed to load TLS config. Exiting", e);
99                 exit(1);
100             }
101         }
102         // Daemon to clean up the log directory on a daily basis
103         Timer rolex = new Timer();
104         rolex.scheduleAtFixedRate(new PurgeLogDirTask(), 0, 86400000L);    // run once per day
105
106         try {
107             // Create and start the Jetty server
108             provServer = ProvServer.getServerInstance();
109             intlogger.info("PROV0000 **** DMaaP Data Router Provisioning Server starting....");
110             provServer.start();
111             provServer.dumpStdErr();
112             provServer.join();
113             intlogger.info("PROV0000 **** DMaaP Data Router Provisioning Server started: " + provServer.getState());
114         } catch (Exception e) {
115             intlogger.error(
116                 "PROV0010 **** DMaaP Data Router Provisioning Server failed to start. Exiting: " + e.getMessage(), e);
117             exit(1);
118         }
119         // Start LogfileLoader
120         LogfileLoader.getLoader();
121     }
122
123     /**
124      * Stop the Jetty server.
125      */
126     static void shutdown() {
127         new Thread(() -> {
128             try {
129                 provServer.stop();
130                 Thread.sleep(5000L);
131                 exit(0);
132             } catch (Exception e) {
133                 intlogger.error("Exception in Main.shutdown(): " + e.getMessage(), e);
134             }
135         });
136     }
137
138     public static Properties getProvProperties() {
139         if (provProperties == null) {
140             try (FileInputStream props = new FileInputStream(getProperty(
141                 "org.onap.dmaap.datarouter.provserver.properties",
142                 "/opt/app/datartr/etc/provserver.properties"))) {
143                 provProperties = new Properties();
144                 provProperties.load(props);
145             } catch (IOException e) {
146                 intlogger.error("Failed to load PROV properties: " + e.getMessage(), e);
147                 exit(1);
148             }
149         }
150         return provProperties;
151     }
152
153     public static Boolean getTlsEnabled() {
154         if (tlsEnabled == null) {
155             tlsEnabled = Boolean.parseBoolean(getProvProperties()
156                 .getProperty("org.onap.dmaap.datarouter.provserver.tlsenabled", "true"));
157         }
158         return tlsEnabled;
159     }
160
161     public static ProvTlsManager getProvTlsManager() {
162         return provTlsManager;
163     }
164 }