Fixing ForwardingRulesManager code that
[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, Dictionary<String, Object> properties) {
69         try {
70             BundleContext bCtx = FrameworkUtil.getBundle(instance.getClass())
71                     .getBundleContext();
72             if (bCtx == null) {
73                 logger.error("Could not retrieve the BundleContext");
74                 return false;
75             }
76
77             ServiceRegistration registration = bCtx.registerService(clazz
78                     .getName(), instance, properties);
79             if (registration == null) {
80                 logger.error("Failed to register {} for instance {}", clazz,
81                         instance);
82             }
83             return true;
84         } catch (Exception e) {
85             logger.error("Exception "+e.getMessage() +" while registering the service "+instance.toString());
86         }
87         return false;
88     }
89
90     /**
91      * Retrieve instance of a class via OSGI registry, if there
92      * are many only the first is returned.
93      *
94      * @param clazz The target class
95      * @param containerName The container name
96      * @param bundle The caller
97      */
98     public static Object getInstance(Class<?> clazz, String containerName,
99             Object bundle) {
100         // Back-end convention: Container id in lower case. Let's enforce it here
101         return getInstance(clazz, containerName.toLowerCase(), bundle, null);
102     }
103
104     /**
105      * Retrieve global instance of a class via OSGI registry, if
106      * there are many only the first is returned.
107      *
108      * @param clazz The target class
109      * @param bundle The caller
110      */
111     public static Object getGlobalInstance(Class<?> clazz, Object bundle) {
112         return getGlobalInstance(clazz, bundle, null);
113     }
114
115     /**
116      * Retrieve instance of a class via OSGI registry, if there
117      * are many only the first is returned. On this version an LDAP
118      * type of filter is applied
119      *
120      * @param clazz The target class
121      * @param containerName The container name
122      * @param bundle The caller
123      * @param serviceFilter LDAP filter to be applied in the search
124      */
125     public static Object getInstance(Class<?> clazz, String containerName,
126             Object bundle, String serviceFilter) {
127         Object[] instances = getInstances(clazz, containerName, bundle,
128                 serviceFilter);
129         if (instances != null) {
130             return instances[0];
131         }
132         return null;
133     }
134
135     /**
136      * Retrieve global instance of a class via OSGI registry, if
137      * there are many only the first is returned. On this version an LDAP
138      * type of filter is applied
139      *
140      * @param clazz The target class
141      * @param bundle The caller
142      * @param serviceFilter LDAP filter to be applied in the search
143      */
144     public static Object getGlobalInstance(Class<?> clazz, Object bundle,
145             String serviceFilter) {
146         Object[] instances = getGlobalInstances(clazz, bundle, serviceFilter);
147         if (instances != null) {
148             return instances[0];
149         }
150         return null;
151     }
152
153     /**
154      * Retrieve all the Instances of a Service, optionally
155      * filtered via serviceFilter if non-null else all the results are
156      * returned if null
157      *
158      * @param clazz The target class
159      * @param containerName The container name
160      * @param bundle The caller
161      * @param serviceFilter LDAP filter to be applied in the search
162      */
163     public static Object[] getInstances(Class<?> clazz, String containerName,
164             Object bundle, String serviceFilter) {
165         Object instances[] = null;
166         try {
167             BundleContext bCtx = FrameworkUtil.getBundle(bundle.getClass())
168                     .getBundleContext();
169
170             ServiceReference[] services = null;
171             if (serviceFilter != null) {
172                 services = bCtx.getServiceReferences(clazz.getName(),
173                         "(&(containerName=" + containerName + ")"
174                                 + serviceFilter + ")");
175             } else {
176                 services = bCtx.getServiceReferences(clazz.getName(),
177                         "(containerName=" + containerName + ")");
178             }
179
180             if (services != null) {
181                 instances = new Object[services.length];
182                 for (int i = 0; i < services.length; i++) {
183                     instances[i] = bCtx.getService(services[i]);
184                 }
185             }
186         } catch (Exception e) {
187             logger.error("Instance reference is NULL");
188         }
189         return instances;
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 bundle The caller
199      * @param serviceFilter LDAP filter to be applied in the search
200      */
201     public static Object[] getGlobalInstances(Class<?> clazz, Object bundle,
202             String serviceFilter) {
203         Object instances[] = null;
204         try {
205             BundleContext bCtx = FrameworkUtil.getBundle(bundle.getClass())
206                     .getBundleContext();
207
208             ServiceReference[] services = bCtx.getServiceReferences(clazz
209                     .getName(), serviceFilter);
210
211             if (services != null) {
212                 instances = new Object[services.length];
213                 for (int i = 0; i < services.length; i++) {
214                     instances[i] = bCtx.getService(services[i]);
215                 }
216             }
217         } catch (Exception e) {
218             logger.error("Instance reference is NULL");
219         }
220         return instances;
221     }
222 }