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.controller.netconf.mdsal.connector;
11 import com.google.common.base.Preconditions;
12 import java.util.concurrent.atomic.AtomicReference;
13 import org.opendaylight.controller.sal.core.api.model.SchemaService;
14 import org.opendaylight.yangtools.concepts.ListenerRegistration;
15 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
16 import org.opendaylight.yangtools.yang.model.api.SchemaContextListener;
18 public class CurrentSchemaContext implements SchemaContextListener, AutoCloseable {
19 final AtomicReference<SchemaContext> currentContext = new AtomicReference<SchemaContext>();
20 private final ListenerRegistration<SchemaContextListener> schemaContextListenerListenerRegistration;
22 public SchemaContext getCurrentContext() {
23 Preconditions.checkState(currentContext.get() != null, "Current context not received");
24 return currentContext.get();
27 public CurrentSchemaContext(final SchemaService schemaService) {
28 schemaContextListenerListenerRegistration = schemaService.registerSchemaContextListener(this);
32 public void onGlobalContextUpdated(final SchemaContext schemaContext) {
33 currentContext.set(schemaContext);
37 public void close() throws Exception {
38 schemaContextListenerListenerRegistration.close();
39 currentContext.set(null);