BUG-4638: Convert to using new types
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / stmt / rfc6020 / effective / LeafEffectiveStatementImpl.java
index 9590f292ed5faa4d5dc429684cd3f62c3b7ba6c7..70d80394699d89810ef8465ad303bd5cd33b12ea 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
 package org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective;
 
 import com.google.common.base.Optional;
-import com.google.common.collect.ImmutableList;
-import java.util.Collection;
-import java.util.LinkedList;
-import java.util.List;
 import java.util.Objects;
 import org.opendaylight.yangtools.yang.common.QName;
-import org.opendaylight.yangtools.yang.model.api.ConstraintDefinition;
 import org.opendaylight.yangtools.yang.model.api.DerivableSchemaNode;
 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
-import org.opendaylight.yangtools.yang.model.api.SchemaPath;
 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
-import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
 import org.opendaylight.yangtools.yang.model.api.stmt.LeafStatement;
+import org.opendaylight.yangtools.yang.model.api.stmt.TypeEffectiveStatement;
+import org.opendaylight.yangtools.yang.model.util.type.ConcreteTypeBuilder;
+import org.opendaylight.yangtools.yang.model.util.type.ConcreteTypes;
 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
-import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext.TypeOfCopy;
-import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.TypeUtils;
-import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.Utils;
+import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
 
-public class LeafEffectiveStatementImpl extends AbstractEffectiveDocumentedNode<QName, LeafStatement> implements
+public final class LeafEffectiveStatementImpl extends AbstractEffectiveDataSchemaNode<LeafStatement> implements
         LeafSchemaNode, DerivableSchemaNode {
-    private final QName qname;
-    private final SchemaPath path;
-
-    // FIXME: should be private
-    boolean augmenting;
-    private boolean addedByUses;
-    private LeafSchemaNode original;
-    private boolean configuration = true;
-    private final ConstraintDefinition constraintsDef;
-    private TypeDefinition<?> type;
-    private String defaultStr;
-    private String unitsStr;
-
-    private ImmutableList<UnknownSchemaNode> unknownNodes;
+    private final LeafSchemaNode original;
+    private final TypeDefinition<?> type;
+    private final String defaultStr;
+    private final String unitsStr;
 
     public LeafEffectiveStatementImpl(final StmtContext<QName, LeafStatement, EffectiveStatement<QName, LeafStatement>> ctx) {
         super(ctx);
-        this.qname = ctx.getStatementArgument();
-        this.path = Utils.getSchemaPath(ctx);
-        this.constraintsDef = new EffectiveConstraintDefinitionImpl(this);
-
-        initSubstatementCollections(ctx);
-        initCopyType(ctx);
-    }
-
-    private void initCopyType(
-            final StmtContext<QName, LeafStatement, EffectiveStatement<QName, LeafStatement>> ctx) {
+        this.original = ctx.getOriginalCtx() == null ? null : (LeafSchemaNode) ctx.getOriginalCtx().buildEffective();
 
-        List<TypeOfCopy> copyTypesFromOriginal = ctx.getCopyHistory();
-
-        if (copyTypesFromOriginal.contains(TypeOfCopy.ADDED_BY_AUGMENTATION)) {
-            augmenting = true;
-        }
-        if (copyTypesFromOriginal.contains(TypeOfCopy.ADDED_BY_USES)) {
-            addedByUses = true;
-        }
-        if (copyTypesFromOriginal.contains(TypeOfCopy.ADDED_BY_USES_AUGMENTATION)) {
-            addedByUses = augmenting = true;
+        final TypeEffectiveStatement<?> typeStmt = firstSubstatementOfType(TypeEffectiveStatement.class);
+        if (typeStmt == null) {
+            throw new SourceException("Leaf is missing a 'type' statement", ctx.getStatementSourceReference());
         }
-        if (ctx.getOriginalCtx() != null) {
-            original = (LeafSchemaNode) ctx.getOriginalCtx().buildEffective();
-        }
-    }
 
-    private void initSubstatementCollections(
-            final StmtContext<QName, LeafStatement, EffectiveStatement<QName, LeafStatement>> ctx) {
-        Collection<? extends EffectiveStatement<?, ?>> effectiveSubstatements = effectiveSubstatements();
-
-        List<UnknownSchemaNode> unknownNodesInit = new LinkedList<>();
-
-        boolean configurationInit = false;
-        boolean defaultInit = false;
-        boolean unitsInit = false;
-        for (EffectiveStatement<?, ?> effectiveStatement : effectiveSubstatements) {
-            if (effectiveStatement instanceof UnknownSchemaNode) {
-                unknownNodesInit.add((UnknownSchemaNode) effectiveStatement);
-            }
-            if (effectiveStatement instanceof TypeDefinition) {
-                type = TypeUtils.getTypeFromEffectiveStatement(effectiveStatement);
-            }
-            if (!configurationInit && effectiveStatement instanceof ConfigEffectiveStatementImpl) {
-                ConfigEffectiveStatementImpl configStmt = (ConfigEffectiveStatementImpl) effectiveStatement;
-                this.configuration = configStmt.argument();
-                configurationInit = true;
-            }
-            if (!defaultInit && effectiveStatement instanceof DefaultEffectiveStatementImpl) {
-                DefaultEffectiveStatementImpl defStmt = (DefaultEffectiveStatementImpl) effectiveStatement;
-                this.defaultStr = defStmt.argument();
-                defaultInit = true;
-            }
-            if (!unitsInit && effectiveStatement instanceof UnitsEffectiveStatementImpl) {
-                UnitsEffectiveStatementImpl unitStmt = (UnitsEffectiveStatementImpl) effectiveStatement;
-                this.unitsStr = unitStmt.argument();
-                unitsInit = true;
+        String dflt = null;
+        String units = null;
+        final ConcreteTypeBuilder<?> builder = ConcreteTypes.concreteTypeBuilder(typeStmt.getTypeDefinition(),
+            ctx.getSchemaPath().get());
+        for (EffectiveStatement<?, ?> stmt : effectiveSubstatements()) {
+            if (stmt instanceof DefaultEffectiveStatementImpl) {
+                dflt = ((DefaultEffectiveStatementImpl)stmt).argument();
+                builder.setDefaultValue(stmt.argument());
+            } else if (stmt instanceof DescriptionEffectiveStatementImpl) {
+                builder.setDescription(((DescriptionEffectiveStatementImpl)stmt).argument());
+            } else if (stmt instanceof ReferenceEffectiveStatementImpl) {
+                builder.setReference(((ReferenceEffectiveStatementImpl)stmt).argument());
+            } else if (stmt instanceof StatusEffectiveStatementImpl) {
+                builder.setStatus(((StatusEffectiveStatementImpl)stmt).argument());
+            } else if (stmt instanceof UnitsEffectiveStatementImpl) {
+                units = ((UnitsEffectiveStatementImpl)stmt).argument();
+                builder.setUnits(units);
             }
         }
 
-        this.unknownNodes = ImmutableList.copyOf(unknownNodesInit);
-    }
-
-    @Override
-    public QName getQName() {
-        return qname;
-    }
-
-    @Override
-    public SchemaPath getPath() {
-        return path;
-    }
-
-    @Override
-    public boolean isAugmenting() {
-        return augmenting;
-    }
-
-    @Override
-    public boolean isAddedByUses() {
-        return addedByUses;
+        defaultStr = dflt;
+        unitsStr = units;
+        type = builder.build();
     }
 
     @Override
@@ -134,26 +67,11 @@ public class LeafEffectiveStatementImpl extends AbstractEffectiveDocumentedNode<
         return Optional.fromNullable(original);
     }
 
-    @Override
-    public boolean isConfiguration() {
-        return configuration;
-    }
-
-    @Override
-    public ConstraintDefinition getConstraints() {
-        return constraintsDef;
-    }
-
     @Override
     public TypeDefinition<?> getType() {
         return type;
     }
 
-    @Override
-    public List<UnknownSchemaNode> getUnknownSchemaNodes() {
-        return unknownNodes;
-    }
-
     @Override
     public String getDefault() {
         return defaultStr;
@@ -168,8 +86,8 @@ public class LeafEffectiveStatementImpl extends AbstractEffectiveDocumentedNode<
     public int hashCode() {
         final int prime = 31;
         int result = 1;
-        result = prime * result + Objects.hashCode(qname);
-        result = prime * result + Objects.hashCode(path);
+        result = prime * result + Objects.hashCode(getQName());
+        result = prime * result + Objects.hashCode(getPath());
         return result;
     }
 
@@ -178,22 +96,19 @@ public class LeafEffectiveStatementImpl extends AbstractEffectiveDocumentedNode<
         if (this == obj) {
             return true;
         }
-        if (obj == null) {
-            return false;
-        }
-        if (getClass() != obj.getClass()) {
+        if (!(obj instanceof LeafEffectiveStatementImpl)) {
             return false;
         }
         LeafEffectiveStatementImpl other = (LeafEffectiveStatementImpl) obj;
-        return Objects.equals(qname, other.qname) && Objects.equals(path, other.path);
+        return Objects.equals(getQName(), other.getQName()) && Objects.equals(getPath(), other.getPath());
     }
 
     @Override
     public String toString() {
         StringBuilder sb = new StringBuilder(LeafEffectiveStatementImpl.class.getSimpleName());
         sb.append("[");
-        sb.append("qname=").append(qname);
-        sb.append(", path=").append(path);
+        sb.append("qname=").append(getQName());
+        sb.append(", path=").append(getPath());
         sb.append("]");
         return sb.toString();
     }