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
9 package org.opendaylight.netconf.mdsal.connector;
11 import com.google.common.base.Preconditions;
12 import com.google.common.collect.Sets;
13 import java.util.Collections;
15 import java.util.concurrent.atomic.AtomicReference;
16 import org.opendaylight.mdsal.dom.api.DOMSchemaService;
17 import org.opendaylight.netconf.api.capability.Capability;
18 import org.opendaylight.netconf.api.monitoring.CapabilityListener;
19 import org.opendaylight.yangtools.concepts.ListenerRegistration;
20 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
21 import org.opendaylight.yangtools.yang.model.api.SchemaContextListener;
22 import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource;
23 import org.opendaylight.yangtools.yang.model.repo.spi.SchemaSourceProvider;
25 public class CurrentSchemaContext implements SchemaContextListener, AutoCloseable {
26 private final AtomicReference<SchemaContext> currentContext = new AtomicReference<>();
27 private final ListenerRegistration<SchemaContextListener> schemaContextListenerListenerRegistration;
28 private final Set<CapabilityListener> listeners1 = Collections.synchronizedSet(Sets.newHashSet());
29 private final SchemaSourceProvider<YangTextSchemaSource> rootSchemaSourceProvider;
31 public SchemaContext getCurrentContext() {
32 Preconditions.checkState(currentContext.get() != null, "Current context not received");
33 return currentContext.get();
36 public CurrentSchemaContext(final DOMSchemaService schemaService,
37 final SchemaSourceProvider<YangTextSchemaSource> rootSchemaSourceProvider) {
38 this.rootSchemaSourceProvider = rootSchemaSourceProvider;
39 schemaContextListenerListenerRegistration = schemaService.registerSchemaContextListener(this);
43 public void onGlobalContextUpdated(final SchemaContext schemaContext) {
44 currentContext.set(schemaContext);
45 // FIXME is notifying all the listeners from this callback wise ?
46 final Set<Capability> addedCaps = MdsalNetconfOperationServiceFactory.transformCapabilities(
47 currentContext.get(), rootSchemaSourceProvider);
48 for (final CapabilityListener listener : listeners1) {
49 listener.onCapabilitiesChanged(addedCaps, Collections.emptySet());
56 schemaContextListenerListenerRegistration.close();
57 currentContext.set(null);
60 public AutoCloseable registerCapabilityListener(final CapabilityListener listener) {
61 listener.onCapabilitiesChanged(MdsalNetconfOperationServiceFactory.transformCapabilities(currentContext.get(),
62 rootSchemaSourceProvider), Collections.emptySet());
63 listeners1.add(listener);
64 return () -> listeners1.remove(listener);