X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=opendaylight%2Fconfig%2Fyang-store-impl%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fconfig%2Fyang%2Fstore%2Fimpl%2FExtenderYangTracker.java;fp=opendaylight%2Fconfig%2Fyang-store-impl%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fconfig%2Fyang%2Fstore%2Fimpl%2FExtenderYangTracker.java;h=ee2864878d1c0a7b5e2bdfb27ad49344d79b46fc;hb=85f16942a4a1ccdf1bc10213cdedfa09f7d325b8;hp=e3be7346248809ac75ec26333ef3fa8a3f2bf3e4;hpb=71ade3766ac30f55774543c0985f67541a369c78;p=controller.git diff --git a/opendaylight/config/yang-store-impl/src/main/java/org/opendaylight/controller/config/yang/store/impl/ExtenderYangTracker.java b/opendaylight/config/yang-store-impl/src/main/java/org/opendaylight/controller/config/yang/store/impl/ExtenderYangTracker.java index e3be734624..ee2864878d 100644 --- a/opendaylight/config/yang-store-impl/src/main/java/org/opendaylight/controller/config/yang/store/impl/ExtenderYangTracker.java +++ b/opendaylight/config/yang-store-impl/src/main/java/org/opendaylight/controller/config/yang/store/impl/ExtenderYangTracker.java @@ -10,13 +10,13 @@ package org.opendaylight.controller.config.yang.store.impl; import java.io.IOException; import java.io.InputStream; import java.net.URL; -import java.util.Collection; -import java.util.Enumeration; -import java.util.Set; +import java.util.*; import org.opendaylight.controller.config.yang.store.api.YangStoreException; +import org.opendaylight.controller.config.yang.store.api.YangStoreListenerRegistration; import org.opendaylight.controller.config.yang.store.api.YangStoreService; import org.opendaylight.controller.config.yang.store.api.YangStoreSnapshot; +import org.opendaylight.controller.config.yang.store.spi.YangStoreListener; import org.osgi.framework.Bundle; import org.osgi.framework.BundleContext; import org.osgi.framework.BundleEvent; @@ -43,6 +43,7 @@ public class ExtenderYangTracker extends BundleTracker implements .create(); private final YangStoreCache cache = new YangStoreCache(); private final MbeParser mbeParser; + private final List listeners = new ArrayList<>(); public ExtenderYangTracker(BundleContext context) { this(context, new MbeParser()); @@ -59,38 +60,54 @@ public class ExtenderYangTracker extends BundleTracker implements @Override public Object addingBundle(Bundle bundle, BundleEvent event) { - // Ignore system bundle - // - // system bundle has config-api on classpath && + // Ignore system bundle: + // system bundle might have config-api on classpath && // config-api contains yang files => - // system bundle contains yang files from that bundle + // system bundle might contain yang files from that bundle if (bundle.getBundleId() == 0) return bundle; - Enumeration yangURLs = bundle.findEntries("META-INF/yang", - "*.yang", false); - - if (yangURLs == null) - return bundle; - - synchronized (this) { - while (yangURLs.hasMoreElements()) { - URL yang = yangURLs.nextElement(); - logger.debug("Bundle {} found yang file {}", bundle, yang); - bundlesToYangURLs.put(bundle, yang); + Enumeration yangURLs = bundle.findEntries("META-INF/yang", "*.yang", false); + if (yangURLs != null) { + synchronized (this) { + while (yangURLs.hasMoreElements()) { + URL url = yangURLs.nextElement(); + logger.debug("Bundle {} found yang file {}", bundle, url); + bundlesToYangURLs.put(bundle, url); + } + Collection urls = bundlesToYangURLs.get(bundle); + notifyListeners(urls, true); } } - return bundle; } + private void notifyListeners(Collection urls, boolean adding) { + if (urls.size() > 0) { + RuntimeException potential = new RuntimeException("Error while notifying listeners"); + for (YangStoreListener listener : listeners) { + try { + if (adding) { + listener.onAddedYangURL(urls); + } else { + listener.onRemovedYangURL(urls); + } + } catch(RuntimeException e) { + potential.addSuppressed(e); + } + } + if (potential.getSuppressed().length > 0) { + throw potential; + } + } + } + @Override - public void removedBundle(Bundle bundle, BundleEvent event, Object object) { - synchronized (this) { - Collection urls = bundlesToYangURLs.removeAll(bundle); - logger.debug( - "Removed following yang URLs {} because of removed bundle {}", - urls, bundle); + public synchronized void removedBundle(Bundle bundle, BundleEvent event, Object object) { + Collection urls = bundlesToYangURLs.removeAll(bundle); + if (urls.size() > 0) { + logger.debug("Removed following yang URLs {} because of removed bundle {}", urls, bundle); + notifyListeners(urls, false); } } @@ -139,6 +156,17 @@ public class ExtenderYangTracker extends BundleTracker implements }); } + @Override + public synchronized YangStoreListenerRegistration registerListener(final YangStoreListener listener) { + listeners.add(listener); + return new YangStoreListenerRegistration() { + @Override + public void close() { + listeners.remove(listener); + } + }; + } + private static final class YangStoreCache { Set cachedUrls;