Push out FIXMEs to 6.0.0
[yangtools.git] / yang / yang-data-impl / src / main / java / org / opendaylight / yangtools / yang / data / impl / leafref / LeafRefContext.java
index 4762290e601764a7d37f8b87aeb38c69d0021b8a..002a6af026569ff7dc56df4c715b06310586b2a1 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,54 +8,55 @@
 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;
+import org.opendaylight.yangtools.yang.model.util.AbstractSchemaContextProvider;
 
-final public class LeafRefContext {
+public final class LeafRefContext extends AbstractSchemaContextProvider {
 
     private final QName currentNodeQName;
     private final SchemaPath currentNodePath;
-    private final SchemaContext schemaContext;
     private final Module module;
 
     private final LeafRefPath leafRefTargetPath;
-    private final LeafRefPath absoluteLeafRefTargetPath ;
+    private final LeafRefPath absoluteLeafRefTargetPath;
     private final String leafRefTargetPathString;
 
     private final boolean isReferencedBy;
     private final boolean isReferencing;
 
-    private final Map<QName, LeafRefContext> referencingChilds;
-    private final Map<QName, LeafRefContext> referencedByChilds;
-    private final Map<QName, LeafRefContext> referencedByLeafRefCtx;
+    private final ImmutableMap<QName, LeafRefContext> referencingChilds;
+    private final ImmutableMap<QName, LeafRefContext> referencedByChilds;
+    private final ImmutableMap<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) {
+        super(leafRefContextBuilder.getSchemaContext());
         this.currentNodeQName = leafRefContextBuilder.getCurrentNodeQName();
         this.currentNodePath = leafRefContextBuilder.getCurrentNodePath();
-        this.schemaContext = leafRefContextBuilder.getSchemaContext();
         this.leafRefTargetPath = leafRefContextBuilder.getLeafRefTargetPath();
-        this.absoluteLeafRefTargetPath = leafRefContextBuilder
-                .getAbsoluteLeafRefTargetPath();
-        this.leafRefTargetPathString = leafRefContextBuilder
-                .getLeafRefTargetPathString();
+        this.absoluteLeafRefTargetPath = leafRefContextBuilder.getAbsoluteLeafRefTargetPath();
+        this.leafRefTargetPathString = leafRefContextBuilder.getLeafRefTargetPathString();
         this.isReferencedBy = leafRefContextBuilder.isReferencedBy();
         this.isReferencing = leafRefContextBuilder.isReferencing();
         this.referencingChilds = ImmutableMap.copyOf(leafRefContextBuilder.getReferencingChilds());
         this.referencedByChilds = ImmutableMap.copyOf(leafRefContextBuilder.getReferencedByChilds());
-        this.referencedByLeafRefCtx = ImmutableMap.copyOf(leafRefContextBuilder
-                .getAllReferencedByLeafRefCtxs());
+        this.referencedByLeafRefCtx = ImmutableMap.copyOf(leafRefContextBuilder.getAllReferencedByLeafRefCtxs());
         this.module = leafRefContextBuilder.getLeafRefContextModule();
     }
 
-    public static final LeafRefContext create(final SchemaContext ctx) {
+    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);
         }
     }
 
@@ -111,10 +112,6 @@ final public class LeafRefContext {
         return currentNodeQName;
     }
 
-    SchemaContext getSchemaContext() {
-        return schemaContext;
-    }
-
     public LeafRefPath getAbsoluteLeafRefTargetPath() {
         return absoluteLeafRefTargetPath;
     }
@@ -131,4 +128,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;
+    }
 }