2 * Copyright (c) 2018 Pantheon Technologies, s.r.o. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.mdsal.binding.dom.adapter.osgi;
10 import static java.util.Objects.requireNonNull;
12 import java.util.function.Function;
13 import org.eclipse.jdt.annotation.NonNull;
14 import org.opendaylight.mdsal.binding.api.BindingService;
15 import org.opendaylight.mdsal.dom.api.DOMService;
16 import org.osgi.framework.BundleContext;
17 import org.osgi.framework.ServiceReference;
18 import org.osgi.util.tracker.ServiceTracker;
19 import org.slf4j.Logger;
20 import org.slf4j.LoggerFactory;
23 * A ServiceTracker which adapts a DOMService to a BindingService.
25 * @param <D> DOMService type
26 * @param <B> BindingService type
27 * @author Robert Varga
29 abstract class AbstractAdaptingTracker<D extends DOMService, B extends BindingService, T>
30 extends ServiceTracker<D, T> {
31 private static final Logger LOG = LoggerFactory.getLogger(AbstractAdaptingTracker.class);
33 private final Function<D, B> bindingFactory;
34 final @NonNull Class<B> bindingClass;
36 AbstractAdaptingTracker(final BundleContext ctx, final Class<D> domClass, final Class<B> bindingClass,
37 final Function<D, B> bindingFactory) {
38 super(ctx, domClass, null);
39 this.bindingClass = requireNonNull(bindingClass);
40 this.bindingFactory = requireNonNull(bindingFactory);
44 public final void open(final boolean trackAllServices) {
45 LOG.debug("Starting tracker for {}", bindingClass.getName());
46 super.open(trackAllServices);
47 LOG.debug("Tracker for {} started", bindingClass.getName());
51 public final T addingService(final ServiceReference<D> reference) {
52 if (reference == null) {
53 LOG.debug("Null reference for {}, ignoring it", bindingClass.getName());
56 if (reference.getProperty(ServiceProperties.IGNORE_PROP) != null) {
57 LOG.debug("Ignoring reference {} due to {}", reference, ServiceProperties.IGNORE_PROP);
61 final D dom = context.getService(reference);
63 LOG.debug("Could not get {} service from {}, ignoring it", bindingClass.getName(), reference);
67 return addingService(reference, dom, bindingFactory.apply(dom));
70 abstract @NonNull T addingService(@NonNull ServiceReference<D> reference, @NonNull D dom, @NonNull B binding);
73 public final void modifiedService(final ServiceReference<D> reference, final T service) {
74 if (service != null && reference != null) {
75 updatedService(reference, service);
79 abstract void updatedService(@NonNull ServiceReference<D> reference, @NonNull T service);
82 public final void removedService(final ServiceReference<D> reference, final T service) {
83 if (service != null) {
84 context.ungetService(reference);
85 removedService(service);
86 LOG.debug("Unregistered service {}", service);
90 abstract void removedService(@NonNull T service);