Merge "BUG-2329 Add test for anyxmls inside rpc resonse for netcfon-connector"
[controller.git] / opendaylight / 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 public class ServiceHelper {
32     private static final Logger logger = LoggerFactory
33             .getLogger(ServiceHelper.class);
34
35     /**
36      * Register a Service in the OSGi service registry
37      *
38      * @param clazz The target class
39      * @param containerName The container name
40      * @param instance of the object exporting the service, be careful
41      * the object must implement/extend clazz else the registration
42      * will fail unless a ServiceFactory is passed as parameter
43      * @param properties The properties to be attached to the service
44      * registration
45      * @return true if registration happened, false otherwise
46      */
47     public static boolean registerService(Class<?> clazz, String containerName,
48             Object instance, Dictionary<String, Object> properties) {
49         if (properties == null) {
50             properties = (Dictionary<String, Object>) new Hashtable<String, Object>();
51         }
52         properties.put("containerName", containerName);
53         return registerGlobalService(clazz, instance, properties);
54     }
55
56     /**
57      * Register a Global Service in the OSGi service registry
58      *
59      * @param clazz The target class
60      * @param instance of the object exporting the service, be careful
61      * the object must implement/extend clazz else the registration
62      * will fail unless a ServiceFactory is passed as parameter
63      * @param properties The properties to be attached to the service
64      * registration
65      * @return true if registration happened, false otherwise
66      */
67     public static boolean registerGlobalService(Class<?> clazz,
68                                                 Object instance,
69                                                 Dictionary<String, Object> properties) {
70         ServiceRegistration<?> registration = registerGlobalServiceWReg(clazz, instance, properties);
71         if (registration == null) {
72             logger.error("Failed to register {} for instance {}", clazz, instance);
73             return false;
74         }
75         return true;
76     }
77
78     /**
79      * Register a Service in the OSGi service registry and return the ServiceRegistration
80      *
81      * @param clazz The target class
82      * @param containerName The container name
83      * @param instance of the object exporting the service, be careful
84      * the object must implement/extend clazz else the registration
85      * will fail unless a ServiceFactory is passed as parameter
86      * @param properties The properties to be attached to the service
87      * registration
88      * @return the ServiceRegistration if registration happened, null otherwise
89      */
90     public static ServiceRegistration<?> registerServiceWReg(Class<?> clazz, String containerName,
91                                                           Object instance, Dictionary<String, Object> properties) {
92         if (properties == null) {
93             properties = (Dictionary<String, Object>) new Hashtable<String, Object>();
94         }
95         properties.put("containerName", containerName);
96         return registerGlobalServiceWReg(clazz, instance, properties);
97     }
98
99     /**
100      * Register a Global Service in the OSGi service registry
101      *
102      * @param clazz The target class
103      * @param instance of the object exporting the service, be careful
104      * the object must implement/extend clazz else the registration
105      * will fail unless a ServiceFactory is passed as parameter
106      * @param properties The properties to be attached to the service
107      * registration
108      * @return the ServiceRegistration if registration happened, null otherwise
109      */
110     public static ServiceRegistration<?> registerGlobalServiceWReg(Class<?> clazz,
111                                                                 Object instance,
112                                                                 Dictionary<String, Object> properties) {
113         try {
114             BundleContext bCtx = FrameworkUtil.getBundle(instance.getClass()).getBundleContext();
115             if (bCtx == null) {
116                 logger.error("Could not retrieve the BundleContext");
117                 return null;
118             }
119
120             ServiceRegistration<?> registration = bCtx.registerService(clazz.getName(), instance, properties);
121             return registration;
122         } catch (Exception e) {
123             logger.error("Exception {} while registering the service {}",
124                          e.getMessage(), instance.toString());
125         }
126         return null;
127     }
128
129     /**
130      * Retrieve instance of a class via OSGI registry, if there
131      * are many only the first is returned.
132      *
133      * @param clazz The target class
134      * @param containerName The container name
135      * @param bundle The caller
136      */
137     public static Object getInstance(Class<?> clazz, String containerName,
138             Object bundle) {
139         // Back-end convention: Container id in lower case. Let's enforce it here
140         return getInstance(clazz, containerName.toLowerCase(), bundle, null);
141     }
142
143     /**
144      * Retrieve global instance of a class via OSGI registry, if
145      * there are many only the first is returned.
146      *
147      * @param clazz The target class
148      * @param bundle The caller
149      */
150     public static Object getGlobalInstance(Class<?> clazz, Object bundle) {
151         return getGlobalInstance(clazz, bundle, null);
152     }
153
154     /**
155      * Retrieve instance of a class via OSGI registry, if there
156      * are many only the first is returned. On this version an LDAP
157      * type of filter is applied
158      *
159      * @param clazz The target class
160      * @param containerName The container name
161      * @param bundle The caller
162      * @param serviceFilter LDAP filter to be applied in the search
163      */
164     public static Object getInstance(Class<?> clazz, String containerName,
165             Object bundle, String serviceFilter) {
166         Object[] instances = getInstances(clazz, containerName, bundle,
167                 serviceFilter);
168         if (instances != null) {
169             return instances[0];
170         }
171         return null;
172     }
173
174     /**
175      * Retrieve global instance of a class via OSGI registry, if
176      * there are many only the first is returned. On this version an LDAP
177      * type of filter is applied
178      *
179      * @param clazz The target class
180      * @param bundle The caller
181      * @param serviceFilter LDAP filter to be applied in the search
182      */
183     public static Object getGlobalInstance(Class<?> clazz, Object bundle,
184             String serviceFilter) {
185         Object[] instances = getGlobalInstances(clazz, bundle, serviceFilter);
186         if (instances != null) {
187             return instances[0];
188         }
189         return null;
190     }
191
192     /**
193      * Retrieve all the Instances of a Service, optionally
194      * filtered via serviceFilter if non-null else all the results are
195      * returned if null
196      *
197      * @param clazz The target class
198      * @param containerName The container name
199      * @param bundle The caller
200      * @param serviceFilter LDAP filter to be applied in the search
201      */
202     public static Object[] getInstances(Class<?> clazz, String containerName,
203             Object bundle, String serviceFilter) {
204         Object instances[] = null;
205         try {
206             BundleContext bCtx = FrameworkUtil.getBundle(bundle.getClass())
207                     .getBundleContext();
208
209             ServiceReference<?>[] services = null;
210             if (serviceFilter != null) {
211                 services = bCtx.getServiceReferences(clazz.getName(),
212                         "(&(containerName=" + containerName + ")"
213                                 + serviceFilter + ")");
214             } else {
215                 services = bCtx.getServiceReferences(clazz.getName(),
216                         "(containerName=" + containerName + ")");
217             }
218
219             if (services != null) {
220                 instances = new Object[services.length];
221                 for (int i = 0; i < services.length; i++) {
222                     instances[i] = bCtx.getService(services[i]);
223                 }
224             }
225         } catch (Exception e) {
226             logger.error("Instance reference is NULL");
227         }
228         return instances;
229     }
230
231     /**
232      * Retrieve all the Instances of a Service, optionally
233      * filtered via serviceFilter if non-null else all the results are
234      * returned if null
235      *
236      * @param clazz The target class
237      * @param bundle The caller
238      * @param serviceFilter LDAP filter to be applied in the search
239      */
240     public static Object[] getGlobalInstances(Class<?> clazz, Object bundle,
241             String serviceFilter) {
242         Object instances[] = null;
243         try {
244             BundleContext bCtx = FrameworkUtil.getBundle(bundle.getClass())
245                     .getBundleContext();
246
247             ServiceReference<?>[] services = bCtx.getServiceReferences(clazz
248                     .getName(), serviceFilter);
249
250             if (services != null) {
251                 instances = new Object[services.length];
252                 for (int i = 0; i < services.length; i++) {
253                     instances[i] = bCtx.getService(services[i]);
254                 }
255             }
256         } catch (Exception e) {
257             logger.error("Instance reference is NULL");
258         }
259         return instances;
260     }
261 }