Handle null BindingRuntimeContext in BindingToNormalizedNodeCodec 10/27210/3
authorTom Pantelis <tpanteli@brocade.com>
Mon, 14 Sep 2015 10:55:43 +0000 (06:55 -0400)
committerGerrit Code Review <gerrit@opendaylight.org>
Fri, 2 Oct 2015 10:42:21 +0000 (10:42 +0000)
In the getModuleBlocking method, if timing is right, runtimeContext may
be null when it is accessed to get the SchemaContext. It is set in the
onGlobalContextUpdated method which may not have been called yet prior
to getModuleBlocking. There's already code to wait for schema via the
futureSchema so it shold check for null runtimeContext as well. Also
runtimeContext shoild be volatile as it's accessed by multiple threads.

Change-Id: I335b0c359d58f3c58da34c7255b75ac793e49bd6
Signed-off-by: Tom Pantelis <tpanteli@brocade.com>
binding/mdsal-binding-dom-adapter/src/main/java/org/opendaylight/mdsal/binding/dom/adapter/BindingToNormalizedNodeCodec.java

index 8b5b9ccf2b9265a7938dfdecc00c41e25185a234..ef9568478075d59e6dce46e586eaece09308e3c0 100644 (file)
@@ -78,7 +78,7 @@ public final class BindingToNormalizedNodeCodec implements BindingCodecTreeFacto
 
             });
 
-    private BindingRuntimeContext runtimeContext;
+    private volatile BindingRuntimeContext runtimeContext;
 
     public BindingToNormalizedNodeCodec(final GeneratedClassLoadingStrategy classLoadingStrategy,
             final BindingNormalizedNodeCodecRegistry codecRegistry) {
@@ -280,9 +280,13 @@ public final class BindingToNormalizedNodeCodec implements BindingCodecTreeFacto
         final QNameModule moduleName = BindingReflections.getQNameModule(modeledClass);
         final URI namespace = moduleName.getNamespace();
         final Date revision = moduleName.getRevision();
-        Module module = runtimeContext.getSchemaContext().findModuleByNamespaceAndRevision(namespace, revision);
+        BindingRuntimeContext localRuntimeContext = runtimeContext;
+        Module module = localRuntimeContext == null ? null :
+            localRuntimeContext.getSchemaContext().findModuleByNamespaceAndRevision(namespace, revision);
         if(module == null && futureSchema != null && futureSchema.waitForSchema(namespace,revision)) {
-            module = runtimeContext.getSchemaContext().findModuleByNamespaceAndRevision(namespace, revision);
+            localRuntimeContext = runtimeContext;
+            Preconditions.checkState(localRuntimeContext != null, "BindingRuntimeContext is not available.");
+            module = localRuntimeContext.getSchemaContext().findModuleByNamespaceAndRevision(namespace, revision);
         }
         Preconditions.checkState(module != null, "Schema for %s is not available.", modeledClass);
         return module;