5cc9bdb36f798e0f80a81edfc8c00a66285d38c8
[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<AdaptingTracker<?, ?>> trackers = ImmutableList.of();
54
55     @Reference
56     AdapterFactory factory = null;
57     @Reference(target = "(component.factory=" + OSGiActionService.FACTORY_NAME + ")")
58     ComponentFactory actionServiceFactory = null;
59     @Reference(target = "(component.factory=" + OSGiActionProviderService.FACTORY_NAME + ")")
60     ComponentFactory actionProviderServiceFactory = null;
61     @Reference(target = "(component.factory=" + OSGiDataBroker.FACTORY_NAME + ")")
62     ComponentFactory dataBrokerFactory = null;
63     @Reference(target = "(component.factory=" + OSGiDataTreeService.FACTORY_NAME + ")")
64     ComponentFactory dataTreeServiceFactory = null;
65     @Reference(target = "(component.factory=" + OSGiMountPointService.FACTORY_NAME + ")")
66     ComponentFactory mountPointServiceFactory = null;
67     @Reference(target = "(component.factory=" + OSGiNotificationService.FACTORY_NAME + ")")
68     ComponentFactory notificationServiceFactory = null;
69     @Reference(target = "(component.factory=" + OSGiNotificationPublishService.FACTORY_NAME + ")")
70     ComponentFactory notificationPublishServiceFactory = null;
71     @Reference(target = "(component.factory=" + OSGiRpcConsumerRegistry.FACTORY_NAME + ")")
72     ComponentFactory rpcConsumerRegistryFactory = null;
73     @Reference(target = "(component.factory=" + OSGiRpcProviderService.FACTORY_NAME + ")")
74     ComponentFactory rpcProviderServiceFactory = null;
75
76     @Activate
77     void activate(final BundleContext ctx) {
78         trackers = ImmutableList.of(
79             new AdaptingTracker<>(ctx, DOMDataBroker.class, DataBroker.class, factory::createDataBroker,
80                     dataBrokerFactory),
81             new AdaptingTracker<>(ctx, DOMDataTreeService.class, DataTreeService.class, factory::createDataTreeService,
82                     dataTreeServiceFactory),
83             new AdaptingTracker<>(ctx, DOMMountPointService.class, MountPointService.class,
84                     factory::createMountPointService, mountPointServiceFactory),
85             new AdaptingTracker<>(ctx, DOMNotificationService.class, NotificationService.class,
86                     factory::createNotificationService, notificationServiceFactory),
87             new AdaptingTracker<>(ctx, DOMNotificationPublishService.class, NotificationPublishService.class,
88                     factory::createNotificationPublishService, notificationPublishServiceFactory),
89             new AdaptingTracker<>(ctx, DOMRpcService.class, RpcConsumerRegistry.class,
90                     factory::createRpcConsumerRegistry, rpcConsumerRegistryFactory),
91             new AdaptingTracker<>(ctx, DOMRpcProviderService.class, RpcProviderService.class,
92                     factory::createRpcProviderService, rpcProviderServiceFactory),
93             new AdaptingTracker<>(ctx, DOMActionService.class, ActionService.class, factory::createActionService,
94                     actionServiceFactory),
95             new AdaptingTracker<>(ctx, DOMActionProviderService.class, ActionProviderService.class,
96                     factory::createActionProviderService, actionProviderServiceFactory));
97
98         LOG.debug("Starting {} DOMService trackers", trackers.size());
99         trackers.forEach(ServiceTracker::open);
100         LOG.info("{} DOMService trackers started", trackers.size());
101     }
102
103     @Deactivate
104     void deactivate() {
105         LOG.debug("Stopping {} DOMService trackers", trackers.size());
106         if (!trackers.isEmpty()) {
107             trackers.forEach(AdaptingTracker::close);
108             LOG.info("{} DOMService trackers stopped", trackers.size());
109         }
110         trackers = ImmutableList.of();
111     }
112 }