1 package org.opendaylight.controller.sal.dom.broker;
3 import java.io.IOException;
4 import java.io.InputStream;
6 import java.util.ArrayList;
7 import java.util.Collection;
8 import java.util.Enumeration;
11 import java.util.zip.Checksum;
13 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
14 import org.osgi.util.tracker.BundleTracker;
15 import org.osgi.util.tracker.BundleTrackerCustomizer;
16 import org.opendaylight.yangtools.yang.model.api.Module;
17 import org.opendaylight.yangtools.yang.model.parser.api.YangModelParser;
18 import org.opendaylight.yangtools.yang.parser.impl.YangParserImpl;
19 import org.osgi.framework.Bundle;
20 import org.osgi.framework.BundleActivator;
21 import org.osgi.framework.BundleContext;
22 import org.osgi.framework.BundleEvent;
23 import org.opendaylight.yangtools.concepts.ListenerRegistration;
24 import org.opendaylight.yangtools.concepts.util.ListenerRegistry;
25 import org.opendaylight.controller.sal.core.api.model.SchemaService;
26 import org.opendaylight.controller.sal.core.api.model.SchemaServiceListener;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
30 import com.google.common.base.Function;
31 import com.google.common.base.Optional;
32 import com.google.common.base.Preconditions;
33 import com.google.common.collect.Collections2;
34 import com.google.common.collect.HashMultimap;
35 import com.google.common.collect.Multimap;
36 import com.google.common.collect.Sets;
37 import static com.google.common.base.Preconditions.*;
39 public class SchemaServiceImpl implements SchemaService, AutoCloseable {
40 private static final Logger logger = LoggerFactory.getLogger(SchemaServiceImpl.class);
42 private ListenerRegistry<SchemaServiceListener> listeners;
43 private YangModelParser parser;
45 private BundleContext context;
46 private BundleScanner scanner = new BundleScanner();
49 * Map of currently problematic yang files that should get fixed eventually
50 * after all events are received.
52 private final Multimap<Bundle, URL> inconsistentBundlesToYangURLs = HashMultimap.create();
53 private final Multimap<Bundle, URL> consistentBundlesToYangURLs = HashMultimap.create();
54 private BundleTracker<Object> bundleTracker;
55 private final YangStoreCache cache = new YangStoreCache();
57 public ListenerRegistry<SchemaServiceListener> getListeners() {
61 public void setListeners(ListenerRegistry<SchemaServiceListener> listeners) {
62 this.listeners = listeners;
65 public YangModelParser getParser() {
69 public void setParser(YangModelParser parser) {
73 public BundleContext getContext() {
77 public void setContext(BundleContext context) {
78 this.context = context;
82 checkState(parser != null);
83 checkState(context != null);
84 if (listeners == null) {
85 listeners = new ListenerRegistry<>();
88 bundleTracker = new BundleTracker<Object>(context, BundleEvent.RESOLVED | BundleEvent.UNRESOLVED, scanner);
92 public SchemaContext getGlobalContext() {
93 return getSchemaContextSnapshot();
96 public synchronized SchemaContext getSchemaContextSnapshot() {
97 Optional<SchemaContext> yangStoreOpt = cache.getCachedSchemaContext(consistentBundlesToYangURLs);
98 if (yangStoreOpt.isPresent()) {
99 return yangStoreOpt.get();
101 SchemaContext snapshot = createSnapshot(parser, consistentBundlesToYangURLs);
102 updateCache(snapshot);
107 public void addModule(Module module) {
108 // TODO Auto-generated method stub
109 throw new UnsupportedOperationException();
113 public SchemaContext getSessionContext() {
114 // TODO Auto-generated method stub
115 throw new UnsupportedOperationException();
119 public void removeModule(Module module) {
120 // TODO Auto-generated method stub
121 throw new UnsupportedOperationException();
126 public ListenerRegistration<SchemaServiceListener> registerSchemaServiceListener(SchemaServiceListener listener) {
127 return listeners.register(listener);
131 public void close() throws Exception {
132 bundleTracker.close();
133 // FIXME: Add listeners.close();
137 private synchronized boolean tryToUpdateState(Collection<URL> changedURLs, Multimap<Bundle, URL> proposedNewState,
139 Preconditions.checkArgument(changedURLs.size() > 0, "No change can occur when no URLs are changed");
144 SchemaContext snapshot = createSnapshot(parser, proposedNewState);
145 consistentBundlesToYangURLs.clear();
146 consistentBundlesToYangURLs.putAll(proposedNewState);
147 inconsistentBundlesToYangURLs.clear();
149 updateCache(snapshot);
150 logger.info("SchemaService updated to new consistent state");
151 logger.trace("SchemaService updated to new consistent state containing {}", consistentBundlesToYangURLs);
153 // notifyListeners(changedURLs, adding);
155 } catch (Exception e) {
156 // inconsistent state
158 "SchemaService is falling back on last consistent state containing {}, inconsistent yang files {}, reason {}",
159 consistentBundlesToYangURLs, inconsistentBundlesToYangURLs, e.toString());
164 private static Collection<InputStream> fromUrlsToInputStreams(Multimap<Bundle, URL> multimap) {
165 return Collections2.transform(multimap.values(), new Function<URL, InputStream>() {
168 public InputStream apply(URL url) {
170 return url.openStream();
171 } catch (IOException e) {
172 logger.warn("Unable to open stream from {}", url);
173 throw new IllegalStateException("Unable to open stream from " + url, e);
179 private static SchemaContext createSnapshot(YangModelParser parser, Multimap<Bundle, URL> multimap) {
180 List<InputStream> models = new ArrayList<>(fromUrlsToInputStreams(multimap));
181 Set<Module> modules = parser.parseYangModelsFromStreams(models);
182 SchemaContext yangStoreSnapshot = parser.resolveSchemaContext(modules);
183 return yangStoreSnapshot;
186 private void updateCache(SchemaContext snapshot) {
187 cache.cacheYangStore(consistentBundlesToYangURLs, snapshot);
188 for (ListenerRegistration<SchemaServiceListener> listener : listeners) {
190 listener.getInstance().onGlobalContextUpdated(snapshot);
191 } catch (Exception e) {
192 logger.error("Exception occured during invoking listener",e);
197 private class BundleScanner implements BundleTrackerCustomizer<Object> {
199 public Object addingBundle(Bundle bundle, BundleEvent event) {
201 // Ignore system bundle:
202 // system bundle might have config-api on classpath &&
203 // config-api contains yang files =>
204 // system bundle might contain yang files from that bundle
205 if (bundle.getBundleId() == 0)
208 Enumeration<URL> enumeration = bundle.findEntries("META-INF/yang", "*.yang", false);
209 if (enumeration != null && enumeration.hasMoreElements()) {
210 synchronized (this) {
211 List<URL> addedURLs = new ArrayList<>();
212 while (enumeration.hasMoreElements()) {
213 URL url = enumeration.nextElement();
216 logger.trace("Bundle {} has event {}, bundle state {}, URLs {}", bundle, event, bundle.getState(),
218 // test that yang store is consistent
219 Multimap<Bundle, URL> proposedNewState = HashMultimap.create(consistentBundlesToYangURLs);
220 proposedNewState.putAll(inconsistentBundlesToYangURLs);
221 proposedNewState.putAll(bundle, addedURLs);
222 boolean adding = true;
223 if (tryToUpdateState(addedURLs, proposedNewState, adding) == false) {
224 inconsistentBundlesToYangURLs.putAll(bundle, addedURLs);
232 public void modifiedBundle(Bundle bundle, BundleEvent event, Object object) {
233 logger.debug("Modified bundle {} {} {}", bundle, event, object);
237 * If removing YANG files makes yang store inconsistent, method
238 * {@link #getYangStoreSnapshot()} will throw exception. There is no
243 public synchronized void removedBundle(Bundle bundle, BundleEvent event, Object object) {
244 inconsistentBundlesToYangURLs.removeAll(bundle);
245 Collection<URL> consistentURLsToBeRemoved = consistentBundlesToYangURLs.removeAll(bundle);
247 if (consistentURLsToBeRemoved.isEmpty()) {
250 boolean adding = false;
251 // notifyListeners(consistentURLsToBeRemoved, adding);
255 private static final class YangStoreCache {
258 SchemaContext cachedContextSnapshot;
260 Optional<SchemaContext> getCachedSchemaContext(Multimap<Bundle, URL> bundlesToYangURLs) {
261 Set<URL> urls = setFromMultimapValues(bundlesToYangURLs);
262 if (cachedUrls != null && cachedUrls.equals(urls)) {
263 Preconditions.checkState(cachedContextSnapshot != null);
264 return Optional.of(cachedContextSnapshot);
266 return Optional.absent();
269 private static Set<URL> setFromMultimapValues(Multimap<Bundle, URL> bundlesToYangURLs) {
270 Set<URL> urls = Sets.newHashSet(bundlesToYangURLs.values());
271 Preconditions.checkState(bundlesToYangURLs.size() == urls.size());
275 void cacheYangStore(Multimap<Bundle, URL> urls, SchemaContext ctx) {
276 this.cachedUrls = setFromMultimapValues(urls);
277 this.cachedContextSnapshot = ctx;