+++ /dev/null
-/*
- * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
- *
- * This program and the accompanying materials are made available under the
- * terms of the Eclipse Public License v1.0 which accompanies this distribution,
- * and is available at http://www.eclipse.org/legal/epl-v10.html
- */
-package org.opendaylight.controller.config.yang.store.impl;
-
-import java.io.IOException;
-import java.io.InputStream;
-import java.net.URL;
-import java.util.*;
-
-import org.opendaylight.controller.config.yang.store.api.YangStoreException;
-import org.opendaylight.controller.config.yang.store.api.YangStoreListenerRegistration;
-import org.opendaylight.controller.config.yang.store.api.YangStoreService;
-import org.opendaylight.controller.config.yang.store.api.YangStoreSnapshot;
-import org.opendaylight.controller.config.yang.store.spi.YangStoreListener;
-import org.osgi.framework.Bundle;
-import org.osgi.framework.BundleContext;
-import org.osgi.framework.BundleEvent;
-import org.osgi.util.tracker.BundleTracker;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
-import com.google.common.annotations.VisibleForTesting;
-import com.google.common.base.Function;
-import com.google.common.base.Optional;
-import com.google.common.base.Preconditions;
-import com.google.common.collect.Collections2;
-import com.google.common.collect.HashMultimap;
-import com.google.common.collect.Multimap;
-import com.google.common.collect.Sets;
-
-public class ExtenderYangTracker extends BundleTracker<Object> implements
- YangStoreService {
-
- private static final Logger logger = LoggerFactory
- .getLogger(ExtenderYangTracker.class);
-
- private final Multimap<Bundle, URL> bundlesToYangURLs = HashMultimap
- .create();
- private final YangStoreCache cache = new YangStoreCache();
- private final MbeParser mbeParser;
- private final List<YangStoreListener> listeners = new ArrayList<>();
-
- public ExtenderYangTracker(BundleContext context) {
- this(context, new MbeParser());
-
- }
-
- @VisibleForTesting
- ExtenderYangTracker(BundleContext context, MbeParser mbeParser) {
- super(context, Bundle.ACTIVE, null);
- this.mbeParser = mbeParser;
- logger.trace("Registered as extender with context {}", context);
- }
-
- @Override
- public Object addingBundle(Bundle bundle, BundleEvent event) {
-
- // Ignore system bundle:
- // system bundle might have config-api on classpath &&
- // config-api contains yang files =>
- // system bundle might contain yang files from that bundle
- if (bundle.getBundleId() == 0)
- return bundle;
-
- Enumeration<URL> yangURLs = bundle.findEntries("META-INF/yang", "*.yang", false);
- if (yangURLs != null) {
- synchronized (this) {
- while (yangURLs.hasMoreElements()) {
- URL url = yangURLs.nextElement();
- logger.debug("Bundle {} found yang file {}", bundle, url);
- bundlesToYangURLs.put(bundle, url);
- }
- Collection<URL> urls = bundlesToYangURLs.get(bundle);
- notifyListeners(urls, true);
- }
- }
- return bundle;
- }
-
- private void notifyListeners(Collection<URL> urls, boolean adding) {
- if (urls.size() > 0) {
- RuntimeException potential = new RuntimeException("Error while notifying listeners");
- for (YangStoreListener listener : listeners) {
- try {
- if (adding) {
- listener.onAddedYangURL(urls);
- } else {
- listener.onRemovedYangURL(urls);
- }
- } catch(RuntimeException e) {
- potential.addSuppressed(e);
- }
- }
- if (potential.getSuppressed().length > 0) {
- throw potential;
- }
- }
- }
-
- @Override
- public synchronized void removedBundle(Bundle bundle, BundleEvent event, Object object) {
- Collection<URL> urls = bundlesToYangURLs.removeAll(bundle);
- if (urls.size() > 0) {
- logger.debug("Removed following yang URLs {} because of removed bundle {}", urls, bundle);
- notifyListeners(urls, false);
- }
- }
-
- @Override
- public synchronized YangStoreSnapshot getYangStoreSnapshot()
- throws YangStoreException {
- Optional<YangStoreSnapshot> yangStoreOpt = cache
- .getCachedYangStore(bundlesToYangURLs);
- if (yangStoreOpt.isPresent()) {
- logger.debug("Returning cached yang store {}", yangStoreOpt.get());
- return yangStoreOpt.get();
- }
-
- try {
- YangStoreSnapshot yangStoreSnapshot = mbeParser
- .parseYangFiles(fromUrlsToInputStreams());
- logger.debug(
- "{} module entries parsed successfully from {} yang files",
- yangStoreSnapshot.countModuleMXBeanEntries(),
- bundlesToYangURLs.values().size());
- cache.cacheYangStore(bundlesToYangURLs, yangStoreSnapshot);
-
- return yangStoreSnapshot;
- } catch (RuntimeException e) {
- logger.warn(
- "Unable to parse yang files, yang files that were picked up so far: {}",
- bundlesToYangURLs, e);
- throw new YangStoreException("Unable to parse yang files", e);
- }
- }
-
- private Collection<InputStream> fromUrlsToInputStreams() {
- return Collections2.transform(bundlesToYangURLs.values(),
- new Function<URL, InputStream>() {
-
- @Override
- public InputStream apply(URL url) {
- try {
- return url.openStream();
- } catch (IOException e) {
- logger.warn("Unable to open stream from {}", url);
- throw new IllegalStateException(
- "Unable to open stream from " + url, e);
- }
- }
- });
- }
-
- @Override
- public synchronized YangStoreListenerRegistration registerListener(final YangStoreListener listener) {
- listeners.add(listener);
- return new YangStoreListenerRegistration() {
- @Override
- public void close() {
- listeners.remove(listener);
- }
- };
- }
-
- private static final class YangStoreCache {
-
- Set<URL> cachedUrls;
- YangStoreSnapshot cachedYangStoreSnapshot;
-
- Optional<YangStoreSnapshot> getCachedYangStore(
- Multimap<Bundle, URL> bundlesToYangURLs) {
- Set<URL> urls = setFromMultimapValues(bundlesToYangURLs);
- if (cachedUrls != null && cachedUrls.equals(urls)) {
- Preconditions.checkState(cachedYangStoreSnapshot != null);
- return Optional.of(cachedYangStoreSnapshot);
- }
- return Optional.absent();
- }
-
- private static Set<URL> setFromMultimapValues(
- Multimap<Bundle, URL> bundlesToYangURLs) {
- Set<URL> urls = Sets.newHashSet(bundlesToYangURLs.values());
- Preconditions.checkState(bundlesToYangURLs.size() == urls.size());
- return urls;
- }
-
- void cacheYangStore(Multimap<Bundle, URL> urls,
- YangStoreSnapshot yangStoreSnapshot) {
- this.cachedUrls = setFromMultimapValues(urls);
- this.cachedYangStoreSnapshot = yangStoreSnapshot;
- }
-
- }
-}
--- /dev/null
+/*
+ * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+package org.opendaylight.controller.config.yang.store.impl;
+
+import com.google.common.annotations.VisibleForTesting;
+import com.google.common.base.Function;
+import com.google.common.base.Optional;
+import com.google.common.base.Preconditions;
+import com.google.common.collect.*;
+import org.opendaylight.controller.config.yang.store.api.YangStoreException;
+import org.opendaylight.controller.config.yang.store.api.YangStoreListenerRegistration;
+import org.opendaylight.controller.config.yang.store.api.YangStoreService;
+import org.opendaylight.controller.config.yang.store.api.YangStoreSnapshot;
+import org.opendaylight.controller.config.yang.store.spi.YangStoreListener;
+import org.osgi.framework.Bundle;
+import org.osgi.framework.BundleEvent;
+import org.osgi.util.tracker.BundleTrackerCustomizer;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.URL;
+import java.util.*;
+
+/**
+ * Note on consistency:
+ * When this bundle is activated after other bundles containing yang files, the resolving order
+ * is not preserved. We thus maintain two maps, one containing consistent snapshot, other inconsistent. The
+ * container should eventually send all events and thus making the inconsistent map redundant.
+ */
+public class ExtenderYangTrackerCustomizer implements BundleTrackerCustomizer<Object>, YangStoreService {
+
+ private static final Logger logger = LoggerFactory
+ .getLogger(ExtenderYangTrackerCustomizer.class);
+
+ private final Multimap<Bundle, URL> consistentBundlesToYangURLs = HashMultimap.create();
+
+ /*
+ Map of currently problematic yang files that should get fixed eventually after all events are received.
+ */
+ private final Multimap<Bundle, URL> inconsistentBundlesToYangURLs = HashMultimap.create();
+
+ private final YangStoreCache cache = new YangStoreCache();
+ private final MbeParser mbeParser;
+ private final List<YangStoreListener> listeners = new ArrayList<>();
+
+ public ExtenderYangTrackerCustomizer() {
+ this(new MbeParser());
+
+ }
+
+ @VisibleForTesting
+ ExtenderYangTrackerCustomizer(MbeParser mbeParser) {
+ this.mbeParser = mbeParser;
+ }
+
+ @Override
+ public Object addingBundle(Bundle bundle, BundleEvent event) {
+
+ // Ignore system bundle:
+ // system bundle might have config-api on classpath &&
+ // config-api contains yang files =>
+ // system bundle might contain yang files from that bundle
+ if (bundle.getBundleId() == 0)
+ return bundle;
+
+ Enumeration<URL> enumeration = bundle.findEntries("META-INF/yang", "*.yang", false);
+ if (enumeration != null && enumeration.hasMoreElements()) {
+ synchronized (this) {
+ List<URL> addedURLs = new ArrayList<>();
+ while (enumeration.hasMoreElements()) {
+ URL url = enumeration.nextElement();
+ addedURLs.add(url);
+ }
+ logger.trace("Bundle {} has event {}, bundle state {}, URLs {}", bundle, event, bundle.getState(), addedURLs);
+ // test that yang store is consistent
+ Multimap<Bundle, URL> proposedNewState = HashMultimap.create(consistentBundlesToYangURLs);
+ proposedNewState.putAll(inconsistentBundlesToYangURLs);
+ proposedNewState.putAll(bundle, addedURLs);
+ boolean adding = true;
+ if (tryToUpdateState(addedURLs, proposedNewState, adding) == false) {
+ inconsistentBundlesToYangURLs.putAll(bundle, addedURLs);
+ }
+ }
+ }
+ return bundle;
+ }
+
+ private synchronized boolean tryToUpdateState(Collection<URL> changedURLs, Multimap<Bundle, URL> proposedNewState, boolean adding) {
+ Preconditions.checkArgument(changedURLs.size() > 0, "No change can occur when no URLs are changed");
+ try(YangStoreSnapshot snapshot = createSnapshot(mbeParser, proposedNewState)) {
+ // consistent state
+ // merge into
+ consistentBundlesToYangURLs.clear();
+ consistentBundlesToYangURLs.putAll(proposedNewState);
+ inconsistentBundlesToYangURLs.clear();
+ // update cache
+ updateCache(snapshot);
+ logger.info("Yang store updated to new consistent state");
+ logger.trace("Yang store updated to new consistent state containing {}", consistentBundlesToYangURLs);
+
+ notifyListeners(changedURLs, adding);
+ return true;
+ } catch(YangStoreException e) {
+ // inconsistent state
+ logger.debug("Yang store is falling back on last consistent state containing {}, inconsistent yang files {}, reason {}",
+ consistentBundlesToYangURLs, inconsistentBundlesToYangURLs, e.toString());
+ return false;
+ }
+ }
+
+ private void updateCache(YangStoreSnapshot snapshot) {
+ cache.cacheYangStore(consistentBundlesToYangURLs, snapshot);
+ }
+
+ @Override
+ public void modifiedBundle(Bundle bundle, BundleEvent event, Object object) {
+ logger.debug("Modified bundle {} {} {}", bundle, event, object);
+ }
+
+ /**
+ * Notifiers get only notified when consistent snapshot has changed.
+ */
+ private void notifyListeners(Collection<URL> changedURLs, boolean adding) {
+ Preconditions.checkArgument(changedURLs.size() > 0, "Cannot notify when no URLs changed");
+ if (changedURLs.size() > 0) {
+ RuntimeException potential = new RuntimeException("Error while notifying listeners");
+ for (YangStoreListener listener : listeners) {
+ try {
+ if (adding) {
+ listener.onAddedYangURL(changedURLs);
+ } else {
+ listener.onRemovedYangURL(changedURLs);
+ }
+ } catch(RuntimeException e) {
+ potential.addSuppressed(e);
+ }
+ }
+ if (potential.getSuppressed().length > 0) {
+ throw potential;
+ }
+ }
+ }
+
+
+ /**
+ * If removing YANG files makes yang store inconsistent, method {@link #getYangStoreSnapshot()}
+ * will throw exception. There is no rollback.
+ */
+ @Override
+ public synchronized void removedBundle(Bundle bundle, BundleEvent event, Object object) {
+ inconsistentBundlesToYangURLs.removeAll(bundle);
+ Collection<URL> consistentURLsToBeRemoved = consistentBundlesToYangURLs.removeAll(bundle);
+
+ if (consistentURLsToBeRemoved.isEmpty()){
+ return; // no change
+ }
+ boolean adding = false;
+ notifyListeners(consistentURLsToBeRemoved, adding);
+ }
+
+ @Override
+ public synchronized YangStoreSnapshot getYangStoreSnapshot()
+ throws YangStoreException {
+ Optional<YangStoreSnapshot> yangStoreOpt = cache.getCachedYangStore(consistentBundlesToYangURLs);
+ if (yangStoreOpt.isPresent()) {
+ logger.trace("Returning cached yang store {}", yangStoreOpt.get());
+ return yangStoreOpt.get();
+ }
+ YangStoreSnapshot snapshot = createSnapshot(mbeParser, consistentBundlesToYangURLs);
+ updateCache(snapshot);
+ return snapshot;
+ }
+
+ private static YangStoreSnapshot createSnapshot(MbeParser mbeParser, Multimap<Bundle, URL> multimap) throws YangStoreException {
+ try {
+ YangStoreSnapshot yangStoreSnapshot = mbeParser.parseYangFiles(fromUrlsToInputStreams(multimap));
+ logger.trace("{} module entries parsed successfully from {} yang files",
+ yangStoreSnapshot.countModuleMXBeanEntries(), multimap.values().size());
+ return yangStoreSnapshot;
+ } catch (RuntimeException e) {
+ throw new YangStoreException("Unable to parse yang files from following URLs: " + multimap, e);
+ }
+ }
+
+ private static Collection<InputStream> fromUrlsToInputStreams(Multimap<Bundle, URL> multimap) {
+ return Collections2.transform(multimap.values(),
+ new Function<URL, InputStream>() {
+
+ @Override
+ public InputStream apply(URL url) {
+ try {
+ return url.openStream();
+ } catch (IOException e) {
+ logger.warn("Unable to open stream from {}", url);
+ throw new IllegalStateException(
+ "Unable to open stream from " + url, e);
+ }
+ }
+ });
+ }
+
+ @Override
+ public synchronized YangStoreListenerRegistration registerListener(final YangStoreListener listener) {
+ listeners.add(listener);
+ return new YangStoreListenerRegistration() {
+ @Override
+ public void close() {
+ listeners.remove(listener);
+ }
+ };
+ }
+
+ private static final class YangStoreCache {
+
+ Set<URL> cachedUrls;
+ YangStoreSnapshot cachedYangStoreSnapshot;
+
+ Optional<YangStoreSnapshot> getCachedYangStore(
+ Multimap<Bundle, URL> bundlesToYangURLs) {
+ Set<URL> urls = setFromMultimapValues(bundlesToYangURLs);
+ if (cachedUrls != null && cachedUrls.equals(urls)) {
+ Preconditions.checkState(cachedYangStoreSnapshot != null);
+ return Optional.of(cachedYangStoreSnapshot);
+ }
+ return Optional.absent();
+ }
+
+ private static Set<URL> setFromMultimapValues(
+ Multimap<Bundle, URL> bundlesToYangURLs) {
+ Set<URL> urls = Sets.newHashSet(bundlesToYangURLs.values());
+ Preconditions.checkState(bundlesToYangURLs.size() == urls.size());
+ return urls;
+ }
+
+ void cacheYangStore(Multimap<Bundle, URL> urls,
+ YangStoreSnapshot yangStoreSnapshot) {
+ this.cachedUrls = setFromMultimapValues(urls);
+ this.cachedYangStoreSnapshot = yangStoreSnapshot;
+ }
+
+ }
+}
import org.opendaylight.controller.config.yang.store.api.YangStoreService;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
+import org.osgi.framework.BundleEvent;
import org.osgi.framework.ServiceRegistration;
+import org.osgi.util.tracker.BundleTracker;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class YangStoreActivator implements BundleActivator {
- private ExtenderYangTracker extender;
+ private BundleTracker bundleTracker;
private ServiceRegistration<YangStoreService> registration;
private static final Logger logger = LoggerFactory
.getLogger(YangStoreActivator.class);
@Override
public void start(BundleContext context) throws Exception {
- extender = new ExtenderYangTracker(context);
- extender.open();
+ ExtenderYangTrackerCustomizer customizerAndService = new ExtenderYangTrackerCustomizer();
+ bundleTracker = new BundleTracker(context, BundleEvent.RESOLVED | BundleEvent.UNRESOLVED, customizerAndService);
+ bundleTracker.open();
Dictionary<String, ?> properties = new Hashtable<>();
registration = context.registerService(YangStoreService.class,
- extender, properties);
+ customizerAndService, properties);
}
@Override
public void stop(BundleContext context) throws Exception {
try {
- extender.close();
+ bundleTracker.close();
} catch (Exception e) {
- logger.warn("Exception while closing extender", e);
+ logger.warn("Exception while closing bundleTracker", e);
}
-
- if (registration != null)
+ if (registration != null) {
try {
registration.unregister();
} catch (Exception e) {
logger.warn("Exception while unregistring yang store service",
e);
}
+ }
}
}
import static org.junit.Assert.assertEquals;
import static org.mockito.Matchers.anyCollectionOf;
-import static org.mockito.Mockito.doReturn;
-import static org.mockito.Mockito.mock;
-import static org.mockito.Mockito.times;
-import static org.mockito.Mockito.verify;
-import static org.mockito.Mockito.verifyNoMoreInteractions;
+import static org.mockito.Mockito.*;
import java.io.InputStream;
import java.net.MalformedURLException;
import com.google.common.collect.Lists;
-public class ExtenderYangTrackerTest {
+public class ExtenderYangTrackerCustomizerTest {
- @Mock
- private BundleContext context;
- private ExtenderYangTracker tested;
+
+ private ExtenderYangTrackerCustomizer tested;
@Mock
private MbeParser parser;
@Mock
@Before
public void setUp() throws YangStoreException {
MockitoAnnotations.initMocks(this);
- doReturn("context").when(context).toString();
- tested = new ExtenderYangTracker(context, parser);
+
+ tested = new ExtenderYangTrackerCustomizer(parser);
doReturn(yangStoreSnapshot).when(parser).parseYangFiles(
anyCollectionOf(InputStream.class));
doReturn(22).when(yangStoreSnapshot).countModuleMXBeanEntries();
doReturn("mock yang store").when(yangStoreSnapshot).toString();
+ doNothing().when(yangStoreSnapshot).close();
}
@Test
bundle = getMockedBundle(10, false);
tested.addingBundle(bundle, null);
- tested.getYangStoreSnapshot();
+ for(int i = 0; i< 10; i++){
+ tested.getYangStoreSnapshot();
+ }
- verify(parser, times(3)).parseYangFiles(
+ verify(parser, times(5)).parseYangFiles(
anyCollectionOf(InputStream.class));
returnedStore = tested.getYangStoreSnapshot();
private Bundle getMockedBundle(int sizeOfUrls, boolean system)
throws MalformedURLException {
Bundle mock = mock(Bundle.class);
+ doReturn(32).when(mock).getState();//mock just for logging
List<URL> urls = Lists.newArrayList();
for (int i = 0; i < sizeOfUrls; i++) {