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