More bug fix and refactoring
[dmaap/datarouter.git] / datarouter-node / src / main / java / org / onap / dmaap / datarouter / node / NodeRunner.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 static java.lang.System.exit;
27
28 import com.att.eelf.configuration.EELFLogger;
29 import com.att.eelf.configuration.EELFManager;
30 import org.eclipse.jetty.server.Server;
31
32 /**
33  * The main starting point for the Data Router node.
34  */
35 public class NodeRunner {
36
37     private static EELFLogger nodeMainLogger = EELFManager.getInstance().getLogger(NodeRunner.class);
38     private static NodeConfigManager nodeConfigManager;
39
40     private NodeRunner() {
41     }
42
43     /**
44      * Start the data router.
45      *
46      * <p>The location of the node configuration file can be set using the org.onap.dmaap.datarouter.node.properties
47      * system property. By default, it is "/opt/app/datartr/etc/node.properties".
48      */
49     public static void main(String[] args) {
50         nodeMainLogger.debug("NODE0001 Data Router Node Starting");
51         IsFrom.setDNSCache();
52         nodeConfigManager = NodeConfigManager.getInstance();
53         nodeMainLogger.debug("NODE0002 I am " + nodeConfigManager.getMyName());
54         (new WaitForConfig(nodeConfigManager)).waitForConfig();
55         new LogManager(nodeConfigManager);
56         try {
57             Server server = NodeServer.getServerInstance();
58             server.start();
59             server.join();
60             nodeMainLogger.debug("NODE00006 Node Server started-" + server.getState());
61         } catch (Exception e) {
62             nodeMainLogger.error("NODE00006 Jetty failed to start. Reporting will we be unavailable: "
63                                          + e.getMessage(), e);
64             exit(1);
65         }
66         nodeMainLogger.debug("NODE00007 Node Server joined");
67     }
68
69     private static class WaitForConfig implements Runnable {
70
71         private NodeConfigManager localNodeConfigManager;
72
73         WaitForConfig(NodeConfigManager ncm) {
74             this.localNodeConfigManager = ncm;
75         }
76
77         public synchronized void run() {
78             notifyAll();
79         }
80
81         synchronized void waitForConfig() {
82             localNodeConfigManager.registerConfigTask(this);
83             while (!localNodeConfigManager.isConfigured()) {
84                 nodeMainLogger.debug("NODE0003 Waiting for Node Configuration");
85                 try {
86                     wait();
87                 } catch (Exception exception) {
88                     nodeMainLogger.error("NodeMain: waitForConfig exception. Exception Message:- "
89                         + exception.toString(), exception);
90                 }
91             }
92             localNodeConfigManager.deregisterConfigTask(this);
93             nodeMainLogger.debug("NODE0004 Node Configuration Data Received");
94         }
95     }
96 }