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 org.opendaylight.controller.config.yang.store.api.YangStoreException;
18 import org.opendaylight.controller.config.yang.store.api.YangStoreService;
19 import org.opendaylight.controller.config.yang.store.api.YangStoreSnapshot;
20 import org.osgi.framework.Bundle;
21 import org.osgi.framework.BundleContext;
22 import org.osgi.framework.BundleEvent;
23 import org.osgi.util.tracker.BundleTracker;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
27 import javax.annotation.concurrent.GuardedBy;
28 import java.io.IOException;
29 import java.io.InputStream;
31 import java.util.ArrayList;
32 import java.util.Collection;
33 import java.util.Collections;
34 import java.util.Enumeration;
35 import java.util.List;
36 import java.util.regex.Matcher;
37 import java.util.regex.Pattern;
40 * Note on consistency:
41 * When this bundle is activated after other bundles containing yang files, the resolving order
42 * is not preserved. We thus maintain two maps, one containing consistent snapshot, other inconsistent. The
43 * container should eventually send all events and thus making the inconsistent map redundant.
45 public class ExtenderYangTracker extends BundleTracker<Object> implements YangStoreService, AutoCloseable {
47 private static final Logger logger = LoggerFactory.getLogger(ExtenderYangTracker.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;
60 public ExtenderYangTracker(Optional<Pattern> maybeBlacklist, BundleContext bundleContext) {
61 this(new MbeParser(), maybeBlacklist, bundleContext);
65 private Optional<Pattern> maybeBlacklist;
68 ExtenderYangTracker(MbeParser mbeParser, Optional<Pattern> maybeBlacklist, BundleContext bundleContext) {
69 super(bundleContext, BundleEvent.RESOLVED | BundleEvent.UNRESOLVED, null);
70 this.mbeParser = mbeParser;
71 this.maybeBlacklist = maybeBlacklist;
76 public synchronized Object addingBundle(Bundle bundle, BundleEvent event) {
78 // Ignore system bundle:
79 // system bundle might have config-api on classpath &&
80 // config-api contains yang files =>
81 // system bundle might contain yang files from that bundle
82 if (bundle.getBundleId() == 0)
85 if (maybeBlacklist.isPresent()) {
86 Matcher m = maybeBlacklist.get().matcher(bundle.getSymbolicName());
88 logger.debug("Ignoring {} because it is in blacklist {}", bundle, maybeBlacklist);
93 Enumeration<URL> enumeration = bundle.findEntries("META-INF/yang", "*.yang", false);
94 if (enumeration != null && enumeration.hasMoreElements()) {
96 List<URL> addedURLs = new ArrayList<>();
97 while (enumeration.hasMoreElements()) {
98 URL url = enumeration.nextElement();
101 logger.trace("Bundle {} has event {}, bundle state {}, URLs {}", bundle, event, bundle.getState(), addedURLs);
102 // test that yang store is consistent
103 Multimap<Bundle, URL> proposedNewState = HashMultimap.create(consistentBundlesToYangURLs);
104 proposedNewState.putAll(inconsistentBundlesToYangURLs);
105 proposedNewState.putAll(bundle, addedURLs);
107 Preconditions.checkArgument(addedURLs.size() > 0, "No change can occur when no URLs are changed");
109 try(YangStoreSnapshotImpl snapshot = createSnapshot(mbeParser, proposedNewState)) {
110 onSnapshotSuccess(proposedNewState, snapshot);
111 } catch(YangStoreException e) {
112 onSnapshotFailure(bundle, addedURLs, e);
119 private synchronized void onSnapshotFailure(Bundle bundle, List<URL> addedURLs, Exception failureReason) {
120 // inconsistent state
121 inconsistentBundlesToYangURLs.putAll(bundle, addedURLs);
123 logger.debug("Yang store is falling back on last consistent state containing {}, inconsistent yang files {}",
124 consistentBundlesToYangURLs, inconsistentBundlesToYangURLs, failureReason);
125 logger.warn("Yang store is falling back on last consistent state containing {} files, inconsistent yang files size is {}, reason {}",
126 consistentBundlesToYangURLs.size(), inconsistentBundlesToYangURLs.size(), failureReason.toString());
127 cache.setInconsistentURLsForReporting(inconsistentBundlesToYangURLs.values());
130 private synchronized void onSnapshotSuccess(Multimap<Bundle, URL> proposedNewState, YangStoreSnapshotImpl snapshot) {
133 consistentBundlesToYangURLs.clear();
134 consistentBundlesToYangURLs.putAll(proposedNewState);
135 inconsistentBundlesToYangURLs.clear();
137 updateCache(snapshot);
138 cache.setInconsistentURLsForReporting(Collections.<URL> emptySet());
139 logger.info("Yang store updated to new consistent state containing {} yang files", consistentBundlesToYangURLs.size());
140 logger.debug("Yang store updated to new consistent state containing {}", consistentBundlesToYangURLs);
143 private synchronized void updateCache(YangStoreSnapshotImpl snapshot) {
144 cache.cacheYangStore(consistentBundlesToYangURLs, snapshot);
148 public void modifiedBundle(Bundle bundle, BundleEvent event, Object object) {
149 logger.debug("Modified bundle {} {} {}", bundle, event, object);
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 logger.debug("Removed bundle {} {} {}", bundle, event, object);
159 inconsistentBundlesToYangURLs.removeAll(bundle);
160 consistentBundlesToYangURLs.removeAll(bundle);
164 public synchronized YangStoreSnapshot getYangStoreSnapshot()
165 throws YangStoreException {
166 Optional<YangStoreSnapshot> yangStoreOpt = cache.getSnapshotIfPossible(consistentBundlesToYangURLs);
167 if (yangStoreOpt.isPresent()) {
168 logger.debug("Returning cached yang store {}", yangStoreOpt.get());
169 return yangStoreOpt.get();
172 YangStoreSnapshotImpl snapshot = createSnapshot(mbeParser, consistentBundlesToYangURLs);
173 updateCache(snapshot);
177 private static YangStoreSnapshotImpl createSnapshot(MbeParser mbeParser, Multimap<Bundle, URL> multimap) throws YangStoreException {
179 YangStoreSnapshotImpl yangStoreSnapshot = mbeParser.parseYangFiles(fromUrlsToInputStreams(multimap));
180 logger.trace("{} module entries parsed successfully from {} yang files",
181 yangStoreSnapshot.countModuleMXBeanEntries(), multimap.values().size());
182 return yangStoreSnapshot;
183 } catch (RuntimeException e) {
184 StringBuffer causeStr = new StringBuffer();
186 while (cause != null) {
187 causeStr.append(e.getMessage());
188 causeStr.append("\n");
189 cause = e.getCause();
191 throw new YangStoreException("Unable to parse yang files. \n" + causeStr.toString() +
192 "URLs: " + multimap, e);
196 private static Collection<InputStream> fromUrlsToInputStreams(Multimap<Bundle, URL> multimap) {
197 return Collections2.transform(multimap.values(),
198 new Function<URL, InputStream>() {
201 public InputStream apply(URL url) {
203 return url.openStream();
204 } catch (IOException e) {
205 logger.warn("Unable to open stream from {}", url);
206 throw new IllegalStateException(
207 "Unable to open stream from " + url, e);
213 public synchronized void setMaybeBlacklist(Optional<Pattern> maybeBlacklistPattern) {
214 maybeBlacklist = maybeBlacklistPattern;