2 * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
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
9 package org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective;
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;
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.meta.EffectiveStatement;
27 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
28 import org.opendaylight.yangtools.yang.model.api.stmt.ListStatement;
29 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier;
30 import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
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;
35 public class ListEffectiveStatementImpl extends AbstractEffectiveDocumentedDataNodeContainer<QName, ListStatement>
36 implements ListSchemaNode, DerivableSchemaNode {
37 private final QName qname;
38 private final SchemaPath path;
41 private boolean addedByUses;
42 ListSchemaNode original;
43 boolean configuration = true;
44 ConstraintDefinition constraints;
47 ImmutableList<QName> keyDefinition;
48 ImmutableSet<AugmentationSchema> augmentations;
49 ImmutableList<UnknownSchemaNode> unknownNodes;
51 public ListEffectiveStatementImpl(StmtContext<QName, ListStatement, EffectiveStatement<QName, ListStatement>> ctx) {
53 this.qname = ctx.getStatementArgument();
54 this.path = Utils.getSchemaPath(ctx);
55 this.constraints = new EffectiveConstraintDefinitionImpl(this);
57 initSubstatementCollectionsAndFields();
60 // should be after initSubstatementCollectionsAndFields()
61 initKeyDefinition(ctx);
64 private void initCopyType(
65 StmtContext<QName, ListStatement, EffectiveStatement<QName, ListStatement>> ctx) {
67 List<TypeOfCopy> copyTypesFromOriginal = ctx.getCopyHistory();
69 if(copyTypesFromOriginal.contains(TypeOfCopy.ADDED_BY_AUGMENTATION)) {
72 if(copyTypesFromOriginal.contains(TypeOfCopy.ADDED_BY_USES)) {
75 if(copyTypesFromOriginal.contains(TypeOfCopy.ADDED_BY_USES_AUGMENTATION)) {
76 addedByUses = augmenting = true;
79 if (ctx.getOriginalCtx() != null) {
80 original = (ListSchemaNode) ctx.getOriginalCtx().buildEffective();
84 private void initKeyDefinition(StmtContext<QName, ListStatement, EffectiveStatement<QName, ListStatement>> ctx) {
85 List<QName> keyDefinitionInit = new LinkedList<>();
86 KeyEffectiveStatementImpl keyEffectiveSubstatement = firstEffective(KeyEffectiveStatementImpl.class);
88 if (keyEffectiveSubstatement != null) {
89 Set<QName> possibleLeafQNamesForKey = new HashSet<>();
91 for (final EffectiveStatement<?, ?> effectiveStatement : effectiveSubstatements()) {
92 if (effectiveStatement instanceof LeafSchemaNode) {
93 possibleLeafQNamesForKey.add(((LeafSchemaNode) effectiveStatement).getQName());
97 Collection<SchemaNodeIdentifier> keys = keyEffectiveSubstatement.argument();
98 for (SchemaNodeIdentifier key : keys) {
99 final QName keyQName = key.getLastComponent();
101 if (!possibleLeafQNamesForKey.contains(keyQName)) {
102 throw new IllegalArgumentException(String.format("Key '%s' misses node '%s' in list '%s', file %s",
103 keyEffectiveSubstatement.getDeclared().rawArgument(), keyQName.getLocalName(), ctx.getStatementArgument(),
104 ctx.getStatementSourceReference()));
107 keyDefinitionInit.add(keyQName);
111 this.keyDefinition = ImmutableList.copyOf(keyDefinitionInit);
114 private void initSubstatementCollectionsAndFields() {
115 Collection<? extends EffectiveStatement<?, ?>> effectiveSubstatements = effectiveSubstatements();
117 List<UnknownSchemaNode> unknownNodesInit = new LinkedList<>();
118 Set<AugmentationSchema> augmentationsInit = new HashSet<>();
120 boolean configurationInit = false;
121 boolean userOrderedInit = false;
122 for (EffectiveStatement<?, ?> effectiveStatement : effectiveSubstatements) {
123 if (effectiveStatement instanceof UnknownSchemaNode) {
124 UnknownSchemaNode unknownNode = (UnknownSchemaNode) effectiveStatement;
125 unknownNodesInit.add(unknownNode);
127 if (effectiveStatement instanceof AugmentationSchema) {
128 AugmentationSchema augmentationSchema = (AugmentationSchema) effectiveStatement;
129 augmentationsInit.add(augmentationSchema);
131 if (!configurationInit && effectiveStatement instanceof ConfigEffectiveStatementImpl) {
132 ConfigEffectiveStatementImpl configStmt = (ConfigEffectiveStatementImpl) effectiveStatement;
133 this.configuration = configStmt.argument();
134 configurationInit = true;
136 if (!userOrderedInit && effectiveStatement instanceof OrderedByEffectiveStatementImpl) {
137 OrderedByEffectiveStatementImpl orderedByStmt = (OrderedByEffectiveStatementImpl) effectiveStatement;
138 this.userOrdered = orderedByStmt.argument().equals("user") ? true : false;
139 userOrderedInit = true;
143 this.unknownNodes = ImmutableList.copyOf(unknownNodesInit);
144 this.augmentations = ImmutableSet.copyOf(augmentationsInit);
148 public QName getQName() {
153 public SchemaPath getPath() {
158 public List<QName> getKeyDefinition() {
159 return keyDefinition;
163 public boolean isAugmenting() {
168 public boolean isAddedByUses() {
173 public Optional<ListSchemaNode> getOriginal() {
174 return Optional.fromNullable(original);
178 public boolean isConfiguration() {
179 return configuration;
183 public ConstraintDefinition getConstraints() {
188 public Set<AugmentationSchema> getAvailableAugmentations() {
189 return augmentations;
193 public boolean isUserOrdered() {
198 public List<UnknownSchemaNode> getUnknownSchemaNodes() {
203 public int hashCode() {
204 final int prime = 31;
206 result = prime * result + Objects.hashCode(qname);
207 result = prime * result + Objects.hashCode(path);
212 public boolean equals(final Object obj) {
219 if (getClass() != obj.getClass()) {
222 final ListEffectiveStatementImpl other = (ListEffectiveStatementImpl) obj;
224 if (other.qname != null) {
227 } else if (!qname.equals(other.qname)) {
231 if (other.path != null) {
234 } else if (!path.equals(other.path)) {
241 public String toString() {
242 return "list " + qname.getLocalName();