Update groupId to org.onap.ccsdk.sli
[ccsdk/sli/core.git] / sli / common / src / main / java / org / onap / ccsdk / sli / core / sli / SvcLogicStoreFactory.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : CCSDK
4  * ================================================================================
5  * Copyright (C) 2017 ONAP
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.onap.ccsdk.sli.core.sli;
22
23 import java.io.File;
24 import java.io.FileInputStream;
25 import java.io.InputStream;
26 import java.util.Properties;
27
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30
31 public class SvcLogicStoreFactory {
32
33         private static final Logger LOG = LoggerFactory.getLogger(SvcLogicStoreFactory.class);
34         
35         public static SvcLogicStore getSvcLogicStore(String propfile)
36                         throws SvcLogicException {
37                 File propFile = new File(propfile);
38                 if (!propFile.canRead()) {
39                         throw new ConfigurationException("Cannot read property file "
40                                         + propfile);
41
42                 }
43
44                 try {
45                         return (getSvcLogicStore(new FileInputStream(propFile)));
46                 } catch (Exception e) {
47                         throw new ConfigurationException(
48                                         "Could load service store from properties file " + propfile,
49                                         e);
50                 }
51
52         }
53
54         public static SvcLogicStore getSvcLogicStore(InputStream inStr) throws SvcLogicException
55         {
56                 Properties props = new Properties();
57                 
58                 try {
59                         props.load(inStr);
60                 } catch (Exception e) {
61                         throw new ConfigurationException("Could not get load properties from input stream", e);
62                 }
63                 
64                 return(getSvcLogicStore(props));
65         }
66
67         public static SvcLogicStore getSvcLogicStore(Properties props)
68                         throws SvcLogicException {
69                 String storeType = props.getProperty("org.openecomp.sdnc.sli.dbtype");
70                 if ((storeType == null) || (storeType.length() == 0)) {
71                         throw new ConfigurationException(
72                                         "property org.openecomp.sdnc.sli.dbtype unset");
73
74                 }
75
76                 SvcLogicStore retval = null;
77                 LOG.debug(String.format("Using org.openecomp.sdnc.sli.dbtype=%s", storeType));
78
79                 if ("jdbc".equalsIgnoreCase(storeType)) {
80                         retval = new SvcLogicJdbcStore();
81
82                 } else if ("dblib".equalsIgnoreCase(storeType)) {
83                         retval = new SvcLogicDblibStore();
84         } else {
85                         throw new ConfigurationException("unsupported dbtype (" + storeType
86                                         + ")");
87
88                 }
89
90                 
91                 retval.init(props);
92                 return (retval);
93         }
94
95 }