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.rfc7950.stmt.list;
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.ArrayList;
14 import java.util.Collection;
15 import java.util.HashSet;
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.ElementCountConstraint;
25 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
26 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
27 import org.opendaylight.yangtools.yang.model.api.MustDefinition;
28 import org.opendaylight.yangtools.yang.model.api.NotificationDefinition;
29 import org.opendaylight.yangtools.yang.model.api.UniqueConstraint;
30 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
31 import org.opendaylight.yangtools.yang.model.api.stmt.KeyEffectiveStatement;
32 import org.opendaylight.yangtools.yang.model.api.stmt.ListEffectiveStatement;
33 import org.opendaylight.yangtools.yang.model.api.stmt.ListStatement;
34 import org.opendaylight.yangtools.yang.model.api.stmt.OrderedByEffectiveStatement;
35 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier;
36 import org.opendaylight.yangtools.yang.parser.rfc7950.stmt.AbstractEffectiveSimpleDataNodeContainer;
37 import org.opendaylight.yangtools.yang.parser.rfc7950.stmt.EffectiveStmtUtils;
38 import org.opendaylight.yangtools.yang.parser.spi.meta.InferenceException;
39 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
41 // FIXME: hide this class
42 public final class ListEffectiveStatementImpl extends AbstractEffectiveSimpleDataNodeContainer<ListStatement>
43 implements ListEffectiveStatement, ListSchemaNode, DerivableSchemaNode {
44 private static final String ORDER_BY_USER_KEYWORD = "user";
46 private final boolean userOrdered;
47 private final List<QName> keyDefinition;
48 private final ListSchemaNode original;
49 private final Set<ActionDefinition> actions;
50 private final Set<NotificationDefinition> notifications;
51 private final Collection<UniqueConstraint> uniqueConstraints;
52 private final ElementCountConstraint elementCountConstraint;
53 private final Collection<MustDefinition> mustConstraints;
55 ListEffectiveStatementImpl(
56 final StmtContext<QName, ListStatement, EffectiveStatement<QName, ListStatement>> ctx) {
59 this.original = (ListSchemaNode) ctx.getOriginalCtx().map(StmtContext::buildEffective).orElse(null);
60 this.userOrdered = findFirstEffectiveSubstatementArgument(OrderedByEffectiveStatement.class)
61 .map(ORDER_BY_USER_KEYWORD::equals).orElse(Boolean.FALSE).booleanValue();
64 final Optional<KeyEffectiveStatement> optKeyStmt = findFirstEffectiveSubstatement(KeyEffectiveStatement.class);
65 if (optKeyStmt.isPresent()) {
66 final KeyEffectiveStatement keyStmt = optKeyStmt.get();
67 final List<QName> keyDefinitionInit = new ArrayList<>(keyStmt.argument().size());
68 final Set<QName> possibleLeafQNamesForKey = new HashSet<>();
69 for (final EffectiveStatement<?, ?> effectiveStatement : effectiveSubstatements()) {
70 if (effectiveStatement instanceof LeafSchemaNode) {
71 possibleLeafQNamesForKey.add(((LeafSchemaNode) effectiveStatement).getQName());
74 for (final SchemaNodeIdentifier key : keyStmt.argument()) {
75 final QName keyQName = key.getLastComponent();
77 if (!possibleLeafQNamesForKey.contains(keyQName)) {
78 throw new InferenceException(ctx.getStatementSourceReference(),
79 "Key '%s' misses node '%s' in list '%s'", keyStmt.getDeclared().rawArgument(),
80 keyQName.getLocalName(), ctx.getStatementArgument());
82 keyDefinitionInit.add(keyQName);
85 this.keyDefinition = ImmutableList.copyOf(keyDefinitionInit);
87 this.keyDefinition = ImmutableList.of();
90 this.uniqueConstraints = ImmutableList.copyOf(allSubstatementsOfType(UniqueConstraint.class));
92 final ImmutableSet.Builder<ActionDefinition> actionsBuilder = ImmutableSet.builder();
93 final Builder<NotificationDefinition> notificationsBuilder = ImmutableSet.builder();
94 for (final EffectiveStatement<?, ?> effectiveStatement : effectiveSubstatements()) {
95 if (effectiveStatement instanceof ActionDefinition) {
96 actionsBuilder.add((ActionDefinition) effectiveStatement);
99 if (effectiveStatement instanceof NotificationDefinition) {
100 notificationsBuilder.add((NotificationDefinition) effectiveStatement);
104 this.actions = actionsBuilder.build();
105 this.notifications = notificationsBuilder.build();
106 elementCountConstraint = EffectiveStmtUtils.createElementCountConstraint(this).orElse(null);
107 mustConstraints = ImmutableSet.copyOf(allSubstatementsOfType(MustDefinition.class));
111 public Optional<ListSchemaNode> getOriginal() {
112 return Optional.ofNullable(original);
116 public List<QName> getKeyDefinition() {
117 return keyDefinition;
121 public Set<ActionDefinition> getActions() {
126 public Set<NotificationDefinition> getNotifications() {
127 return notifications;
132 public Collection<UniqueConstraint> getUniqueConstraints() {
133 return uniqueConstraints;
137 public boolean isUserOrdered() {
142 public Optional<ElementCountConstraint> getElementCountConstraint() {
143 return Optional.ofNullable(elementCountConstraint);
147 public Collection<MustDefinition> getMustConstraints() {
148 return mustConstraints;
152 public int hashCode() {
153 final int prime = 31;
155 result = prime * result + Objects.hashCode(getQName());
156 result = prime * result + Objects.hashCode(getPath());
161 public boolean equals(final Object obj) {
168 if (getClass() != obj.getClass()) {
171 final ListEffectiveStatementImpl other = (ListEffectiveStatementImpl) obj;
172 return Objects.equals(getQName(), other.getQName()) && Objects.equals(getPath(), other.getPath());
176 public String toString() {
177 return "list " + getQName().getLocalName();