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.osgi.util.tracker.ServiceTracker;
17 import org.osgi.util.tracker.ServiceTrackerCustomizer;
18 import org.opendaylight.yangtools.yang.model.api.Module;
19 import org.opendaylight.yangtools.yang.model.parser.api.YangModelParser;
20 import org.opendaylight.yangtools.yang.parser.impl.YangParserImpl;
21 import org.osgi.framework.Bundle;
22 import org.osgi.framework.BundleActivator;
23 import org.osgi.framework.BundleContext;
24 import org.osgi.framework.BundleEvent;
25 import org.osgi.framework.ServiceReference;
26 import org.opendaylight.yangtools.concepts.ListenerRegistration;
27 import org.opendaylight.yangtools.concepts.util.ListenerRegistry;
28 import org.opendaylight.controller.sal.core.api.model.SchemaService;
29 import org.opendaylight.controller.sal.core.api.model.SchemaServiceListener;
30 import org.opendaylight.controller.sal.dom.broker.impl.SchemaContextProvider;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
34 import com.google.common.base.Function;
35 import com.google.common.base.Optional;
36 import com.google.common.base.Preconditions;
37 import com.google.common.collect.Collections2;
38 import com.google.common.collect.HashMultimap;
39 import com.google.common.collect.Multimap;
40 import com.google.common.collect.Sets;
42 import static com.google.common.base.Preconditions.*;
44 public class SchemaServiceImpl implements //
45 SchemaContextProvider, //
47 ServiceTrackerCustomizer<SchemaServiceListener, SchemaServiceListener>, //
49 private static final Logger logger = LoggerFactory.getLogger(SchemaServiceImpl.class);
51 private ListenerRegistry<SchemaServiceListener> listeners;
52 private YangModelParser parser;
54 private BundleContext context;
55 private BundleScanner scanner = new BundleScanner();
58 * Map of currently problematic yang files that should get fixed eventually
59 * after all events are received.
61 private final Multimap<Bundle, URL> inconsistentBundlesToYangURLs = HashMultimap.create();
62 private final Multimap<Bundle, URL> consistentBundlesToYangURLs = HashMultimap.create();
63 private BundleTracker<Object> bundleTracker;
64 private final YangStoreCache cache = new YangStoreCache();
66 private ServiceTracker<SchemaServiceListener, SchemaServiceListener> listenerTracker;
68 public ListenerRegistry<SchemaServiceListener> getListeners() {
72 public void setListeners(ListenerRegistry<SchemaServiceListener> listeners) {
73 this.listeners = listeners;
76 public YangModelParser getParser() {
80 public void setParser(YangModelParser parser) {
84 public BundleContext getContext() {
88 public void setContext(BundleContext context) {
89 this.context = context;
93 checkState(parser != null);
94 checkState(context != null);
95 if (listeners == null) {
96 listeners = new ListenerRegistry<>();
99 listenerTracker = new ServiceTracker<>(context, SchemaServiceListener.class, this);
100 bundleTracker = new BundleTracker<Object>(context, BundleEvent.RESOLVED | BundleEvent.UNRESOLVED, scanner);
101 bundleTracker.open();
102 listenerTracker.open();
107 public SchemaContext getSchemaContext() {
108 return getGlobalContext();
111 public SchemaContext getGlobalContext() {
112 return getSchemaContextSnapshot();
115 public synchronized SchemaContext getSchemaContextSnapshot() {
116 Optional<SchemaContext> yangStoreOpt = cache.getCachedSchemaContext(consistentBundlesToYangURLs);
117 if (yangStoreOpt.isPresent()) {
118 return yangStoreOpt.get();
120 SchemaContext snapshot = createSnapshot(parser, consistentBundlesToYangURLs);
121 updateCache(snapshot);
126 public void addModule(Module module) {
127 // TODO Auto-generated method stub
128 throw new UnsupportedOperationException();
132 public SchemaContext getSessionContext() {
133 // TODO Auto-generated method stub
134 throw new UnsupportedOperationException();
138 public void removeModule(Module module) {
139 // TODO Auto-generated method stub
140 throw new UnsupportedOperationException();
144 public ListenerRegistration<SchemaServiceListener> registerSchemaServiceListener(SchemaServiceListener listener) {
145 return listeners.register(listener);
149 public void close() throws Exception {
150 bundleTracker.close();
151 // FIXME: Add listeners.close();
155 private synchronized boolean tryToUpdateState(Collection<URL> changedURLs, Multimap<Bundle, URL> proposedNewState,
157 Preconditions.checkArgument(!changedURLs.isEmpty(), "No change can occur when no URLs are changed");
162 SchemaContext snapshot = createSnapshot(parser, proposedNewState);
163 consistentBundlesToYangURLs.clear();
164 consistentBundlesToYangURLs.putAll(proposedNewState);
165 inconsistentBundlesToYangURLs.clear();
167 updateCache(snapshot);
168 logger.trace("SchemaService updated to new consistent state");
169 logger.trace("SchemaService updated to new consistent state containing {}", consistentBundlesToYangURLs);
171 // notifyListeners(changedURLs, adding);
173 } catch (Exception e) {
174 // inconsistent state
176 "SchemaService is falling back on last consistent state containing {}, inconsistent yang files {}",
177 consistentBundlesToYangURLs, inconsistentBundlesToYangURLs, e);
182 private static Collection<InputStream> fromUrlsToInputStreams(Multimap<Bundle, URL> multimap) {
183 return Collections2.transform(multimap.values(), new Function<URL, InputStream>() {
186 public InputStream apply(URL url) {
188 return url.openStream();
189 } catch (IOException e) {
190 logger.warn("Unable to open stream from {}", url);
191 throw new IllegalStateException("Unable to open stream from " + url, e);
197 private static SchemaContext createSnapshot(YangModelParser parser, Multimap<Bundle, URL> multimap) {
198 List<InputStream> models = new ArrayList<>(fromUrlsToInputStreams(multimap));
199 Set<Module> modules = parser.parseYangModelsFromStreams(models);
200 SchemaContext yangStoreSnapshot = parser.resolveSchemaContext(modules);
201 return yangStoreSnapshot;
204 private void updateCache(SchemaContext snapshot) {
205 cache.cacheYangStore(consistentBundlesToYangURLs, snapshot);
207 Object[] services = listenerTracker.getServices();
208 if (services != null) {
209 for (Object rawListener : services) {
210 SchemaServiceListener listener = (SchemaServiceListener) rawListener;
212 listener.onGlobalContextUpdated(snapshot);
213 } catch (Exception e) {
214 logger.error("Exception occured during invoking listener", e);
218 for (ListenerRegistration<SchemaServiceListener> listener : listeners) {
220 listener.getInstance().onGlobalContextUpdated(snapshot);
221 } catch (Exception e) {
222 logger.error("Exception occured during invoking listener", e);
227 private class BundleScanner implements BundleTrackerCustomizer<Object> {
229 public Object addingBundle(Bundle bundle, BundleEvent event) {
231 // Ignore system bundle:
232 // system bundle might have config-api on classpath &&
233 // config-api contains yang files =>
234 // system bundle might contain yang files from that bundle
235 if (bundle.getBundleId() == 0) {
239 Enumeration<URL> enumeration = bundle.findEntries("META-INF/yang", "*.yang", false);
240 if (enumeration != null && enumeration.hasMoreElements()) {
241 synchronized (this) {
242 List<URL> addedURLs = new ArrayList<>();
243 while (enumeration.hasMoreElements()) {
244 URL url = enumeration.nextElement();
247 logger.trace("Bundle {} has event {}, bundle state {}, URLs {}", bundle, event, bundle.getState(),
249 // test that yang store is consistent
250 Multimap<Bundle, URL> proposedNewState = HashMultimap.create(consistentBundlesToYangURLs);
251 proposedNewState.putAll(inconsistentBundlesToYangURLs);
252 proposedNewState.putAll(bundle, addedURLs);
253 boolean adding = true;
255 if (tryToUpdateState(addedURLs, proposedNewState, adding) == false) {
256 inconsistentBundlesToYangURLs.putAll(bundle, addedURLs);
264 public void modifiedBundle(Bundle bundle, BundleEvent event, Object object) {
265 logger.debug("Modified bundle {} {} {}", bundle, event, object);
269 * If removing YANG files makes yang store inconsistent, method
270 * {@link #getYangStoreSnapshot()} will throw exception. There is no
275 public synchronized void removedBundle(Bundle bundle, BundleEvent event, Object object) {
276 inconsistentBundlesToYangURLs.removeAll(bundle);
277 Collection<URL> consistentURLsToBeRemoved = consistentBundlesToYangURLs.removeAll(bundle);
279 if (consistentURLsToBeRemoved.isEmpty()) {
282 boolean adding = false;
283 // notifyListeners(consistentURLsToBeRemoved, adding);
287 private static final class YangStoreCache {
290 SchemaContext cachedContextSnapshot;
292 Optional<SchemaContext> getCachedSchemaContext(Multimap<Bundle, URL> bundlesToYangURLs) {
293 Set<URL> urls = setFromMultimapValues(bundlesToYangURLs);
294 if (cachedUrls != null && cachedUrls.equals(urls)) {
295 Preconditions.checkState(cachedContextSnapshot != null);
296 return Optional.of(cachedContextSnapshot);
298 return Optional.absent();
301 private static Set<URL> setFromMultimapValues(Multimap<Bundle, URL> bundlesToYangURLs) {
302 Set<URL> urls = Sets.newHashSet(bundlesToYangURLs.values());
303 Preconditions.checkState(bundlesToYangURLs.size() == urls.size());
307 void cacheYangStore(Multimap<Bundle, URL> urls, SchemaContext ctx) {
308 this.cachedUrls = setFromMultimapValues(urls);
309 this.cachedContextSnapshot = ctx;
314 public SchemaServiceListener addingService(ServiceReference<SchemaServiceListener> reference) {
316 SchemaServiceListener listener = context.getService(reference);
317 SchemaContext _ctxContext = getGlobalContext();
318 if (getContext() != null) {
319 listener.onGlobalContextUpdated(_ctxContext);
325 public void modifiedService(ServiceReference<SchemaServiceListener> reference, SchemaServiceListener service) {
330 public void removedService(ServiceReference<SchemaServiceListener> reference, SchemaServiceListener service) {
331 context.ungetService(reference);