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.mdsal.dom.api.DOMSchemaService.YangTextSourceExtension;
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;
23 // Non-final for mocking
24 @SuppressWarnings("checkstyle:FinalClass")
25 public class CurrentSchemaContext implements AutoCloseable {
26 private final AtomicReference<EffectiveModelContext> currentContext = new AtomicReference<>();
27 private final Set<CapabilityListener> listeners = Collections.synchronizedSet(new HashSet<>());
28 private final YangTextSourceExtension yangTextSourceExtension;
30 private Registration schemaContextListenerListenerRegistration;
32 private CurrentSchemaContext(final YangTextSourceExtension yangTextSourceExtension) {
33 this.yangTextSourceExtension = yangTextSourceExtension;
36 // keep spotbugs from complaining about overridable method in constructor
37 public static CurrentSchemaContext create(final DOMSchemaService schemaService,
38 final YangTextSourceExtension yangTextSourceExtension) {
39 var context = new CurrentSchemaContext(yangTextSourceExtension);
40 final var registration = schemaService.registerSchemaContextListener(context::onModelContextUpdated);
41 context.setRegistration(registration);
45 private void setRegistration(final Registration registration) {
46 schemaContextListenerListenerRegistration = registration;
49 public @NonNull EffectiveModelContext getCurrentContext() {
50 final var ret = currentContext.get();
51 checkState(ret != null, "Current context not received");
55 private void onModelContextUpdated(final EffectiveModelContext schemaContext) {
56 currentContext.set(schemaContext);
57 // FIXME is notifying all the listeners from this callback wise ?
58 final var addedCaps = MdsalNetconfOperationServiceFactory.transformCapabilities(schemaContext,
59 yangTextSourceExtension);
60 for (var listener : listeners) {
61 listener.onCapabilitiesChanged(addedCaps, Set.of());
68 schemaContextListenerListenerRegistration.close();
69 currentContext.set(null);
72 public Registration registerCapabilityListener(final CapabilityListener listener) {
73 listener.onCapabilitiesChanged(MdsalNetconfOperationServiceFactory.transformCapabilities(currentContext.get(),
74 yangTextSourceExtension), Set.of());
75 listeners.add(listener);
76 return () -> listeners.remove(listener);