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