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.ListEffectiveStatement;
30 import org.opendaylight.yangtools.yang.model.api.stmt.ListStatement;
31 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier;
32 import org.opendaylight.yangtools.yang.parser.spi.meta.InferenceException;
33 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
35 public final class ListEffectiveStatementImpl extends AbstractEffectiveSimpleDataNodeContainer<ListStatement>
36 implements ListEffectiveStatement, ListSchemaNode, DerivableSchemaNode {
37 private static final String ORDER_BY_USER_KEYWORD = "user";
39 private final boolean userOrdered;
40 private final List<QName> keyDefinition;
41 private final ListSchemaNode original;
42 private final Set<ActionDefinition> actions;
43 private final Set<NotificationDefinition> notifications;
44 private final Collection<UniqueConstraint> uniqueConstraints;
46 public ListEffectiveStatementImpl(
47 final StmtContext<QName, ListStatement, EffectiveStatement<QName, ListStatement>> ctx) {
50 this.original = (ListSchemaNode) ctx.getOriginalCtx().map(StmtContext::buildEffective).orElse(null);
52 final OrderedByEffectiveStatementImpl orderedByStmt = firstEffective(OrderedByEffectiveStatementImpl.class);
53 if (orderedByStmt != null && ORDER_BY_USER_KEYWORD.equals(orderedByStmt.argument())) {
54 this.userOrdered = true;
56 this.userOrdered = false;
60 final List<QName> keyDefinitionInit = new LinkedList<>();
61 final KeyEffectiveStatementImpl keyEffectiveSubstatement = firstEffective(KeyEffectiveStatementImpl.class);
62 if (keyEffectiveSubstatement != null) {
63 final Set<QName> possibleLeafQNamesForKey = new HashSet<>();
64 for (final EffectiveStatement<?, ?> effectiveStatement : effectiveSubstatements()) {
65 if (effectiveStatement instanceof LeafSchemaNode) {
66 possibleLeafQNamesForKey.add(((LeafSchemaNode) effectiveStatement).getQName());
69 for (final SchemaNodeIdentifier key : keyEffectiveSubstatement.argument()) {
70 final QName keyQName = key.getLastComponent();
72 if (!possibleLeafQNamesForKey.contains(keyQName)) {
73 throw new InferenceException(ctx.getStatementSourceReference(),
74 "Key '%s' misses node '%s' in list '%s'", keyEffectiveSubstatement.getDeclared()
75 .rawArgument(), keyQName.getLocalName(), ctx.getStatementArgument());
77 keyDefinitionInit.add(keyQName);
80 this.keyDefinition = ImmutableList.copyOf(keyDefinitionInit);
81 this.uniqueConstraints = ImmutableList.copyOf(allSubstatementsOfType(UniqueConstraint.class));
83 final ImmutableSet.Builder<ActionDefinition> actionsBuilder = ImmutableSet.builder();
84 final Builder<NotificationDefinition> notificationsBuilder = ImmutableSet.builder();
85 for (final EffectiveStatement<?, ?> effectiveStatement : effectiveSubstatements()) {
86 if (effectiveStatement instanceof ActionDefinition) {
87 actionsBuilder.add((ActionDefinition) effectiveStatement);
90 if (effectiveStatement instanceof NotificationDefinition) {
91 notificationsBuilder.add((NotificationDefinition) effectiveStatement);
95 this.actions = actionsBuilder.build();
96 this.notifications = notificationsBuilder.build();
100 public Optional<ListSchemaNode> getOriginal() {
101 return Optional.ofNullable(original);
105 public List<QName> getKeyDefinition() {
106 return keyDefinition;
110 public Set<ActionDefinition> getActions() {
115 public Set<NotificationDefinition> getNotifications() {
116 return notifications;
121 public Collection<UniqueConstraint> getUniqueConstraints() {
122 return uniqueConstraints;
126 public boolean isUserOrdered() {
131 public int hashCode() {
132 final int prime = 31;
134 result = prime * result + Objects.hashCode(getQName());
135 result = prime * result + Objects.hashCode(getPath());
140 public boolean equals(final Object obj) {
147 if (getClass() != obj.getClass()) {
150 final ListEffectiveStatementImpl other = (ListEffectiveStatementImpl) obj;
151 return Objects.equals(getQName(), other.getQName()) && Objects.equals(getPath(), other.getPath());
155 public String toString() {
156 return "list " + getQName().getLocalName();