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 org.eclipse.jdt.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.NotificationDefinition;
28 import org.opendaylight.yangtools.yang.model.api.UniqueConstraint;
29 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
30 import org.opendaylight.yangtools.yang.model.api.stmt.KeyEffectiveStatement;
31 import org.opendaylight.yangtools.yang.model.api.stmt.ListEffectiveStatement;
32 import org.opendaylight.yangtools.yang.model.api.stmt.ListStatement;
33 import org.opendaylight.yangtools.yang.model.api.stmt.OrderedByEffectiveStatement;
34 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier;
35 import org.opendaylight.yangtools.yang.model.api.stmt.compat.ActionNodeContainerCompat;
36 import org.opendaylight.yangtools.yang.model.api.stmt.compat.NotificationNodeContainerCompat;
37 import org.opendaylight.yangtools.yang.parser.rfc7950.stmt.AbstractEffectiveMustConstraintAwareSimpleDataNodeContainer;
38 import org.opendaylight.yangtools.yang.parser.rfc7950.stmt.EffectiveStmtUtils;
39 import org.opendaylight.yangtools.yang.parser.spi.meta.InferenceException;
40 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
41 import org.slf4j.Logger;
42 import org.slf4j.LoggerFactory;
44 final class ListEffectiveStatementImpl
45 extends AbstractEffectiveMustConstraintAwareSimpleDataNodeContainer<ListStatement>
46 implements ListEffectiveStatement, ListSchemaNode, DerivableSchemaNode,
47 ActionNodeContainerCompat<QName, ListStatement>, NotificationNodeContainerCompat<QName, ListStatement> {
48 private static final Logger LOG = LoggerFactory.getLogger(ListEffectiveStatementImpl.class);
49 private static final String ORDER_BY_USER_KEYWORD = "user";
51 private final boolean userOrdered;
52 private final ImmutableList<QName> keyDefinition;
53 private final ListSchemaNode original;
54 private final @NonNull ImmutableSet<ActionDefinition> actions;
55 private final @NonNull ImmutableSet<NotificationDefinition> notifications;
56 private final @NonNull ImmutableList<UniqueConstraint> uniqueConstraints;
57 private final ElementCountConstraint elementCountConstraint;
59 ListEffectiveStatementImpl(
60 final StmtContext<QName, ListStatement, EffectiveStatement<QName, ListStatement>> ctx) {
63 this.original = (ListSchemaNode) ctx.getOriginalCtx().map(StmtContext::buildEffective).orElse(null);
64 this.userOrdered = findFirstEffectiveSubstatementArgument(OrderedByEffectiveStatement.class)
65 .map(ORDER_BY_USER_KEYWORD::equals).orElse(Boolean.FALSE).booleanValue();
68 final Optional<KeyEffectiveStatement> optKeyStmt = findFirstEffectiveSubstatement(KeyEffectiveStatement.class);
69 if (optKeyStmt.isPresent()) {
70 final KeyEffectiveStatement keyStmt = optKeyStmt.get();
71 final List<QName> keyDefinitionInit = new ArrayList<>(keyStmt.argument().size());
72 final Set<QName> possibleLeafQNamesForKey = new HashSet<>();
73 for (final EffectiveStatement<?, ?> effectiveStatement : effectiveSubstatements()) {
74 if (effectiveStatement instanceof LeafSchemaNode) {
75 possibleLeafQNamesForKey.add(((LeafSchemaNode) effectiveStatement).getQName());
78 for (final SchemaNodeIdentifier key : keyStmt.argument()) {
79 final QName keyQName = key.getLastComponent();
81 if (!possibleLeafQNamesForKey.contains(keyQName)) {
82 throw new InferenceException(ctx.getStatementSourceReference(),
83 "Key '%s' misses node '%s' in list '%s'", keyStmt.getDeclared().rawArgument(),
84 keyQName.getLocalName(), ctx.getStatementArgument());
86 keyDefinitionInit.add(keyQName);
89 this.keyDefinition = ImmutableList.copyOf(keyDefinitionInit);
91 this.keyDefinition = ImmutableList.of();
94 if (isConfiguration() && keyDefinition.isEmpty()) {
95 LOG.info("Configuration list {} does not define any keys in violation of RFC7950 section 7.8.2. While "
96 + " this is fine with OpenDaylight, it can cause interoperability issues with other systems "
97 + "[at {}]", ctx.getStatementArgument(), ctx.getStatementSourceReference());
100 this.uniqueConstraints = ImmutableList.copyOf(allSubstatementsOfType(UniqueConstraint.class));
102 final Builder<ActionDefinition> actionsBuilder = ImmutableSet.builder();
103 final Builder<NotificationDefinition> notificationsBuilder = ImmutableSet.builder();
104 for (final EffectiveStatement<?, ?> effectiveStatement : effectiveSubstatements()) {
105 if (effectiveStatement instanceof ActionDefinition) {
106 actionsBuilder.add((ActionDefinition) effectiveStatement);
109 if (effectiveStatement instanceof NotificationDefinition) {
110 notificationsBuilder.add((NotificationDefinition) effectiveStatement);
114 this.actions = actionsBuilder.build();
115 this.notifications = notificationsBuilder.build();
116 elementCountConstraint = EffectiveStmtUtils.createElementCountConstraint(this).orElse(null);
120 public Optional<ListSchemaNode> getOriginal() {
121 return Optional.ofNullable(original);
125 public List<QName> getKeyDefinition() {
126 return keyDefinition;
130 public Set<ActionDefinition> getActions() {
135 public Set<NotificationDefinition> getNotifications() {
136 return notifications;
140 public Collection<UniqueConstraint> getUniqueConstraints() {
141 return uniqueConstraints;
145 public boolean isUserOrdered() {
150 public Optional<ElementCountConstraint> getElementCountConstraint() {
151 return Optional.ofNullable(elementCountConstraint);
155 public int hashCode() {
156 final int prime = 31;
158 result = prime * result + Objects.hashCode(getQName());
159 result = prime * result + Objects.hashCode(getPath());
164 public boolean equals(final Object obj) {
171 if (getClass() != obj.getClass()) {
174 final ListEffectiveStatementImpl other = (ListEffectiveStatementImpl) obj;
175 return Objects.equals(getQName(), other.getQName()) && Objects.equals(getPath(), other.getPath());
179 public String toString() {
180 return "list " + getQName().getLocalName();