Do not synchronize around ReactorStmtCtx.schemaPath
[yangtools.git] / yang / yang-parser-reactor / src / main / java / org / opendaylight / yangtools / yang / parser / stmt / reactor / ReactorStmtCtx.java
index 402e0d2c18a823b5cc06f9bc785237bf239dcd31..5e9f5983e2f2e697cb465a7798414a659adc17f4 100644 (file)
@@ -154,8 +154,8 @@ abstract class ReactorStmtCtx<A, D extends DeclaredStatement<A>, E extends Effec
 
     // SchemaPath cache for use with SubstatementContext and InferredStatementContext. This hurts RootStatementContext
     // a bit in terms of size -- but those are only a few and SchemaPath is on its way out anyway.
-    @Deprecated
-    private volatile SchemaPath schemaPath;
+    // FIXME: this should become 'QName'
+    private SchemaPath schemaPath;
 
     ReactorStmtCtx() {
         // Empty on purpose
@@ -313,6 +313,19 @@ abstract class ReactorStmtCtx<A, D extends DeclaredStatement<A>, E extends Effec
         // definition().onNamespaceElementAdded(this, type, key, value);
     }
 
+    /**
+     * Return the effective statement view of a copy operation. This method may return one of:
+     * <ul>
+     *   <li>{@code this}, when the effective view did not change</li>
+     *   <li>an InferredStatementContext, when there is a need for inference-equivalent copy</li>
+     *   <li>{@code null}, when the statement failed to materialize</li>
+     * </ul>
+     *
+     * @param parent Proposed new parent
+     * @param type Copy operation type
+     * @param targetModule New target module
+     * @return {@link ReactorStmtCtx} holding effective view
+     */
     abstract @Nullable ReactorStmtCtx<?, ?, ?> asEffectiveChildOf(StatementContextBase<?, ?, ?> parent, CopyType type,
         QNameModule targetModule);
 
@@ -517,6 +530,26 @@ abstract class ReactorStmtCtx<A, D extends DeclaredStatement<A>, E extends Effec
         flags |= ALL_INDEPENDENT;
     }
 
+    //
+    //
+    // Various functionality from AbstractTypeStatementSupport. This used to work on top of SchemaPath, now it still
+    // lives here. Ultimate future is either proper graduation or (more likely) move to AbstractTypeStatementSupport.
+    //
+    //
+
+    @Override
+    public final QName argumentAsTypeQName() {
+        final Object argument = argument();
+        verify(argument instanceof String, "Unexpected argument %s", argument);
+        return interpretAsQName((String) argument);
+    }
+
+    @Override
+    public final QNameModule effectiveNamespace() {
+        // FIXME: there has to be a better way to do this
+        return getSchemaPath().getLastComponent().getModule();
+    }
+
     //
     //
     // Common SchemaPath cache. All of this is bound to be removed once YANGTOOLS-1066 is done.
@@ -525,26 +558,17 @@ abstract class ReactorStmtCtx<A, D extends DeclaredStatement<A>, E extends Effec
 
     // Exists only to support {SubstatementContext,InferredStatementContext}.schemaPath()
     @Deprecated
-    final @NonNull Optional<SchemaPath> substatementGetSchemaPath() {
-        SchemaPath local = schemaPath;
-        if (local == null) {
-            synchronized (this) {
-                local = schemaPath;
-                if (local == null) {
-                    schemaPath = local = createSchemaPath((StatementContextBase<?, ?, ?>) coerceParentContext());
-                }
-            }
+    final @Nullable SchemaPath substatementGetSchemaPath() {
+        if (schemaPath == null) {
+            schemaPath = createSchemaPath((StatementContextBase<?, ?, ?>) coerceParentContext());
         }
-
-        return Optional.ofNullable(local);
+        return schemaPath;
     }
 
+    // FIXME: 7.0.0: this method's logic needs to be moved to the respective StatementSupport classes
     @Deprecated
     private SchemaPath createSchemaPath(final StatementContextBase<?, ?, ?> parent) {
-        final Optional<SchemaPath> maybeParentPath = parent.schemaPath();
-        verify(maybeParentPath.isPresent(), "Parent %s does not have a SchemaPath", parent);
-        final SchemaPath parentPath = maybeParentPath.get();
-
+        final SchemaPath parentPath = parent.getSchemaPath();
         if (StmtContextUtils.isUnknownStatement(this)) {
             return parentPath.createChild(publicDefinition().getStatementName());
         }
@@ -552,16 +576,13 @@ abstract class ReactorStmtCtx<A, D extends DeclaredStatement<A>, E extends Effec
         if (argument instanceof QName) {
             final QName qname = (QName) argument;
             if (producesDeclared(UsesStatement.class)) {
-                return maybeParentPath.orElse(null);
+                return parentPath;
             }
 
             return parentPath.createChild(qname);
         }
         if (argument instanceof String) {
-            // FIXME: This may yield illegal argument exceptions
-            final Optional<StmtContext<A, D, E>> originalCtx = getOriginalCtx();
-            final QName qname = StmtContextUtils.qnameFromArgument(originalCtx.orElse(this), (String) argument);
-            return parentPath.createChild(qname);
+            return parentPath.createChild(interpretAsQName((String) argument));
         }
         if (argument instanceof SchemaNodeIdentifier
                 && (producesDeclared(AugmentStatement.class) || producesDeclared(RefineStatement.class)
@@ -570,8 +591,13 @@ abstract class ReactorStmtCtx<A, D extends DeclaredStatement<A>, E extends Effec
             return parentPath.createChild(((SchemaNodeIdentifier) argument).getNodeIdentifiers());
         }
 
-        // FIXME: this does not look right
-        return maybeParentPath.orElse(null);
+        // FIXME: this does not look right, investigate more?
+        return parentPath;
+    }
+
+    private @NonNull QName interpretAsQName(final String argument) {
+        // FIXME: This may yield illegal argument exceptions
+        return StmtContextUtils.qnameFromArgument(getOriginalCtx().orElse(this), argument);
     }
 
     //