2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.controller.config.yang.store.impl;
10 import com.google.common.base.Optional;
11 import com.google.common.base.Preconditions;
12 import com.google.common.collect.Multimap;
13 import com.google.common.collect.Sets;
14 import org.opendaylight.controller.config.yang.store.api.YangStoreSnapshot;
15 import org.opendaylight.controller.config.yangjmxgenerator.ModuleMXBeanEntry;
16 import org.opendaylight.yangtools.yang.common.QName;
17 import org.opendaylight.yangtools.yang.model.api.Module;
18 import org.osgi.framework.Bundle;
19 import org.slf4j.Logger;
20 import org.slf4j.LoggerFactory;
22 import javax.annotation.concurrent.GuardedBy;
24 import java.util.Collection;
25 import java.util.Collections;
29 class YangStoreCache {
30 private static final Logger logger = LoggerFactory.getLogger(YangStoreCache.class);
33 private Set<URL> cachedUrls = null;
35 private Optional<YangStoreSnapshot> cachedYangStoreSnapshot = getInitialSnapshot();
37 private Collection<URL> inconsistentURLsForReporting = Collections.emptySet();
39 synchronized Optional<YangStoreSnapshot> getSnapshotIfPossible(Multimap<Bundle, URL> bundlesToYangURLs) {
40 Set<URL> urls = setFromMultimapValues(bundlesToYangURLs);
42 if (cachedUrls==null || cachedUrls.equals(urls)) {
43 Preconditions.checkState(cachedYangStoreSnapshot.isPresent());
44 YangStoreSnapshot freshSnapshot = YangStoreSnapshotImpl.copy(cachedYangStoreSnapshot.get());
45 if (inconsistentURLsForReporting.size() > 0){
46 logger.warn("Some yang URLs are ignored: {}", inconsistentURLsForReporting);
48 return Optional.of(freshSnapshot);
51 return Optional.absent();
54 private static Set<URL> setFromMultimapValues(
55 Multimap<Bundle, URL> bundlesToYangURLs) {
56 Set<URL> urls = Sets.newHashSet(bundlesToYangURLs.values());
57 Preconditions.checkState(bundlesToYangURLs.size() == urls.size());
61 synchronized void cacheYangStore(Multimap<Bundle, URL> urls,
62 YangStoreSnapshot yangStoreSnapshot) {
63 this.cachedUrls = setFromMultimapValues(urls);
64 this.cachedYangStoreSnapshot = Optional.of(yangStoreSnapshot);
67 synchronized void invalidate() {
69 if (cachedYangStoreSnapshot.isPresent()){
70 cachedYangStoreSnapshot.get().close();
71 cachedYangStoreSnapshot = Optional.absent();
75 public synchronized void setInconsistentURLsForReporting(Collection<URL> urls){
76 inconsistentURLsForReporting = urls;
79 private Optional<YangStoreSnapshot> getInitialSnapshot() {
80 YangStoreSnapshot initialSnapshot = new YangStoreSnapshot() {
82 public Map<String, Map<String, ModuleMXBeanEntry>> getModuleMXBeanEntryMap() {
83 return Collections.emptyMap();
87 public Map<QName, Map<String, ModuleMXBeanEntry>> getQNamesToIdentitiesToModuleMXBeanEntries() {
88 return Collections.emptyMap();
92 public Set<Module> getModules() {
93 return Collections.emptySet();
97 public Map<Module, String> getModulesToSources() {
98 return Collections.emptyMap();
102 public String getModuleSource(Module module) {
103 throw new IllegalArgumentException("Cannot get sources in empty snapshot");
107 public int countModuleMXBeanEntries() {
112 public void close() {
115 return Optional.of(initialSnapshot);