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.*;
15 import org.opendaylight.controller.config.yang.store.api.YangStoreException;
16 import org.opendaylight.controller.config.yang.store.api.YangStoreListenerRegistration;
17 import org.opendaylight.controller.config.yang.store.api.YangStoreService;
18 import org.opendaylight.controller.config.yang.store.api.YangStoreSnapshot;
19 import org.opendaylight.controller.config.yang.store.spi.YangStoreListener;
20 import org.osgi.framework.Bundle;
21 import org.osgi.framework.BundleEvent;
22 import org.osgi.util.tracker.BundleTrackerCustomizer;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
26 import java.io.IOException;
27 import java.io.InputStream;
32 * Note on consistency:
33 * When this bundle is activated after other bundles containing yang files, the resolving order
34 * is not preserved. We thus maintain two maps, one containing consistent snapshot, other inconsistent. The
35 * container should eventually send all events and thus making the inconsistent map redundant.
37 public class ExtenderYangTrackerCustomizer implements BundleTrackerCustomizer<Object>, YangStoreService {
39 private static final Logger logger = LoggerFactory
40 .getLogger(ExtenderYangTrackerCustomizer.class);
42 private final Multimap<Bundle, URL> consistentBundlesToYangURLs = HashMultimap.create();
45 Map of currently problematic yang files that should get fixed eventually after all events are received.
47 private final Multimap<Bundle, URL> inconsistentBundlesToYangURLs = HashMultimap.create();
49 private final YangStoreCache cache = new YangStoreCache();
50 private final MbeParser mbeParser;
51 private final List<YangStoreListener> listeners = new ArrayList<>();
53 public ExtenderYangTrackerCustomizer() {
54 this(new MbeParser());
59 ExtenderYangTrackerCustomizer(MbeParser mbeParser) {
60 this.mbeParser = mbeParser;
64 public Object addingBundle(Bundle bundle, BundleEvent event) {
66 // Ignore system bundle:
67 // system bundle might have config-api on classpath &&
68 // config-api contains yang files =>
69 // system bundle might contain yang files from that bundle
70 if (bundle.getBundleId() == 0)
73 Enumeration<URL> enumeration = bundle.findEntries("META-INF/yang", "*.yang", false);
74 if (enumeration != null && enumeration.hasMoreElements()) {
76 List<URL> addedURLs = new ArrayList<>();
77 while (enumeration.hasMoreElements()) {
78 URL url = enumeration.nextElement();
81 logger.trace("Bundle {} has event {}, bundle state {}, URLs {}", bundle, event, bundle.getState(), addedURLs);
82 // test that yang store is consistent
83 Multimap<Bundle, URL> proposedNewState = HashMultimap.create(consistentBundlesToYangURLs);
84 proposedNewState.putAll(inconsistentBundlesToYangURLs);
85 proposedNewState.putAll(bundle, addedURLs);
86 boolean adding = true;
87 if (tryToUpdateState(addedURLs, proposedNewState, adding) == false) {
88 inconsistentBundlesToYangURLs.putAll(bundle, addedURLs);
95 private synchronized boolean tryToUpdateState(Collection<URL> changedURLs, Multimap<Bundle, URL> proposedNewState, boolean adding) {
96 Preconditions.checkArgument(changedURLs.size() > 0, "No change can occur when no URLs are changed");
97 try(YangStoreSnapshot snapshot = createSnapshot(mbeParser, proposedNewState)) {
100 consistentBundlesToYangURLs.clear();
101 consistentBundlesToYangURLs.putAll(proposedNewState);
102 inconsistentBundlesToYangURLs.clear();
104 updateCache(snapshot);
105 logger.info("Yang store updated to new consistent state");
106 logger.trace("Yang store updated to new consistent state containing {}", consistentBundlesToYangURLs);
108 notifyListeners(changedURLs, adding);
110 } catch(YangStoreException e) {
111 // inconsistent state
112 logger.debug("Yang store is falling back on last consistent state containing {}, inconsistent yang files {}, reason {}",
113 consistentBundlesToYangURLs, inconsistentBundlesToYangURLs, e.toString());
118 private void updateCache(YangStoreSnapshot snapshot) {
119 cache.cacheYangStore(consistentBundlesToYangURLs, snapshot);
123 public void modifiedBundle(Bundle bundle, BundleEvent event, Object object) {
124 logger.debug("Modified bundle {} {} {}", bundle, event, object);
128 * Notifiers get only notified when consistent snapshot has changed.
130 private void notifyListeners(Collection<URL> changedURLs, boolean adding) {
131 Preconditions.checkArgument(changedURLs.size() > 0, "Cannot notify when no URLs changed");
132 if (changedURLs.size() > 0) {
133 RuntimeException potential = new RuntimeException("Error while notifying listeners");
134 for (YangStoreListener listener : listeners) {
137 listener.onAddedYangURL(changedURLs);
139 listener.onRemovedYangURL(changedURLs);
141 } catch(RuntimeException e) {
142 potential.addSuppressed(e);
145 if (potential.getSuppressed().length > 0) {
153 * If removing YANG files makes yang store inconsistent, method {@link #getYangStoreSnapshot()}
154 * will throw exception. There is no rollback.
157 public synchronized void removedBundle(Bundle bundle, BundleEvent event, Object object) {
158 inconsistentBundlesToYangURLs.removeAll(bundle);
159 Collection<URL> consistentURLsToBeRemoved = consistentBundlesToYangURLs.removeAll(bundle);
161 if (consistentURLsToBeRemoved.isEmpty()){
164 boolean adding = false;
165 notifyListeners(consistentURLsToBeRemoved, adding);
169 public synchronized YangStoreSnapshot getYangStoreSnapshot()
170 throws YangStoreException {
171 Optional<YangStoreSnapshot> yangStoreOpt = cache.getCachedYangStore(consistentBundlesToYangURLs);
172 if (yangStoreOpt.isPresent()) {
173 logger.trace("Returning cached yang store {}", yangStoreOpt.get());
174 return yangStoreOpt.get();
176 YangStoreSnapshot snapshot = createSnapshot(mbeParser, consistentBundlesToYangURLs);
177 updateCache(snapshot);
181 private static YangStoreSnapshot createSnapshot(MbeParser mbeParser, Multimap<Bundle, URL> multimap) throws YangStoreException {
183 YangStoreSnapshot yangStoreSnapshot = mbeParser.parseYangFiles(fromUrlsToInputStreams(multimap));
184 logger.trace("{} module entries parsed successfully from {} yang files",
185 yangStoreSnapshot.countModuleMXBeanEntries(), multimap.values().size());
186 return yangStoreSnapshot;
187 } catch (RuntimeException e) {
188 throw new YangStoreException("Unable to parse yang files from following URLs: " + multimap, e);
192 private static Collection<InputStream> fromUrlsToInputStreams(Multimap<Bundle, URL> multimap) {
193 return Collections2.transform(multimap.values(),
194 new Function<URL, InputStream>() {
197 public InputStream apply(URL url) {
199 return url.openStream();
200 } catch (IOException e) {
201 logger.warn("Unable to open stream from {}", url);
202 throw new IllegalStateException(
203 "Unable to open stream from " + url, e);
210 public synchronized YangStoreListenerRegistration registerListener(final YangStoreListener listener) {
211 listeners.add(listener);
212 return new YangStoreListenerRegistration() {
214 public void close() {
215 listeners.remove(listener);
220 private static final class YangStoreCache {
223 YangStoreSnapshot cachedYangStoreSnapshot;
225 Optional<YangStoreSnapshot> getCachedYangStore(
226 Multimap<Bundle, URL> bundlesToYangURLs) {
227 Set<URL> urls = setFromMultimapValues(bundlesToYangURLs);
228 if (cachedUrls != null && cachedUrls.equals(urls)) {
229 Preconditions.checkState(cachedYangStoreSnapshot != null);
230 return Optional.of(cachedYangStoreSnapshot);
232 return Optional.absent();
235 private static Set<URL> setFromMultimapValues(
236 Multimap<Bundle, URL> bundlesToYangURLs) {
237 Set<URL> urls = Sets.newHashSet(bundlesToYangURLs.values());
238 Preconditions.checkState(bundlesToYangURLs.size() == urls.size());
242 void cacheYangStore(Multimap<Bundle, URL> urls,
243 YangStoreSnapshot yangStoreSnapshot) {
244 this.cachedUrls = setFromMultimapValues(urls);
245 this.cachedYangStoreSnapshot = yangStoreSnapshot;