Use Objects.equals() in effective statements
[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
9 package org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective;
10
11 import com.google.common.base.Optional;
12 import com.google.common.collect.ImmutableList;
13 import com.google.common.collect.ImmutableSet;
14 import java.util.Collection;
15 import java.util.HashSet;
16 import java.util.LinkedList;
17 import java.util.List;
18 import java.util.Objects;
19 import java.util.Set;
20 import org.opendaylight.yangtools.yang.common.QName;
21 import org.opendaylight.yangtools.yang.model.api.AugmentationSchema;
22 import org.opendaylight.yangtools.yang.model.api.ConstraintDefinition;
23 import org.opendaylight.yangtools.yang.model.api.DerivableSchemaNode;
24 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
25 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
26 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
27 import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
28 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
29 import org.opendaylight.yangtools.yang.model.api.stmt.ListStatement;
30 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier;
31 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
32 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext.TypeOfCopy;
33 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.Utils;
34
35 public class ListEffectiveStatementImpl extends AbstractEffectiveDocumentedDataNodeContainer<QName, ListStatement>
36         implements ListSchemaNode, DerivableSchemaNode {
37     private final QName qname;
38     private final SchemaPath path;
39
40     // FIXME: should be private
41     boolean augmenting;
42     private boolean addedByUses;
43     private ListSchemaNode original;
44     private boolean configuration = true;
45     private final ConstraintDefinition constraints;
46     private boolean userOrdered;
47
48     private ImmutableList<QName> keyDefinition;
49     private ImmutableSet<AugmentationSchema> augmentations;
50     private ImmutableList<UnknownSchemaNode> unknownNodes;
51
52     public ListEffectiveStatementImpl(final StmtContext<QName, ListStatement, EffectiveStatement<QName, ListStatement>> ctx) {
53         super(ctx);
54         this.qname = ctx.getStatementArgument();
55         this.path = Utils.getSchemaPath(ctx);
56         this.constraints = new EffectiveConstraintDefinitionImpl(this);
57
58         initSubstatementCollectionsAndFields();
59         initCopyType(ctx);
60
61         // should be after initSubstatementCollectionsAndFields()
62         initKeyDefinition(ctx);
63     }
64
65     private void initCopyType(final StmtContext<QName, ListStatement, EffectiveStatement<QName, ListStatement>> ctx) {
66
67         List<TypeOfCopy> copyTypesFromOriginal = ctx.getCopyHistory();
68
69         if (copyTypesFromOriginal.contains(TypeOfCopy.ADDED_BY_AUGMENTATION)) {
70             augmenting = true;
71         }
72         if (copyTypesFromOriginal.contains(TypeOfCopy.ADDED_BY_USES)) {
73             addedByUses = true;
74         }
75         if (copyTypesFromOriginal.contains(TypeOfCopy.ADDED_BY_USES_AUGMENTATION)) {
76             addedByUses = augmenting = true;
77         }
78         if (ctx.getOriginalCtx() != null) {
79             original = (ListSchemaNode) ctx.getOriginalCtx().buildEffective();
80         }
81     }
82
83     private void initKeyDefinition(final StmtContext<QName, ListStatement, EffectiveStatement<QName, ListStatement>> ctx) {
84         List<QName> keyDefinitionInit = new LinkedList<>();
85         KeyEffectiveStatementImpl keyEffectiveSubstatement = firstEffective(KeyEffectiveStatementImpl.class);
86
87         if (keyEffectiveSubstatement != null) {
88             Set<QName> possibleLeafQNamesForKey = new HashSet<>();
89
90             for (final EffectiveStatement<?, ?> effectiveStatement : effectiveSubstatements()) {
91                 if (effectiveStatement instanceof LeafSchemaNode) {
92                     possibleLeafQNamesForKey.add(((LeafSchemaNode) effectiveStatement).getQName());
93                 }
94             }
95
96             Collection<SchemaNodeIdentifier> keys = keyEffectiveSubstatement.argument();
97             for (SchemaNodeIdentifier key : keys) {
98                 final QName keyQName = key.getLastComponent();
99
100                 if (!possibleLeafQNamesForKey.contains(keyQName)) {
101                     throw new IllegalArgumentException(String.format("Key '%s' misses node '%s' in list '%s', file %s",
102                             keyEffectiveSubstatement.getDeclared().rawArgument(), keyQName.getLocalName(), ctx.getStatementArgument(),
103                             ctx.getStatementSourceReference()));
104                 }
105
106                 keyDefinitionInit.add(keyQName);
107             }
108         }
109
110         this.keyDefinition = ImmutableList.copyOf(keyDefinitionInit);
111     }
112
113     private void initSubstatementCollectionsAndFields() {
114         Collection<? extends EffectiveStatement<?, ?>> effectiveSubstatements = effectiveSubstatements();
115
116         List<UnknownSchemaNode> unknownNodesInit = new LinkedList<>();
117         Set<AugmentationSchema> augmentationsInit = new HashSet<>();
118
119         boolean configurationInit = false;
120         boolean userOrderedInit = false;
121         for (EffectiveStatement<?, ?> effectiveStatement : effectiveSubstatements) {
122             if (effectiveStatement instanceof UnknownSchemaNode) {
123                 UnknownSchemaNode unknownNode = (UnknownSchemaNode) effectiveStatement;
124                 unknownNodesInit.add(unknownNode);
125             }
126             if (effectiveStatement instanceof AugmentationSchema) {
127                 AugmentationSchema augmentationSchema = (AugmentationSchema) effectiveStatement;
128                 augmentationsInit.add(augmentationSchema);
129             }
130             if (!configurationInit && effectiveStatement instanceof ConfigEffectiveStatementImpl) {
131                 ConfigEffectiveStatementImpl configStmt = (ConfigEffectiveStatementImpl) effectiveStatement;
132                 this.configuration = configStmt.argument();
133                 configurationInit = true;
134             }
135             if (!userOrderedInit && effectiveStatement instanceof OrderedByEffectiveStatementImpl) {
136                 OrderedByEffectiveStatementImpl orderedByStmt = (OrderedByEffectiveStatementImpl) effectiveStatement;
137                 this.userOrdered = orderedByStmt.argument().equals("user") ? true : false;
138                 userOrderedInit = true;
139             }
140         }
141
142         this.unknownNodes = ImmutableList.copyOf(unknownNodesInit);
143         this.augmentations = ImmutableSet.copyOf(augmentationsInit);
144     }
145
146     @Override
147     public QName getQName() {
148         return qname;
149     }
150
151     @Override
152     public SchemaPath getPath() {
153         return path;
154     }
155
156     @Override
157     public List<QName> getKeyDefinition() {
158         return keyDefinition;
159     }
160
161     @Override
162     public boolean isAugmenting() {
163         return augmenting;
164     }
165
166     @Override
167     public boolean isAddedByUses() {
168         return addedByUses;
169     }
170
171     @Override
172     public Optional<ListSchemaNode> getOriginal() {
173         return Optional.fromNullable(original);
174     }
175
176     @Override
177     public boolean isConfiguration() {
178         return configuration;
179     }
180
181     @Override
182     public ConstraintDefinition getConstraints() {
183         return constraints;
184     }
185
186     @Override
187     public Set<AugmentationSchema> getAvailableAugmentations() {
188         return augmentations;
189     }
190
191     @Override
192     public boolean isUserOrdered() {
193         return userOrdered;
194     }
195
196     @Override
197     public List<UnknownSchemaNode> getUnknownSchemaNodes() {
198         return unknownNodes;
199     }
200
201     @Override
202     public int hashCode() {
203         final int prime = 31;
204         int result = 1;
205         result = prime * result + Objects.hashCode(qname);
206         result = prime * result + Objects.hashCode(path);
207         return result;
208     }
209
210     @Override
211     public boolean equals(final Object obj) {
212         if (this == obj) {
213             return true;
214         }
215         if (obj == null) {
216             return false;
217         }
218         if (getClass() != obj.getClass()) {
219             return false;
220         }
221         final ListEffectiveStatementImpl other = (ListEffectiveStatementImpl) obj;
222         return Objects.equals(qname, other.qname) && Objects.equals(path, other.path);
223     }
224
225     @Override
226     public String toString() {
227         return "list " + qname.getLocalName();
228     }
229 }