Update groupId to org.onap.ccsdk.sli
[ccsdk/sli/core.git] / sli / provider / src / main / java / org / onap / ccsdk / sli / core / sli / provider / SvcLogicAdaptorFactory.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.provider;
22
23 import java.util.HashMap;
24
25 import org.onap.ccsdk.sli.core.sli.SvcLogicAdaptor;
26 import org.osgi.framework.BundleContext;
27 import org.osgi.framework.FrameworkUtil;
28 import org.osgi.framework.ServiceReference;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31
32 public class SvcLogicAdaptorFactory {
33
34         private static final Logger LOG = LoggerFactory
35                         .getLogger(SvcLogicAdaptorFactory.class);
36
37         private static HashMap<String, SvcLogicAdaptor> adaptorMap = new HashMap<String, SvcLogicAdaptor>();
38
39         public static void registerAdaptor(SvcLogicAdaptor adaptor) {
40                 String name = adaptor.getClass().getName();
41                 LOG.info("Registering adaptor " + name);
42                 adaptorMap.put(name, adaptor);
43
44         }
45
46         public static void unregisterAdaptor(String name) {
47                 if (adaptorMap.containsKey(name)) {
48                         LOG.info("Unregistering " + name);
49                         adaptorMap.remove(name);
50                 }
51         }
52
53         public static SvcLogicAdaptor getInstance(String name) {
54                 if (adaptorMap.containsKey(name)) {
55                         return (adaptorMap.get(name));
56                 } else {
57                         BundleContext bctx = null;
58                         try
59                         {
60                          bctx = FrameworkUtil.getBundle(SvcLogicAdaptorFactory.class)
61                                         .getBundleContext();
62                         }
63                         catch (Exception e)
64                         {
65                                 LOG.debug("Caught exception trying to locate device adaptor "+name, e);
66                                 return(null);
67                         }
68
69                         ServiceReference sref = bctx.getServiceReference(name);
70
71                         if (sref != null) {
72                                 SvcLogicAdaptor adaptor = (SvcLogicAdaptor) bctx
73                                                 .getService(sref);
74
75                                 if (adaptor != null) {
76                                         registerAdaptor(adaptor);
77
78                                         return (adaptor);
79                                 }
80                                 return (null);
81                         }
82                 }
83                 return(null);
84         }
85 }