Move netconf.api.monitoring
[netconf.git] / netconf / mdsal-netconf-connector / src / main / java / org / opendaylight / netconf / mdsal / connector / CurrentSchemaContext.java
index 5409f1cda257d765c3647e4227ae17713627c34b..cd542c1d5fd491023747195cc5e956fd8f96dbb3 100644 (file)
@@ -16,24 +16,37 @@ import java.util.concurrent.atomic.AtomicReference;
 import org.eclipse.jdt.annotation.NonNull;
 import org.opendaylight.mdsal.dom.api.DOMSchemaService;
 import org.opendaylight.netconf.api.capability.Capability;
-import org.opendaylight.netconf.api.monitoring.CapabilityListener;
-import org.opendaylight.yangtools.concepts.ListenerRegistration;
+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;
 
 // Non-final for mocking
+@SuppressWarnings("checkstyle:FinalClass")
 public class CurrentSchemaContext implements EffectiveModelContextListener, AutoCloseable {
     private final AtomicReference<EffectiveModelContext> currentContext = new AtomicReference<>();
-    private final ListenerRegistration<?> schemaContextListenerListenerRegistration;
     private final Set<CapabilityListener> listeners1 = Collections.synchronizedSet(new HashSet<>());
     private final SchemaSourceProvider<YangTextSchemaSource> rootSchemaSourceProvider;
 
-    public CurrentSchemaContext(final DOMSchemaService schemaService,
-                                final SchemaSourceProvider<YangTextSchemaSource> rootSchemaSourceProvider) {
+    private Registration schemaContextListenerListenerRegistration;
+
+    private CurrentSchemaContext(final SchemaSourceProvider<YangTextSchemaSource> 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<YangTextSchemaSource> 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() {
@@ -49,7 +62,7 @@ public class CurrentSchemaContext implements EffectiveModelContextListener, Auto
         final Set<Capability> addedCaps = MdsalNetconfOperationServiceFactory.transformCapabilities(
                 currentContext.get(), rootSchemaSourceProvider);
         for (final CapabilityListener listener : listeners1) {
-            listener.onCapabilitiesChanged(addedCaps, Collections.emptySet());
+            listener.onCapabilitiesChanged(addedCaps, Set.of());
         }
     }
 
@@ -60,9 +73,9 @@ public class CurrentSchemaContext implements EffectiveModelContextListener, Auto
         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);
     }