Merge branch 'master' of ../controller
[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.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.model.api.stmt.compat.ActionNodeContainerCompat;
37 import org.opendaylight.yangtools.yang.model.api.stmt.compat.NotificationNodeContainerCompat;
38 import org.opendaylight.yangtools.yang.parser.rfc7950.stmt.AbstractEffectiveSimpleDataNodeContainer;
39 import org.opendaylight.yangtools.yang.parser.rfc7950.stmt.EffectiveStmtUtils;
40 import org.opendaylight.yangtools.yang.parser.spi.meta.InferenceException;
41 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
42
43 final class ListEffectiveStatementImpl extends AbstractEffectiveSimpleDataNodeContainer<ListStatement>
44         implements ListEffectiveStatement, ListSchemaNode, DerivableSchemaNode,
45             ActionNodeContainerCompat<QName, ListStatement>, NotificationNodeContainerCompat<QName, ListStatement> {
46     private static final String ORDER_BY_USER_KEYWORD = "user";
47
48     private final boolean userOrdered;
49     private final ImmutableList<QName> keyDefinition;
50     private final ListSchemaNode original;
51     private final @NonNull ImmutableSet<ActionDefinition> actions;
52     private final @NonNull ImmutableSet<NotificationDefinition> notifications;
53     private final @NonNull ImmutableList<UniqueConstraint> uniqueConstraints;
54     private final ElementCountConstraint elementCountConstraint;
55     private final ImmutableSet<MustDefinition> mustConstraints;
56
57     ListEffectiveStatementImpl(
58             final StmtContext<QName, ListStatement, EffectiveStatement<QName, ListStatement>> ctx) {
59         super(ctx);
60
61         this.original = (ListSchemaNode) ctx.getOriginalCtx().map(StmtContext::buildEffective).orElse(null);
62         this.userOrdered = findFirstEffectiveSubstatementArgument(OrderedByEffectiveStatement.class)
63                 .map(ORDER_BY_USER_KEYWORD::equals).orElse(Boolean.FALSE).booleanValue();
64
65         // initKeyDefinition
66         final Optional<KeyEffectiveStatement> optKeyStmt = findFirstEffectiveSubstatement(KeyEffectiveStatement.class);
67         if (optKeyStmt.isPresent()) {
68             final KeyEffectiveStatement keyStmt = optKeyStmt.get();
69             final List<QName> keyDefinitionInit = new ArrayList<>(keyStmt.argument().size());
70             final Set<QName> possibleLeafQNamesForKey = new HashSet<>();
71             for (final EffectiveStatement<?, ?> effectiveStatement : effectiveSubstatements()) {
72                 if (effectiveStatement instanceof LeafSchemaNode) {
73                     possibleLeafQNamesForKey.add(((LeafSchemaNode) effectiveStatement).getQName());
74                 }
75             }
76             for (final SchemaNodeIdentifier key : keyStmt.argument()) {
77                 final QName keyQName = key.getLastComponent();
78
79                 if (!possibleLeafQNamesForKey.contains(keyQName)) {
80                     throw new InferenceException(ctx.getStatementSourceReference(),
81                             "Key '%s' misses node '%s' in list '%s'", keyStmt.getDeclared().rawArgument(),
82                             keyQName.getLocalName(), ctx.getStatementArgument());
83                 }
84                 keyDefinitionInit.add(keyQName);
85             }
86
87             this.keyDefinition = ImmutableList.copyOf(keyDefinitionInit);
88         } else {
89             this.keyDefinition = ImmutableList.of();
90         }
91
92         this.uniqueConstraints = ImmutableList.copyOf(allSubstatementsOfType(UniqueConstraint.class));
93
94         final Builder<ActionDefinition> actionsBuilder = ImmutableSet.builder();
95         final Builder<NotificationDefinition> notificationsBuilder = ImmutableSet.builder();
96         for (final EffectiveStatement<?, ?> effectiveStatement : effectiveSubstatements()) {
97             if (effectiveStatement instanceof ActionDefinition) {
98                 actionsBuilder.add((ActionDefinition) effectiveStatement);
99             }
100
101             if (effectiveStatement instanceof NotificationDefinition) {
102                 notificationsBuilder.add((NotificationDefinition) effectiveStatement);
103             }
104         }
105
106         this.actions = actionsBuilder.build();
107         this.notifications = notificationsBuilder.build();
108         elementCountConstraint = EffectiveStmtUtils.createElementCountConstraint(this).orElse(null);
109         mustConstraints = ImmutableSet.copyOf(allSubstatementsOfType(MustDefinition.class));
110     }
111
112     @Override
113     public Optional<ListSchemaNode> getOriginal() {
114         return Optional.ofNullable(original);
115     }
116
117     @Override
118     public List<QName> getKeyDefinition() {
119         return keyDefinition;
120     }
121
122     @Override
123     public Set<ActionDefinition> getActions() {
124         return actions;
125     }
126
127     @Override
128     public Set<NotificationDefinition> getNotifications() {
129         return notifications;
130     }
131
132     @Override
133     public Collection<UniqueConstraint> getUniqueConstraints() {
134         return uniqueConstraints;
135     }
136
137     @Override
138     public boolean isUserOrdered() {
139         return userOrdered;
140     }
141
142     @Override
143     public Optional<ElementCountConstraint> getElementCountConstraint() {
144         return Optional.ofNullable(elementCountConstraint);
145     }
146
147     @Override
148     public Collection<MustDefinition> getMustConstraints() {
149         return mustConstraints;
150     }
151
152     @Override
153     public int hashCode() {
154         final int prime = 31;
155         int result = 1;
156         result = prime * result + Objects.hashCode(getQName());
157         result = prime * result + Objects.hashCode(getPath());
158         return result;
159     }
160
161     @Override
162     public boolean equals(final Object obj) {
163         if (this == obj) {
164             return true;
165         }
166         if (obj == null) {
167             return false;
168         }
169         if (getClass() != obj.getClass()) {
170             return false;
171         }
172         final ListEffectiveStatementImpl other = (ListEffectiveStatementImpl) obj;
173         return Objects.equals(getQName(), other.getQName()) && Objects.equals(getPath(), other.getPath());
174     }
175
176     @Override
177     public String toString() {
178         return "list " + getQName().getLocalName();
179     }
180 }