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