remove the policy and security issue dependencies
[dmaap/datarouter.git] / datarouter-prov / src / main / java / org / onap / dmaap / datarouter / provisioning / Main.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 java.security.*;
28 import java.util.*;
29
30 import org.apache.log4j.Logger;
31 import org.eclipse.jetty.http.HttpVersion;
32 import org.eclipse.jetty.server.Connector;
33 import org.eclipse.jetty.server.Handler;
34 import org.eclipse.jetty.server.HttpConfiguration;
35 import org.eclipse.jetty.server.HttpConnectionFactory;
36 import org.eclipse.jetty.server.NCSARequestLog;
37 import org.eclipse.jetty.server.Server;
38 import org.eclipse.jetty.server.ServerConnector;
39 import org.eclipse.jetty.server.handler.ContextHandlerCollection;
40 import org.eclipse.jetty.server.handler.DefaultHandler;
41 import org.eclipse.jetty.server.handler.HandlerCollection;
42 import org.eclipse.jetty.server.handler.RequestLogHandler;
43 import org.eclipse.jetty.server.SslConnectionFactory;
44 import org.eclipse.jetty.util.ssl.SslContextFactory;
45 import org.eclipse.jetty.servlet.FilterHolder;
46 import org.eclipse.jetty.servlet.ServletContextHandler;
47 import org.eclipse.jetty.servlet.ServletHolder;
48 import org.eclipse.jetty.util.thread.QueuedThreadPool;
49 import org.onap.dmaap.datarouter.provisioning.utils.DB;
50 import org.onap.dmaap.datarouter.provisioning.utils.LogfileLoader;
51 import org.onap.dmaap.datarouter.provisioning.utils.PurgeLogDirTask;
52 import org.onap.dmaap.datarouter.provisioning.utils.ThrottleFilter;
53
54 import javax.servlet.DispatcherType;
55
56 /**
57  * <p>
58  * A main class which may be used to start the provisioning server with an "embedded" Jetty server.
59  * Configuration is done via the properties file <i>provserver.properties</i>, which should be in the CLASSPATH.
60  * The provisioning server may also be packaged with a web.xml and started as a traditional webapp.
61  * </p>
62  * <p>
63  * Most of the work of the provisioning server is carried out within the eight servlets (configured below)
64  * that are used to handle each of the eight types of requests the server may receive.
65  * In addition, there are background threads started to perform other tasks:
66  * </p>
67  * <ul>
68  * <li>One background Thread runs the {@link LogfileLoader} in order to process incoming logfiles.
69  * This Thread is created as a side effect of the first successful POST to the /internal/logs/ servlet.</li>
70  * <li>One background Thread runs the {@link SynchronizerTask} which is used to periodically
71  * synchronize the database between active and standby servers.</li>
72  * <li>One background Thread runs the {@link Poker} which is used to notify the nodes whenever
73  * provisioning data changes.</li>
74  * <li>One task is run once a day to run {@link PurgeLogDirTask} which purges older logs from the
75  * /opt/app/datartr/logs directory.</li>
76  * </ul>
77  * <p>
78  * The provisioning server is stopped by issuing a GET to the URL http://127.0.0.1/internal/halt
79  * using <i>curl</i> or some other such tool.
80  * </p>
81  *
82  * @author Robert Eby
83  * @version $Id: Main.java,v 1.12 2014/03/12 19:45:41 eby Exp $
84  */
85 public class Main {
86     /**
87      * The truststore to use if none is specified
88      */
89     public static final String DEFAULT_TRUSTSTORE = "/opt/java/jdk/jdk180/jre/lib/security/cacerts";
90     public static final String KEYSTORE_TYPE_PROPERTY = "org.onap.dmaap.datarouter.provserver.keystore.type";
91     public static final String KEYSTORE_PATH_PROPERTY = "org.onap.dmaap.datarouter.provserver.keystore.path";
92     public static final String KEYSTORE_PASSWORD_PROPERTY = "org.onap.dmaap.datarouter.provserver.keystore.password";
93     public static final String TRUSTSTORE_PATH_PROPERTY = "org.onap.dmaap.datarouter.provserver.truststore.path";
94     public static final String TRUSTSTORE_PASSWORD_PROPERTY = "org.onap.dmaap.datarouter.provserver.truststore.password";
95
96     /**
97      * The one and only {@link Server} instance in this JVM
98      */
99     private static Server server;
100
101     /**
102      * Starts the Data Router Provisioning server.
103      *
104      * @param args not used
105      * @throws Exception if Jetty has a problem starting
106      */
107     public static void main(String[] args) throws Exception {
108         Security.setProperty("networkaddress.cache.ttl", "4");
109         Logger logger = Logger.getLogger("org.onap.dmaap.datarouter.provisioning.internal");
110
111         // Check DB is accessible and contains the expected tables
112         if (!checkDatabase(logger))
113             System.exit(1);
114
115         logger.info("PROV0000 **** AT&T Data Router Provisioning Server starting....");
116
117         // Get properties
118         Properties p = (new DB()).getProperties();
119         int http_port = Integer.parseInt(p.getProperty("org.onap.dmaap.datarouter.provserver.http.port", "8080"));
120         int https_port = Integer.parseInt(p.getProperty("org.onap.dmaap.datarouter.provserver.https.port", "8443"));
121
122         // HTTP connector
123         HttpConfiguration http_config = new HttpConfiguration();
124         http_config.setSecureScheme("https");
125         http_config.setSecurePort(https_port);
126         http_config.setOutputBufferSize(32768);
127         http_config.setRequestHeaderSize(2048);
128         http_config.setIdleTimeout(300000);
129         http_config.setSendServerVersion(true);
130         http_config.setSendDateHeader(false);
131
132         ServerConnector http = new ServerConnector(server, new HttpConnectionFactory(http_config));
133         http.setPort(http_port);
134         http.setAcceptQueueSize(2);
135
136         // HTTPS config
137         HttpConfiguration https_config = new HttpConfiguration(http_config);
138         https_config.setRequestHeaderSize(8192);
139
140         // HTTPS connector
141         SslContextFactory sslContextFactory = new SslContextFactory();
142         sslContextFactory.setKeyStorePath(p.getProperty(KEYSTORE_PATH_PROPERTY));
143         sslContextFactory.setKeyStorePassword(p.getProperty(KEYSTORE_PASSWORD_PROPERTY));
144         sslContextFactory.setKeyManagerPassword(p.getProperty("org.onap.dmaap.datarouter.provserver.keymanager.password"));
145
146         ServerConnector https = new ServerConnector(server,
147                 new SslConnectionFactory(sslContextFactory,HttpVersion.HTTP_1_1.asString()),
148                 new HttpConnectionFactory(https_config));
149         https.setPort(https_port);
150         https.setIdleTimeout(30000);
151         https.setAcceptQueueSize(2);
152
153         // SSL stuff
154         /* Skip SSLv3 Fixes */
155         sslContextFactory.addExcludeProtocols("SSLv3");
156         logger.info("Excluded protocols prov-" + sslContextFactory.getExcludeProtocols());
157         /* End of SSLv3 Fixes */
158
159         sslContextFactory.setKeyStoreType(p.getProperty(KEYSTORE_TYPE_PROPERTY, "jks"));
160         sslContextFactory.setKeyStorePath(p.getProperty(KEYSTORE_PATH_PROPERTY));
161         sslContextFactory.setKeyStorePassword(p.getProperty(KEYSTORE_PASSWORD_PROPERTY));
162         sslContextFactory.setKeyManagerPassword(p.getProperty("org.onap.dmaap.datarouter.provserver.keymanager.password"));
163         String ts = p.getProperty(TRUSTSTORE_PATH_PROPERTY);
164         if (ts != null && ts.length() > 0) {
165             System.out.println("@@ TS -> " + ts);
166             sslContextFactory.setTrustStorePath(ts);
167             sslContextFactory.setTrustStorePassword(p.getProperty(TRUSTSTORE_PASSWORD_PROPERTY));
168         } else {
169             sslContextFactory.setTrustStorePath(DEFAULT_TRUSTSTORE);
170             sslContextFactory.setTrustStorePassword("changeit");
171         }
172         sslContextFactory.setTrustStorePath("/opt/app/datartr/self_signed/cacerts.jks");
173         sslContextFactory.setTrustStorePassword("changeit");
174         sslContextFactory.setWantClientAuth(true);
175
176         // Servlet and Filter configuration
177         ServletContextHandler ctxt = new ServletContextHandler(0);
178         ctxt.setContextPath("/");
179         ctxt.addServlet(new ServletHolder(new FeedServlet()), "/feed/*");
180         ctxt.addServlet(new ServletHolder(new FeedLogServlet()), "/feedlog/*");
181         ctxt.addServlet(new ServletHolder(new PublishServlet()), "/publish/*");
182         ctxt.addServlet(new ServletHolder(new SubscribeServlet()), "/subscribe/*");
183         ctxt.addServlet(new ServletHolder(new StatisticsServlet()), "/statistics/*");
184         ctxt.addServlet(new ServletHolder(new SubLogServlet()), "/sublog/*");
185         ctxt.addServlet(new ServletHolder(new GroupServlet()), "/group/*"); //Provision groups - Rally US708115 -1610
186         ctxt.addServlet(new ServletHolder(new SubscriptionServlet()), "/subs/*");
187         ctxt.addServlet(new ServletHolder(new InternalServlet()), "/internal/*");
188         ctxt.addServlet(new ServletHolder(new RouteServlet()), "/internal/route/*");
189         ctxt.addServlet(new ServletHolder(new DRFeedsServlet()), "/");
190         ctxt.addFilter(new FilterHolder(new ThrottleFilter()), "/publish/*", EnumSet.of(DispatcherType.REQUEST));
191
192         ContextHandlerCollection contexts = new ContextHandlerCollection();
193         contexts.addHandler(ctxt);
194
195         // Request log configuration
196         NCSARequestLog nrl = new NCSARequestLog();
197         nrl.setFilename(p.getProperty("org.onap.dmaap.datarouter.provserver.accesslog.dir") + "/request.log.yyyy_mm_dd");
198         nrl.setFilenameDateFormat("yyyyMMdd");
199         nrl.setRetainDays(90);
200         nrl.setAppend(true);
201         nrl.setExtended(false);
202         nrl.setLogCookies(false);
203         nrl.setLogTimeZone("GMT");
204
205         RequestLogHandler reqlog = new RequestLogHandler();
206         reqlog.setRequestLog(nrl);
207
208         // Server's Handler collection
209         HandlerCollection hc = new HandlerCollection();
210         hc.setHandlers(new Handler[]{contexts, new DefaultHandler()});
211         hc.addHandler(reqlog);
212
213         // Server's thread pool
214         QueuedThreadPool queuedThreadPool = new QueuedThreadPool();
215         queuedThreadPool.setMinThreads(10);
216         queuedThreadPool.setMaxThreads(200);
217         queuedThreadPool.setDetailedDump(false);
218
219         // Daemon to clean up the log directory on a daily basis
220         Timer rolex = new Timer();
221         rolex.scheduleAtFixedRate(new PurgeLogDirTask(), 0, 86400000L);    // run once per day
222
223         // Start LogfileLoader
224         LogfileLoader.getLoader();
225
226         // The server itself
227         server = new Server(queuedThreadPool);
228
229         ServerConnector serverConnector = new ServerConnector(server,
230                 new SslConnectionFactory(sslContextFactory,HttpVersion.HTTP_1_1.asString()),
231                 new HttpConnectionFactory(https_config));
232         serverConnector.setPort(https_port);
233         serverConnector.setIdleTimeout(500000);
234
235         server.setConnectors(new Connector[]{http, https});
236         server.setHandler(hc);
237         server.setStopAtShutdown(true);
238         server.setStopTimeout(5000);
239
240         server.setDumpAfterStart(false);
241         server.setDumpBeforeStop(false);
242
243         server.start();
244         server.join();
245         logger.info("PROV0001 **** AT&T Data Router Provisioning Server halted.");
246     }
247
248     private static boolean checkDatabase(Logger logger) {
249         DB db = new DB();
250         return db.runRetroFits();
251     }
252
253     /**
254      * Stop the Jetty server.
255      */
256     static void shutdown() {
257         new Thread(() -> {
258             try {
259                 server.stop();
260                 Thread.sleep(5000L);
261                 System.exit(0);
262             } catch (Exception e) {
263                 // ignore
264             }
265         });
266     }
267 }