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.opendaylight.mdsal.binding.api.BindingService;
14 import org.opendaylight.mdsal.dom.api.DOMService;
15 import org.osgi.framework.BundleContext;
16 import org.osgi.framework.ServiceReference;
17 import org.osgi.framework.ServiceRegistration;
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 final class AdaptingTracker<D extends DOMService, B extends BindingService>
30 extends ServiceTracker<D, ServiceRegistration<B>> {
31 private static final Logger LOG = LoggerFactory.getLogger(AdaptingTracker.class);
33 private final Function<D, B> bindingFactory;
34 private final Class<B> bindingClass;
36 AdaptingTracker(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 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 ServiceRegistration<B> 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);
66 final B binding = bindingFactory.apply(dom);
67 final Dict props = Dict.fromReference(reference);
68 final ServiceRegistration<B> reg = context.registerService(bindingClass, binding, props);
69 LOG.debug("Registered {} adapter {} of {} with {} as {}", bindingClass.getName(), binding, dom, props, reg);
74 public void modifiedService(final ServiceReference<D> reference, final ServiceRegistration<B> service) {
75 if (service != null && reference != null) {
76 final Dict newProps = Dict.fromReference(reference);
77 LOG.debug("Updating service {} with properties {}", service, newProps);
78 service.setProperties(newProps);
83 public void removedService(final ServiceReference<D> reference, final ServiceRegistration<B> service) {
84 if (service != null) {
85 context.ungetService(reference);
87 LOG.debug("Unregistered service {}", service);