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
8 package org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective;
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;
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;
28 public final class ListEffectiveStatementImpl extends AbstractEffectiveSimpleDataNodeContainer<ListStatement> implements
29 ListSchemaNode, DerivableSchemaNode {
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;
36 public ListEffectiveStatementImpl(
37 final StmtContext<QName, ListStatement, EffectiveStatement<QName, ListStatement>> ctx) {
40 this.original = ctx.getOriginalCtx() == null ? null : (ListSchemaNode) ctx.getOriginalCtx().buildEffective();
42 OrderedByEffectiveStatementImpl orderedByStmt = firstEffective(OrderedByEffectiveStatementImpl.class);
43 if (orderedByStmt != null && orderedByStmt.argument().equals(ORDER_BY_USER_KEYWORD)) {
44 this.userOrdered = true;
46 this.userOrdered = false;
50 List<QName> keyDefinitionInit = new LinkedList<>();
51 KeyEffectiveStatementImpl keyEffectiveSubstatement = firstEffective(KeyEffectiveStatementImpl.class);
53 if (keyEffectiveSubstatement != null) {
54 Set<QName> possibleLeafQNamesForKey = new HashSet<>();
56 for (final EffectiveStatement<?, ?> effectiveStatement : effectiveSubstatements()) {
57 if (effectiveStatement instanceof LeafSchemaNode) {
58 possibleLeafQNamesForKey.add(((LeafSchemaNode) effectiveStatement).getQName());
62 Collection<SchemaNodeIdentifier> keys = keyEffectiveSubstatement.argument();
63 for (SchemaNodeIdentifier key : keys) {
64 final QName keyQName = key.getLastComponent();
66 if (!possibleLeafQNamesForKey.contains(keyQName)) {
67 throw new InferenceException(ctx.getStatementSourceReference(),
68 "Key '%s' misses node '%s' in list '%s'", keyEffectiveSubstatement.getDeclared().rawArgument(),
69 keyQName.getLocalName(), ctx.getStatementArgument());
72 keyDefinitionInit.add(keyQName);
76 this.keyDefinition = ImmutableList.copyOf(keyDefinitionInit);
80 public Optional<ListSchemaNode> getOriginal() {
81 return Optional.fromNullable(original);
85 public List<QName> getKeyDefinition() {
90 public boolean isUserOrdered() {
95 public int hashCode() {
98 result = prime * result + Objects.hashCode(getQName());
99 result = prime * result + Objects.hashCode(getPath());
104 public boolean equals(final Object obj) {
111 if (getClass() != obj.getClass()) {
114 final ListEffectiveStatementImpl other = (ListEffectiveStatementImpl) obj;
115 return Objects.equals(getQName(), other.getQName()) && Objects.equals(getPath(), other.getPath());
119 public String toString() {
120 return "list " + getQName().getLocalName();