Add binding adapter components for notification services
[mdsal.git] / binding / mdsal-binding-dom-adapter / src / main / java / org / opendaylight / mdsal / binding / dom / adapter / osgi / AbstractAdaptingTracker.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.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;
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 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);
32
33     private final Function<D, B> bindingFactory;
34     final @NonNull Class<B> bindingClass;
35
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);
41     }
42
43     @Override
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());
48     }
49
50     @Override
51     public final T 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
67         return addingService(reference, dom, bindingFactory.apply(dom));
68     }
69
70     abstract @NonNull T addingService(@NonNull ServiceReference<D> reference, @NonNull D dom, @NonNull B binding);
71
72     @Override
73     public final void modifiedService(final ServiceReference<D> reference, final T service) {
74         if (service != null && reference != null) {
75             updatedService(reference, service);
76         }
77     }
78
79     abstract void updatedService(@NonNull ServiceReference<D> reference, @NonNull T service);
80
81     @Override
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);
87         }
88     }
89
90     abstract void removedService(@NonNull T service);
91
92 }