BUG-4456: add RecursiveExtensionResolver
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / stmt / rfc6020 / effective / ListEffectiveStatementImpl.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective;
9
10 import com.google.common.base.Optional;
11 import com.google.common.collect.ImmutableList;
12 import java.util.Collection;
13 import java.util.HashSet;
14 import java.util.LinkedList;
15 import java.util.List;
16 import java.util.Objects;
17 import java.util.Set;
18 import javax.annotation.Nonnull;
19 import org.opendaylight.yangtools.yang.common.QName;
20 import org.opendaylight.yangtools.yang.model.api.DerivableSchemaNode;
21 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
22 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
23 import org.opendaylight.yangtools.yang.model.api.UniqueConstraint;
24 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
25 import org.opendaylight.yangtools.yang.model.api.stmt.ListStatement;
26 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier;
27 import org.opendaylight.yangtools.yang.parser.spi.meta.InferenceException;
28 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
29
30 public final class ListEffectiveStatementImpl extends AbstractEffectiveSimpleDataNodeContainer<ListStatement> implements
31         ListSchemaNode, DerivableSchemaNode {
32     private static final String ORDER_BY_USER_KEYWORD = "user";
33
34     private final boolean userOrdered;
35     private final List<QName> keyDefinition;
36     private final ListSchemaNode original;
37     private final Collection<UniqueConstraint> uniqueConstraints;
38
39     public ListEffectiveStatementImpl(
40             final StmtContext<QName, ListStatement, EffectiveStatement<QName, ListStatement>> ctx) {
41         super(ctx);
42
43         this.original = ctx.getOriginalCtx() == null ? null : (ListSchemaNode) ctx.getOriginalCtx().buildEffective();
44
45         OrderedByEffectiveStatementImpl orderedByStmt = firstEffective(OrderedByEffectiveStatementImpl.class);
46         if (orderedByStmt != null && ORDER_BY_USER_KEYWORD.equals(orderedByStmt.argument())) {
47             this.userOrdered = true;
48         } else {
49             this.userOrdered = false;
50         }
51
52         // initKeyDefinition
53         List<QName> keyDefinitionInit = new LinkedList<>();
54         KeyEffectiveStatementImpl keyEffectiveSubstatement = firstEffective(KeyEffectiveStatementImpl.class);
55
56         if (keyEffectiveSubstatement != null) {
57             Set<QName> possibleLeafQNamesForKey = new HashSet<>();
58
59             for (final EffectiveStatement<?, ?> effectiveStatement : effectiveSubstatements()) {
60                 if (effectiveStatement instanceof LeafSchemaNode) {
61                     possibleLeafQNamesForKey.add(((LeafSchemaNode) effectiveStatement).getQName());
62                 }
63             }
64
65             for (SchemaNodeIdentifier key : keyEffectiveSubstatement.argument()) {
66                 final QName keyQName = key.getLastComponent();
67
68                 if (!possibleLeafQNamesForKey.contains(keyQName)) {
69                     throw new InferenceException(ctx.getStatementSourceReference(),
70                         "Key '%s' misses node '%s' in list '%s'", keyEffectiveSubstatement.getDeclared().rawArgument(),
71                         keyQName.getLocalName(), ctx.getStatementArgument());
72                 }
73
74                 keyDefinitionInit.add(keyQName);
75             }
76         }
77
78         this.keyDefinition = ImmutableList.copyOf(keyDefinitionInit);
79         this.uniqueConstraints = ImmutableList.copyOf(allSubstatementsOfType(UniqueConstraint.class));
80     }
81
82     @Override
83     public Optional<ListSchemaNode> getOriginal() {
84         return Optional.fromNullable(original);
85     }
86
87     @Override
88     public List<QName> getKeyDefinition() {
89         return keyDefinition;
90     }
91
92     @Override
93     @Nonnull
94     public Collection<UniqueConstraint> getUniqueConstraints() {
95         return uniqueConstraints;
96     }
97
98     @Override
99     public boolean isUserOrdered() {
100         return userOrdered;
101     }
102
103     @Override
104     public int hashCode() {
105         final int prime = 31;
106         int result = 1;
107         result = prime * result + Objects.hashCode(getQName());
108         result = prime * result + Objects.hashCode(getPath());
109         return result;
110     }
111
112     @Override
113     public boolean equals(final Object obj) {
114         if (this == obj) {
115             return true;
116         }
117         if (obj == null) {
118             return false;
119         }
120         if (getClass() != obj.getClass()) {
121             return false;
122         }
123         final ListEffectiveStatementImpl other = (ListEffectiveStatementImpl) obj;
124         return Objects.equals(getQName(), other.getQName()) && Objects.equals(getPath(), other.getPath());
125     }
126
127     @Override
128     public String toString() {
129         return "list " + getQName().getLocalName();
130     }
131 }