05d9a70a2d731cc9531a3f02f07514c8fbfd749d
[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.cloud.CloudConfigIdentityMapper;
31 import org.openecomp.mso.logger.MessageEnum;
32 import org.openecomp.mso.logger.MsoLogger;
33
34 /**
35  * This class will attempt to initialize Cloud Config when part of a web application.
36  * 
37  *
38  *
39  */
40 @WebListener
41 public class CloudConfigInitializer implements ServletContextListener
42 {
43
44         private CloudConfigFactory cloudConfigFactory=new CloudConfigFactory();
45
46         public CloudConfigInitializer () {
47         }
48
49         @Override
50         public void contextDestroyed(ServletContextEvent event) {
51                 // Nothing to do...
52         }
53
54
55         @Override
56         public void contextInitialized(ServletContextEvent event)
57         {
58
59                 // Note - this logger may be before or after MSO Logging configuration applied
60                 MsoLogger initLogger = MsoLogger.getMsoLogger(MsoLogger.Catalog.GENERAL);
61                 try {
62                         // Look first in the init-parameters
63                         String msoPropConfigParam = event.getServletContext().getInitParameter("mso.cloud_config.configuration");
64
65                         String[] configFileSplit = msoPropConfigParam.split(",");
66                         for (String msoPropConfig:configFileSplit) {
67                                 String[] msoPropDecoded = msoPropConfig.split("=");
68
69                                 try {
70                                         cloudConfigFactory.initializeCloudConfig(msoPropDecoded[0], Integer.valueOf(msoPropDecoded[1]));
71                                         initLogger.info(MessageEnum.RA_CONFIG_LOAD, msoPropDecoded[0], "", "");
72                                         initLogger.debug("Mso properties successfully loaded:"+msoPropDecoded[0]+"(Timer(mins):"+Integer.valueOf(msoPropDecoded[1]));
73                                 } catch (NumberFormatException ne) {
74                                         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);
75                                 }
76                         }
77
78                         // Second, obtain class name that will register all mappings
79                         String msoMapperClassParam = event.getServletContext().getInitParameter("mso.cloud_config.mapper.class");
80                         if (msoMapperClassParam != null) {
81                                 Class<?> mapperClass = Class.forName(msoMapperClassParam);
82                                 if (CloudConfigIdentityMapper.class.isAssignableFrom(mapperClass)) {
83                                         ((CloudConfigIdentityMapper)mapperClass.newInstance()).registerAllMappings();
84                                         initLogger.info(MessageEnum.RA_CONFIG_LOAD,msoMapperClassParam+"(Openstack authentication mapper class)","","");
85                                 } else {
86                                     initLogger.info(MessageEnum.RA_CONFIG_LOAD,msoMapperClassParam+"(Openstack authentication mapper class not an implementation of CloudConfigIdentityMapper)","","");
87                                 }
88                         } else {
89                             initLogger.info(MessageEnum.RA_CONFIG_LOAD,"Openstack authentication mapper class not specified in web.xml (ONLY core authentication mechanisms will be loaded)","","");
90                         }
91
92                 }
93                 catch (Exception e) {
94                         initLogger.error(MessageEnum.RA_CONFIG_EXC,  "Unknown. MSO Properties failed to initialize completely", "", "", MsoLogger.ErrorCode.AvailabilityError, "Exception - MSO Properties failed to initialize completely", e);
95                 }
96         }
97 }