/*
 * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
 *
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
 * and is available at http://www.eclipse.org/legal/epl-v10.html
 */
package org.opendaylight.bgpcep.topology.provider.bgp.impl;

import org.opendaylight.bgpcep.topology.provider.bgp.LocRIBListener;
import org.opendaylight.bgpcep.topology.provider.bgp.LocRIBListeners;
import org.opendaylight.controller.md.sal.common.api.data.DataChangeEvent;
import org.opendaylight.controller.md.sal.common.api.data.DataModification;
import org.opendaylight.controller.sal.binding.api.data.DataChangeListener;
import org.opendaylight.controller.sal.binding.api.data.DataProviderService;
import org.opendaylight.protocol.concepts.ListenerRegistration;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.LocRib;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.rib.Tables;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.rib.TablesKey;
import org.opendaylight.yangtools.yang.binding.DataObject;
import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceReference;
import org.osgi.util.tracker.ServiceTracker;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.common.base.Preconditions;

final class LocRIBListenerSubscriptionTracker extends ServiceTracker<LocRIBListeners.Subscribtion, ListenerRegistration<LocRIBListener>> {
	private static final InstanceIdentifier<LocRib> locRIBPath = InstanceIdentifier.builder().node(LocRib.class).toInstance();
	private static final Logger logger = LoggerFactory.getLogger(LocRIBListenerSubscriptionTracker.class);
	private final DataProviderService dps;

	LocRIBListenerSubscriptionTracker(final BundleContext context, final DataProviderService dps) {
		super(context, LocRIBListeners.Subscribtion.class, null);
		this.dps = Preconditions.checkNotNull(dps);
	}

	@Override
	public ListenerRegistration<LocRIBListener> addingService(final ServiceReference<LocRIBListeners.Subscribtion> reference) {
		final LocRIBListeners.Subscribtion service = context.getService(reference);
		if (service == null) {
			logger.trace("Service for reference {} disappeared", reference);
			return null;
		}

		final InstanceIdentifier<Tables> path = InstanceIdentifier.builder(locRIBPath).
				node(Tables.class, new TablesKey(service.getAfi(), service.getSafi())).toInstance();
		final LocRIBListener listener = service.getLocRIBListener();

		final DataChangeListener dcl = new DataChangeListener() {

			@Override
			public void onDataChanged(final DataChangeEvent<InstanceIdentifier<?>, DataObject> change) {
				final DataModification trans = dps.beginTransaction();

				try {
					listener.onLocRIBChange(trans, change);
				} catch (Exception e) {
					logger.info("Data change {} was not completely propagated to listener {}", change, listener, e);
				}

				// FIXME: abort the transaction if it's not committing
			}
		};

		dps.registerChangeListener(path, dcl);

		return new ListenerRegistration<LocRIBListener>(listener) {
			@Override
			protected void removeRegistration() {
				dps.unregisterChangeListener(path, dcl);
			}
		};
	}

	@Override
	public void removedService(final ServiceReference<LocRIBListeners.Subscribtion> reference, final ListenerRegistration<LocRIBListener> service) {
		service.close();
		context.ungetService(reference);
	}
}

@@ -809,72 +809,8 @@
 ce;%0A
-import org.opendaylight.protocol.concepts.ListenerRegistration;%0A
 impo
@@ -1110,16 +1110,81 @@
 lesKey;%0A
+import org.opendaylight.yangtools.concepts.ListenerRegistration;%0A
 import o
@@ -1827,22 +1827,19 @@
  Logger 
-logger
+LOG
  = Logge
@@ -2389,22 +2389,19 @@
 l) %7B%0A%09%09%09
-logger
+LOG
 .trace(%22
@@ -2903,16 +2903,22 @@
 fication
+%3C?, ?%3E
  trans =
@@ -3035,14 +3035,11 @@
 %09%09%09%09
-logger
+LOG
 .inf
@@ -3204,16 +3204,69 @@
 %09%09%7D;%0A%0A%09%09
+final ListenerRegistration%3CDataChangeListener%3E reg = 
 dps.regi
@@ -3261,32 +3261,36 @@
 g = dps.register
+Data
 ChangeListener(p
@@ -3351,24 +3351,16 @@
 stener%3E(
-listener
 ) %7B%0A%09%09%09@
@@ -3376,88 +3376,142 @@
 %09%09%09p
-rotected void removeRegistra
+ublic void close() throws Excep
 tion
-()
  %7B%0A%09%09%09%09
-dps.unregisterChangeListener(path, dcl)
+reg.close();%0A%09%09%09%7D%0A%0A%09%09%09@Override%0A%09%09%09public LocRIBListener getInstance() %7B%0A%09%09%09%09return listener
 ;%0A%09%09
@@ -3679,16 +3679,25 @@
 vice) %7B%0A
+%09%09try %7B%0A%09
 %09%09servic
@@ -3707,16 +3707,98 @@
 lose();%0A
+%09%09%7D catch (Exception e) %7B%0A%09%09%09LOG.error(%22Failed to unregister service %7B%7D%22, e);%0A%09%09%7D%0A
 %09%09contex

Switch to yangtools ListenerRegistration