Update DmaapConfig and AAIAppServlet to start
[aai/traversal.git] / aai-traversal / src / main / java / org / onap / aai / util / AAIAppServletContextListener.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
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 package org.onap.aai.util;
24
25 import java.io.IOException;
26 import java.util.UUID;
27 import javax.servlet.ServletContextEvent;
28 import javax.servlet.ServletContextListener;
29
30 import org.onap.aai.dbmap.AAIGraph;
31 import org.onap.aai.exceptions.AAIException;
32 import org.onap.aai.introspection.ModelInjestor;
33 import org.onap.aai.logging.ErrorLogHelper;
34 import org.onap.aai.logging.LogFormatTools;
35 import org.onap.aai.logging.LoggingContext;
36 import org.onap.aai.logging.LoggingContext.StatusCode;
37
38 import com.att.eelf.configuration.EELFLogger;
39 import com.att.eelf.configuration.EELFManager;
40
41 public class AAIAppServletContextListener implements ServletContextListener {
42
43         private static final String MICRO_SVC="aai-traversal";
44         private static final String ACTIVEMQ_TCP_URL = "tcp://localhost:61446";
45         private static final EELFLogger LOGGER = EELFManager.getInstance().getLogger(AAIAppServletContextListener.class.getName());     
46
47         /**
48          * Destroys Context
49          * 
50          * @param arg0 the ServletContextEvent
51          */
52         public void contextDestroyed(ServletContextEvent arg0) {                
53         }
54
55         /**
56          * Initializes Context
57          * 
58          * @param arg0 the ServletContextEvent
59          */
60         public void contextInitialized(ServletContextEvent arg0) {
61                 System.setProperty("org.onap.aai.serverStarted", "false");
62                 System.setProperty("aai.service.name", "traversal");
63                 
64                 LoggingContext.save();
65                 LoggingContext.component("init");
66                 LoggingContext.partnerName("NA");
67                 LoggingContext.targetEntity(MICRO_SVC);
68                 LoggingContext.requestId(UUID.randomUUID().toString());
69                 LoggingContext.serviceName(MICRO_SVC);
70                 LoggingContext.targetServiceName("contextInitialized");
71                 LoggingContext.statusCode(StatusCode.COMPLETE);
72                 LOGGER.info("AAI Server initialization started...");
73                 try {
74                         LOGGER.info("Loading aaiconfig.properties");
75                         AAIConfig.init();
76
77                         LOGGER.info("Loading error.properties");
78                         ErrorLogHelper.loadProperties();
79
80                         LOGGER.info("Loading graph database");
81
82                         AAIGraph.getInstance();
83                         ModelInjestor.getInstance();
84
85                         LOGGER.info("A&AI Server initialization succcessful.");
86                         System.setProperty("activemq.tcp.url", ACTIVEMQ_TCP_URL);
87                         System.setProperty("org.onap.aai.serverStarted", "true");
88
89                         Runtime.getRuntime().addShutdownHook(new Thread() {
90                                 public void run() {
91                                         LOGGER.info("AAIGraph shutting down");
92                                         AAIGraph.getInstance().graphShutdown();
93                                         LOGGER.info("AAIGraph shutdown");
94                                         System.out.println("Shutdown hook triggered.");
95                                 }
96                         });
97
98                 } catch (AAIException e) {
99                         ErrorLogHelper.logException(e);
100                         throw new RuntimeException("AAIException caught while initializing A&AI server", e);
101                 } catch (IOException e) {
102                         ErrorLogHelper.logError("AAI_4000", e.getMessage());
103                         throw new RuntimeException("IOException caught while initializing A&AI server", e);
104                 } catch (Exception e) {
105                         LOGGER.error("Unknown failure while initializing A&AI Server" + LogFormatTools.getStackTop(e));
106                         throw new RuntimeException("Unknown failure while initializing A&AI server", e);
107                 }
108
109                 LOGGER.info("Graph-Query MicroService Started");
110                 LOGGER.debug("Graph-Query MicroService Started");
111                 LoggingContext.restore();
112
113         }
114 }