cb6166415ae98803c26f1ac579a8de732a393a59
[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 org.opendaylight.yangtools.yang.common.QName;
19 import org.opendaylight.yangtools.yang.model.api.DerivableSchemaNode;
20 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
21 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
22 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
23 import org.opendaylight.yangtools.yang.model.api.stmt.ListStatement;
24 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier;
25 import org.opendaylight.yangtools.yang.parser.spi.meta.InferenceException;
26 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
27
28 public final class ListEffectiveStatementImpl extends AbstractEffectiveSimpleDataNodeContainer<ListStatement> implements
29         ListSchemaNode, DerivableSchemaNode {
30
31     private final boolean userOrdered;
32     private final List<QName> keyDefinition;
33     private static final String ORDER_BY_USER_KEYWORD = "user";
34     private final ListSchemaNode original;
35
36     public ListEffectiveStatementImpl(
37             final StmtContext<QName, ListStatement, EffectiveStatement<QName, ListStatement>> ctx) {
38         super(ctx);
39
40         this.original = ctx.getOriginalCtx() == null ? null : (ListSchemaNode) ctx.getOriginalCtx().buildEffective();
41
42         OrderedByEffectiveStatementImpl orderedByStmt = firstEffective(OrderedByEffectiveStatementImpl.class);
43         if (orderedByStmt != null && orderedByStmt.argument().equals(ORDER_BY_USER_KEYWORD)) {
44             this.userOrdered = true;
45         } else {
46             this.userOrdered = false;
47         }
48
49         // initKeyDefinition
50         List<QName> keyDefinitionInit = new LinkedList<>();
51         KeyEffectiveStatementImpl keyEffectiveSubstatement = firstEffective(KeyEffectiveStatementImpl.class);
52
53         if (keyEffectiveSubstatement != null) {
54             Set<QName> possibleLeafQNamesForKey = new HashSet<>();
55
56             for (final EffectiveStatement<?, ?> effectiveStatement : effectiveSubstatements()) {
57                 if (effectiveStatement instanceof LeafSchemaNode) {
58                     possibleLeafQNamesForKey.add(((LeafSchemaNode) effectiveStatement).getQName());
59                 }
60             }
61
62             Collection<SchemaNodeIdentifier> keys = keyEffectiveSubstatement.argument();
63             for (SchemaNodeIdentifier key : keys) {
64                 final QName keyQName = key.getLastComponent();
65
66                 if (!possibleLeafQNamesForKey.contains(keyQName)) {
67                     throw new InferenceException(String.format("Key '%s' misses node '%s' in list '%s'",
68                             keyEffectiveSubstatement.getDeclared().rawArgument(), keyQName.getLocalName(),
69                             ctx.getStatementArgument()), ctx.getStatementSourceReference());
70                 }
71
72                 keyDefinitionInit.add(keyQName);
73             }
74         }
75
76         this.keyDefinition = ImmutableList.copyOf(keyDefinitionInit);
77     }
78
79     @Override
80     public Optional<ListSchemaNode> getOriginal() {
81         return Optional.fromNullable(original);
82     }
83
84     @Override
85     public List<QName> getKeyDefinition() {
86         return keyDefinition;
87     }
88
89     @Override
90     public boolean isUserOrdered() {
91         return userOrdered;
92     }
93
94     @Override
95     public int hashCode() {
96         final int prime = 31;
97         int result = 1;
98         result = prime * result + Objects.hashCode(getQName());
99         result = prime * result + Objects.hashCode(getPath());
100         return result;
101     }
102
103     @Override
104     public boolean equals(final Object obj) {
105         if (this == obj) {
106             return true;
107         }
108         if (obj == null) {
109             return false;
110         }
111         if (getClass() != obj.getClass()) {
112             return false;
113         }
114         final ListEffectiveStatementImpl other = (ListEffectiveStatementImpl) obj;
115         return Objects.equals(getQName(), other.getQName()) && Objects.equals(getPath(), other.getPath());
116     }
117
118     @Override
119     public String toString() {
120         return "list " + getQName().getLocalName();
121     }
122 }