Refactor Leaf(List)EffectiveStatementImpl
[yangtools.git] / yang / yang-parser-rfc7950 / src / main / java / org / opendaylight / yangtools / yang / parser / rfc7950 / stmt / list / ListEffectiveStatementImpl.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.yangtools.yang.parser.rfc7950.stmt.list;
9
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;
19 import java.util.Set;
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;
43
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";
50
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;
58
59     ListEffectiveStatementImpl(
60             final StmtContext<QName, ListStatement, EffectiveStatement<QName, ListStatement>> ctx) {
61         super(ctx);
62
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();
66
67         // initKeyDefinition
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());
76                 }
77             }
78             for (final SchemaNodeIdentifier key : keyStmt.argument()) {
79                 final QName keyQName = key.getLastComponent();
80
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());
85                 }
86                 keyDefinitionInit.add(keyQName);
87             }
88
89             this.keyDefinition = ImmutableList.copyOf(keyDefinitionInit);
90         } else {
91             this.keyDefinition = ImmutableList.of();
92         }
93
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());
98         }
99
100         this.uniqueConstraints = ImmutableList.copyOf(allSubstatementsOfType(UniqueConstraint.class));
101
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);
107             }
108
109             if (effectiveStatement instanceof NotificationDefinition) {
110                 notificationsBuilder.add((NotificationDefinition) effectiveStatement);
111             }
112         }
113
114         this.actions = actionsBuilder.build();
115         this.notifications = notificationsBuilder.build();
116         elementCountConstraint = EffectiveStmtUtils.createElementCountConstraint(this).orElse(null);
117     }
118
119     @Override
120     public Optional<ListSchemaNode> getOriginal() {
121         return Optional.ofNullable(original);
122     }
123
124     @Override
125     public List<QName> getKeyDefinition() {
126         return keyDefinition;
127     }
128
129     @Override
130     public Set<ActionDefinition> getActions() {
131         return actions;
132     }
133
134     @Override
135     public Set<NotificationDefinition> getNotifications() {
136         return notifications;
137     }
138
139     @Override
140     public Collection<UniqueConstraint> getUniqueConstraints() {
141         return uniqueConstraints;
142     }
143
144     @Override
145     public boolean isUserOrdered() {
146         return userOrdered;
147     }
148
149     @Override
150     public Optional<ElementCountConstraint> getElementCountConstraint() {
151         return Optional.ofNullable(elementCountConstraint);
152     }
153
154     @Override
155     public int hashCode() {
156         final int prime = 31;
157         int result = 1;
158         result = prime * result + Objects.hashCode(getQName());
159         result = prime * result + Objects.hashCode(getPath());
160         return result;
161     }
162
163     @Override
164     public boolean equals(final Object obj) {
165         if (this == obj) {
166             return true;
167         }
168         if (obj == null) {
169             return false;
170         }
171         if (getClass() != obj.getClass()) {
172             return false;
173         }
174         final ListEffectiveStatementImpl other = (ListEffectiveStatementImpl) obj;
175         return Objects.equals(getQName(), other.getQName()) && Objects.equals(getPath(), other.getPath());
176     }
177
178     @Override
179     public String toString() {
180         return "list " + getQName().getLocalName();
181     }
182 }