8ce63c3d902067704e6ee57b2dc1f96e20a58d05
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / stmt / rfc6020 / effective / 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.stmt.rfc6020.effective;
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.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;
19 import java.util.Set;
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.ListStatement;
30 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier;
31 import org.opendaylight.yangtools.yang.parser.spi.meta.InferenceException;
32 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
33
34 public final class ListEffectiveStatementImpl extends AbstractEffectiveSimpleDataNodeContainer<ListStatement> implements
35         ListSchemaNode, DerivableSchemaNode {
36     private static final String ORDER_BY_USER_KEYWORD = "user";
37
38     private final boolean userOrdered;
39     private final List<QName> keyDefinition;
40     private final ListSchemaNode original;
41     private final Set<ActionDefinition> actions;
42     private final Set<NotificationDefinition> notifications;
43     private final Collection<UniqueConstraint> uniqueConstraints;
44
45     public ListEffectiveStatementImpl(
46             final StmtContext<QName, ListStatement, EffectiveStatement<QName, ListStatement>> ctx) {
47         super(ctx);
48
49         this.original = (ListSchemaNode) ctx.getOriginalCtx().map(StmtContext::buildEffective).orElse(null);
50
51         final OrderedByEffectiveStatementImpl orderedByStmt = firstEffective(OrderedByEffectiveStatementImpl.class);
52         if (orderedByStmt != null && ORDER_BY_USER_KEYWORD.equals(orderedByStmt.argument())) {
53             this.userOrdered = true;
54         } else {
55             this.userOrdered = false;
56         }
57
58         // initKeyDefinition
59         final List<QName> keyDefinitionInit = new LinkedList<>();
60         final KeyEffectiveStatementImpl keyEffectiveSubstatement = firstEffective(KeyEffectiveStatementImpl.class);
61         if (keyEffectiveSubstatement != null) {
62             final Set<QName> possibleLeafQNamesForKey = new HashSet<>();
63             for (final EffectiveStatement<?, ?> effectiveStatement : effectiveSubstatements()) {
64                 if (effectiveStatement instanceof LeafSchemaNode) {
65                     possibleLeafQNamesForKey.add(((LeafSchemaNode) effectiveStatement).getQName());
66                 }
67             }
68             for (final SchemaNodeIdentifier key : keyEffectiveSubstatement.argument()) {
69                 final QName keyQName = key.getLastComponent();
70
71                 if (!possibleLeafQNamesForKey.contains(keyQName)) {
72                     throw new InferenceException(ctx.getStatementSourceReference(),
73                             "Key '%s' misses node '%s' in list '%s'", keyEffectiveSubstatement.getDeclared()
74                                     .rawArgument(), keyQName.getLocalName(), ctx.getStatementArgument());
75                 }
76                 keyDefinitionInit.add(keyQName);
77             }
78         }
79         this.keyDefinition = ImmutableList.copyOf(keyDefinitionInit);
80         this.uniqueConstraints = ImmutableList.copyOf(allSubstatementsOfType(UniqueConstraint.class));
81
82         final ImmutableSet.Builder<ActionDefinition> actionsBuilder = ImmutableSet.builder();
83         final Builder<NotificationDefinition> notificationsBuilder = ImmutableSet.builder();
84         for (final EffectiveStatement<?, ?> effectiveStatement : effectiveSubstatements()) {
85             if (effectiveStatement instanceof ActionDefinition) {
86                 actionsBuilder.add((ActionDefinition) effectiveStatement);
87             }
88
89             if (effectiveStatement instanceof NotificationDefinition) {
90                 notificationsBuilder.add((NotificationDefinition) effectiveStatement);
91             }
92         }
93
94         this.actions = actionsBuilder.build();
95         this.notifications = notificationsBuilder.build();
96     }
97
98     @Override
99     public Optional<ListSchemaNode> getOriginal() {
100         return Optional.ofNullable(original);
101     }
102
103     @Override
104     public List<QName> getKeyDefinition() {
105         return keyDefinition;
106     }
107
108     @Override
109     public Set<ActionDefinition> getActions() {
110         return actions;
111     }
112
113     @Override
114     public Set<NotificationDefinition> getNotifications() {
115         return notifications;
116     }
117
118     @Override
119     @Nonnull
120     public Collection<UniqueConstraint> getUniqueConstraints() {
121         return uniqueConstraints;
122     }
123
124     @Override
125     public boolean isUserOrdered() {
126         return userOrdered;
127     }
128
129     @Override
130     public int hashCode() {
131         final int prime = 31;
132         int result = 1;
133         result = prime * result + Objects.hashCode(getQName());
134         result = prime * result + Objects.hashCode(getPath());
135         return result;
136     }
137
138     @Override
139     public boolean equals(final Object obj) {
140         if (this == obj) {
141             return true;
142         }
143         if (obj == null) {
144             return false;
145         }
146         if (getClass() != obj.getClass()) {
147             return false;
148         }
149         final ListEffectiveStatementImpl other = (ListEffectiveStatementImpl) obj;
150         return Objects.equals(getQName(), other.getQName()) && Objects.equals(getPath(), other.getPath());
151     }
152
153     @Override
154     public String toString() {
155         return "list " + getQName().getLocalName();
156     }
157 }