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