2 * Copyright (c) 2013 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.annotations.VisibleForTesting;
11 import com.google.common.base.Function;
12 import com.google.common.base.Optional;
13 import com.google.common.base.Preconditions;
14 import com.google.common.collect.Collections2;
15 import com.google.common.collect.HashMultimap;
16 import com.google.common.collect.Multimap;
17 import com.google.common.collect.Sets;
18 import org.opendaylight.controller.config.yang.store.api.YangStoreException;
19 import org.opendaylight.controller.config.yang.store.api.YangStoreListenerRegistration;
20 import org.opendaylight.controller.config.yang.store.api.YangStoreService;
21 import org.opendaylight.controller.config.yang.store.api.YangStoreSnapshot;
22 import org.opendaylight.controller.config.yang.store.spi.YangStoreListener;
23 import org.osgi.framework.Bundle;
24 import org.osgi.framework.BundleEvent;
25 import org.osgi.util.tracker.BundleTrackerCustomizer;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
29 import java.io.IOException;
30 import java.io.InputStream;
32 import java.util.ArrayList;
33 import java.util.Collection;
34 import java.util.Enumeration;
35 import java.util.List;
39 * Note on consistency:
40 * When this bundle is activated after other bundles containing yang files, the resolving order
41 * is not preserved. We thus maintain two maps, one containing consistent snapshot, other inconsistent. The
42 * container should eventually send all events and thus making the inconsistent map redundant.
44 public class ExtenderYangTrackerCustomizer implements BundleTrackerCustomizer<Object>, YangStoreService {
46 private static final Logger logger = LoggerFactory
47 .getLogger(ExtenderYangTrackerCustomizer.class);
49 private final Multimap<Bundle, URL> consistentBundlesToYangURLs = HashMultimap.create();
52 Map of currently problematic yang files that should get fixed eventually after all events are received.
54 private final Multimap<Bundle, URL> inconsistentBundlesToYangURLs = HashMultimap.create();
56 private final YangStoreCache cache = new YangStoreCache();
57 private final MbeParser mbeParser;
58 private final List<YangStoreListener> listeners = new ArrayList<>();
60 public ExtenderYangTrackerCustomizer() {
61 this(new MbeParser());
66 ExtenderYangTrackerCustomizer(MbeParser mbeParser) {
67 this.mbeParser = mbeParser;
71 public Object addingBundle(Bundle bundle, BundleEvent event) {
73 // Ignore system bundle:
74 // system bundle might have config-api on classpath &&
75 // config-api contains yang files =>
76 // system bundle might contain yang files from that bundle
77 if (bundle.getBundleId() == 0)
80 Enumeration<URL> enumeration = bundle.findEntries("META-INF/yang", "*.yang", false);
81 if (enumeration != null && enumeration.hasMoreElements()) {
83 List<URL> addedURLs = new ArrayList<>();
84 while (enumeration.hasMoreElements()) {
85 URL url = enumeration.nextElement();
88 logger.trace("Bundle {} has event {}, bundle state {}, URLs {}", bundle, event, bundle.getState(), addedURLs);
89 // test that yang store is consistent
90 Multimap<Bundle, URL> proposedNewState = HashMultimap.create(consistentBundlesToYangURLs);
91 proposedNewState.putAll(inconsistentBundlesToYangURLs);
92 proposedNewState.putAll(bundle, addedURLs);
93 boolean adding = true;
94 if (tryToUpdateState(addedURLs, proposedNewState, adding) == false) {
95 inconsistentBundlesToYangURLs.putAll(bundle, addedURLs);
102 private synchronized boolean tryToUpdateState(Collection<URL> changedURLs, Multimap<Bundle, URL> proposedNewState, boolean adding) {
103 Preconditions.checkArgument(changedURLs.size() > 0, "No change can occur when no URLs are changed");
104 try(YangStoreSnapshot snapshot = createSnapshot(mbeParser, proposedNewState)) {
107 consistentBundlesToYangURLs.clear();
108 consistentBundlesToYangURLs.putAll(proposedNewState);
109 inconsistentBundlesToYangURLs.clear();
111 updateCache(snapshot);
112 logger.info("Yang store updated to new consistent state");
113 logger.trace("Yang store updated to new consistent state containing {}", consistentBundlesToYangURLs);
115 notifyListeners(changedURLs, adding);
117 } catch(YangStoreException e) {
118 // inconsistent state
119 logger.debug("Yang store is falling back on last consistent state containing {}, inconsistent yang files {}, reason {}",
120 consistentBundlesToYangURLs, inconsistentBundlesToYangURLs, e.toString());
125 private void updateCache(YangStoreSnapshot snapshot) {
126 cache.cacheYangStore(consistentBundlesToYangURLs, snapshot);
130 public void modifiedBundle(Bundle bundle, BundleEvent event, Object object) {
131 logger.debug("Modified bundle {} {} {}", bundle, event, object);
135 * Notifiers get only notified when consistent snapshot has changed.
137 private void notifyListeners(Collection<URL> changedURLs, boolean adding) {
138 Preconditions.checkArgument(changedURLs.size() > 0, "Cannot notify when no URLs changed");
139 if (changedURLs.size() > 0) {
140 RuntimeException potential = new RuntimeException("Error while notifying listeners");
141 for (YangStoreListener listener : listeners) {
144 listener.onAddedYangURL(changedURLs);
146 listener.onRemovedYangURL(changedURLs);
148 } catch(RuntimeException e) {
149 potential.addSuppressed(e);
152 if (potential.getSuppressed().length > 0) {
160 * If removing YANG files makes yang store inconsistent, method {@link #getYangStoreSnapshot()}
161 * will throw exception. There is no rollback.
164 public synchronized void removedBundle(Bundle bundle, BundleEvent event, Object object) {
165 inconsistentBundlesToYangURLs.removeAll(bundle);
166 Collection<URL> consistentURLsToBeRemoved = consistentBundlesToYangURLs.removeAll(bundle);
168 if (consistentURLsToBeRemoved.isEmpty()){
171 boolean adding = false;
172 notifyListeners(consistentURLsToBeRemoved, adding);
176 public synchronized YangStoreSnapshot getYangStoreSnapshot()
177 throws YangStoreException {
178 Optional<YangStoreSnapshot> yangStoreOpt = cache.getCachedYangStore(consistentBundlesToYangURLs);
179 if (yangStoreOpt.isPresent()) {
180 logger.trace("Returning cached yang store {}", yangStoreOpt.get());
181 return yangStoreOpt.get();
183 YangStoreSnapshot snapshot = createSnapshot(mbeParser, consistentBundlesToYangURLs);
184 updateCache(snapshot);
188 private static YangStoreSnapshot createSnapshot(MbeParser mbeParser, Multimap<Bundle, URL> multimap) throws YangStoreException {
190 YangStoreSnapshot yangStoreSnapshot = mbeParser.parseYangFiles(fromUrlsToInputStreams(multimap));
191 logger.trace("{} module entries parsed successfully from {} yang files",
192 yangStoreSnapshot.countModuleMXBeanEntries(), multimap.values().size());
193 return yangStoreSnapshot;
194 } catch (RuntimeException e) {
195 throw new YangStoreException("Unable to parse yang files from following URLs: " + multimap, e);
199 private static Collection<InputStream> fromUrlsToInputStreams(Multimap<Bundle, URL> multimap) {
200 return Collections2.transform(multimap.values(),
201 new Function<URL, InputStream>() {
204 public InputStream apply(URL url) {
206 return url.openStream();
207 } catch (IOException e) {
208 logger.warn("Unable to open stream from {}", url);
209 throw new IllegalStateException(
210 "Unable to open stream from " + url, e);
217 public synchronized YangStoreListenerRegistration registerListener(final YangStoreListener listener) {
218 listeners.add(listener);
219 return new YangStoreListenerRegistration() {
221 public void close() {
222 listeners.remove(listener);
227 private static final class YangStoreCache {
230 YangStoreSnapshot cachedYangStoreSnapshot;
232 Optional<YangStoreSnapshot> getCachedYangStore(
233 Multimap<Bundle, URL> bundlesToYangURLs) {
234 Set<URL> urls = setFromMultimapValues(bundlesToYangURLs);
235 if (cachedUrls != null && cachedUrls.equals(urls)) {
236 Preconditions.checkState(cachedYangStoreSnapshot != null);
237 return Optional.of(cachedYangStoreSnapshot);
239 return Optional.absent();
242 private static Set<URL> setFromMultimapValues(
243 Multimap<Bundle, URL> bundlesToYangURLs) {
244 Set<URL> urls = Sets.newHashSet(bundlesToYangURLs.values());
245 Preconditions.checkState(bundlesToYangURLs.size() == urls.size());
249 void cacheYangStore(Multimap<Bundle, URL> urls,
250 YangStoreSnapshot yangStoreSnapshot) {
251 this.cachedUrls = setFromMultimapValues(urls);
252 this.cachedYangStoreSnapshot = yangStoreSnapshot;