BUG-2633 - Netconf northbound mapping.
[controller.git] / opendaylight / netconf / mdsal-netconf-connector / src / main / java / org / opendaylight / controller / netconf / mdsal / connector / CurrentSchemaContext.java
diff --git a/opendaylight/netconf/mdsal-netconf-connector/src/main/java/org/opendaylight/controller/netconf/mdsal/connector/CurrentSchemaContext.java b/opendaylight/netconf/mdsal-netconf-connector/src/main/java/org/opendaylight/controller/netconf/mdsal/connector/CurrentSchemaContext.java
new file mode 100644 (file)
index 0000000..df671e8
--- /dev/null
@@ -0,0 +1,41 @@
+/*
+ * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+
+package org.opendaylight.controller.netconf.mdsal.connector;
+
+import com.google.common.base.Preconditions;
+import java.util.concurrent.atomic.AtomicReference;
+import org.opendaylight.controller.sal.core.api.model.SchemaService;
+import org.opendaylight.yangtools.concepts.ListenerRegistration;
+import org.opendaylight.yangtools.yang.model.api.SchemaContext;
+import org.opendaylight.yangtools.yang.model.api.SchemaContextListener;
+
+public class CurrentSchemaContext implements SchemaContextListener, AutoCloseable {
+    final AtomicReference<SchemaContext> currentContext = new AtomicReference<SchemaContext>();
+    private final ListenerRegistration<SchemaContextListener> schemaContextListenerListenerRegistration;
+
+    public SchemaContext getCurrentContext() {
+        Preconditions.checkState(currentContext.get() != null, "Current context not received");
+        return currentContext.get();
+    }
+
+    public CurrentSchemaContext(final SchemaService schemaService) {
+        schemaContextListenerListenerRegistration = schemaService.registerSchemaContextListener(this);
+    }
+
+    @Override
+    public void onGlobalContextUpdated(final SchemaContext schemaContext) {
+        currentContext.set(schemaContext);
+    }
+
+    @Override
+    public void close() throws Exception {
+        schemaContextListenerListenerRegistration.close();
+        currentContext.set(null);
+    }
+}
\ No newline at end of file