BUG-2633 - Netconf northbound mapping.
[controller.git] / opendaylight / netconf / mdsal-netconf-connector / src / main / java / org / opendaylight / controller / netconf / mdsal / connector / CurrentSchemaContext.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.controller.netconf.mdsal.connector;
10
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;
17
18 public class CurrentSchemaContext implements SchemaContextListener, AutoCloseable {
19     final AtomicReference<SchemaContext> currentContext = new AtomicReference<SchemaContext>();
20     private final ListenerRegistration<SchemaContextListener> schemaContextListenerListenerRegistration;
21
22     public SchemaContext getCurrentContext() {
23         Preconditions.checkState(currentContext.get() != null, "Current context not received");
24         return currentContext.get();
25     }
26
27     public CurrentSchemaContext(final SchemaService schemaService) {
28         schemaContextListenerListenerRegistration = schemaService.registerSchemaContextListener(this);
29     }
30
31     @Override
32     public void onGlobalContextUpdated(final SchemaContext schemaContext) {
33         currentContext.set(schemaContext);
34     }
35
36     @Override
37     public void close() throws Exception {
38         schemaContextListenerListenerRegistration.close();
39         currentContext.set(null);
40     }
41 }