Update groupId to org.onap.ccsdk.sli
[ccsdk/sli/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                 DbConfigPool xmlConfig = new DbConfigPool(properties);
49                 ArrayList<Properties> propertySets = new ArrayList<Properties>();
50
51                 if("JDBC".equalsIgnoreCase(xmlConfig.getType())) {
52                         String hosts = properties.getProperty(BaseDBConfiguration.DATABASE_HOSTS);
53                         if(hosts == null || hosts.isEmpty()) {
54                                 propertySets.add(properties);
55                         } else {
56                                 String[] newhost = hosts.split(",");
57                                 for(int i=0; i< newhost.length; i++) {
58                                         Properties localset = new Properties();
59                                         localset.putAll(properties);
60                                         String url = localset.getProperty(BaseDBConfiguration.DATABASE_URL);
61                                         if(url.contains("DBHOST"))
62                                                 url = url.replace("DBHOST", newhost[i]);
63                                         if(url.contains("dbhost"))
64                                                 url = url.replace("dbhost", newhost[i]);
65                                         localset.setProperty(BaseDBConfiguration.DATABASE_URL, url);
66                                         localset.setProperty(BaseDBConfiguration.CONNECTION_NAME, newhost[i]);
67                                         propertySets.add(localset);
68                                 }
69                         }
70                 } else {
71                         propertySets.add(properties);
72                 }
73                 try {
74                         Iterator<Properties>  it = propertySets.iterator();
75                         while(it.hasNext()) {
76                                 BaseDBConfiguration config = parse(it.next());
77                                 xmlConfig.addConfiguration(config);
78                         }
79
80                 } catch (Exception e) {
81                         LoggerFactory.getLogger(DBConfigFactory.class).warn("",e);
82                 }
83
84                 return xmlConfig;
85         }
86
87         public static BaseDBConfiguration parse(Properties props) throws Exception {
88
89                 String type = props.getProperty(BaseDBConfiguration.DATABASE_TYPE);
90
91                 BaseDBConfiguration config = null;
92
93                 if("JDBC".equalsIgnoreCase(type)) {
94                         config = new JDBCConfiguration(props);
95                 }
96
97                 return config;
98
99         }
100 }