Add binding adapter components for RPC services
[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 java.util.function.Function;
11 import org.opendaylight.mdsal.binding.api.BindingService;
12 import org.opendaylight.mdsal.dom.api.DOMService;
13 import org.osgi.framework.BundleContext;
14 import org.osgi.framework.ServiceReference;
15 import org.osgi.framework.ServiceRegistration;
16 import org.slf4j.Logger;
17 import org.slf4j.LoggerFactory;
18
19 /**
20  * A ServiceTracker which adapts a DOMService to a BindingService.
21  *
22  * @param <D> DOMService type
23  * @param <B> BindingService type
24  * @author Robert Varga
25  */
26 final class AdaptingTracker<D extends DOMService, B extends BindingService>
27         extends AbstractAdaptingTracker<D, B, ServiceRegistration<B>> {
28     private static final Logger LOG = LoggerFactory.getLogger(AdaptingTracker.class);
29
30     AdaptingTracker(final BundleContext ctx, final Class<D> domClass, final Class<B> bindingClass,
31             final Function<D, B> bindingFactory) {
32         super(ctx, domClass, bindingClass, bindingFactory);
33     }
34
35     @Override
36     ServiceRegistration<B> addingService(final ServiceReference<D> reference, final D dom, final B binding) {
37         final Dict props = Dict.fromReference(reference);
38         final ServiceRegistration<B> reg = context.registerService(bindingClass, binding, props);
39         LOG.debug("Registered {} adapter {} of {} with {} as {}", bindingClass.getName(), binding, dom, props, reg);
40         return reg;
41     }
42
43     @Override
44     void removedService(final ServiceRegistration<B> service) {
45         service.unregister();
46     }
47
48     @Override
49     void updatedService(final ServiceReference<D> reference, final ServiceRegistration<B> service) {
50         final Dict newProps = Dict.fromReference(reference);
51         LOG.debug("Updating service {} with properties {}", service, newProps);
52         service.setProperties(newProps);
53     }
54 }