Merge "Take advantage of MultipartTransactionAware"
[controller.git] / opendaylight / config / yang-store-impl / src / main / java / org / opendaylight / controller / config / yang / store / impl / ExtenderYangTracker.java
index e3be7346248809ac75ec26333ef3fa8a3f2bf3e4..889d246784a05bfa1dd425ff3a6e2daaa09a7f39 100644 (file)
@@ -7,13 +7,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 com.google.common.annotations.VisibleForTesting;
+import com.google.common.base.Function;
+import com.google.common.base.Optional;
+import com.google.common.base.Preconditions;
+import com.google.common.collect.Collections2;
+import com.google.common.collect.HashMultimap;
+import com.google.common.collect.Multimap;
 import org.opendaylight.controller.config.yang.store.api.YangStoreException;
 import org.opendaylight.controller.config.yang.store.api.YangStoreService;
 import org.opendaylight.controller.config.yang.store.api.YangStoreSnapshot;
@@ -24,106 +24,182 @@ import org.osgi.util.tracker.BundleTracker;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-import com.google.common.annotations.VisibleForTesting;
-import com.google.common.base.Function;
-import com.google.common.base.Optional;
-import com.google.common.base.Preconditions;
-import com.google.common.collect.Collections2;
-import com.google.common.collect.HashMultimap;
-import com.google.common.collect.Multimap;
-import com.google.common.collect.Sets;
+import javax.annotation.concurrent.GuardedBy;
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.URL;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.Enumeration;
+import java.util.List;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+/**
+ * Note on consistency:
+ * When this bundle is activated after other bundles containing yang files, the resolving order
+ * is not preserved. We thus maintain two maps, one containing consistent snapshot, other inconsistent. The
+ * container should eventually send all events and thus making the inconsistent map redundant.
+ */
+public class ExtenderYangTracker extends BundleTracker<Object> implements YangStoreService, AutoCloseable {
 
-public class ExtenderYangTracker extends BundleTracker<Object> implements
-        YangStoreService {
+    private static final Logger logger = LoggerFactory.getLogger(ExtenderYangTracker.class);
 
-    private static final Logger logger = LoggerFactory
-            .getLogger(ExtenderYangTracker.class);
+    private final Multimap<Bundle, URL> consistentBundlesToYangURLs = HashMultimap.create();
+
+    /*
+    Map of currently problematic yang files that should get fixed eventually after all events are received.
+     */
+    private final Multimap<Bundle, URL> inconsistentBundlesToYangURLs = HashMultimap.create();
 
-    private final Multimap<Bundle, URL> bundlesToYangURLs = HashMultimap
-            .create();
     private final YangStoreCache cache = new YangStoreCache();
     private final MbeParser mbeParser;
 
-    public ExtenderYangTracker(BundleContext context) {
-        this(context, new MbeParser());
 
+    public ExtenderYangTracker(Optional<Pattern> maybeBlacklist, BundleContext bundleContext) {
+        this(new MbeParser(), maybeBlacklist, bundleContext);
     }
 
+    @GuardedBy("this")
+    private Optional<Pattern> maybeBlacklist;
+
     @VisibleForTesting
-    ExtenderYangTracker(BundleContext context, MbeParser mbeParser) {
-        super(context, Bundle.ACTIVE, null);
+    ExtenderYangTracker(MbeParser mbeParser, Optional<Pattern> maybeBlacklist, BundleContext bundleContext) {
+        super(bundleContext, BundleEvent.RESOLVED | BundleEvent.UNRESOLVED, null);
         this.mbeParser = mbeParser;
-        logger.trace("Registered as extender with context {}", context);
+        this.maybeBlacklist = maybeBlacklist;
+        open();
     }
 
     @Override
-    public Object addingBundle(Bundle bundle, BundleEvent event) {
+    public synchronized 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<URL> 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);
+        if (maybeBlacklist.isPresent()) {
+            Matcher m = maybeBlacklist.get().matcher(bundle.getSymbolicName());
+            if (m.matches()) {
+                logger.debug("Ignoring {} because it is in blacklist {}", bundle, maybeBlacklist);
+                return bundle;
             }
         }
 
+        Enumeration<URL> enumeration = bundle.findEntries("META-INF/yang", "*.yang", false);
+        if (enumeration != null && enumeration.hasMoreElements()) {
+            synchronized (this) {
+                List<URL> addedURLs = new ArrayList<>();
+                while (enumeration.hasMoreElements()) {
+                    URL url = enumeration.nextElement();
+                    addedURLs.add(url);
+                }
+                logger.trace("Bundle {} has event {}, bundle state {}, URLs {}", bundle, event, bundle.getState(), addedURLs);
+                // test that yang store is consistent
+                Multimap<Bundle, URL> proposedNewState = HashMultimap.create(consistentBundlesToYangURLs);
+                proposedNewState.putAll(inconsistentBundlesToYangURLs);
+                proposedNewState.putAll(bundle, addedURLs);
+
+                Preconditions.checkArgument(addedURLs.size() > 0, "No change can occur when no URLs are changed");
+
+                try(YangStoreSnapshotImpl snapshot = createSnapshot(mbeParser, proposedNewState)) {
+                    onSnapshotSuccess(proposedNewState, snapshot);
+                } catch(YangStoreException e) {
+                    onSnapshotFailure(bundle, addedURLs, e);
+                }
+            }
+        }
         return bundle;
     }
 
-    @Override
-    public void removedBundle(Bundle bundle, BundleEvent event, Object object) {
-        synchronized (this) {
-            Collection<URL> urls = bundlesToYangURLs.removeAll(bundle);
-            logger.debug(
-                    "Removed following yang URLs {} because of removed bundle {}",
-                    urls, bundle);
+    private synchronized void onSnapshotFailure(Bundle bundle, List<URL> addedURLs, Exception failureReason) {
+        // inconsistent state
+        inconsistentBundlesToYangURLs.putAll(bundle, addedURLs);
+
+        logger.debug("Yang store is falling back to last consistent state containing {}, inconsistent yang files {}",
+                consistentBundlesToYangURLs, inconsistentBundlesToYangURLs, failureReason);
+        logger.info("Yang store is falling back to last consistent state containing {} files, keeping {} inconsistent yang files due to {}",
+                consistentBundlesToYangURLs.size(), inconsistentBundlesToYangURLs.size(), failureReason.toString());
+        cache.setInconsistentURLsForReporting(inconsistentBundlesToYangURLs.values());
+    }
+
+    private synchronized void onSnapshotSuccess(Multimap<Bundle, URL> proposedNewState, YangStoreSnapshotImpl snapshot) {
+        // consistent state
+        // merge into
+        consistentBundlesToYangURLs.clear();
+        consistentBundlesToYangURLs.putAll(proposedNewState);
+
+        logger.debug("Yang store updated to new consistent state containing {}", consistentBundlesToYangURLs);
+
+        // If we cleared up some inconsistent models, report that
+        if (!inconsistentBundlesToYangURLs.isEmpty()) {
+            inconsistentBundlesToYangURLs.clear();
+            logger.info("Yang store updated to new consistent state containing {} yang files", consistentBundlesToYangURLs.size());
         }
+
+        updateCache(snapshot);
+        cache.setInconsistentURLsForReporting(Collections.<URL> emptySet());
+    }
+
+    private synchronized void updateCache(YangStoreSnapshotImpl snapshot) {
+        cache.cacheYangStore(consistentBundlesToYangURLs, snapshot);
+    }
+
+    @Override
+    public void modifiedBundle(Bundle bundle, BundleEvent event, Object object) {
+        logger.debug("Modified bundle {} {} {}", bundle, event, object);
+    }
+
+    /**
+     * If removing YANG files makes yang store inconsistent, method {@link #getYangStoreSnapshot()}
+     * will throw exception. There is no rollback.
+     */
+    @Override
+    public synchronized void removedBundle(Bundle bundle, BundleEvent event, Object object) {
+        logger.debug("Removed bundle {} {} {}", bundle, event, object);
+        inconsistentBundlesToYangURLs.removeAll(bundle);
+        consistentBundlesToYangURLs.removeAll(bundle);
     }
 
     @Override
     public synchronized YangStoreSnapshot getYangStoreSnapshot()
             throws YangStoreException {
-        Optional<YangStoreSnapshot> yangStoreOpt = cache
-                .getCachedYangStore(bundlesToYangURLs);
+        Optional<YangStoreSnapshot> yangStoreOpt = cache.getSnapshotIfPossible(consistentBundlesToYangURLs);
         if (yangStoreOpt.isPresent()) {
             logger.debug("Returning cached yang store {}", yangStoreOpt.get());
             return yangStoreOpt.get();
         }
 
-        try {
-            YangStoreSnapshot yangStoreSnapshot = mbeParser
-                    .parseYangFiles(fromUrlsToInputStreams());
-            logger.debug(
-                    "{} module entries parsed successfully from {} yang files",
-                    yangStoreSnapshot.countModuleMXBeanEntries(),
-                    bundlesToYangURLs.values().size());
-            cache.cacheYangStore(bundlesToYangURLs, yangStoreSnapshot);
+        YangStoreSnapshotImpl snapshot = createSnapshot(mbeParser, consistentBundlesToYangURLs);
+        updateCache(snapshot);
+        return snapshot;
+    }
 
+    private static YangStoreSnapshotImpl createSnapshot(MbeParser mbeParser, Multimap<Bundle, URL> multimap) throws YangStoreException {
+        try {
+            YangStoreSnapshotImpl yangStoreSnapshot = mbeParser.parseYangFiles(fromUrlsToInputStreams(multimap));
+            logger.trace("{} module entries parsed successfully from {} yang files",
+                    yangStoreSnapshot.countModuleMXBeanEntries(), multimap.values().size());
             return yangStoreSnapshot;
         } catch (RuntimeException e) {
-            logger.warn(
-                    "Unable to parse yang files, yang files that were picked up so far: {}",
-                    bundlesToYangURLs, e);
-            throw new YangStoreException("Unable to parse yang files", e);
+            StringBuffer causeStr = new StringBuffer();
+            Throwable cause = e;
+            while (cause != null) {
+                causeStr.append(e.getMessage());
+                causeStr.append("\n");
+                cause = e.getCause();
+            }
+            throw new YangStoreException("Unable to parse yang files. \n" + causeStr.toString() +
+                    "URLs: " + multimap, e);
         }
     }
 
-    private Collection<InputStream> fromUrlsToInputStreams() {
-        return Collections2.transform(bundlesToYangURLs.values(),
+    private static Collection<InputStream> fromUrlsToInputStreams(Multimap<Bundle, URL> multimap) {
+        return Collections2.transform(multimap.values(),
                 new Function<URL, InputStream>() {
 
                     @Override
@@ -139,33 +215,8 @@ public class ExtenderYangTracker extends BundleTracker<Object> implements
                 });
     }
 
-    private static final class YangStoreCache {
-
-        Set<URL> cachedUrls;
-        YangStoreSnapshot cachedYangStoreSnapshot;
-
-        Optional<YangStoreSnapshot> getCachedYangStore(
-                Multimap<Bundle, URL> bundlesToYangURLs) {
-            Set<URL> urls = setFromMultimapValues(bundlesToYangURLs);
-            if (cachedUrls != null && cachedUrls.equals(urls)) {
-                Preconditions.checkState(cachedYangStoreSnapshot != null);
-                return Optional.of(cachedYangStoreSnapshot);
-            }
-            return Optional.absent();
-        }
-
-        private static Set<URL> setFromMultimapValues(
-                Multimap<Bundle, URL> bundlesToYangURLs) {
-            Set<URL> urls = Sets.newHashSet(bundlesToYangURLs.values());
-            Preconditions.checkState(bundlesToYangURLs.size() == urls.size());
-            return urls;
-        }
-
-        void cacheYangStore(Multimap<Bundle, URL> urls,
-                YangStoreSnapshot yangStoreSnapshot) {
-            this.cachedUrls = setFromMultimapValues(urls);
-            this.cachedYangStoreSnapshot = yangStoreSnapshot;
-        }
-
+    public synchronized void setMaybeBlacklist(Optional<Pattern> maybeBlacklistPattern) {
+        maybeBlacklist = maybeBlacklistPattern;
+        cache.invalidate();
     }
 }