Merge "rephrase sentence"
[dmaap/datarouter.git] / datarouter-node / src / main / java / org / onap / dmaap / datarouter / node / NodeMain.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 package org.onap.dmaap.datarouter.node;
25
26 import java.util.Arrays;
27 import org.apache.log4j.Logger;
28 import org.eclipse.jetty.http.HttpVersion;
29 import org.eclipse.jetty.server.Connector;
30 import org.eclipse.jetty.server.HttpConfiguration;
31 import org.eclipse.jetty.server.HttpConnectionFactory;
32 import org.eclipse.jetty.server.SecureRequestCustomizer;
33 import org.eclipse.jetty.server.Server;
34 import org.eclipse.jetty.server.ServerConnector;
35 import org.eclipse.jetty.server.SslConnectionFactory;
36 import org.eclipse.jetty.servlet.ServletContextHandler;
37 import org.eclipse.jetty.servlet.ServletHolder;
38 import org.eclipse.jetty.util.ssl.SslContextFactory;
39
40 /**
41  * The main starting point for the Data Router node
42  */
43 public class NodeMain {
44
45     private NodeMain() {
46     }
47
48     private static Logger nodeMainLogger = Logger.getLogger("org.onap.dmaap.datarouter.node.NodeMain");
49
50     private static class WaitForConfig implements Runnable {
51
52         private NodeConfigManager localNodeConfigManager;
53
54         WaitForConfig(NodeConfigManager ncm) {
55             this.localNodeConfigManager = ncm;
56         }
57
58         public synchronized void run() {
59             notify();
60         }
61
62         synchronized void waitForConfig() {
63             localNodeConfigManager.registerConfigTask(this);
64             while (!localNodeConfigManager.isConfigured()) {
65                 nodeMainLogger.info("NODE0003 Waiting for Node Configuration");
66                 try {
67                     wait();
68                 } catch (Exception exception) {
69                     nodeMainLogger
70                         .debug("NodeMain: waitForConfig exception. Exception Message:- " + exception.toString(),
71                             exception);
72                 }
73             }
74             localNodeConfigManager.deregisterConfigTask(this);
75             nodeMainLogger.info("NODE0004 Node Configuration Data Received");
76         }
77     }
78
79     private static Delivery delivery;
80     private static NodeConfigManager nodeConfigManager;
81
82     /**
83      * Reset the retry timer for a subscription
84      */
85     static void resetQueue(String subid, String ip) {
86         delivery.resetQueue(nodeConfigManager.getSpoolDir(subid, ip));
87     }
88
89     /**
90      * Start the data router.
91      * <p>
92      * The location of the node configuration file can be set using the org.onap.dmaap.datarouter.node.ConfigFile system
93      * property.  By default, it is "etc/node.properties".
94      */
95     public static void main(String[] args) throws Exception {
96         nodeMainLogger.info("NODE0001 Data Router Node Starting");
97         IsFrom.setDNSCache();
98         nodeConfigManager = NodeConfigManager.getInstance();
99         nodeMainLogger.info("NODE0002 I am " + nodeConfigManager.getMyName());
100         (new WaitForConfig(nodeConfigManager)).waitForConfig();
101         delivery = new Delivery(nodeConfigManager);
102         new LogManager(nodeConfigManager);
103         Server server = new Server();
104         // HTTP configuration
105         HttpConfiguration httpConfiguration = new HttpConfiguration();
106         httpConfiguration.setRequestHeaderSize(2048);
107
108         // HTTP connector
109         ServletContextHandler ctxt;
110         try (ServerConnector httpServerConnector = new ServerConnector(server,
111             new HttpConnectionFactory(httpConfiguration))) {
112             httpServerConnector.setPort(nodeConfigManager.getHttpPort());
113             httpServerConnector.setIdleTimeout(2000);
114
115             // HTTPS configuration
116             SslContextFactory sslContextFactory = new SslContextFactory();
117             sslContextFactory.setKeyStoreType(nodeConfigManager.getKSType());
118             sslContextFactory.setKeyStorePath(nodeConfigManager.getKSFile());
119             sslContextFactory.setKeyStorePassword(nodeConfigManager.getKSPass());
120             sslContextFactory.setKeyManagerPassword(nodeConfigManager.getKPass());
121             /* Skip SSLv3 Fixes */
122             sslContextFactory.addExcludeProtocols("SSLv3");
123             nodeMainLogger.info("Excluded protocols node-" + Arrays.toString(sslContextFactory.getExcludeProtocols()));
124             /* End of SSLv3 Fixes */
125
126             HttpConfiguration httpsConfiguration = new HttpConfiguration(httpConfiguration);
127             httpsConfiguration.setRequestHeaderSize(8192);
128
129             SecureRequestCustomizer secureRequestCustomizer = new SecureRequestCustomizer();
130             secureRequestCustomizer.setStsMaxAge(2000);
131             secureRequestCustomizer.setStsIncludeSubDomains(true);
132             httpsConfiguration.addCustomizer(secureRequestCustomizer);
133
134             // HTTPS connector
135             try (ServerConnector httpsServerConnector = new ServerConnector(server,
136                 new SslConnectionFactory(sslContextFactory, HttpVersion.HTTP_1_1.asString()),
137                 new HttpConnectionFactory(httpsConfiguration))) {
138                 httpsServerConnector.setPort(nodeConfigManager.getHttpsPort());
139                 httpsServerConnector.setIdleTimeout(500000);
140                 httpsServerConnector.setAcceptQueueSize(2);
141
142                 server.setConnectors(new Connector[]{httpServerConnector, httpsServerConnector});
143             }
144         }
145         ctxt = new ServletContextHandler(0);
146         ctxt.setContextPath("/");
147         server.setHandler(ctxt);
148         ctxt.addServlet(new ServletHolder(new NodeServlet()), "/*");
149         nodeMainLogger.info("NODE0005 Data Router Node Activating Service");
150         server.start();
151         server.join();
152     }
153 }