BUG-6522: create a specialized CopyHistory object
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / stmt / reactor / RootStatementContext.java
index f5bf42e66df19f6f4e4e71ae329c11e67d8dc925..36e406233a3c166057c562cb83797a356037d7b9 100644 (file)
  */
 package org.opendaylight.yangtools.yang.parser.stmt.reactor;
 
-import java.util.List;
-
-import java.util.LinkedList;
-import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
-import org.opendaylight.yangtools.yang.common.QNameModule;
+import com.google.common.base.Optional;
+import java.util.ArrayList;
 import java.util.Collection;
+import java.util.Map;
+import javax.annotation.Nullable;
+import org.opendaylight.yangtools.yang.common.QNameModule;
+import org.opendaylight.yangtools.yang.model.api.SchemaPath;
 import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement;
 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
+import org.opendaylight.yangtools.yang.model.api.meta.IdentifierNamespace;
 import org.opendaylight.yangtools.yang.parser.spi.meta.NamespaceBehaviour.NamespaceStorageNode;
 import org.opendaylight.yangtools.yang.parser.spi.meta.NamespaceBehaviour.Registry;
-import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
+import org.opendaylight.yangtools.yang.parser.spi.meta.NamespaceBehaviour.StorageNodeType;
+import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
+import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContextUtils;
+import org.opendaylight.yangtools.yang.parser.spi.meta.CopyType;
+import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.IncludedModuleContext;
 
-class RootStatementContext<A, D extends DeclaredStatement<A>, E extends EffectiveStatement<A, D>>
-        extends StatementContextBase<A, D, E> {
+/**
+ * root statement class for a Yang source
+ */
+public class RootStatementContext<A, D extends DeclaredStatement<A>, E extends EffectiveStatement<A, D>> extends
+        StatementContextBase<A, D, E> {
 
     private final SourceSpecificContext sourceContext;
+    private final Collection<NamespaceStorageNode> includedContexts = new ArrayList<>();
     private final A argument;
-    private LinkedList<?> linkedList;
 
-    RootStatementContext(ContextBuilder<A, D, E> builder,
-            SourceSpecificContext sourceContext) throws SourceException {
+    RootStatementContext(final ContextBuilder<A, D, E> builder, final SourceSpecificContext sourceContext) {
         super(builder);
         this.sourceContext = sourceContext;
-        this.argument = builder.getDefinition().parseArgumentValue(this,
-                builder.getRawArgument());
+        this.argument = builder.getDefinition().parseArgumentValue(this, builder.getRawArgument());
     }
 
-    RootStatementContext(RootStatementContext<A, D, E> original,
-            QNameModule newQNameModule) throws SourceException {
+    RootStatementContext(final RootStatementContext<A, D, E> original, final QNameModule newQNameModule,
+        final CopyType typeOfCopy) {
         super(original);
 
         sourceContext = original.sourceContext;
         this.argument = original.argument;
 
-        copyDeclaredStmts(original, newQNameModule);
+        copyDeclaredStmts(original, newQNameModule, typeOfCopy);
 
-        copyEffectiveStmts(original, newQNameModule);
+        copyEffectiveStmts(original, newQNameModule, typeOfCopy);
 
     }
 
-    private void copyDeclaredStmts(RootStatementContext<A, D, E> original,
-            QNameModule newQNameModule) throws SourceException {
-        Collection<? extends StmtContext<?, ?, ?>> originalDeclaredSubstatements = original
-                .declaredSubstatements();
-        for (StmtContext<?, ?, ?> stmtContext : originalDeclaredSubstatements) {
-            this.addEffectiveSubstatement(stmtContext
-                    .createCopy(newQNameModule,this));
+    /**
+     * copies declared statements from original to this' substatements
+     *
+     * @param typeOfCopy
+     *            determines whether copy is used by augmentation or uses
+     * @throws org.opendaylight.yangtools.yang.parser.spi.source.SourceException
+     */
+    private void copyDeclaredStmts(final RootStatementContext<A, D, E> original, final QNameModule newQNameModule,
+            final CopyType typeOfCopy) {
+        final Collection<StatementContextBase<?, ?, ?>> originalDeclaredSubstatements = original.declaredSubstatements();
+        for (final StatementContextBase<?, ?, ?> stmtContext : originalDeclaredSubstatements) {
+            if (!StmtContextUtils.areFeaturesSupported(stmtContext)) {
+                continue;
+            }
+            this.addEffectiveSubstatement(stmtContext.createCopy(newQNameModule, this, typeOfCopy));
         }
     }
 
-    private void copyEffectiveStmts(RootStatementContext<A, D, E> original,
-            QNameModule newQNameModule) throws SourceException {
-        Collection<? extends StmtContext<?, ?, ?>> originalEffectiveSubstatements = original
-                .effectiveSubstatements();
-        for (StmtContext<?, ?, ?> stmtContext : originalEffectiveSubstatements) {
-            this.addEffectiveSubstatement(stmtContext
-                    .createCopy(newQNameModule,this));
+    /**
+     * copies effective statements from original to this' substatements
+     *
+     * @param typeOfCopy
+     *            determines whether copy is used by augmentation or uses
+     * @throws org.opendaylight.yangtools.yang.parser.spi.source.SourceException
+     */
+    private void copyEffectiveStmts(final RootStatementContext<A, D, E> original, final QNameModule newQNameModule,
+            final CopyType typeOfCopy) {
+        final Collection<? extends StmtContext<?, ?, ?>> originalEffectiveSubstatements = original.effectiveSubstatements();
+        for (final StmtContext<?, ?, ?> stmtContext : originalEffectiveSubstatements) {
+            this.addEffectiveSubstatement(stmtContext.createCopy(newQNameModule, this, typeOfCopy));
         }
     }
 
+    /**
+     * @return null as root cannot have parent
+     */
     @Override
     public StatementContextBase<?, ?, ?> getParentContext() {
         return null;
     }
 
+    /**
+     * @return namespace storage of source context
+     */
     @Override
     public NamespaceStorageNode getParentNamespaceStorage() {
         return sourceContext;
     }
 
+    /**
+     * @return registry of source context
+     */
     @Override
     public Registry getBehaviourRegistry() {
         return sourceContext;
     }
 
+    @Override
+    public StorageNodeType getStorageNodeType() {
+        return StorageNodeType.ROOT_STATEMENT_LOCAL;
+    }
+    /**
+     * @return this as its own root
+     */
     @Override
     public RootStatementContext<?, ?, ?> getRoot() {
         return this;
@@ -96,20 +132,98 @@ class RootStatementContext<A, D extends DeclaredStatement<A>, E extends Effectiv
         return argument;
     }
 
+    /**
+     * @return copy of this considering {@link CopyType} (augment, uses)
+     *
+     * @throws org.opendaylight.yangtools.yang.parser.spi.source.SourceException instance of SourceException
+     */
     @Override
-    public StatementContextBase<A, D, E> createCopy(QNameModule newQNameModule, StatementContextBase<?, ?, ?> newParent)
-            throws SourceException {
+    public StatementContextBase<?, ?, ?> createCopy(final StatementContextBase<?, ?, ?> newParent,
+            final CopyType typeOfCopy) {
+        return createCopy(null, newParent, typeOfCopy);
+    }
 
-        StatementContextBase<A, D, E> copy = new RootStatementContext<A, D, E>(
-                this, newQNameModule);
+    /**
+     * @return copy of this considering {@link CopyType} (augment, uses)
+     *
+     * @throws org.opendaylight.yangtools.yang.parser.spi.source.SourceException instance of SourceException
+     */
+    @Override
+    public StatementContextBase<A, D, E> createCopy(final QNameModule newQNameModule,
+            final StatementContextBase<?, ?, ?> newParent, final CopyType typeOfCopy) {
+        final RootStatementContext<A, D, E> copy = new RootStatementContext<>(this, newQNameModule, typeOfCopy);
+
+        copy.appendCopyHistory(typeOfCopy, this.getCopyHistory());
+
+        if (this.getOriginalCtx() != null) {
+            copy.setOriginalCtx(this.getOriginalCtx());
+        } else {
+            copy.setOriginalCtx(this);
+        }
+        definition().onStatementAdded(copy);
         return copy;
     }
 
     @Override
-    public List<Object> getArgumentsFromRoot() {
-        LinkedList<Object> argumentList = new LinkedList<Object>();
-        argumentList.add(argument);
-        return argumentList;
+    public Optional<SchemaPath> getSchemaPath() {
+        return Optional.of(SchemaPath.ROOT);
+    }
+
+    /**
+     * @return true
+     */
+    @Override
+    public boolean isRootContext() {
+        return true;
+    }
+
+    @Override
+    public boolean isConfiguration() {
+        return true;
     }
 
+    @Override
+    public boolean isEnabledSemanticVersioning() {
+        return sourceContext.isEnabledSemanticVersioning();
+    }
+
+    @Override
+    public <K, V, N extends IdentifierNamespace<K, V>> void addToLocalStorage(final Class<N> type, final K key,
+            final V value) {
+        if (IncludedModuleContext.class.isAssignableFrom(type)) {
+            includedContexts.add((NamespaceStorageNode) value);
+        }
+        super.addToLocalStorage(type, key, value);
+    }
+
+    @Override
+    public <K, V, N extends IdentifierNamespace<K, V>> V getFromLocalStorage(final Class<N> type, final K key) {
+        final V potentialLocal = super.getFromLocalStorage(type, key);
+        if (potentialLocal != null) {
+            return potentialLocal;
+        }
+        for (final NamespaceStorageNode includedSource : includedContexts) {
+            final V potential = includedSource.getFromLocalStorage(type, key);
+            if (potential != null) {
+                return potential;
+            }
+        }
+        return null;
+    }
+
+    @Nullable
+    @Override
+    public <K, V, N extends IdentifierNamespace<K, V>> Map<K, V> getAllFromLocalStorage(final Class<N> type) {
+        final Map<K, V> potentialLocal = super.getAllFromLocalStorage(type);
+        if (potentialLocal != null) {
+            return potentialLocal;
+        }
+        for (final NamespaceStorageNode includedSource : includedContexts) {
+            final Map<K, V> potential = includedSource.getAllFromLocalStorage(type);
+            if (potential != null) {
+                return potential;
+            }
+        }
+        return null;
+    }
 }