Bump odlparent/yangtools to 7.0.5/5.0.5
[mdsal.git] / binding / mdsal-binding-dom-adapter / src / main / java / org / opendaylight / mdsal / binding / dom / adapter / osgi / AdaptingTracker.java
1 /*
2  * Copyright (c) 2018 Pantheon Technologies, s.r.o. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.mdsal.binding.dom.adapter.osgi;
9
10 import static java.util.Objects.requireNonNull;
11
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;
21
22 /**
23  * A ServiceTracker which adapts a DOMService to a BindingService.
24  *
25  * @param <D> DOMService type
26  * @param <B> BindingService type
27  * @author Robert Varga
28  */
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);
32
33     private final Function<D, B> bindingFactory;
34     private final Class<B> bindingClass;
35
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);
41     }
42
43     @Override
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());
48     }
49
50     @Override
51     public ServiceRegistration<B> addingService(final ServiceReference<D> reference) {
52         if (reference == null) {
53             LOG.debug("Null reference for {}, ignoring it", bindingClass.getName());
54             return null;
55         }
56         if (reference.getProperty(ServiceProperties.IGNORE_PROP) != null) {
57             LOG.debug("Ignoring reference {} due to {}", reference, ServiceProperties.IGNORE_PROP);
58             return null;
59         }
60
61         final D dom = context.getService(reference);
62         if (dom == null) {
63             LOG.debug("Could not get {} service from {}, ignoring it", bindingClass.getName(), reference);
64             return null;
65         }
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);
70         return reg;
71     }
72
73     @Override
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);
79         }
80     }
81
82     @Override
83     public void removedService(final ServiceReference<D> reference, final ServiceRegistration<B> service) {
84         if (service != null) {
85             context.ungetService(reference);
86             service.unregister();
87             LOG.debug("Unregistered service {}", service);
88         }
89     }
90 }