1 /*******************************************************************************
2 * ============LICENSE_START==================================================
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
11 * * http://www.apache.org/licenses/LICENSE-2.0
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====================================================
20 * * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22 ******************************************************************************/
25 package org.onap.dmaap.datarouter.provisioning;
27 import java.security.*;
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;
54 import javax.servlet.DispatcherType;
58 * A main class which may be used to start the provisioning server with an "embedded" Jetty server. Configuration is
59 * done via the properties file <i>provserver.properties</i>, which should be in the CLASSPATH. The provisioning server
60 * may also be packaged with a web.xml and started as a traditional webapp.
63 * Most of the work of the provisioning server is carried out within the eight servlets (configured below) that are used
64 * to handle each of the eight types of requests the server may receive. In addition, there are background threads
65 * started to perform other tasks:
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>
78 * The provisioning server is stopped by issuing a GET to the URL http://127.0.0.1/internal/halt using <i>curl</i> or
79 * some other such tool.
83 * @version $Id: Main.java,v 1.12 2014/03/12 19:45:41 eby Exp $
88 * The truststore to use if none is specified
90 public static final String DEFAULT_TRUSTSTORE = "/opt/java/jdk/jdk180/jre/lib/security/cacerts";
91 public static final String KEYSTORE_TYPE_PROPERTY = "org.onap.dmaap.datarouter.provserver.keystore.type";
92 public static final String KEYSTORE_PATH_PROPERTY = "org.onap.dmaap.datarouter.provserver.keystore.path";
93 public static final String KEYSTORE_PASS_PROPERTY = "org.onap.dmaap.datarouter.provserver.keystore.password";
94 public static final String TRUSTSTORE_PATH_PROPERTY = "org.onap.dmaap.datarouter.provserver.truststore.path";
95 public static final String TRUSTSTORE_PASS_PROPERTY = "org.onap.dmaap.datarouter.provserver.truststore.password";
98 * The one and only {@link Server} instance in this JVM
100 private static Server server;
103 * Starts the Data Router Provisioning server.
105 * @param args not used
106 * @throws Exception if Jetty has a problem starting
108 public static void main(String[] args) throws Exception {
109 Security.setProperty("networkaddress.cache.ttl", "4");
110 Logger logger = Logger.getLogger("org.onap.dmaap.datarouter.provisioning.internal");
112 // Check DB is accessible and contains the expected tables
113 if (!checkDatabase()) {
117 logger.info("PROV0000 **** AT&T Data Router Provisioning Server starting....");
120 Properties p = (new DB()).getProperties();
121 int httpPort = Integer.parseInt(p.getProperty("org.onap.dmaap.datarouter.provserver.http.port", "8080"));
122 int httpsPort = Integer.parseInt(p.getProperty("org.onap.dmaap.datarouter.provserver.https.port", "8443"));
124 // HTTP configuration
125 HttpConfiguration httpConfiguration = new HttpConfiguration();
126 httpConfiguration.setSecureScheme("https");
127 httpConfiguration.setSecurePort(httpsPort);
128 httpConfiguration.setOutputBufferSize(32768);
129 httpConfiguration.setRequestHeaderSize(2048);
130 httpConfiguration.setSendServerVersion(true);
131 httpConfiguration.setSendDateHeader(false);
133 // Server's thread pool
134 QueuedThreadPool queuedThreadPool = new QueuedThreadPool();
135 queuedThreadPool.setMinThreads(10);
136 queuedThreadPool.setMaxThreads(200);
137 queuedThreadPool.setDetailedDump(false);
140 server = new Server(queuedThreadPool);
143 HandlerCollection hc;
144 try (ServerConnector httpServerConnector = new ServerConnector(server,
145 new HttpConnectionFactory(httpConfiguration))) {
146 httpServerConnector.setPort(httpPort);
147 httpServerConnector.setAcceptQueueSize(2);
148 httpServerConnector.setIdleTimeout(300000);
150 // HTTPS configuration
151 HttpConfiguration httpsConfiguration = new HttpConfiguration(httpConfiguration);
152 httpsConfiguration.setRequestHeaderSize(8192);
155 SslContextFactory sslContextFactory = new SslContextFactory();
156 sslContextFactory.setKeyStorePath(p.getProperty(KEYSTORE_PATH_PROPERTY));
157 sslContextFactory.setKeyStorePassword(p.getProperty(KEYSTORE_PASS_PROPERTY));
159 .setKeyManagerPassword(p.getProperty("org.onap.dmaap.datarouter.provserver.keymanager.password"));
161 /* Skip SSLv3 Fixes */
162 sslContextFactory.addExcludeProtocols("SSLv3");
163 logger.info("Excluded protocols prov-" + Arrays.toString(sslContextFactory.getExcludeProtocols()));
164 /* End of SSLv3 Fixes */
166 try (ServerConnector httpsServerConnector = new ServerConnector(server,
167 new SslConnectionFactory(sslContextFactory, HttpVersion.HTTP_1_1.asString()),
168 new HttpConnectionFactory(httpsConfiguration))) {
169 httpsServerConnector.setPort(httpsPort);
170 httpsServerConnector.setIdleTimeout(30000);
171 httpsServerConnector.setAcceptQueueSize(2);
173 sslContextFactory.setKeyStoreType(p.getProperty(KEYSTORE_TYPE_PROPERTY, "jks"));
174 sslContextFactory.setKeyStorePath(p.getProperty(KEYSTORE_PATH_PROPERTY));
175 sslContextFactory.setKeyStorePassword(p.getProperty(KEYSTORE_PASS_PROPERTY));
177 .setKeyManagerPassword(p.getProperty("org.onap.dmaap.datarouter.provserver.keymanager.password"));
179 String ts = p.getProperty(TRUSTSTORE_PATH_PROPERTY);
180 if (ts != null && ts.length() > 0) {
181 logger.info("@@ TS -> " + ts);
182 sslContextFactory.setTrustStorePath(ts);
183 sslContextFactory.setTrustStorePassword(p.getProperty(TRUSTSTORE_PASS_PROPERTY));
185 sslContextFactory.setTrustStorePath(DEFAULT_TRUSTSTORE);
186 sslContextFactory.setTrustStorePassword("changeit");
188 sslContextFactory.setWantClientAuth(true);
190 // Servlet and Filter configuration
191 ServletContextHandler ctxt = new ServletContextHandler(0);
192 ctxt.setContextPath("/");
193 ctxt.addServlet(new ServletHolder(new FeedServlet()), "/feed/*");
194 ctxt.addServlet(new ServletHolder(new FeedLogServlet()), "/feedlog/*");
195 ctxt.addServlet(new ServletHolder(new PublishServlet()), "/publish/*");
196 ctxt.addServlet(new ServletHolder(new SubscribeServlet()), "/subscribe/*");
197 ctxt.addServlet(new ServletHolder(new StatisticsServlet()), "/statistics/*");
198 ctxt.addServlet(new ServletHolder(new SubLogServlet()), "/sublog/*");
199 ctxt.addServlet(new ServletHolder(new GroupServlet()),
200 "/group/*"); //Provision groups - Rally US708115 -1610
201 ctxt.addServlet(new ServletHolder(new SubscriptionServlet()), "/subs/*");
202 ctxt.addServlet(new ServletHolder(new InternalServlet()), "/internal/*");
203 ctxt.addServlet(new ServletHolder(new RouteServlet()), "/internal/route/*");
204 ctxt.addServlet(new ServletHolder(new DRFeedsServlet()), "/");
205 ctxt.addFilter(new FilterHolder(new ThrottleFilter()), "/publish/*", EnumSet.of(DispatcherType.REQUEST));
207 ContextHandlerCollection contexts = new ContextHandlerCollection();
208 contexts.addHandler(ctxt);
210 // Request log configuration
211 NCSARequestLog nrl = new NCSARequestLog();
213 p.getProperty("org.onap.dmaap.datarouter.provserver.accesslog.dir") + "/request.log.yyyy_mm_dd");
214 nrl.setFilenameDateFormat("yyyyMMdd");
215 nrl.setRetainDays(90);
217 nrl.setExtended(false);
218 nrl.setLogCookies(false);
219 nrl.setLogTimeZone("GMT");
221 RequestLogHandler reqlog = new RequestLogHandler();
222 reqlog.setRequestLog(nrl);
224 // Server's Handler collection
225 hc = new HandlerCollection();
226 hc.setHandlers(new Handler[]{contexts, new DefaultHandler()});
227 hc.addHandler(reqlog);
229 // Daemon to clean up the log directory on a daily basis
230 Timer rolex = new Timer();
231 rolex.scheduleAtFixedRate(new PurgeLogDirTask(), 0, 86400000L); // run once per day
233 // Start LogfileLoader
234 LogfileLoader.getLoader();
236 try (ServerConnector serverConnector = new ServerConnector(server,
237 new SslConnectionFactory(sslContextFactory, HttpVersion.HTTP_1_1.asString()),
238 new HttpConnectionFactory(httpsConfiguration))) {
239 serverConnector.setPort(httpsPort);
240 serverConnector.setIdleTimeout(500000);
243 server.setConnectors(new Connector[]{httpServerConnector, httpsServerConnector});
246 server.setHandler(hc);
247 server.setStopAtShutdown(true);
248 server.setStopTimeout(5000);
250 server.setDumpAfterStart(false);
251 server.setDumpBeforeStop(false);
255 logger.info("PROV0001 **** AT&T Data Router Provisioning Server halted.");
258 private static boolean checkDatabase() {
260 return db.runRetroFits();
264 * Stop the Jetty server.
266 static void shutdown() {
272 } catch (Exception e) {