2 * Copyright (c) 2015 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.netconf.server.mdsal;
10 import static com.google.common.base.Preconditions.checkState;
12 import java.util.Collections;
13 import java.util.HashSet;
15 import java.util.concurrent.atomic.AtomicReference;
16 import org.eclipse.jdt.annotation.NonNull;
17 import org.opendaylight.mdsal.dom.api.DOMSchemaService;
18 import org.opendaylight.netconf.server.api.monitoring.Capability;
19 import org.opendaylight.netconf.server.api.monitoring.CapabilityListener;
20 import org.opendaylight.yangtools.concepts.Registration;
21 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
22 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContextListener;
23 import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource;
24 import org.opendaylight.yangtools.yang.model.repo.spi.SchemaSourceProvider;
26 // Non-final for mocking
27 @SuppressWarnings("checkstyle:FinalClass")
28 public class CurrentSchemaContext implements EffectiveModelContextListener, AutoCloseable {
29 private final AtomicReference<EffectiveModelContext> currentContext = new AtomicReference<>();
30 private final Set<CapabilityListener> listeners1 = Collections.synchronizedSet(new HashSet<>());
31 private final SchemaSourceProvider<YangTextSchemaSource> rootSchemaSourceProvider;
33 private Registration schemaContextListenerListenerRegistration;
35 private CurrentSchemaContext(final SchemaSourceProvider<YangTextSchemaSource> rootSchemaSourceProvider) {
36 this.rootSchemaSourceProvider = rootSchemaSourceProvider;
39 // keep spotbugs from complaining about overridable method in constructor
40 public static CurrentSchemaContext create(final DOMSchemaService schemaService,
41 final SchemaSourceProvider<YangTextSchemaSource> rootSchemaSourceProvider) {
42 var context = new CurrentSchemaContext(rootSchemaSourceProvider);
43 final Registration registration = schemaService.registerSchemaContextListener(context);
44 context.setRegistration(registration);
48 private void setRegistration(final Registration registration) {
49 schemaContextListenerListenerRegistration = registration;
52 public @NonNull EffectiveModelContext getCurrentContext() {
53 final var ret = currentContext.get();
54 checkState(ret != null, "Current context not received");
59 public void onModelContextUpdated(final EffectiveModelContext schemaContext) {
60 currentContext.set(schemaContext);
61 // FIXME is notifying all the listeners from this callback wise ?
62 final Set<Capability> addedCaps = MdsalNetconfOperationServiceFactory.transformCapabilities(
63 currentContext.get(), rootSchemaSourceProvider);
64 for (final CapabilityListener listener : listeners1) {
65 listener.onCapabilitiesChanged(addedCaps, Set.of());
72 schemaContextListenerListenerRegistration.close();
73 currentContext.set(null);
76 public Registration registerCapabilityListener(final CapabilityListener listener) {
77 listener.onCapabilitiesChanged(MdsalNetconfOperationServiceFactory.transformCapabilities(currentContext.get(),
78 rootSchemaSourceProvider), Set.of());
79 listeners1.add(listener);
80 return () -> listeners1.remove(listener);