Mark AD-SAL interfaces as deprecated
[controller.git] / opendaylight / adsal / sal / api / src / main / java / org / opendaylight / controller / sal / utils / ServiceHelper.java
1
2 /*
3  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
4  *
5  * This program and the accompanying materials are made available under the
6  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
7  * and is available at http://www.eclipse.org/legal/epl-v10.html
8  */
9
10 /**
11  * @file   ServiceHelper.java
12  *
13  * @brief  The class helps to register and retrieve OSGi service registry
14  */
15 package org.opendaylight.controller.sal.utils;
16
17 import java.util.Hashtable;
18 import org.osgi.framework.ServiceRegistration;
19 import java.util.Dictionary;
20 import org.osgi.framework.BundleContext;
21 import org.osgi.framework.FrameworkUtil;
22 import org.osgi.framework.ServiceReference;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
25
26 /**
27  * The class helps to register and retrieve OSGi service registry
28  *
29  *
30  */
31 @Deprecated
32 public class ServiceHelper {
33     private static final Logger logger = LoggerFactory
34             .getLogger(ServiceHelper.class);
35
36     /**
37      * Register a Service in the OSGi service registry
38      *
39      * @param clazz The target class
40      * @param containerName The container name
41      * @param instance of the object exporting the service, be careful
42      * the object must implement/extend clazz else the registration
43      * will fail unless a ServiceFactory is passed as parameter
44      * @param properties The properties to be attached to the service
45      * registration
46      * @return true if registration happened, false otherwise
47      */
48     public static boolean registerService(Class<?> clazz, String containerName,
49             Object instance, Dictionary<String, Object> properties) {
50         if (properties == null) {
51             properties = (Dictionary<String, Object>) new Hashtable<String, Object>();
52         }
53         properties.put("containerName", containerName);
54         return registerGlobalService(clazz, instance, properties);
55     }
56
57     /**
58      * Register a Global Service in the OSGi service registry
59      *
60      * @param clazz The target class
61      * @param instance of the object exporting the service, be careful
62      * the object must implement/extend clazz else the registration
63      * will fail unless a ServiceFactory is passed as parameter
64      * @param properties The properties to be attached to the service
65      * registration
66      * @return true if registration happened, false otherwise
67      */
68     public static boolean registerGlobalService(Class<?> clazz,
69                                                 Object instance,
70                                                 Dictionary<String, Object> properties) {
71         ServiceRegistration<?> registration = registerGlobalServiceWReg(clazz, instance, properties);
72         if (registration == null) {
73             logger.error("Failed to register {} for instance {}", clazz, instance);
74             return false;
75         }
76         return true;
77     }
78
79     /**
80      * Register a Service in the OSGi service registry and return the ServiceRegistration
81      *
82      * @param clazz The target class
83      * @param containerName The container name
84      * @param instance of the object exporting the service, be careful
85      * the object must implement/extend clazz else the registration
86      * will fail unless a ServiceFactory is passed as parameter
87      * @param properties The properties to be attached to the service
88      * registration
89      * @return the ServiceRegistration if registration happened, null otherwise
90      */
91     public static ServiceRegistration<?> registerServiceWReg(Class<?> clazz, String containerName,
92                                                           Object instance, Dictionary<String, Object> properties) {
93         if (properties == null) {
94             properties = (Dictionary<String, Object>) new Hashtable<String, Object>();
95         }
96         properties.put("containerName", containerName);
97         return registerGlobalServiceWReg(clazz, instance, properties);
98     }
99
100     /**
101      * Register a Global Service in the OSGi service registry
102      *
103      * @param clazz The target class
104      * @param instance of the object exporting the service, be careful
105      * the object must implement/extend clazz else the registration
106      * will fail unless a ServiceFactory is passed as parameter
107      * @param properties The properties to be attached to the service
108      * registration
109      * @return the ServiceRegistration if registration happened, null otherwise
110      */
111     public static ServiceRegistration<?> registerGlobalServiceWReg(Class<?> clazz,
112                                                                 Object instance,
113                                                                 Dictionary<String, Object> properties) {
114         try {
115             BundleContext bCtx = FrameworkUtil.getBundle(instance.getClass()).getBundleContext();
116             if (bCtx == null) {
117                 logger.error("Could not retrieve the BundleContext");
118                 return null;
119             }
120
121             ServiceRegistration<?> registration = bCtx.registerService(clazz.getName(), instance, properties);
122             return registration;
123         } catch (Exception e) {
124             logger.error("Exception {} while registering the service {}",
125                          e.getMessage(), instance.toString());
126         }
127         return null;
128     }
129
130     /**
131      * Retrieve instance of a class via OSGI registry, if there
132      * are many only the first is returned.
133      *
134      * @param clazz The target class
135      * @param containerName The container name
136      * @param bundle The caller
137      */
138     public static Object getInstance(Class<?> clazz, String containerName,
139             Object bundle) {
140         // Back-end convention: Container id in lower case. Let's enforce it here
141         return getInstance(clazz, containerName.toLowerCase(), bundle, null);
142     }
143
144     /**
145      * Retrieve global instance of a class via OSGI registry, if
146      * there are many only the first is returned.
147      *
148      * @param clazz The target class
149      * @param bundle The caller
150      */
151     public static Object getGlobalInstance(Class<?> clazz, Object bundle) {
152         return getGlobalInstance(clazz, bundle, null);
153     }
154
155     /**
156      * Retrieve instance of a class via OSGI registry, if there
157      * are many only the first is returned. On this version an LDAP
158      * type of filter is applied
159      *
160      * @param clazz The target class
161      * @param containerName The container name
162      * @param bundle The caller
163      * @param serviceFilter LDAP filter to be applied in the search
164      */
165     public static Object getInstance(Class<?> clazz, String containerName,
166             Object bundle, String serviceFilter) {
167         Object[] instances = getInstances(clazz, containerName, bundle,
168                 serviceFilter);
169         if (instances != null) {
170             return instances[0];
171         }
172         return null;
173     }
174
175     /**
176      * Retrieve global instance of a class via OSGI registry, if
177      * there are many only the first is returned. On this version an LDAP
178      * type of filter is applied
179      *
180      * @param clazz The target class
181      * @param bundle The caller
182      * @param serviceFilter LDAP filter to be applied in the search
183      */
184     public static Object getGlobalInstance(Class<?> clazz, Object bundle,
185             String serviceFilter) {
186         Object[] instances = getGlobalInstances(clazz, bundle, serviceFilter);
187         if (instances != null) {
188             return instances[0];
189         }
190         return null;
191     }
192
193     /**
194      * Retrieve all the Instances of a Service, optionally
195      * filtered via serviceFilter if non-null else all the results are
196      * returned if null
197      *
198      * @param clazz The target class
199      * @param containerName The container name
200      * @param bundle The caller
201      * @param serviceFilter LDAP filter to be applied in the search
202      */
203     public static Object[] getInstances(Class<?> clazz, String containerName,
204             Object bundle, String serviceFilter) {
205         Object instances[] = null;
206         try {
207             BundleContext bCtx = FrameworkUtil.getBundle(bundle.getClass())
208                     .getBundleContext();
209
210             ServiceReference<?>[] services = null;
211             if (serviceFilter != null) {
212                 services = bCtx.getServiceReferences(clazz.getName(),
213                         "(&(containerName=" + containerName + ")"
214                                 + serviceFilter + ")");
215             } else {
216                 services = bCtx.getServiceReferences(clazz.getName(),
217                         "(containerName=" + containerName + ")");
218             }
219
220             if (services != null) {
221                 instances = new Object[services.length];
222                 for (int i = 0; i < services.length; i++) {
223                     instances[i] = bCtx.getService(services[i]);
224                 }
225             }
226         } catch (Exception e) {
227             logger.error("Instance reference is NULL");
228         }
229         return instances;
230     }
231
232     /**
233      * Retrieve all the Instances of a Service, optionally
234      * filtered via serviceFilter if non-null else all the results are
235      * returned if null
236      *
237      * @param clazz The target class
238      * @param bundle The caller
239      * @param serviceFilter LDAP filter to be applied in the search
240      */
241     public static Object[] getGlobalInstances(Class<?> clazz, Object bundle,
242             String serviceFilter) {
243         Object instances[] = null;
244         try {
245             BundleContext bCtx = FrameworkUtil.getBundle(bundle.getClass())
246                     .getBundleContext();
247
248             ServiceReference<?>[] services = bCtx.getServiceReferences(clazz
249                     .getName(), serviceFilter);
250
251             if (services != null) {
252                 instances = new Object[services.length];
253                 for (int i = 0; i < services.length; i++) {
254                     instances[i] = bCtx.getService(services[i]);
255                 }
256             }
257         } catch (Exception e) {
258             logger.error("Instance reference is NULL");
259         }
260         return instances;
261     }
262 }