Consider AugmentationNodes when direct descendant is not found
[yangtools.git] / yang / yang-data-impl / src / main / java / org / opendaylight / yangtools / yang / data / impl / leafref / LeafRefContext.java
index cb33d9c6d7429b0a04ff99a4d11f92ff14858219..409e6ba1aa755193c045ed0a67bd3bd16e265753 100644 (file)
@@ -1,4 +1,4 @@
-/**
+/*
  * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
  *
  * This program and the accompanying materials are made available under the
@@ -8,14 +8,14 @@
 package org.opendaylight.yangtools.yang.data.impl.leafref;
 
 import com.google.common.collect.ImmutableMap;
-import java.io.IOException;
 import java.util.Map;
 import org.opendaylight.yangtools.yang.common.QName;
 import org.opendaylight.yangtools.yang.model.api.Module;
 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
 
-final public class LeafRefContext {
+// FIXME: 3.0.0 hide this class
+public final class LeafRefContext {
 
     private final QName currentNodeQName;
     private final SchemaPath currentNodePath;
@@ -23,7 +23,7 @@ final public class LeafRefContext {
     private final Module module;
 
     private final LeafRefPath leafRefTargetPath;
-    private final LeafRefPath absoluteLeafRefTargetPath ;
+    private final LeafRefPath absoluteLeafRefTargetPath;
     private final String leafRefTargetPathString;
 
     private final boolean isReferencedBy;
@@ -33,6 +33,11 @@ final public class LeafRefContext {
     private final Map<QName, LeafRefContext> referencedByChilds;
     private final Map<QName, LeafRefContext> referencedByLeafRefCtx;
 
+    // FIXME: this looks like it's related to absoluteLeafRefTargetPath, but the original use in LeafRefValidation
+    //        fast path did not make it clear. Analyze the relationship between this field and
+    //        absoluteLeafRefTargetPath.
+    private volatile LeafRefPath leafRefNodePath = null;
+
     LeafRefContext(final LeafRefContextBuilder leafRefContextBuilder) {
         this.currentNodeQName = leafRefContextBuilder.getCurrentNodeQName();
         this.currentNodePath = leafRefContextBuilder.getCurrentNodePath();
@@ -51,8 +56,8 @@ final public class LeafRefContext {
     public static LeafRefContext create(final SchemaContext ctx) {
         try {
             return new LeafRefContextTreeBuilder(ctx).buildLeafRefContextTree();
-        } catch (IOException | LeafRefYangSyntaxErrorException e) {
-            throw new RuntimeException(e);
+        } catch (LeafRefYangSyntaxErrorException e) {
+            throw new IllegalArgumentException(e);
         }
     }
 
@@ -128,4 +133,16 @@ final public class LeafRefContext {
         return referencedByLeafRefCtx;
     }
 
+    LeafRefPath getLeafRefNodePath() {
+        LeafRefPath ret = leafRefNodePath;
+        if (ret == null) {
+            synchronized (this) {
+                ret = leafRefNodePath;
+                if (ret == null) {
+                    ret = leafRefNodePath = LeafRefUtils.schemaPathToLeafRefPath(currentNodePath, module);
+                }
+            }
+        }
+        return ret;
+    }
 }