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.CapabilityListener;
19 import org.opendaylight.yangtools.concepts.Registration;
20 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
21 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContextListener;
22 import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource;
23 import org.opendaylight.yangtools.yang.model.repo.spi.SchemaSourceProvider;
25 // Non-final for mocking
26 @SuppressWarnings("checkstyle:FinalClass")
27 public class CurrentSchemaContext implements EffectiveModelContextListener, AutoCloseable {
28 private final AtomicReference<EffectiveModelContext> currentContext = new AtomicReference<>();
29 private final Set<CapabilityListener> listeners = Collections.synchronizedSet(new HashSet<>());
30 private final SchemaSourceProvider<YangTextSchemaSource> rootSchemaSourceProvider;
32 private Registration schemaContextListenerListenerRegistration;
34 private CurrentSchemaContext(final SchemaSourceProvider<YangTextSchemaSource> rootSchemaSourceProvider) {
35 this.rootSchemaSourceProvider = rootSchemaSourceProvider;
38 // keep spotbugs from complaining about overridable method in constructor
39 public static CurrentSchemaContext create(final DOMSchemaService schemaService,
40 final SchemaSourceProvider<YangTextSchemaSource> rootSchemaSourceProvider) {
41 var context = new CurrentSchemaContext(rootSchemaSourceProvider);
42 final Registration registration = schemaService.registerSchemaContextListener(context);
43 context.setRegistration(registration);
47 private void setRegistration(final Registration registration) {
48 schemaContextListenerListenerRegistration = registration;
51 public @NonNull EffectiveModelContext getCurrentContext() {
52 final var ret = currentContext.get();
53 checkState(ret != null, "Current context not received");
58 public void onModelContextUpdated(final EffectiveModelContext schemaContext) {
59 currentContext.set(schemaContext);
60 // FIXME is notifying all the listeners from this callback wise ?
61 final var addedCaps = MdsalNetconfOperationServiceFactory.transformCapabilities(schemaContext,
62 rootSchemaSourceProvider);
63 for (var listener : listeners) {
64 listener.onCapabilitiesChanged(addedCaps, Set.of());
71 schemaContextListenerListenerRegistration.close();
72 currentContext.set(null);
75 public Registration registerCapabilityListener(final CapabilityListener listener) {
76 listener.onCapabilitiesChanged(MdsalNetconfOperationServiceFactory.transformCapabilities(currentContext.get(),
77 rootSchemaSourceProvider), Set.of());
78 listeners.add(listener);
79 return () -> listeners.remove(listener);