3530459217ce1596781cfb1c0807c092b18a9e52
[sdnc/core.git] / dblib / provider / src / main / java / org / openecomp / sdnc / sli / resource / dblib / factory / DBConfigFactory.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * openecomp
4  * ================================================================================
5  * Copyright (C) 2016 - 2017 AT&T
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.sdnc.sli.resource.dblib.factory;
22
23
24 import java.util.ArrayList;
25 import java.util.Iterator;
26 import java.util.Properties;
27
28 import org.slf4j.LoggerFactory;
29
30 import org.openecomp.sdnc.sli.resource.dblib.config.BaseDBConfiguration;
31 import org.openecomp.sdnc.sli.resource.dblib.config.DbConfigPool;
32 import org.openecomp.sdnc.sli.resource.dblib.config.JDBCConfiguration;
33
34 /**
35  * @version $Revision: 1.1 $
36  * Change Log
37  * Author         Date     Comments
38  * ============== ======== ====================================================
39  * Rich Tabedzki  01/17/08 Initial version
40  */
41 public class DBConfigFactory {
42
43         public static DbConfigPool createConfig(Properties resource) {
44                 return getConfigparams(resource);
45         }
46
47         static DbConfigPool getConfigparams(Properties properties){
48                 LoggerFactory.getLogger(DBConfigFactory.class).info(properties.toString());
49                 DbConfigPool xmlConfig = new DbConfigPool(properties);
50                 ArrayList<Properties> propertySets = new ArrayList<Properties>();
51
52                 if("JDBC".equalsIgnoreCase(xmlConfig.getType())) {
53                         String hosts = properties.getProperty(BaseDBConfiguration.DATABASE_HOSTS);
54                         if(hosts == null || hosts.isEmpty()) {
55                                 propertySets.add(properties);
56                         } else {
57                                 String[] newhost = hosts.split(",");
58                                 for(int i=0; i< newhost.length; i++) {
59                                         Properties localset = new Properties();
60                                         localset.putAll(properties);
61                                         String url = localset.getProperty(BaseDBConfiguration.DATABASE_URL);
62                                         if(url.contains("DBHOST"))
63                                                 url = url.replace("DBHOST", newhost[i]);
64                                         if(url.contains("dbhost"))
65                                                 url = url.replace("dbhost", newhost[i]);
66                                         localset.setProperty(BaseDBConfiguration.DATABASE_URL, url);
67                                         localset.setProperty(BaseDBConfiguration.CONNECTION_NAME, newhost[i]);
68                                         propertySets.add(localset);
69                                 }
70                         }
71                 } else {
72                         propertySets.add(properties);
73                 }
74                 try {
75                         Iterator<Properties>  it = propertySets.iterator();
76                         while(it.hasNext()) {
77                                 BaseDBConfiguration config = parse(it.next());
78                                 xmlConfig.addConfiguration(config);
79                         }
80
81                 } catch (Exception e) {
82                         LoggerFactory.getLogger(DBConfigFactory.class).warn("",e);
83                 }
84
85                 return xmlConfig;
86         }
87
88         public static BaseDBConfiguration parse(Properties props) throws Exception {
89
90                 String type = props.getProperty(BaseDBConfiguration.DATABASE_TYPE);
91
92                 BaseDBConfiguration config = null;
93
94                 if("JDBC".equalsIgnoreCase(type)) {
95                         config = new JDBCConfiguration(props);
96                 }
97
98                 return config;
99
100         }
101 }