2 * Copyright (c) 2020 PANTHEON.tech, s.r.o. 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;
10 import com.google.common.annotations.Beta;
11 import com.google.common.collect.Collections2;
12 import com.google.common.collect.ImmutableList;
13 import com.google.common.collect.ImmutableSet;
14 import java.util.Collection;
15 import java.util.List;
16 import java.util.Optional;
18 import org.eclipse.jdt.annotation.NonNullByDefault;
19 import org.opendaylight.yangtools.concepts.Mutable;
20 import org.opendaylight.yangtools.yang.common.QName;
21 import org.opendaylight.yangtools.yang.model.api.ActionDefinition;
22 import org.opendaylight.yangtools.yang.model.api.ActionNodeContainer;
23 import org.opendaylight.yangtools.yang.model.api.AddedByUsesAware;
24 import org.opendaylight.yangtools.yang.model.api.AugmentationSchemaNode;
25 import org.opendaylight.yangtools.yang.model.api.AugmentationTarget;
26 import org.opendaylight.yangtools.yang.model.api.CopyableNode;
27 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
28 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
29 import org.opendaylight.yangtools.yang.model.api.DocumentedNode;
30 import org.opendaylight.yangtools.yang.model.api.GroupingDefinition;
31 import org.opendaylight.yangtools.yang.model.api.MandatoryAware;
32 import org.opendaylight.yangtools.yang.model.api.MustConstraintAware;
33 import org.opendaylight.yangtools.yang.model.api.MustDefinition;
34 import org.opendaylight.yangtools.yang.model.api.NotificationDefinition;
35 import org.opendaylight.yangtools.yang.model.api.NotificationNodeContainer;
36 import org.opendaylight.yangtools.yang.model.api.RevisionAwareXPath;
37 import org.opendaylight.yangtools.yang.model.api.SchemaNode;
38 import org.opendaylight.yangtools.yang.model.api.Status;
39 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
40 import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
41 import org.opendaylight.yangtools.yang.model.api.UsesNode;
42 import org.opendaylight.yangtools.yang.model.api.WhenConditionAware;
43 import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement;
44 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
45 import org.opendaylight.yangtools.yang.model.api.stmt.DescriptionEffectiveStatement;
46 import org.opendaylight.yangtools.yang.model.api.stmt.ReferenceEffectiveStatement;
47 import org.opendaylight.yangtools.yang.model.api.stmt.TypedefEffectiveStatement;
48 import org.opendaylight.yangtools.yang.model.api.stmt.WhenEffectiveStatement;
49 import org.opendaylight.yangtools.yang.parser.spi.meta.CopyHistory;
50 import org.opendaylight.yangtools.yang.parser.spi.meta.CopyType;
53 * Mix-in interfaces providing services required by SchemaNode et al. These interfaces provide implementations, or
54 * implementation helpers based on default methods, so the correct behavior can be logically centralized.
57 public final class EffectiveStatementMixins {
58 // Marker interface requiring all mixins to be derived from EffectiveStatement.
59 private interface Mixin<A, D extends DeclaredStatement<A>> extends EffectiveStatement<A, D> {
60 @SuppressWarnings("unchecked")
61 default <T> Collection<T> filterEffectiveStatements(final Class<T> type) {
62 // Yeah, this is not nice, but saves one transformation
63 return (Collection<T>) Collections2.filter(effectiveSubstatements(), type::isInstance);
66 // FIXME: YANGTOOLS-1068: eliminate this once we can return collections
67 default <T> List<T> filterEffectiveStatementsList(final Class<T> type) {
68 return ImmutableList.copyOf(filterEffectiveStatements(type));
71 // FIXME: YANGTOOLS-1068: eliminate this once we can return collections
72 default <T> Set<T> filterEffectiveStatementsSet(final Class<T> type) {
73 return ImmutableSet.copyOf(filterEffectiveStatements(type));
78 * Bridge between {@link EffectiveStatement} and {@link AugmentationTarget}.
80 * @param <A> Argument type ({@link Void} if statement does not have argument.)
81 * @param <D> Class representing declared version of this statement.
83 public interface AugmentationTargetMixin<A, D extends DeclaredStatement<A>>
84 extends Mixin<A, D>, AugmentationTarget {
86 default Set<AugmentationSchemaNode> getAvailableAugmentations() {
87 return filterEffectiveStatementsSet(AugmentationSchemaNode.class);
92 * Bridge between {@link EffectiveStatementWithFlags} and {@link AddedByUsesAware}.
94 * @param <A> Argument type ({@link Void} if statement does not have argument.)
95 * @param <D> Class representing declared version of this statement.
97 public interface AddedByUsesMixin<A, D extends DeclaredStatement<A>>
98 extends EffectiveStatementWithFlags<A, D>, AddedByUsesAware {
100 default boolean isAddedByUses() {
101 return (flags() & FlagsBuilder.ADDED_BY_USES) != 0;
106 * Bridge between {@link EffectiveStatementWithFlags} and {@link ActionNodeContainer}.
108 * @param <A> Argument type ({@link Void} if statement does not have argument.)
109 * @param <D> Class representing declared version of this statement.
111 public interface ActionNodeContainerMixin<A, D extends DeclaredStatement<A>>
112 extends Mixin<A, D>, ActionNodeContainer {
114 default Set<ActionDefinition> getActions() {
115 return filterEffectiveStatementsSet(ActionDefinition.class);
120 * Bridge between {@link EffectiveStatementWithFlags} and {@link NotificationNodeContainer}.
122 * @param <A> Argument type ({@link Void} if statement does not have argument.)
123 * @param <D> Class representing declared version of this statement.
125 public interface NotificationNodeContainerMixin<A, D extends DeclaredStatement<A>>
126 extends Mixin<A, D>, NotificationNodeContainer {
128 default Set<NotificationDefinition> getNotifications() {
129 return filterEffectiveStatementsSet(NotificationDefinition.class);
134 * Bridge between {@link EffectiveStatementWithFlags} and {@link MustConstraintAware}.
136 * @param <A> Argument type ({@link Void} if statement does not have argument.)
137 * @param <D> Class representing declared version of this statement.
139 public interface MustConstraintMixin<A, D extends DeclaredStatement<A>> extends Mixin<A, D>, MustConstraintAware {
141 default Collection<MustDefinition> getMustConstraints() {
142 return filterEffectiveStatements(MustDefinition.class);
147 * Bridge between {@link EffectiveStatementWithFlags} and {@link CopyableNode}.
149 * @param <A> Argument type ({@link Void} if statement does not have argument.)
150 * @param <D> Class representing declared version of this statement.
152 public interface CopyableMixin<A, D extends DeclaredStatement<A>> extends AddedByUsesMixin<A, D>, CopyableNode {
154 default boolean isAugmenting() {
155 return (flags() & FlagsBuilder.AUGMENTING) != 0;
160 * Bridge between {@link EffectiveStatementWithFlags} and {@link DataNodeContainer}.
162 * @param <A> Argument type ({@link Void} if statement does not have argument.)
163 * @param <D> Class representing declared version of this statement.
165 public interface DataNodeContainerMixin<A, D extends DeclaredStatement<A>> extends DataNodeContainer, Mixin<A, D> {
167 default Set<TypeDefinition<?>> getTypeDefinitions() {
168 // TODO: the cast here is needed to work around Java 11 javac type inference issue
169 return (Set) effectiveSubstatements().stream().filter(TypedefEffectiveStatement.class::isInstance)
170 .map(stmt -> ((TypedefEffectiveStatement) stmt).getTypeDefinition())
171 .collect(ImmutableSet.toImmutableSet());
175 default Collection<DataSchemaNode> getChildNodes() {
176 return filterEffectiveStatements(DataSchemaNode.class);
180 default Set<GroupingDefinition> getGroupings() {
181 return filterEffectiveStatementsSet(GroupingDefinition.class);
185 default Set<UsesNode> getUses() {
186 return filterEffectiveStatementsSet(UsesNode.class);
191 * Bridge between {@link EffectiveStatementWithFlags} and {@link DataSchemaNode}.
193 * @param <A> Argument type ({@link Void} if statement does not have argument.)
194 * @param <D> Class representing declared version of this statement.
196 public interface DataSchemaNodeMixin<A, D extends DeclaredStatement<A>>
197 extends DataSchemaNode, CopyableMixin<A, D>, SchemaNodeMixin<A, D>, WhenConditionMixin<A, D> {
199 default boolean isConfiguration() {
200 return (flags() & FlagsBuilder.CONFIGURATION) != 0;
205 * Bridge between {@link EffectiveStatementWithFlags} and {@link DocumentedNode}.
207 * @param <A> Argument type ({@link Void} if statement does not have argument.)
208 * @param <D> Class representing declared version of this statement.
210 public interface DocumentedNodeMixin<A, D extends DeclaredStatement<A>> extends Mixin<A, D>, DocumentedNode {
212 * Bridge between {@link EffectiveStatementWithFlags} and
213 * {@link org.opendaylight.yangtools.yang.model.api.DocumentedNode.WithStatus}.
215 * @param <A> Argument type ({@link Void} if statement does not have argument.)
216 * @param <D> Class representing declared version of this statement.
218 interface WithStatus<A, D extends DeclaredStatement<A>>
219 extends EffectiveStatementWithFlags<A, D>, DocumentedNodeMixin<A, D>, DocumentedNode.WithStatus {
221 default Status getStatus() {
222 final int status = flags() & FlagsBuilder.MASK_STATUS;
224 case FlagsBuilder.STATUS_CURRENT:
225 return Status.CURRENT;
226 case FlagsBuilder.STATUS_DEPRECATED:
227 return Status.DEPRECATED;
228 case FlagsBuilder.STATUS_OBSOLETE:
229 return Status.OBSOLETE;
231 throw new IllegalStateException("Illegal status " + status);
237 default Optional<String> getDescription() {
238 return findFirstEffectiveSubstatementArgument(DescriptionEffectiveStatement.class);
242 default Optional<String> getReference() {
243 return findFirstEffectiveSubstatementArgument(ReferenceEffectiveStatement.class);
247 default List<UnknownSchemaNode> getUnknownSchemaNodes() {
248 return filterEffectiveStatementsList(UnknownSchemaNode.class);
253 * Bridge between {@link EffectiveStatementWithFlags} and {@link MandatoryAware}.
255 * @param <A> Argument type ({@link Void} if statement does not have argument.)
256 * @param <D> Class representing declared version of this statement.
258 public interface MandatoryMixin<A, D extends DeclaredStatement<A>>
259 extends EffectiveStatementWithFlags<A, D>, MandatoryAware {
261 default boolean isMandatory() {
262 return (flags() & FlagsBuilder.MANDATORY) != 0;
267 * Bridge between {@link EffectiveStatementWithFlags} and {@code presence} statement.
269 * @param <A> Argument type ({@link Void} if statement does not have argument.)
270 * @param <D> Class representing declared version of this statement.
272 public interface PresenceMixin<A, D extends DeclaredStatement<A>> extends EffectiveStatementWithFlags<A, D> {
273 default boolean presence() {
274 return (flags() & FlagsBuilder.PRESENCE) != 0;
279 * Bridge between {@link EffectiveStatementWithFlags} and {@link SchemaNode}.
281 * @param <A> Argument type ({@link Void} if statement does not have argument.)
282 * @param <D> Class representing declared version of this statement.
284 public interface SchemaNodeMixin<A, D extends DeclaredStatement<A>>
285 extends DocumentedNodeMixin.WithStatus<A, D>, SchemaNode {
287 default QName getQName() {
288 return getPath().getLastComponent();
293 * Bridge between {@link EffectiveStatementWithFlags} and {@code ordered-by} statement.
295 * @param <A> Argument type ({@link Void} if statement does not have argument.)
296 * @param <D> Class representing declared version of this statement.
298 public interface UserOrderedMixin<A, D extends DeclaredStatement<A>> extends EffectiveStatementWithFlags<A, D> {
299 default boolean userOrdered() {
300 return (flags() & FlagsBuilder.USER_ORDERED) != 0;
305 * Helper used to locate the effective {@code when} statement and exposing its argument as per
306 * {@link WhenConditionAware}.
308 * @param <A> Argument type ({@link Void} if statement does not have argument.)
309 * @param <D> Class representing declared version of this statement.
311 public interface WhenConditionMixin<A, D extends DeclaredStatement<A>> extends Mixin<A, D>, WhenConditionAware {
313 default Optional<RevisionAwareXPath> getWhenCondition() {
314 return findFirstEffectiveSubstatementArgument(WhenEffectiveStatement.class);
319 * Support interface for various mixins. Implementations are required to store 32bits worth of flags, which are
320 * globally assigned to sub-interfaces -- thus providing storage for many low-cardinality properties.
322 * @param <A> Argument type ({@link Void} if statement does not have argument.)
323 * @param <D> Class representing declared version of this statement.
325 public interface EffectiveStatementWithFlags<A, D extends DeclaredStatement<A>> extends Mixin<A, D> {
327 * Return flags assicated with this statements. Flags can be built using {@link FlagsBuilder}.
329 * @return Flag field value (32 bits).
334 final class FlagsBuilder implements Mutable {
335 // We still have 24 flags remaining
336 static final int STATUS_CURRENT = 0x0001;
337 static final int STATUS_DEPRECATED = 0x0002;
338 static final int STATUS_OBSOLETE = 0x0003;
339 static final int MASK_STATUS = 0x0003;
341 static final int CONFIGURATION = 0x0004;
342 static final int MANDATORY = 0x0008;
344 static final int AUGMENTING = 0x0010;
345 static final int ADDED_BY_USES = 0x0020;
346 private static final int MASK_HISTORY = 0x0030;
348 static final int USER_ORDERED = 0x0040;
349 static final int PRESENCE = 0x0080;
353 public FlagsBuilder setConfiguration(final boolean config) {
355 flags |= CONFIGURATION;
357 flags &= ~CONFIGURATION;
362 public FlagsBuilder setHistory(final CopyHistory history) {
364 if (history.contains(CopyType.ADDED_BY_USES_AUGMENTATION)) {
365 bits = AUGMENTING | ADDED_BY_USES;
368 if (history.contains(CopyType.ADDED_BY_AUGMENTATION)) {
371 if (history.contains(CopyType.ADDED_BY_USES)) {
372 bits |= ADDED_BY_USES;
376 flags = flags & ~MASK_HISTORY | bits;
380 public FlagsBuilder setMandatory(final boolean mandatory) {
389 public FlagsBuilder setPresence(final boolean presence) {
398 public FlagsBuilder setStatus(final Status status) {
402 bits = STATUS_CURRENT;
405 bits = STATUS_DEPRECATED;
408 bits = STATUS_DEPRECATED;
411 throw new IllegalStateException("Unhandled status " + status);
414 flags = flags & ~MASK_STATUS | bits;
418 public FlagsBuilder setUserOrdered(final boolean userOrdered) {
420 flags |= USER_ORDERED;
422 flags &= ~USER_ORDERED;
427 public int toFlags() {