Refactor Prov DB handling
[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 com.att.eelf.configuration.EELFLogger;
27 import com.att.eelf.configuration.EELFManager;
28 import java.util.EnumSet;
29 import javax.servlet.DispatcherType;
30 import org.eclipse.jetty.http.HttpVersion;
31 import org.eclipse.jetty.server.Connector;
32 import org.eclipse.jetty.server.HttpConfiguration;
33 import org.eclipse.jetty.server.HttpConnectionFactory;
34 import org.eclipse.jetty.server.SecureRequestCustomizer;
35 import org.eclipse.jetty.server.Server;
36 import org.eclipse.jetty.server.ServerConnector;
37 import org.eclipse.jetty.server.SslConnectionFactory;
38 import org.eclipse.jetty.servlet.FilterHolder;
39 import org.eclipse.jetty.servlet.ServletContextHandler;
40 import org.eclipse.jetty.servlet.ServletHolder;
41 import org.eclipse.jetty.util.ssl.SslContextFactory;
42
43 /**
44  * The main starting point for the Data Router node.
45  */
46 public class NodeMain {
47
48     private static EELFLogger nodeMainLogger = EELFManager.getInstance().getLogger(NodeMain.class);
49     private static Delivery delivery;
50     private static NodeConfigManager nodeConfigManager;
51
52     private NodeMain() {
53     }
54
55     /**
56      * Reset the retry timer for a subscription.
57      */
58
59     static void resetQueue(String subid, String ip) {
60         delivery.resetQueue(nodeConfigManager.getSpoolDir(subid, ip));
61     }
62
63     /**
64      * Start the data router.
65      *
66      * <p>The location of the node configuration file can be set using the org.onap.dmaap.datarouter.node.properties
67      * system property. By default, it is "/opt/app/datartr/etc/node.properties".
68      */
69     public static void main(String[] args) throws Exception {
70         nodeMainLogger.debug("NODE0001 Data Router Node Starting");
71         IsFrom.setDNSCache();
72         nodeConfigManager = NodeConfigManager.getInstance();
73         nodeMainLogger.debug("NODE0002 I am " + nodeConfigManager.getMyName());
74         (new WaitForConfig(nodeConfigManager)).waitForConfig();
75         delivery = new Delivery(nodeConfigManager);
76         new LogManager(nodeConfigManager);
77
78         Server server = new Server();
79
80         // HTTP configuration
81         HttpConfiguration httpConfiguration = new HttpConfiguration();
82         httpConfiguration.setRequestHeaderSize(2048);
83
84         // HTTP connector
85         try (ServerConnector httpServerConnector = new ServerConnector(server,
86                 new HttpConnectionFactory(httpConfiguration))) {
87             httpServerConnector.setPort(nodeConfigManager.getHttpPort());
88             httpServerConnector.setIdleTimeout(2000);
89
90             // HTTPS configuration
91             SslContextFactory sslContextFactory = new SslContextFactory();
92             sslContextFactory.setKeyStoreType(nodeConfigManager.getKSType());
93             sslContextFactory.setKeyStorePath(nodeConfigManager.getKSFile());
94             sslContextFactory.setKeyStorePassword(nodeConfigManager.getKSPass());
95             sslContextFactory.setKeyManagerPassword(nodeConfigManager.getKPass());
96
97             //SP-6: Fixes for SDV scan to exclude/remove DES/3DES
98             // ciphers are taken care by upgrading jdk in descriptor.xml
99             sslContextFactory.setExcludeCipherSuites(
100                     "SSL_RSA_WITH_DES_CBC_SHA",
101                     "SSL_DHE_RSA_WITH_DES_CBC_SHA",
102                     "SSL_DHE_DSS_WITH_DES_CBC_SHA",
103                     "SSL_RSA_EXPORT_WITH_RC4_40_MD5",
104                     "SSL_RSA_EXPORT_WITH_DES40_CBC_SHA",
105                     "SSL_DHE_RSA_EXPORT_WITH_DES40_CBC_SHA",
106                     "SSL_DHE_DSS_EXPORT_WITH_DES40_CBC_SHA"
107             );
108
109             sslContextFactory.addExcludeProtocols("SSLv3");
110             sslContextFactory.setIncludeProtocols(nodeConfigManager.getEnabledprotocols());
111             nodeMainLogger.debug("NODE00004 Unsupported protocols node server:-"
112                     + String.join(",", sslContextFactory.getExcludeProtocols()));
113             nodeMainLogger.debug("NODE00004 Supported protocols node server:-"
114                     + String.join(",", sslContextFactory.getIncludeProtocols()));
115             nodeMainLogger.debug("NODE00004 Unsupported ciphers node server:-"
116                     + String.join(",", sslContextFactory.getExcludeCipherSuites()));
117
118             HttpConfiguration httpsConfiguration = new HttpConfiguration(httpConfiguration);
119             httpsConfiguration.setRequestHeaderSize(8192);
120
121             SecureRequestCustomizer secureRequestCustomizer = new SecureRequestCustomizer();
122             secureRequestCustomizer.setStsMaxAge(2000);
123             secureRequestCustomizer.setStsIncludeSubDomains(true);
124             httpsConfiguration.addCustomizer(secureRequestCustomizer);
125
126             // HTTPS connector
127             try (ServerConnector httpsServerConnector = new ServerConnector(server,
128                     new SslConnectionFactory(sslContextFactory, HttpVersion.HTTP_1_1.asString()),
129                     new HttpConnectionFactory(httpsConfiguration))) {
130
131                 httpsServerConnector.setPort(nodeConfigManager.getHttpsPort());
132                 httpsServerConnector.setIdleTimeout(3600000);
133                 httpsServerConnector.setAcceptQueueSize(2);
134
135                 //Context Handler
136                 ServletContextHandler servletContextHandler = new ServletContextHandler(0);
137                 servletContextHandler.setContextPath("/");
138                 servletContextHandler.addServlet(new ServletHolder(new NodeServlet(delivery)), "/*");
139
140                 //CADI Filter activation check
141                 if (nodeConfigManager.getCadiEnabled()) {
142                     servletContextHandler.addFilter(new FilterHolder(new DRNodeCadiFilter(true,
143                         nodeConfigManager.getNodeAafPropsUtils().getPropAccess())), "/*", EnumSet.of(DispatcherType.REQUEST));
144                 }
145
146                 server.setHandler(servletContextHandler);
147                 server.setConnectors(new Connector[]{httpServerConnector, httpsServerConnector});
148             }
149         }
150
151         try {
152             server.start();
153             nodeMainLogger.debug("NODE00006 Node Server started-" + server.getState());
154         } catch (Exception e) {
155             nodeMainLogger.error("NODE00006 Jetty failed to start. Reporting will we unavailable: "
156                                          + e.getMessage(), e);
157         }
158         server.join();
159         nodeMainLogger.debug("NODE00007 Node Server joined - " + server.getState());
160     }
161
162     private static class WaitForConfig implements Runnable {
163
164         private NodeConfigManager localNodeConfigManager;
165
166         WaitForConfig(NodeConfigManager ncm) {
167             this.localNodeConfigManager = ncm;
168         }
169
170         public synchronized void run() {
171             notifyAll();
172         }
173
174         synchronized void waitForConfig() {
175             localNodeConfigManager.registerConfigTask(this);
176             while (!localNodeConfigManager.isConfigured()) {
177                 nodeMainLogger.debug("NODE0003 Waiting for Node Configuration");
178                 try {
179                     wait();
180                 } catch (Exception exception) {
181                     nodeMainLogger.error("NodeMain: waitForConfig exception. Exception Message:- "
182                         + exception.toString(), exception);
183                 }
184             }
185             localNodeConfigManager.deregisterConfigTask(this);
186             nodeMainLogger.debug("NODE0004 Node Configuration Data Received");
187         }
188     }
189 }