remove ad-sal dependency and implement ServiceHelper 89/24189/1
authordongshu <dongs2011@gmail.com>
Thu, 16 Jul 2015 09:05:17 +0000 (05:05 -0400)
committerdongshu <dongs2011@gmail.com>
Thu, 16 Jul 2015 09:05:17 +0000 (05:05 -0400)
Change-Id: I3dc35d3916e4788bd48b0144e9a5df9bacb665c6
Signed-off-by: dongshu <dongs2011@gmail.com>
alto-commons/pom.xml
alto-commons/src/main/java/org/opendaylight/alto/commons/helper/ServiceHelper.java [new file with mode: 0644]
alto-northbound/pom.xml
alto-northbound/src/main/java/org/opendaylight/alto/northbound/AltoNorthbound.java
alto-services/provider/simple-alto/pom.xml
alto-services/provider/simple-alto/src/main/java/org/opendaylight/alto/services/provider/simple/SimpleAltoService.java

index 170ea6a7362975cd34b59523394878c2b3dcf6a6..ba83c78819c979acc5bba3638e439f732adbdc77 100644 (file)
@@ -51,5 +51,9 @@
       <groupId>com.fasterxml.jackson.core</groupId>
       <artifactId>jackson-core</artifactId>
     </dependency>
+    <dependency>
+      <groupId>org.osgi</groupId>
+      <artifactId>org.osgi.core</artifactId>
+    </dependency>
   </dependencies>
 </project>
diff --git a/alto-commons/src/main/java/org/opendaylight/alto/commons/helper/ServiceHelper.java b/alto-commons/src/main/java/org/opendaylight/alto/commons/helper/ServiceHelper.java
new file mode 100644 (file)
index 0000000..1bfa61a
--- /dev/null
@@ -0,0 +1,118 @@
+package org.opendaylight.alto.commons.helper;
+
+import java.util.Dictionary;
+
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.FrameworkUtil;
+import org.osgi.framework.ServiceReference;
+import org.osgi.framework.ServiceRegistration;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+@SuppressWarnings("rawtypes")
+public class ServiceHelper {
+  private static final Logger logger = LoggerFactory
+      .getLogger(ServiceHelper.class);
+
+  /**
+   * Register a Global Service in the OSGi service registry
+   *
+   * @param clazz
+   *          The target class
+   * @param instance
+   *          of the object exporting the service, be careful the object must
+   *          implement/extend clazz else the registration will fail unless a
+   *          ServiceFactory is passed as parameter
+   * @param properties
+   *          The properties to be attached to the service registration
+   * @return the ServiceRegistration if registration happened, null otherwise
+   */
+  public static ServiceRegistration registerGlobalServiceWReg(Class<?> clazz,
+      Object instance, Dictionary<String, Object> properties) {
+    try {
+      BundleContext bCtx = FrameworkUtil.getBundle(instance.getClass())
+          .getBundleContext();
+      if (bCtx == null) {
+        logger.error("Could not retrieve the BundleContext");
+        return null;
+      }
+
+      ServiceRegistration registration = bCtx.registerService(clazz.getName(),
+          instance, properties);
+      return registration;
+    } catch (Exception e) {
+      logger.error("Exception {} while registering the service {}",
+          e.getMessage(), instance.toString());
+    }
+    return null;
+  }
+
+  /**
+   * Retrieve global instance of a class via OSGI registry, if there are many
+   * only the first is returned.
+   *
+   * @param clazz
+   *          The target class
+   * @param bundle
+   *          The caller
+   */
+  public static Object getGlobalInstance(Class<?> clazz, Object bundle) {
+    return getGlobalInstance(clazz, bundle, null);
+  }
+
+
+  /**
+   * Retrieve global instance of a class via OSGI registry, if there are many
+   * only the first is returned. On this version an LDAP type of filter is
+   * applied
+   *
+   * @param clazz
+   *          The target class
+   * @param bundle
+   *          The caller
+   * @param serviceFilter
+   *          LDAP filter to be applied in the search
+   */
+  public static Object getGlobalInstance(Class<?> clazz, Object bundle,
+      String serviceFilter) {
+    Object[] instances = getGlobalInstances(clazz, bundle, serviceFilter);
+    if (instances != null) {
+      return instances[0];
+    }
+    return null;
+  }
+
+  /**
+   * Retrieve all the Instances of a Service, optionally filtered via
+   * serviceFilter if non-null else all the results are returned if null
+   *
+   * @param clazz
+   *          The target class
+   * @param bundle
+   *          The caller
+   * @param serviceFilter
+   *          LDAP filter to be applied in the search
+   */
+  @SuppressWarnings("unchecked")
+  public static Object[] getGlobalInstances(Class<?> clazz, Object bundle,
+      String serviceFilter) {
+    Object instances[] = null;
+    try {
+      BundleContext bCtx = FrameworkUtil.getBundle(bundle.getClass())
+          .getBundleContext();
+
+      ServiceReference[] services = bCtx.getServiceReferences(clazz.getName(),
+          serviceFilter);
+
+      if (services != null) {
+        instances = new Object[services.length];
+        for (int i = 0; i < services.length; i++) {
+          instances[i] = bCtx.getService(services[i]);
+        }
+      }
+    } catch (Exception e) {
+      logger.error("Instance reference is NULL");
+    }
+    return instances;
+  }
+}
index a15f35092c2904ff103de073e6fad0f627d2b7aa..94e92e93fc32d6105035f214e8d181e293de34c0 100644 (file)
       <plugin>
         <groupId>org.codehaus.enunciate</groupId>
         <artifactId>maven-enunciate-plugin</artifactId>
-        <dependencies>
-          <dependency>
-            <groupId>org.opendaylight.controller</groupId>
-            <artifactId>sal</artifactId>
-            <version>${sal.version}</version>
-          </dependency>
-        </dependencies>
       </plugin>
 
       <plugin>
@@ -39,7 +32,6 @@
               org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924,
               org.apache.commons.logging,
               com.sun.jersey.spi.container.servlet,
-              org.opendaylight.controller.sal.utils,
               org.opendaylight.controller.sal.authorization,
               org.opendaylight.controller.sal.packet.address,
               javax.ws.rs,
index 09abd6621fe4fa3a9fc89413c6e45f640bccfb18..9a33a19052096b9cfdd02cff4468e338a67d8ebf 100644 (file)
@@ -16,6 +16,7 @@ import javax.ws.rs.Produces;
 import javax.ws.rs.core.Response;
 import javax.ws.rs.core.Response.Status;
 
+import org.opendaylight.alto.commons.helper.ServiceHelper;
 import org.opendaylight.alto.commons.types.rfc7285.RFC7285JSONMapper;
 import org.opendaylight.alto.commons.types.rfc7285.FormatValidator;
 import org.opendaylight.alto.commons.types.rfc7285.MediaType;
@@ -25,20 +26,14 @@ import org.opendaylight.alto.commons.types.rfc7285.RFC7285IRD;
 import org.opendaylight.alto.commons.types.rfc7285.RFC7285VersionTag;
 import org.opendaylight.alto.commons.types.rfc7285.RFC7285CostMap;
 import org.opendaylight.alto.commons.types.rfc7285.RFC7285Endpoint;
-
 import org.opendaylight.alto.services.api.rfc7285.AltoService;
 import org.opendaylight.alto.services.api.rfc7285.IRDService;
 import org.opendaylight.alto.services.api.rfc7285.NetworkMapService;
 import org.opendaylight.alto.services.api.rfc7285.CostMapService;
 import org.opendaylight.alto.services.api.rfc7285.EndpointCostService;
 import org.opendaylight.alto.services.api.rfc7285.EndpointPropertyService;
-
-
 import org.opendaylight.alto.northbound.exception.AltoBasicException;
 import org.opendaylight.alto.northbound.exception.AltoBadFormatException;
-
-import org.opendaylight.controller.sal.utils.ServiceHelper;
-
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -49,6 +44,7 @@ public class AltoNorthbound {
 
     private RFC7285JSONMapper mapper = new RFC7285JSONMapper();
 
+    @SuppressWarnings("unchecked")
     private <E> E getService(Class<E> clazz) {
         E service = (E)ServiceHelper.getGlobalInstance(clazz, this);
         if (service == null) {
index ccc41252c47a29bc61e820534bb80f7d43eb9457..4145914bf6876f0a147792b9c02c4da69656d7ee 100644 (file)
       <scope>test</scope>
     </dependency>
 
-    <dependency>
-      <groupId>org.opendaylight.controller</groupId>
-      <artifactId>sal</artifactId>
-      <version>0.10.0-SNAPSHOT</version>
-    </dependency>
-
     <dependency>
       <groupId>org.opendaylight.controller</groupId>
       <artifactId>config-api</artifactId>
index 776a38ffab9d7935d21b127d8581760faca33697..3a452799e26a985a6b92a0c724ffae6af358d9ee 100644 (file)
@@ -2,16 +2,14 @@ package org.opendaylight.alto.services.provider.simple;
 
 import org.opendaylight.alto.services.api.rfc7285.NetworkMapService;
 import org.opendaylight.alto.services.api.rfc7285.CostMapService;
-
+import org.opendaylight.alto.commons.helper.ServiceHelper;
 import org.opendaylight.alto.commons.types.rfc7285.RFC7285NetworkMap;
 import org.opendaylight.alto.commons.types.rfc7285.RFC7285Endpoint;
 import org.opendaylight.alto.commons.types.rfc7285.RFC7285VersionTag;
 import org.opendaylight.alto.commons.types.rfc7285.RFC7285CostMap;
 import org.opendaylight.alto.commons.types.rfc7285.RFC7285CostType;
-
 import org.opendaylight.alto.commons.types.converter.YANGJSON2RFCNetworkMapConverter;
 import org.opendaylight.alto.commons.types.converter.YANGJSON2RFCCostMapConverter;
-
 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
 import org.opendaylight.yangtools.yang.binding.Augmentation;
 import org.opendaylight.yangtools.yang.binding.util.BindingReflections;
@@ -27,12 +25,9 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.alto.rev150404.cost.map.map
 import org.opendaylight.yang.gen.v1.urn.opendaylight.alto.service.types.rev150404.ResourceId;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.alto.service.types.rev150404.ird.meta.DefaultAltoNetworkMap;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.alto.service.types.rev150404.ird.Meta;
-
 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
 import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
-
-import org.opendaylight.controller.sal.utils.ServiceHelper;
 import org.osgi.framework.ServiceRegistration;
 
 import com.google.common.base.Optional;
@@ -54,6 +49,7 @@ import com.fasterxml.jackson.databind.JsonSerializer;
 import com.fasterxml.jackson.databind.SerializerProvider;
 import com.fasterxml.jackson.databind.module.SimpleModule;
 
+@SuppressWarnings("rawtypes")
 public class SimpleAltoService implements NetworkMapService, CostMapService, AutoCloseable {
 
     private final Logger m_logger = LoggerFactory.getLogger(SimpleAltoService.class);