48bce78378ff0f9afd3db9c1fb18f5c554c0f5bb
[controller.git] / opendaylight / config / yang-store-impl / src / main / java / org / opendaylight / controller / config / yang / store / impl / YangStoreCache.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.controller.config.yang.store.impl;
9
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;
21
22 import javax.annotation.concurrent.GuardedBy;
23 import java.net.URL;
24 import java.util.Collection;
25 import java.util.Collections;
26 import java.util.Map;
27 import java.util.Set;
28
29 class YangStoreCache {
30     private static final Logger logger = LoggerFactory.getLogger(YangStoreCache.class);
31
32     @GuardedBy("this")
33     private Set<URL> cachedUrls = null;
34     @GuardedBy("this")
35     private Optional<YangStoreSnapshot> cachedYangStoreSnapshot = getInitialSnapshot();
36     @GuardedBy("this")
37     private Collection<URL> inconsistentURLsForReporting = Collections.emptySet();
38
39     synchronized Optional<YangStoreSnapshot> getSnapshotIfPossible(Multimap<Bundle, URL> bundlesToYangURLs) {
40         Set<URL> urls = setFromMultimapValues(bundlesToYangURLs);
41
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);
47             }
48             return Optional.of(freshSnapshot);
49         }
50
51         return Optional.absent();
52     }
53
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());
58         return urls;
59     }
60
61     synchronized void cacheYangStore(Multimap<Bundle, URL> urls,
62                                      YangStoreSnapshot yangStoreSnapshot) {
63         this.cachedUrls = setFromMultimapValues(urls);
64         this.cachedYangStoreSnapshot = Optional.of(yangStoreSnapshot);
65     }
66
67     synchronized void invalidate() {
68         cachedUrls.clear();
69         if (cachedYangStoreSnapshot.isPresent()){
70             cachedYangStoreSnapshot.get().close();
71             cachedYangStoreSnapshot = Optional.absent();
72         }
73     }
74
75     public synchronized void setInconsistentURLsForReporting(Collection<URL> urls){
76         inconsistentURLsForReporting = urls;
77     }
78
79     private Optional<YangStoreSnapshot> getInitialSnapshot() {
80         YangStoreSnapshot initialSnapshot = new YangStoreSnapshot() {
81             @Override
82             public Map<String, Map<String, ModuleMXBeanEntry>> getModuleMXBeanEntryMap() {
83                 return Collections.emptyMap();
84             }
85
86             @Override
87             public Map<QName, Map<String, ModuleMXBeanEntry>> getQNamesToIdentitiesToModuleMXBeanEntries() {
88                 return Collections.emptyMap();
89             }
90
91             @Override
92             public Set<Module> getModules() {
93                 return Collections.emptySet();
94             }
95
96             @Override
97             public Map<Module, String> getModulesToSources() {
98                 return Collections.emptyMap();
99             }
100
101             @Override
102             public String getModuleSource(Module module) {
103                 throw new IllegalArgumentException("Cannot get sources in empty snapshot");
104             }
105
106             @Override
107             public int countModuleMXBeanEntries() {
108                 return 0;
109             }
110
111             @Override
112             public void close() {
113             }
114         };
115         return Optional.of(initialSnapshot);
116     }
117 }