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.collect.ImmutableList;
11 import com.google.common.collect.ImmutableSet;
12 import com.google.common.collect.ImmutableSet.Builder;
13 import java.util.Collection;
14 import java.util.HashSet;
15 import java.util.LinkedList;
16 import java.util.List;
17 import java.util.Objects;
18 import java.util.Optional;
20 import javax.annotation.Nonnull;
21 import org.opendaylight.yangtools.yang.common.QName;
22 import org.opendaylight.yangtools.yang.model.api.ActionDefinition;
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.NotificationDefinition;
27 import org.opendaylight.yangtools.yang.model.api.UniqueConstraint;
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.InferenceException;
32 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
34 public final class ListEffectiveStatementImpl extends AbstractEffectiveSimpleDataNodeContainer<ListStatement> implements
35 ListSchemaNode, DerivableSchemaNode {
36 private static final String ORDER_BY_USER_KEYWORD = "user";
38 private final boolean userOrdered;
39 private final List<QName> keyDefinition;
40 private final ListSchemaNode original;
41 private final Set<ActionDefinition> actions;
42 private final Set<NotificationDefinition> notifications;
43 private final Collection<UniqueConstraint> uniqueConstraints;
45 public ListEffectiveStatementImpl(
46 final StmtContext<QName, ListStatement, EffectiveStatement<QName, ListStatement>> ctx) {
49 this.original = (ListSchemaNode) ctx.getOriginalCtx().map(StmtContext::buildEffective).orElse(null);
51 final OrderedByEffectiveStatementImpl orderedByStmt = firstEffective(OrderedByEffectiveStatementImpl.class);
52 if (orderedByStmt != null && ORDER_BY_USER_KEYWORD.equals(orderedByStmt.argument())) {
53 this.userOrdered = true;
55 this.userOrdered = false;
59 final List<QName> keyDefinitionInit = new LinkedList<>();
60 final KeyEffectiveStatementImpl keyEffectiveSubstatement = firstEffective(KeyEffectiveStatementImpl.class);
61 if (keyEffectiveSubstatement != null) {
62 final Set<QName> possibleLeafQNamesForKey = new HashSet<>();
63 for (final EffectiveStatement<?, ?> effectiveStatement : effectiveSubstatements()) {
64 if (effectiveStatement instanceof LeafSchemaNode) {
65 possibleLeafQNamesForKey.add(((LeafSchemaNode) effectiveStatement).getQName());
68 for (final SchemaNodeIdentifier key : keyEffectiveSubstatement.argument()) {
69 final QName keyQName = key.getLastComponent();
71 if (!possibleLeafQNamesForKey.contains(keyQName)) {
72 throw new InferenceException(ctx.getStatementSourceReference(),
73 "Key '%s' misses node '%s' in list '%s'", keyEffectiveSubstatement.getDeclared()
74 .rawArgument(), keyQName.getLocalName(), ctx.getStatementArgument());
76 keyDefinitionInit.add(keyQName);
79 this.keyDefinition = ImmutableList.copyOf(keyDefinitionInit);
80 this.uniqueConstraints = ImmutableList.copyOf(allSubstatementsOfType(UniqueConstraint.class));
82 final ImmutableSet.Builder<ActionDefinition> actionsBuilder = ImmutableSet.builder();
83 final Builder<NotificationDefinition> notificationsBuilder = ImmutableSet.builder();
84 for (final EffectiveStatement<?, ?> effectiveStatement : effectiveSubstatements()) {
85 if (effectiveStatement instanceof ActionDefinition) {
86 actionsBuilder.add((ActionDefinition) effectiveStatement);
89 if (effectiveStatement instanceof NotificationDefinition) {
90 notificationsBuilder.add((NotificationDefinition) effectiveStatement);
94 this.actions = actionsBuilder.build();
95 this.notifications = notificationsBuilder.build();
99 public Optional<ListSchemaNode> getOriginal() {
100 return Optional.ofNullable(original);
104 public List<QName> getKeyDefinition() {
105 return keyDefinition;
109 public Set<ActionDefinition> getActions() {
114 public Set<NotificationDefinition> getNotifications() {
115 return notifications;
120 public Collection<UniqueConstraint> getUniqueConstraints() {
121 return uniqueConstraints;
125 public boolean isUserOrdered() {
130 public int hashCode() {
131 final int prime = 31;
133 result = prime * result + Objects.hashCode(getQName());
134 result = prime * result + Objects.hashCode(getPath());
139 public boolean equals(final Object obj) {
146 if (getClass() != obj.getClass()) {
149 final ListEffectiveStatementImpl other = (ListEffectiveStatementImpl) obj;
150 return Objects.equals(getQName(), other.getQName()) && Objects.equals(getPath(), other.getPath());
154 public String toString() {
155 return "list " + getQName().getLocalName();