<modules>
<module>config</module>
<module>mdsal-openflow</module>
+ <module>servicehelper</module>
</modules>
</project>
--- /dev/null
+<?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>
--- /dev/null
+/*
+ * 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;
+ }
+}
--- /dev/null
+/*
+ * 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);
+ }
+}