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