3d9c23cc323b2fa1f3c611ff1b19ca65a777924d
[so.git] / adapters / mso-adapter-utils / src / main / java / org / openecomp / mso / openstack / utils / CloudConfigInitializer.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * OPENECOMP - MSO
4  * ================================================================================
5  * Copyright (C) 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
21 package org.openecomp.mso.openstack.utils;
22
23
24 import javax.ejb.EJB;
25 import javax.servlet.ServletContextEvent;
26 import javax.servlet.ServletContextListener;
27 import javax.servlet.annotation.WebListener;
28
29 import org.openecomp.mso.cloud.CloudConfigFactory;
30 import org.openecomp.mso.logger.MessageEnum;
31 import org.openecomp.mso.logger.MsoLogger;
32
33 /**
34  * This class will attempt to initialize Cloud Config when part of a web application.
35  * 
36  *
37  *
38  */
39 @WebListener
40 public class CloudConfigInitializer implements ServletContextListener
41 {
42
43         private CloudConfigFactory cloudConfigFactory=new CloudConfigFactory();
44
45         public CloudConfigInitializer () {
46         }
47
48         @Override
49         public void contextDestroyed(ServletContextEvent event) {
50                 // Nothing to do...
51         }
52
53
54         @Override
55         public void contextInitialized(ServletContextEvent event)
56         {
57
58                 // Note - this logger may be before or after MSO Logging configuration applied
59                 MsoLogger initLogger = MsoLogger.getMsoLogger(MsoLogger.Catalog.GENERAL);
60                 try {
61                         // Look first in the init-parameters
62                         String msoPropConfigParam = event.getServletContext().getInitParameter("mso.cloud_config.configuration");
63
64                         String[] configFileSplit = msoPropConfigParam.split(",");
65                         for (String msoPropConfig:configFileSplit) {
66                                 String[] msoPropDecoded = msoPropConfig.split("=");
67
68                                 try {
69                                         cloudConfigFactory.initializeCloudConfig(msoPropDecoded[0], Integer.valueOf(msoPropDecoded[1]));
70                                         initLogger.info(MessageEnum.RA_CONFIG_LOAD, msoPropDecoded[0], "", "");
71                                         initLogger.debug("Mso properties successfully loaded:"+msoPropDecoded[0]+"(Timer(mins):"+Integer.valueOf(msoPropDecoded[1]));
72                                 } catch (NumberFormatException ne) {
73                                         initLogger.error(MessageEnum.RA_CONFIG_EXC, msoPropDecoded[0] + ". MSO Properties failed due to conversion error (in web.xml file)", "", "", MsoLogger.ErrorCode.DataError, "MSO Properties failed due to conversion error (in web.xml file)", ne);
74                                 }
75                         }
76                 }
77                 catch (Exception e) {
78                         initLogger.error(MessageEnum.RA_CONFIG_EXC,  "Unknown. MSO Properties failed to initialize completely", "", "", MsoLogger.ErrorCode.AvailabilityError, "Exception - MSO Properties failed to initialize completely", e);
79                 }
80         }
81 }