From: Tomas Olvecky Date: Tue, 22 Oct 2013 12:11:09 +0000 (+0200) Subject: Add change notification functionality to yang-store. X-Git-Tag: jenkins-controller-bulk-release-prepare-only-2-1~583 X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?p=controller.git;a=commitdiff_plain;h=85f16942a4a1ccdf1bc10213cdedfa09f7d325b8 Add change notification functionality to yang-store. Add ability to listen for change notifications to yang-store. Listeners get notified when new yang files are found in bundles and also when bundle with yang files is stopped. Change-Id: I7c665a57ba7face18bf128d6a7db17c1a23526ce Signed-off-by: Tomas Olvecky --- diff --git a/opendaylight/commons/opendaylight/pom.xml b/opendaylight/commons/opendaylight/pom.xml index 77edba5b38..10b4ceab81 100644 --- a/opendaylight/commons/opendaylight/pom.xml +++ b/opendaylight/commons/opendaylight/pom.xml @@ -77,6 +77,7 @@ 0.4.1-SNAPSHOT 0.2.1-SNAPSHOT 0.2.2-SNAPSHOT + 0.2.2-SNAPSHOT 1.0-SNAPSHOT 0.5.1-SNAPSHOT 0.5.1-SNAPSHOT diff --git a/opendaylight/config/yang-store-api/pom.xml b/opendaylight/config/yang-store-api/pom.xml index 9388bdc50a..4393903fd5 100644 --- a/opendaylight/config/yang-store-api/pom.xml +++ b/opendaylight/config/yang-store-api/pom.xml @@ -10,12 +10,13 @@ yang-store-api ${project.artifactId} bundle + ${config.yangstore.version} org.opendaylight.controller yang-jmx-generator - 0.2.1-SNAPSHOT + ${config.version} @@ -26,25 +27,17 @@ maven-bundle-plugin - - - - org.opendaylight.controller.config.yangjmxgenerator, org.opendaylight.yangtools.yang.model.api - org.opendaylight.controller.config.yang.store.api + org.opendaylight.controller.config.yang.store.api, + org.opendaylight.controller.config.yang.store.spi - - - org.apache.maven.plugins - maven-jar-plugin - diff --git a/opendaylight/config/yang-store-api/src/main/java/org/opendaylight/controller/config/yang/store/api/YangStoreListenerRegistration.java b/opendaylight/config/yang-store-api/src/main/java/org/opendaylight/controller/config/yang/store/api/YangStoreListenerRegistration.java new file mode 100644 index 0000000000..81015953b4 --- /dev/null +++ b/opendaylight/config/yang-store-api/src/main/java/org/opendaylight/controller/config/yang/store/api/YangStoreListenerRegistration.java @@ -0,0 +1,14 @@ +/* + * 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.controller.config.yang.store.api; + +public interface YangStoreListenerRegistration extends AutoCloseable { + + @Override + void close(); +} diff --git a/opendaylight/config/yang-store-api/src/main/java/org/opendaylight/controller/config/yang/store/api/YangStoreService.java b/opendaylight/config/yang-store-api/src/main/java/org/opendaylight/controller/config/yang/store/api/YangStoreService.java index 15619a88cc..3ac4b84fdb 100644 --- a/opendaylight/config/yang-store-api/src/main/java/org/opendaylight/controller/config/yang/store/api/YangStoreService.java +++ b/opendaylight/config/yang-store-api/src/main/java/org/opendaylight/controller/config/yang/store/api/YangStoreService.java @@ -7,6 +7,8 @@ */ package org.opendaylight.controller.config.yang.store.api; +import org.opendaylight.controller.config.yang.store.spi.YangStoreListener; + /** * Yang store OSGi service */ @@ -19,4 +21,10 @@ public interface YangStoreService { */ YangStoreSnapshot getYangStoreSnapshot() throws YangStoreException; + + /** + * Allows for registering for change notifications. + */ + YangStoreListenerRegistration registerListener(YangStoreListener listener); + } diff --git a/opendaylight/config/yang-store-api/src/main/java/org/opendaylight/controller/config/yang/store/spi/YangStoreListener.java b/opendaylight/config/yang-store-api/src/main/java/org/opendaylight/controller/config/yang/store/spi/YangStoreListener.java new file mode 100644 index 0000000000..72c3d347c8 --- /dev/null +++ b/opendaylight/config/yang-store-api/src/main/java/org/opendaylight/controller/config/yang/store/spi/YangStoreListener.java @@ -0,0 +1,23 @@ +/* + * 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.controller.config.yang.store.spi; + +import java.net.URL; +import java.util.Collection; + +/** + * Implementation of this interface gets notified when bundle containing yang files in META-INF/yang has been + * added or removed. One notification is sent per one bundle. + */ +public interface YangStoreListener { + + void onAddedYangURL(Collection url); + + void onRemovedYangURL(Collection url); + +} diff --git a/opendaylight/config/yang-store-impl/pom.xml b/opendaylight/config/yang-store-impl/pom.xml index 11224cef22..c9105262f9 100644 --- a/opendaylight/config/yang-store-impl/pom.xml +++ b/opendaylight/config/yang-store-impl/pom.xml @@ -10,12 +10,13 @@ yang-store-impl ${project.artifactId} bundle + ${config.yangstore.version} ${project.groupId} yang-store-api - 0.2.1-SNAPSHOT + ${config.yangstore.version} org.osgi @@ -28,7 +29,7 @@ ${project.groupId} yang-jmx-generator - 0.2.1-SNAPSHOT + ${config.version} org.opendaylight.yangtools @@ -66,6 +67,7 @@ org.opendaylight.controller.config.yang.store.api, + org.opendaylight.controller.config.yang.store.spi, org.opendaylight.controller.config.yangjmxgenerator, com.google.common.base, com.google.common.collect, @@ -74,7 +76,6 @@ org.osgi.framework, org.osgi.util.tracker, org.slf4j, - javax.*, org.opendaylight.yangtools.sal.binding.yang.types, org.opendaylight.yangtools.yang.common, 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; diff --git a/opendaylight/config/yang-store-impl/src/test/java/org/opendaylight/controller/config/yang/store/impl/HardcodedYangStoreService.java b/opendaylight/config/yang-store-impl/src/test/java/org/opendaylight/controller/config/yang/store/impl/HardcodedYangStoreService.java index 96833bd507..844f682b7a 100644 --- a/opendaylight/config/yang-store-impl/src/test/java/org/opendaylight/controller/config/yang/store/impl/HardcodedYangStoreService.java +++ b/opendaylight/config/yang-store-impl/src/test/java/org/opendaylight/controller/config/yang/store/impl/HardcodedYangStoreService.java @@ -17,8 +17,10 @@ import java.util.Collection; import org.apache.commons.io.IOUtils; 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; public class HardcodedYangStoreService implements YangStoreService { @@ -48,4 +50,9 @@ public class HardcodedYangStoreService implements YangStoreService { } return new MbeParser().parseYangFiles(byteArrayInputStreams); } + + @Override + public YangStoreListenerRegistration registerListener(YangStoreListener listener){ + throw new UnsupportedOperationException("Cannot register for changes on this service"); + } } diff --git a/opendaylight/distribution/opendaylight/pom.xml b/opendaylight/distribution/opendaylight/pom.xml index d95a6dca01..a3a323fc97 100644 --- a/opendaylight/distribution/opendaylight/pom.xml +++ b/opendaylight/distribution/opendaylight/pom.xml @@ -160,12 +160,12 @@ org.opendaylight.controller yang-store-api - ${config.version} + ${config.yangstore.version} org.opendaylight.controller yang-store-impl - ${config.version} + ${config.yangstore.version} org.opendaylight.controller diff --git a/opendaylight/netconf/pom.xml b/opendaylight/netconf/pom.xml index 8436a1388b..b9f9314499 100644 --- a/opendaylight/netconf/pom.xml +++ b/opendaylight/netconf/pom.xml @@ -93,12 +93,12 @@ ${project.groupId} yang-store-api - ${config.version} + ${config.yangstore.version} ${project.groupId} yang-store-impl - ${config.version} + ${config.yangstore.version} ${project.groupId}