Switch to yangtools ListenerRegistration
[bgpcep.git] / topology / provider-bgp / src / main / java / org / opendaylight / bgpcep / topology / provider / bgp / impl / LocRIBListenerSubscriptionTracker.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. 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.bgpcep.topology.provider.bgp.impl;
9
10 import org.opendaylight.bgpcep.topology.provider.bgp.LocRIBListener;
11 import org.opendaylight.bgpcep.topology.provider.bgp.LocRIBListeners;
12 import org.opendaylight.controller.md.sal.common.api.data.DataChangeEvent;
13 import org.opendaylight.controller.md.sal.common.api.data.DataModification;
14 import org.opendaylight.controller.sal.binding.api.data.DataChangeListener;
15 import org.opendaylight.controller.sal.binding.api.data.DataProviderService;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.LocRib;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.rib.Tables;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.rib.TablesKey;
19 import org.opendaylight.yangtools.concepts.ListenerRegistration;
20 import org.opendaylight.yangtools.yang.binding.DataObject;
21 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
22 import org.osgi.framework.BundleContext;
23 import org.osgi.framework.ServiceReference;
24 import org.osgi.util.tracker.ServiceTracker;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
27
28 import com.google.common.base.Preconditions;
29
30 final class LocRIBListenerSubscriptionTracker extends ServiceTracker<LocRIBListeners.Subscribtion, ListenerRegistration<LocRIBListener>> {
31         private static final InstanceIdentifier<LocRib> locRIBPath = InstanceIdentifier.builder().node(LocRib.class).toInstance();
32         private static final Logger LOG = LoggerFactory.getLogger(LocRIBListenerSubscriptionTracker.class);
33         private final DataProviderService dps;
34
35         LocRIBListenerSubscriptionTracker(final BundleContext context, final DataProviderService dps) {
36                 super(context, LocRIBListeners.Subscribtion.class, null);
37                 this.dps = Preconditions.checkNotNull(dps);
38         }
39
40         @Override
41         public ListenerRegistration<LocRIBListener> addingService(final ServiceReference<LocRIBListeners.Subscribtion> reference) {
42                 final LocRIBListeners.Subscribtion service = context.getService(reference);
43                 if (service == null) {
44                         LOG.trace("Service for reference {} disappeared", reference);
45                         return null;
46                 }
47
48                 final InstanceIdentifier<Tables> path = InstanceIdentifier.builder(locRIBPath).
49                                 node(Tables.class, new TablesKey(service.getAfi(), service.getSafi())).toInstance();
50                 final LocRIBListener listener = service.getLocRIBListener();
51
52                 final DataChangeListener dcl = new DataChangeListener() {
53
54                         @Override
55                         public void onDataChanged(final DataChangeEvent<InstanceIdentifier<?>, DataObject> change) {
56                                 final DataModification<?, ?> trans = dps.beginTransaction();
57
58                                 try {
59                                         listener.onLocRIBChange(trans, change);
60                                 } catch (Exception e) {
61                                         LOG.info("Data change {} was not completely propagated to listener {}", change, listener, e);
62                                 }
63
64                                 // FIXME: abort the transaction if it's not committing
65                         }
66                 };
67
68                 final ListenerRegistration<DataChangeListener> reg = dps.registerDataChangeListener(path, dcl);
69
70                 return new ListenerRegistration<LocRIBListener>() {
71                         @Override
72                         public void close() throws Exception {
73                                 reg.close();
74                         }
75
76                         @Override
77                         public LocRIBListener getInstance() {
78                                 return listener;
79                         }
80                 };
81         }
82
83         @Override
84         public void removedService(final ServiceReference<LocRIBListeners.Subscribtion> reference, final ListenerRegistration<LocRIBListener> service) {
85                 try {
86                         service.close();
87                 } catch (Exception e) {
88                         LOG.error("Failed to unregister service {}", e);
89                 }
90                 context.ungetService(reference);
91         }
92 }