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.YangStoreService;
20 import org.opendaylight.controller.config.yang.store.api.YangStoreSnapshot;
21 import org.osgi.framework.Bundle;
22 import org.osgi.framework.BundleContext;
23 import org.osgi.framework.BundleEvent;
24 import org.osgi.util.tracker.BundleTracker;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
28 import javax.annotation.concurrent.GuardedBy;
29 import java.io.IOException;
30 import java.io.InputStream;
32 import java.util.ArrayList;
33 import java.util.Collection;
34 import java.util.Collections;
35 import java.util.Enumeration;
36 import java.util.List;
38 import java.util.regex.Matcher;
39 import java.util.regex.Pattern;
42 * Note on consistency:
43 * When this bundle is activated after other bundles containing yang files, the resolving order
44 * is not preserved. We thus maintain two maps, one containing consistent snapshot, other inconsistent. The
45 * container should eventually send all events and thus making the inconsistent map redundant.
47 public class ExtenderYangTracker extends BundleTracker<Object> implements YangStoreService, AutoCloseable {
49 private static final Logger logger = LoggerFactory.getLogger(ExtenderYangTracker.class);
51 private final Multimap<Bundle, URL> consistentBundlesToYangURLs = HashMultimap.create();
54 Map of currently problematic yang files that should get fixed eventually after all events are received.
56 private final Multimap<Bundle, URL> inconsistentBundlesToYangURLs = HashMultimap.create();
58 private final YangStoreCache cache = new YangStoreCache();
59 private final MbeParser mbeParser;
62 public ExtenderYangTracker(Optional<Pattern> maybeBlacklist, BundleContext bundleContext) {
63 this(new MbeParser(), maybeBlacklist, bundleContext);
67 private Optional<Pattern> maybeBlacklist;
70 ExtenderYangTracker(MbeParser mbeParser, Optional<Pattern> maybeBlacklist, BundleContext bundleContext) {
71 super(bundleContext, BundleEvent.RESOLVED | BundleEvent.UNRESOLVED, null);
72 this.mbeParser = mbeParser;
73 this.maybeBlacklist = maybeBlacklist;
78 public synchronized Object addingBundle(Bundle bundle, BundleEvent event) {
80 // Ignore system bundle:
81 // system bundle might have config-api on classpath &&
82 // config-api contains yang files =>
83 // system bundle might contain yang files from that bundle
84 if (bundle.getBundleId() == 0)
87 if (maybeBlacklist.isPresent()) {
88 Matcher m = maybeBlacklist.get().matcher(bundle.getSymbolicName());
90 logger.debug("Ignoring {} because it is in blacklist {}", bundle, maybeBlacklist);
95 Enumeration<URL> enumeration = bundle.findEntries("META-INF/yang", "*.yang", false);
96 if (enumeration != null && enumeration.hasMoreElements()) {
98 List<URL> addedURLs = new ArrayList<>();
99 while (enumeration.hasMoreElements()) {
100 URL url = enumeration.nextElement();
103 logger.trace("Bundle {} has event {}, bundle state {}, URLs {}", bundle, event, bundle.getState(), addedURLs);
104 // test that yang store is consistent
105 Multimap<Bundle, URL> proposedNewState = HashMultimap.create(consistentBundlesToYangURLs);
106 proposedNewState.putAll(inconsistentBundlesToYangURLs);
107 proposedNewState.putAll(bundle, addedURLs);
109 Preconditions.checkArgument(addedURLs.size() > 0, "No change can occur when no URLs are changed");
111 String failureReason = null;
112 try(YangStoreSnapshotImpl snapshot = createSnapshot(mbeParser, proposedNewState)) {
113 updateCache(snapshot);
115 } catch(YangStoreException e) {
116 failureReason = e.toString();
122 consistentBundlesToYangURLs.clear();
123 consistentBundlesToYangURLs.putAll(proposedNewState);
124 inconsistentBundlesToYangURLs.clear();
126 logger.info("Yang store updated to new consistent state containing {} yang files", consistentBundlesToYangURLs.size());
127 logger.trace("Yang store updated to new consistent state containing {}", consistentBundlesToYangURLs);
129 // inconsistent state
130 logger.debug("Yang store is falling back on last consistent state containing {}, inconsistent yang files {}, reason {}",
131 consistentBundlesToYangURLs, inconsistentBundlesToYangURLs, failureReason);
132 logger.warn("Yang store is falling back on last consistent state containing {} files, inconsistent yang files size is {}, reason {}",
133 consistentBundlesToYangURLs.size(), inconsistentBundlesToYangURLs.size(), failureReason);
134 inconsistentBundlesToYangURLs.putAll(bundle, addedURLs);
141 private void updateCache(YangStoreSnapshotImpl snapshot) {
142 cache.cacheYangStore(consistentBundlesToYangURLs, snapshot);
146 public void modifiedBundle(Bundle bundle, BundleEvent event, Object object) {
147 logger.debug("Modified bundle {} {} {}", bundle, event, object);
151 * If removing YANG files makes yang store inconsistent, method {@link #getYangStoreSnapshot()}
152 * will throw exception. There is no rollback.
155 public synchronized void removedBundle(Bundle bundle, BundleEvent event, Object object) {
156 inconsistentBundlesToYangURLs.removeAll(bundle);
157 consistentBundlesToYangURLs.removeAll(bundle);
161 public synchronized YangStoreSnapshot getYangStoreSnapshot()
162 throws YangStoreException {
163 Optional<YangStoreSnapshot> yangStoreOpt = cache.getSnapshotIfPossible(consistentBundlesToYangURLs);
164 if (yangStoreOpt.isPresent()) {
165 logger.trace("Returning cached yang store {}", yangStoreOpt.get());
166 return yangStoreOpt.get();
168 YangStoreSnapshotImpl snapshot = createSnapshot(mbeParser, consistentBundlesToYangURLs);
169 updateCache(snapshot);
173 private static YangStoreSnapshotImpl createSnapshot(MbeParser mbeParser, Multimap<Bundle, URL> multimap) throws YangStoreException {
175 YangStoreSnapshotImpl yangStoreSnapshot = mbeParser.parseYangFiles(fromUrlsToInputStreams(multimap));
176 logger.trace("{} module entries parsed successfully from {} yang files",
177 yangStoreSnapshot.countModuleMXBeanEntries(), multimap.values().size());
178 return yangStoreSnapshot;
179 } catch (RuntimeException e) {
180 throw new YangStoreException("Unable to parse yang files from following URLs: " + multimap, e);
184 private static Collection<InputStream> fromUrlsToInputStreams(Multimap<Bundle, URL> multimap) {
185 return Collections2.transform(multimap.values(),
186 new Function<URL, InputStream>() {
189 public InputStream apply(URL url) {
191 return url.openStream();
192 } catch (IOException e) {
193 logger.warn("Unable to open stream from {}", url);
194 throw new IllegalStateException(
195 "Unable to open stream from " + url, e);
201 public synchronized void setMaybeBlacklist(Optional<Pattern> maybeBlacklistPattern) {
202 maybeBlacklist = maybeBlacklistPattern;
207 class YangStoreCache {
209 private Set<URL> cachedUrls = Collections.emptySet();
211 private Optional<YangStoreSnapshotImpl> cachedYangStoreSnapshot = Optional.absent();
213 synchronized Optional<YangStoreSnapshot> getSnapshotIfPossible(Multimap<Bundle, URL> bundlesToYangURLs) {
214 Set<URL> urls = setFromMultimapValues(bundlesToYangURLs);
215 if (cachedUrls != null && cachedUrls.equals(urls)) {
216 Preconditions.checkState(cachedYangStoreSnapshot.isPresent());
217 YangStoreSnapshot freshSnapshot = new YangStoreSnapshotImpl(cachedYangStoreSnapshot.get());
218 return Optional.of(freshSnapshot);
220 return Optional.absent();
223 private static Set<URL> setFromMultimapValues(
224 Multimap<Bundle, URL> bundlesToYangURLs) {
225 Set<URL> urls = Sets.newHashSet(bundlesToYangURLs.values());
226 Preconditions.checkState(bundlesToYangURLs.size() == urls.size());
230 synchronized void cacheYangStore(Multimap<Bundle, URL> urls,
231 YangStoreSnapshotImpl yangStoreSnapshot) {
232 this.cachedUrls = setFromMultimapValues(urls);
233 this.cachedYangStoreSnapshot = Optional.of(yangStoreSnapshot);
236 synchronized void invalidate() {
238 if (cachedYangStoreSnapshot.isPresent()){
239 cachedYangStoreSnapshot.get().close();
240 cachedYangStoreSnapshot = Optional.absent();