X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=opendaylight%2Fmd-sal%2Fsal-dom-broker%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fsal%2Fdom%2Fbroker%2FSchemaServiceImpl.java;h=8afa1eeb5f0e475cfe65fb43936967a6f35bb0f3;hb=d1c4fb2d767d72a8e215c789ba5cd6c2b1cd4425;hp=ba558c51fdd039990a8a7a7e8c5eda4629476c0b;hpb=f467b1de7ed7a25d19d9210c0372bfb8b8fd697f;p=controller.git diff --git a/opendaylight/md-sal/sal-dom-broker/src/main/java/org/opendaylight/controller/sal/dom/broker/SchemaServiceImpl.java b/opendaylight/md-sal/sal-dom-broker/src/main/java/org/opendaylight/controller/sal/dom/broker/SchemaServiceImpl.java index ba558c51fd..8afa1eeb5f 100644 --- a/opendaylight/md-sal/sal-dom-broker/src/main/java/org/opendaylight/controller/sal/dom/broker/SchemaServiceImpl.java +++ b/opendaylight/md-sal/sal-dom-broker/src/main/java/org/opendaylight/controller/sal/dom/broker/SchemaServiceImpl.java @@ -13,6 +13,8 @@ import java.util.zip.Checksum; import org.opendaylight.yangtools.yang.model.api.SchemaContext; import org.osgi.util.tracker.BundleTracker; import org.osgi.util.tracker.BundleTrackerCustomizer; +import org.osgi.util.tracker.ServiceTracker; +import org.osgi.util.tracker.ServiceTrackerCustomizer; import org.opendaylight.yangtools.yang.model.api.Module; import org.opendaylight.yangtools.yang.model.parser.api.YangModelParser; import org.opendaylight.yangtools.yang.parser.impl.YangParserImpl; @@ -20,10 +22,12 @@ import org.osgi.framework.Bundle; import org.osgi.framework.BundleActivator; import org.osgi.framework.BundleContext; import org.osgi.framework.BundleEvent; +import org.osgi.framework.ServiceReference; import org.opendaylight.yangtools.concepts.ListenerRegistration; import org.opendaylight.yangtools.concepts.util.ListenerRegistry; import org.opendaylight.controller.sal.core.api.model.SchemaService; import org.opendaylight.controller.sal.core.api.model.SchemaServiceListener; +import org.opendaylight.controller.sal.dom.broker.impl.SchemaContextProvider; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -34,9 +38,14 @@ import com.google.common.collect.Collections2; import com.google.common.collect.HashMultimap; import com.google.common.collect.Multimap; import com.google.common.collect.Sets; + import static com.google.common.base.Preconditions.*; -public class SchemaServiceImpl implements SchemaService, AutoCloseable { +public class SchemaServiceImpl implements // + SchemaContextProvider, // + SchemaService, // + ServiceTrackerCustomizer, // + AutoCloseable { private static final Logger logger = LoggerFactory.getLogger(SchemaServiceImpl.class); private ListenerRegistry listeners; @@ -54,6 +63,8 @@ public class SchemaServiceImpl implements SchemaService, AutoCloseable { private BundleTracker bundleTracker; private final YangStoreCache cache = new YangStoreCache(); + private ServiceTracker listenerTracker; + public ListenerRegistry getListeners() { return listeners; } @@ -85,10 +96,18 @@ public class SchemaServiceImpl implements SchemaService, AutoCloseable { listeners = new ListenerRegistry<>(); } + listenerTracker = new ServiceTracker<>(context, SchemaServiceListener.class, this); bundleTracker = new BundleTracker(context, BundleEvent.RESOLVED | BundleEvent.UNRESOLVED, scanner); bundleTracker.open(); + listenerTracker.open(); } + + @Override + public SchemaContext getSchemaContext() { + return getGlobalContext(); + } + public SchemaContext getGlobalContext() { return getSchemaContextSnapshot(); } @@ -121,12 +140,11 @@ public class SchemaServiceImpl implements SchemaService, AutoCloseable { throw new UnsupportedOperationException(); } - @Override public ListenerRegistration registerSchemaServiceListener(SchemaServiceListener listener) { return listeners.register(listener); } - + @Override public void close() throws Exception { bundleTracker.close(); @@ -136,7 +154,7 @@ public class SchemaServiceImpl implements SchemaService, AutoCloseable { private synchronized boolean tryToUpdateState(Collection changedURLs, Multimap proposedNewState, boolean adding) { - Preconditions.checkArgument(changedURLs.size() > 0, "No change can occur when no URLs are changed"); + Preconditions.checkArgument(!changedURLs.isEmpty(), "No change can occur when no URLs are changed"); try { // consistent state @@ -155,8 +173,8 @@ public class SchemaServiceImpl implements SchemaService, AutoCloseable { } catch (Exception e) { // inconsistent state logger.debug( - "SchemaService is falling back on last consistent state containing {}, inconsistent yang files {}, reason {}", - consistentBundlesToYangURLs, inconsistentBundlesToYangURLs, e.toString()); + "SchemaService is falling back on last consistent state containing {}, inconsistent yang files {}", + consistentBundlesToYangURLs, inconsistentBundlesToYangURLs, e); return false; } } @@ -185,11 +203,23 @@ public class SchemaServiceImpl implements SchemaService, AutoCloseable { private void updateCache(SchemaContext snapshot) { cache.cacheYangStore(consistentBundlesToYangURLs, snapshot); + + Object[] services = listenerTracker.getServices(); + if (services != null) { + for (Object rawListener : services) { + SchemaServiceListener listener = (SchemaServiceListener) rawListener; + try { + listener.onGlobalContextUpdated(snapshot); + } catch (Exception e) { + logger.error("Exception occured during invoking listener", e); + } + } + } for (ListenerRegistration listener : listeners) { try { listener.getInstance().onGlobalContextUpdated(snapshot); } catch (Exception e) { - logger.error("Exception occured during invoking listener",e); + logger.error("Exception occured during invoking listener", e); } } } @@ -202,8 +232,9 @@ public class SchemaServiceImpl implements SchemaService, AutoCloseable { // 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) + if (bundle.getBundleId() == 0) { return bundle; + } Enumeration enumeration = bundle.findEntries("META-INF/yang", "*.yang", false); if (enumeration != null && enumeration.hasMoreElements()) { @@ -220,6 +251,7 @@ public class SchemaServiceImpl implements SchemaService, AutoCloseable { proposedNewState.putAll(inconsistentBundlesToYangURLs); proposedNewState.putAll(bundle, addedURLs); boolean adding = true; + if (tryToUpdateState(addedURLs, proposedNewState, adding) == false) { inconsistentBundlesToYangURLs.putAll(bundle, addedURLs); } @@ -276,6 +308,26 @@ public class SchemaServiceImpl implements SchemaService, AutoCloseable { this.cachedUrls = setFromMultimapValues(urls); this.cachedContextSnapshot = ctx; } + } + @Override + public SchemaServiceListener addingService(ServiceReference reference) { + + SchemaServiceListener listener = context.getService(reference); + SchemaContext _ctxContext = getGlobalContext(); + if (getContext() != null) { + listener.onGlobalContextUpdated(_ctxContext); + } + return listener; + } + + @Override + public void modifiedService(ServiceReference reference, SchemaServiceListener service) { + // NOOP + } + + @Override + public void removedService(ServiceReference reference, SchemaServiceListener service) { + context.ungetService(reference); } }