X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?p=controller.git;a=blobdiff_plain;f=opendaylight%2Fconfig%2Fyang-store-impl%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fconfig%2Fyang%2Fstore%2Fimpl%2FExtenderYangTracker.java;h=889d246784a05bfa1dd425ff3a6e2daaa09a7f39;hp=2ead596d05f7be50610b574177be44a0555e466c;hb=e1658face07c354aa8fd5b12da700401ce1efc37;hpb=1d86c5cf27410934076c10eaa74f4bab4418215c 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 2ead596d05..889d246784 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 @@ -14,7 +14,6 @@ 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 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; @@ -34,7 +33,6 @@ import java.util.Collection; import java.util.Collections; import java.util.Enumeration; import java.util.List; -import java.util.Set; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -107,38 +105,47 @@ public class ExtenderYangTracker extends BundleTracker implements YangSt proposedNewState.putAll(bundle, addedURLs); Preconditions.checkArgument(addedURLs.size() > 0, "No change can occur when no URLs are changed"); - boolean success; - String failureReason = null; + try(YangStoreSnapshotImpl snapshot = createSnapshot(mbeParser, proposedNewState)) { - updateCache(snapshot); - success = true; + onSnapshotSuccess(proposedNewState, snapshot); } catch(YangStoreException e) { - failureReason = e.toString(); - success = false; - } - if (success){ - // consistent state - // merge into - consistentBundlesToYangURLs.clear(); - consistentBundlesToYangURLs.putAll(proposedNewState); - inconsistentBundlesToYangURLs.clear(); - - logger.info("Yang store updated to new consistent state containing {} yang files", consistentBundlesToYangURLs.size()); - logger.trace("Yang store updated to new consistent state containing {}", consistentBundlesToYangURLs); - } else { - // inconsistent state - logger.debug("Yang store is falling back on last consistent state containing {}, inconsistent yang files {}, reason {}", - consistentBundlesToYangURLs, inconsistentBundlesToYangURLs, failureReason); - logger.warn("Yang store is falling back on last consistent state containing {} files, inconsistent yang files size is {}, reason {}", - consistentBundlesToYangURLs.size(), inconsistentBundlesToYangURLs.size(), failureReason); - inconsistentBundlesToYangURLs.putAll(bundle, addedURLs); + onSnapshotFailure(bundle, addedURLs, e); } } } return bundle; } - private void updateCache(YangStoreSnapshotImpl snapshot) { + private synchronized void onSnapshotFailure(Bundle bundle, List 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 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. emptySet()); + } + + private synchronized void updateCache(YangStoreSnapshotImpl snapshot) { cache.cacheYangStore(consistentBundlesToYangURLs, snapshot); } @@ -153,6 +160,7 @@ public class ExtenderYangTracker extends BundleTracker implements YangSt */ @Override public synchronized void removedBundle(Bundle bundle, BundleEvent event, Object object) { + logger.debug("Removed bundle {} {} {}", bundle, event, object); inconsistentBundlesToYangURLs.removeAll(bundle); consistentBundlesToYangURLs.removeAll(bundle); } @@ -162,9 +170,10 @@ public class ExtenderYangTracker extends BundleTracker implements YangSt throws YangStoreException { Optional yangStoreOpt = cache.getSnapshotIfPossible(consistentBundlesToYangURLs); if (yangStoreOpt.isPresent()) { - logger.trace("Returning cached yang store {}", yangStoreOpt.get()); + logger.debug("Returning cached yang store {}", yangStoreOpt.get()); return yangStoreOpt.get(); } + YangStoreSnapshotImpl snapshot = createSnapshot(mbeParser, consistentBundlesToYangURLs); updateCache(snapshot); return snapshot; @@ -177,7 +186,15 @@ public class ExtenderYangTracker extends BundleTracker implements YangSt yangStoreSnapshot.countModuleMXBeanEntries(), multimap.values().size()); return yangStoreSnapshot; } catch (RuntimeException e) { - throw new YangStoreException("Unable to parse yang files from following URLs: " + multimap, 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); } } @@ -203,41 +220,3 @@ public class ExtenderYangTracker extends BundleTracker implements YangSt cache.invalidate(); } } - -class YangStoreCache { - @GuardedBy("this") - private Set cachedUrls = Collections.emptySet(); - @GuardedBy("this") - private Optional cachedYangStoreSnapshot = Optional.absent(); - - synchronized Optional getSnapshotIfPossible(Multimap bundlesToYangURLs) { - Set urls = setFromMultimapValues(bundlesToYangURLs); - if (cachedUrls != null && cachedUrls.equals(urls)) { - Preconditions.checkState(cachedYangStoreSnapshot.isPresent()); - YangStoreSnapshot freshSnapshot = new YangStoreSnapshotImpl(cachedYangStoreSnapshot.get()); - return Optional.of(freshSnapshot); - } - return Optional.absent(); - } - - private static Set setFromMultimapValues( - Multimap bundlesToYangURLs) { - Set urls = Sets.newHashSet(bundlesToYangURLs.values()); - Preconditions.checkState(bundlesToYangURLs.size() == urls.size()); - return urls; - } - - synchronized void cacheYangStore(Multimap urls, - YangStoreSnapshotImpl yangStoreSnapshot) { - this.cachedUrls = setFromMultimapValues(urls); - this.cachedYangStoreSnapshot = Optional.of(yangStoreSnapshot); - } - - synchronized void invalidate() { - cachedUrls.clear(); - if (cachedYangStoreSnapshot.isPresent()){ - cachedYangStoreSnapshot.get().close(); - cachedYangStoreSnapshot = Optional.absent(); - } - } -}