1 package org.opendaylight.controller.config.yang.store.impl;
3 import com.google.common.base.Optional;
4 import com.google.common.base.Preconditions;
5 import com.google.common.collect.Multimap;
6 import com.google.common.collect.Sets;
7 import org.opendaylight.controller.config.yang.store.api.YangStoreSnapshot;
8 import org.opendaylight.controller.config.yangjmxgenerator.ModuleMXBeanEntry;
9 import org.opendaylight.yangtools.yang.common.QName;
10 import org.opendaylight.yangtools.yang.model.api.Module;
11 import org.osgi.framework.Bundle;
12 import org.slf4j.Logger;
13 import org.slf4j.LoggerFactory;
15 import javax.annotation.concurrent.GuardedBy;
17 import java.util.Collection;
18 import java.util.Collections;
22 class YangStoreCache {
23 private static final Logger logger = LoggerFactory.getLogger(YangStoreCache.class);
26 private Set<URL> cachedUrls = null;
28 private Optional<YangStoreSnapshot> cachedYangStoreSnapshot = getInitialSnapshot();
30 private Collection<URL> inconsistentURLsForReporting = Collections.emptySet();
32 synchronized Optional<YangStoreSnapshot> getSnapshotIfPossible(Multimap<Bundle, URL> bundlesToYangURLs) {
33 Set<URL> urls = setFromMultimapValues(bundlesToYangURLs);
35 if (cachedUrls==null || cachedUrls.equals(urls)) {
36 Preconditions.checkState(cachedYangStoreSnapshot.isPresent());
37 YangStoreSnapshot freshSnapshot = YangStoreSnapshotImpl.copy(cachedYangStoreSnapshot.get());
38 if (inconsistentURLsForReporting.size() > 0){
39 logger.warn("Some yang URLs are ignored: {}", inconsistentURLsForReporting);
41 return Optional.of(freshSnapshot);
44 return Optional.absent();
47 private static Set<URL> setFromMultimapValues(
48 Multimap<Bundle, URL> bundlesToYangURLs) {
49 Set<URL> urls = Sets.newHashSet(bundlesToYangURLs.values());
50 Preconditions.checkState(bundlesToYangURLs.size() == urls.size());
54 synchronized void cacheYangStore(Multimap<Bundle, URL> urls,
55 YangStoreSnapshot yangStoreSnapshot) {
56 this.cachedUrls = setFromMultimapValues(urls);
57 this.cachedYangStoreSnapshot = Optional.of(yangStoreSnapshot);
60 synchronized void invalidate() {
62 if (cachedYangStoreSnapshot.isPresent()){
63 cachedYangStoreSnapshot.get().close();
64 cachedYangStoreSnapshot = Optional.absent();
68 public synchronized void setInconsistentURLsForReporting(Collection<URL> urls){
69 inconsistentURLsForReporting = urls;
72 private Optional<YangStoreSnapshot> getInitialSnapshot() {
73 YangStoreSnapshot initialSnapshot = new YangStoreSnapshot() {
75 public Map<String, Map<String, ModuleMXBeanEntry>> getModuleMXBeanEntryMap() {
76 return Collections.emptyMap();
80 public Map<QName, Map<String, ModuleMXBeanEntry>> getQNamesToIdentitiesToModuleMXBeanEntries() {
81 return Collections.emptyMap();
85 public Set<Module> getModules() {
86 return Collections.emptySet();
90 public Map<Module, String> getModulesToSources() {
91 return Collections.emptyMap();
95 public String getModuleSource(Module module) {
96 throw new IllegalArgumentException("Cannot get sources in empty snapshot");
100 public int countModuleMXBeanEntries() {
105 public void close() {
108 return Optional.of(initialSnapshot);