Initial patch to deprecate sal utils Servicehelper 25/16125/2
authorSam Hague <shague@redhat.com>
Fri, 6 Mar 2015 03:20:11 +0000 (22:20 -0500)
committerSam Hague <shague@redhat.com>
Fri, 6 Mar 2015 13:06:26 +0000 (08:06 -0500)
Change-Id: I952e22647bf64f0bd889e1151536751f935bc0eb
Signed-off-by: Sam Hague <shague@redhat.com>
utils/pom.xml
utils/servicehelper/pom.xml [new file with mode: 0644]
utils/servicehelper/src/main/java/org/opendaylight/ovsdb/utils/servicehelper/ServiceHelper.java [new file with mode: 0644]
utils/servicehelper/src/test/java/org/opendaylight/ovsdb/utils/servicehelper/ServiceHelperTest.java [new file with mode: 0644]

index 34b1da19e834bd45a0f91e2d56011cc986d61145..0546955005fa8adc43fc0813a125a4e70e8a4018 100644 (file)
@@ -29,5 +29,6 @@ and is available at http://www.eclipse.org/legal/epl-v10.html
   <modules>
     <module>config</module>
     <module>mdsal-openflow</module>
+    <module>servicehelper</module>
   </modules>
 </project>
diff --git a/utils/servicehelper/pom.xml b/utils/servicehelper/pom.xml
new file mode 100644 (file)
index 0000000..403300a
--- /dev/null
@@ -0,0 +1,72 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+Copyright (C) 2015 Red Hat, Inc. and others. All rights reserved.
+
+This program and the accompanying materials are made available under the
+terms of the Eclipse Public License v1.0 which accompanies this distribution,
+and is available at http://www.eclipse.org/legal/epl-v10.html
+-->
+<project xmlns="http://maven.apache.org/POM/4.0.0"
+         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+  <modelVersion>4.0.0</modelVersion>
+
+  <parent>
+    <artifactId>commons</artifactId>
+    <groupId>org.opendaylight.ovsdb</groupId>
+    <version>1.3.0-SNAPSHOT</version>
+    <relativePath>../../commons/parent/pom.xml</relativePath>
+  </parent>
+
+  <groupId>org.opendaylight.ovsdb</groupId>
+  <artifactId>servicehelper</artifactId>
+  <version>1.3.0-SNAPSHOT</version>
+  <name>${project.artifactId}</name>
+  <packaging>jar</packaging>
+
+  <dependencies>
+    <dependency>
+      <groupId>junit</groupId>
+      <artifactId>junit</artifactId>
+      <scope>test</scope>
+    </dependency>
+    <dependency>
+      <groupId>org.osgi</groupId>
+      <artifactId>org.osgi.core</artifactId>
+    </dependency>
+    <dependency>
+      <groupId>org.slf4j</groupId>
+      <artifactId>slf4j-api</artifactId>
+    </dependency>
+    <dependency>
+      <groupId>org.slf4j</groupId>
+      <artifactId>slf4j-simple</artifactId>
+    </dependency>
+    <dependency>
+      <groupId>org.mockito</groupId>
+      <artifactId>mockito-core</artifactId>
+      <scope>test</scope>
+    </dependency>
+    <dependency>
+      <groupId>org.powermock</groupId>
+      <artifactId>powermock-api-mockito</artifactId>
+      <scope>test</scope>
+    </dependency>
+    <dependency>
+      <groupId>org.powermock</groupId>
+      <artifactId>powermock-core</artifactId>
+      <scope>test</scope>
+    </dependency>
+    <dependency>
+      <groupId>org.powermock</groupId>
+      <artifactId>powermock-module-junit4</artifactId>
+      <scope>test</scope>
+    </dependency>
+    <dependency>
+      <groupId>org.springframework.osgi</groupId>
+      <artifactId>spring-osgi-mock</artifactId>
+      <version>1.2.1</version>
+      <scope>test</scope>
+    </dependency>
+  </dependencies>
+</project>
diff --git a/utils/servicehelper/src/main/java/org/opendaylight/ovsdb/utils/servicehelper/ServiceHelper.java b/utils/servicehelper/src/main/java/org/opendaylight/ovsdb/utils/servicehelper/ServiceHelper.java
new file mode 100644 (file)
index 0000000..f33e907
--- /dev/null
@@ -0,0 +1,86 @@
+/*
+ * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+/**
+ * @file   ServiceHelper.java
+ *
+ * @brief  This class verifies {@link ServiceHelper}
+ */
+
+package org.opendaylight.ovsdb.utils.servicehelper;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.FrameworkUtil;
+import org.osgi.framework.ServiceReference;
+
+/**
+ * The class helps to register and retrieve OSGi service registry
+ */
+public class ServiceHelper {
+    private static final Logger LOG = LoggerFactory.getLogger(ServiceHelper.class);
+    /**
+     * 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
+     */
+    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) {
+            LOG.error("Instance reference is NULL");
+        }
+        return instances;
+    }
+}
diff --git a/utils/servicehelper/src/test/java/org/opendaylight/ovsdb/utils/servicehelper/ServiceHelperTest.java b/utils/servicehelper/src/test/java/org/opendaylight/ovsdb/utils/servicehelper/ServiceHelperTest.java
new file mode 100644 (file)
index 0000000..92db5c4
--- /dev/null
@@ -0,0 +1,51 @@
+/*
+ *  Copyright (C) 2015 Red Hat, Inc.
+ *
+ *  This program and the accompanying materials are made available under the
+ *  terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ *  and is available at http://www.eclipse.org/legal/epl-v10.html
+ *
+ *  Authors : Sam Hague
+ */
+package org.opendaylight.ovsdb.utils.servicehelper;
+
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
+import static org.mockito.Matchers.any;
+
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.osgi.framework.Bundle;
+import org.osgi.framework.FrameworkUtil;
+import org.powermock.api.mockito.PowerMockito;
+import org.powermock.core.classloader.annotations.PrepareForTest;
+import org.powermock.modules.junit4.PowerMockRunner;
+import org.springframework.osgi.mock.MockBundle;
+
+/**
+ * JUnit test for
+ * {@link org.opendaylight.ovsdb.utils.servicehelper.ServiceHelper}
+ */
+@RunWith(PowerMockRunner.class)
+@PrepareForTest(FrameworkUtil.class)
+public class ServiceHelperTest {
+    @Test
+    /**
+     * Test method for
+     * {@link ServiceHelper#getGlobalInstance(Class, Object)}
+     */
+    public void getGlobalInstanceTest () {
+        Bundle bundle = new MockBundle();
+
+        PowerMockito.mockStatic(FrameworkUtil.class);
+        PowerMockito.when(FrameworkUtil.getBundle(any(Class.class)))
+                .thenReturn(null)
+                .thenReturn(bundle);
+
+        Object object = ServiceHelper.getGlobalInstance(Test.class, this);
+        assertNull("Service should be null", object);
+
+        object = ServiceHelper.getGlobalInstance(Test.class, this);
+        assertNotNull("Service should not be null", object);
+    }
+}