Add binding adapter components for RPC services
[mdsal.git] / binding / mdsal-binding-dom-adapter / src / main / java / org / opendaylight / mdsal / binding / dom / adapter / osgi / DynamicBindingAdapter.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 com.google.common.collect.ImmutableList;
11 import java.util.List;
12 import org.opendaylight.mdsal.binding.api.ActionProviderService;
13 import org.opendaylight.mdsal.binding.api.ActionService;
14 import org.opendaylight.mdsal.binding.api.BindingService;
15 import org.opendaylight.mdsal.binding.api.DataBroker;
16 import org.opendaylight.mdsal.binding.api.DataTreeService;
17 import org.opendaylight.mdsal.binding.api.MountPointService;
18 import org.opendaylight.mdsal.binding.api.NotificationPublishService;
19 import org.opendaylight.mdsal.binding.api.NotificationService;
20 import org.opendaylight.mdsal.binding.api.RpcConsumerRegistry;
21 import org.opendaylight.mdsal.binding.api.RpcProviderService;
22 import org.opendaylight.mdsal.binding.dom.adapter.spi.AdapterFactory;
23 import org.opendaylight.mdsal.dom.api.DOMActionProviderService;
24 import org.opendaylight.mdsal.dom.api.DOMActionService;
25 import org.opendaylight.mdsal.dom.api.DOMDataBroker;
26 import org.opendaylight.mdsal.dom.api.DOMDataTreeService;
27 import org.opendaylight.mdsal.dom.api.DOMMountPointService;
28 import org.opendaylight.mdsal.dom.api.DOMNotificationPublishService;
29 import org.opendaylight.mdsal.dom.api.DOMNotificationService;
30 import org.opendaylight.mdsal.dom.api.DOMRpcProviderService;
31 import org.opendaylight.mdsal.dom.api.DOMRpcService;
32 import org.opendaylight.mdsal.dom.api.DOMService;
33 import org.osgi.framework.BundleContext;
34 import org.osgi.service.component.ComponentFactory;
35 import org.osgi.service.component.annotations.Activate;
36 import org.osgi.service.component.annotations.Component;
37 import org.osgi.service.component.annotations.Deactivate;
38 import org.osgi.service.component.annotations.Reference;
39 import org.osgi.util.tracker.ServiceTracker;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
42
43 /**
44  * A component which watches the OSGi Service Registry for known {@link DOMService}s and publishes corresponding
45  * {@link BindingService}s backed by them.
46  *
47  * @author Robert Varga
48  */
49 @Component(immediate = true)
50 public final class DynamicBindingAdapter {
51     private static final Logger LOG = LoggerFactory.getLogger(DynamicBindingAdapter.class);
52
53     private List<AbstractAdaptingTracker<?, ?, ?>> trackers = ImmutableList.of();
54
55     @Reference
56     AdapterFactory factory = null;
57     @Reference(target = "(component.factory=" + OSGiRpcConsumerRegistry.FACTORY_NAME + ")")
58     ComponentFactory rpcConsumerRegistryFactory = null;
59     @Reference(target = "(component.factory=" + OSGiRpcProviderService.FACTORY_NAME + ")")
60     ComponentFactory rpcProviderServiceFactory = null;
61
62     @Activate
63     void activate(final BundleContext ctx) {
64         trackers = ImmutableList.of(
65             new AdaptingTracker<>(ctx, DOMDataBroker.class, DataBroker.class, factory::createDataBroker),
66             new AdaptingTracker<>(ctx, DOMDataTreeService.class, DataTreeService.class, factory::createDataTreeService),
67             new AdaptingTracker<>(ctx, DOMMountPointService.class, MountPointService.class,
68                     factory::createMountPointService),
69             new AdaptingTracker<>(ctx, DOMNotificationService.class, NotificationService.class,
70                     factory::createNotificationService),
71             new AdaptingTracker<>(ctx, DOMNotificationPublishService.class, NotificationPublishService.class,
72                     factory::createNotificationPublishService),
73             new AdaptingComponentTracker<>(ctx, DOMRpcService.class, RpcConsumerRegistry.class,
74                     factory::createRpcConsumerRegistry, rpcConsumerRegistryFactory),
75             new AdaptingComponentTracker<>(ctx, DOMRpcProviderService.class, RpcProviderService.class,
76                     factory::createRpcProviderService, rpcProviderServiceFactory),
77             new AdaptingTracker<>(ctx, DOMActionService.class, ActionService.class, factory::createActionService),
78             new AdaptingTracker<>(ctx, DOMActionProviderService.class, ActionProviderService.class,
79                 factory::createActionProviderService));
80
81         LOG.debug("Starting {} DOMService trackers", trackers.size());
82         trackers.forEach(ServiceTracker::open);
83         LOG.info("{} DOMService trackers started", trackers.size());
84     }
85
86     @Deactivate
87     void deactivate() {
88         LOG.debug("Stopping {} DOMService trackers", trackers.size());
89         if (!trackers.isEmpty()) {
90             trackers.forEach(AbstractAdaptingTracker::close);
91             LOG.info("{} DOMService trackers stopped", trackers.size());
92         }
93         trackers = ImmutableList.of();
94     }
95 }