X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=netconf%2Fmdsal-netconf-connector%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fnetconf%2Fmdsal%2Fconnector%2FCurrentSchemaContext.java;h=cd542c1d5fd491023747195cc5e956fd8f96dbb3;hb=9ea4622349b8f1e149e9d8339334c450607e3f48;hp=d83cfb56f40d07bf0965a4ebd56c7303686efa2b;hpb=9c56fbbb1c62243df9baa3b95140153f91ffdde9;p=netconf.git diff --git a/netconf/mdsal-netconf-connector/src/main/java/org/opendaylight/netconf/mdsal/connector/CurrentSchemaContext.java b/netconf/mdsal-netconf-connector/src/main/java/org/opendaylight/netconf/mdsal/connector/CurrentSchemaContext.java index d83cfb56f4..cd542c1d5f 100644 --- a/netconf/mdsal-netconf-connector/src/main/java/org/opendaylight/netconf/mdsal/connector/CurrentSchemaContext.java +++ b/netconf/mdsal-netconf-connector/src/main/java/org/opendaylight/netconf/mdsal/connector/CurrentSchemaContext.java @@ -5,62 +5,78 @@ * 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.netconf.mdsal.connector; -import com.google.common.base.Preconditions; -import com.google.common.collect.Sets; +import static com.google.common.base.Preconditions.checkState; + import java.util.Collections; +import java.util.HashSet; import java.util.Set; import java.util.concurrent.atomic.AtomicReference; -import org.opendaylight.controller.config.util.capability.Capability; -import org.opendaylight.controller.sal.core.api.model.SchemaService; -import org.opendaylight.netconf.api.monitoring.CapabilityListener; -import org.opendaylight.yangtools.concepts.ListenerRegistration; -import org.opendaylight.yangtools.yang.model.api.SchemaContext; -import org.opendaylight.yangtools.yang.model.api.SchemaContextListener; +import org.eclipse.jdt.annotation.NonNull; +import org.opendaylight.mdsal.dom.api.DOMSchemaService; +import org.opendaylight.netconf.api.capability.Capability; +import org.opendaylight.netconf.server.api.monitoring.CapabilityListener; +import org.opendaylight.yangtools.concepts.Registration; +import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext; +import org.opendaylight.yangtools.yang.model.api.EffectiveModelContextListener; import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource; import org.opendaylight.yangtools.yang.model.repo.spi.SchemaSourceProvider; -public class CurrentSchemaContext implements SchemaContextListener, AutoCloseable { - private final AtomicReference currentContext = new AtomicReference<>(); - private final ListenerRegistration schemaContextListenerListenerRegistration; - private final Set listeners1 = Collections.synchronizedSet(Sets.newHashSet()); +// Non-final for mocking +@SuppressWarnings("checkstyle:FinalClass") +public class CurrentSchemaContext implements EffectiveModelContextListener, AutoCloseable { + private final AtomicReference currentContext = new AtomicReference<>(); + private final Set listeners1 = Collections.synchronizedSet(new HashSet<>()); private final SchemaSourceProvider rootSchemaSourceProvider; - public SchemaContext getCurrentContext() { - Preconditions.checkState(currentContext.get() != null, "Current context not received"); - return currentContext.get(); - } + private Registration schemaContextListenerListenerRegistration; - public CurrentSchemaContext(final SchemaService schemaService, - final SchemaSourceProvider rootSchemaSourceProvider) { + private CurrentSchemaContext(final SchemaSourceProvider rootSchemaSourceProvider) { this.rootSchemaSourceProvider = rootSchemaSourceProvider; - schemaContextListenerListenerRegistration = schemaService.registerSchemaContextListener(this); + } + + // keep spotbugs from complaining about overridable method in constructor + public static CurrentSchemaContext create(final DOMSchemaService schemaService, + final SchemaSourceProvider rootSchemaSourceProvider) { + var context = new CurrentSchemaContext(rootSchemaSourceProvider); + final Registration registration = schemaService.registerSchemaContextListener(context); + context.setRegistration(registration); + return context; + } + + private void setRegistration(final Registration registration) { + schemaContextListenerListenerRegistration = registration; + } + + public @NonNull EffectiveModelContext getCurrentContext() { + final var ret = currentContext.get(); + checkState(ret != null, "Current context not received"); + return ret; } @Override - public void onGlobalContextUpdated(final SchemaContext schemaContext) { + public void onModelContextUpdated(final EffectiveModelContext schemaContext) { currentContext.set(schemaContext); // FIXME is notifying all the listeners from this callback wise ? final Set addedCaps = MdsalNetconfOperationServiceFactory.transformCapabilities( currentContext.get(), rootSchemaSourceProvider); for (final CapabilityListener listener : listeners1) { - listener.onCapabilitiesChanged(addedCaps, Collections.emptySet()); + listener.onCapabilitiesChanged(addedCaps, Set.of()); } } @Override - public void close() throws Exception { + public void close() { listeners1.clear(); schemaContextListenerListenerRegistration.close(); currentContext.set(null); } - public AutoCloseable registerCapabilityListener(final CapabilityListener listener) { + public Registration registerCapabilityListener(final CapabilityListener listener) { listener.onCapabilitiesChanged(MdsalNetconfOperationServiceFactory.transformCapabilities(currentContext.get(), - rootSchemaSourceProvider), Collections.emptySet()); + rootSchemaSourceProvider), Set.of()); listeners1.add(listener); return () -> listeners1.remove(listener); } -} \ No newline at end of file +}