<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
</dependency>
+ <dependency>
+ <groupId>org.osgi</groupId>
+ <artifactId>org.osgi.core</artifactId>
+ </dependency>
</dependencies>
</project>
--- /dev/null
+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;
+ }
+}
<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>
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,
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;
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;
private RFC7285JSONMapper mapper = new RFC7285JSONMapper();
+ @SuppressWarnings("unchecked")
private <E> E getService(Class<E> clazz) {
E service = (E)ServiceHelper.getGlobalInstance(clazz, this);
if (service == null) {
<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>
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;
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;
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);