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.model.spi.meta;
10 import com.google.common.annotations.Beta;
11 import com.google.common.base.MoreObjects;
12 import com.google.common.base.Strings;
13 import com.google.common.collect.Collections2;
14 import com.google.common.collect.ImmutableSet;
15 import java.util.Collection;
16 import java.util.Optional;
17 import org.eclipse.jdt.annotation.NonNull;
18 import org.eclipse.jdt.annotation.NonNullByDefault;
19 import org.eclipse.jdt.annotation.Nullable;
20 import org.opendaylight.yangtools.concepts.Immutable;
21 import org.opendaylight.yangtools.concepts.Mutable;
22 import org.opendaylight.yangtools.yang.common.QName;
23 import org.opendaylight.yangtools.yang.model.api.ActionDefinition;
24 import org.opendaylight.yangtools.yang.model.api.ActionNodeContainer;
25 import org.opendaylight.yangtools.yang.model.api.AddedByUsesAware;
26 import org.opendaylight.yangtools.yang.model.api.AugmentationSchemaNode;
27 import org.opendaylight.yangtools.yang.model.api.AugmentationTarget;
28 import org.opendaylight.yangtools.yang.model.api.ConstraintMetaDefinition;
29 import org.opendaylight.yangtools.yang.model.api.ContainerLike;
30 import org.opendaylight.yangtools.yang.model.api.CopyableNode;
31 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
32 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
33 import org.opendaylight.yangtools.yang.model.api.DerivableSchemaNode;
34 import org.opendaylight.yangtools.yang.model.api.DocumentedNode;
35 import org.opendaylight.yangtools.yang.model.api.GroupingDefinition;
36 import org.opendaylight.yangtools.yang.model.api.InputSchemaNode;
37 import org.opendaylight.yangtools.yang.model.api.MandatoryAware;
38 import org.opendaylight.yangtools.yang.model.api.MustConstraintAware;
39 import org.opendaylight.yangtools.yang.model.api.MustDefinition;
40 import org.opendaylight.yangtools.yang.model.api.NotificationDefinition;
41 import org.opendaylight.yangtools.yang.model.api.NotificationNodeContainer;
42 import org.opendaylight.yangtools.yang.model.api.OperationDefinition;
43 import org.opendaylight.yangtools.yang.model.api.OutputSchemaNode;
44 import org.opendaylight.yangtools.yang.model.api.SchemaNode;
45 import org.opendaylight.yangtools.yang.model.api.SchemaNodeDefaults;
46 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
47 import org.opendaylight.yangtools.yang.model.api.Status;
48 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
49 import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
50 import org.opendaylight.yangtools.yang.model.api.UsesNode;
51 import org.opendaylight.yangtools.yang.model.api.WhenConditionAware;
52 import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement;
53 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
54 import org.opendaylight.yangtools.yang.model.api.stmt.DescriptionEffectiveStatement;
55 import org.opendaylight.yangtools.yang.model.api.stmt.ErrorAppTagEffectiveStatement;
56 import org.opendaylight.yangtools.yang.model.api.stmt.ErrorMessageEffectiveStatement;
57 import org.opendaylight.yangtools.yang.model.api.stmt.InputEffectiveStatement;
58 import org.opendaylight.yangtools.yang.model.api.stmt.OutputEffectiveStatement;
59 import org.opendaylight.yangtools.yang.model.api.stmt.ReferenceEffectiveStatement;
60 import org.opendaylight.yangtools.yang.model.api.stmt.StatusEffectiveStatement;
61 import org.opendaylight.yangtools.yang.model.api.stmt.TypedefEffectiveStatement;
62 import org.opendaylight.yangtools.yang.model.api.stmt.WhenEffectiveStatement;
63 import org.opendaylight.yangtools.yang.model.spi.meta.EffectiveStatementMixins.EffectiveStatementWithFlags.FlagsBuilder;
64 import org.opendaylight.yangtools.yang.xpath.api.YangXPathExpression.QualifiedBound;
67 * Mix-in interfaces providing services required by SchemaNode et al. These interfaces provide implementations, or
68 * implementation helpers based on default methods, so the correct behavior can be logically centralized.
71 public final class EffectiveStatementMixins {
72 // Marker interface requiring all mixins to be derived from EffectiveStatement.
73 private interface Mixin<A, D extends DeclaredStatement<A>> extends EffectiveStatement<A, D> {
74 @SuppressWarnings("unchecked")
75 default <T> @NonNull Collection<? extends @NonNull T> filterEffectiveStatements(final Class<T> type) {
76 // Yeah, this is not nice, but saves one transformation
77 return (Collection<? extends T>) Collections2.filter(effectiveSubstatements(), type::isInstance);
82 * Bridge between {@link EffectiveStatement} and {@link AugmentationTarget}.
84 * @param <A> Argument type ({@link Void} if statement does not have argument.)
85 * @param <D> Class representing declared version of this statement.
87 public interface AugmentationTargetMixin<A, D extends DeclaredStatement<A>>
88 extends Mixin<A, D>, AugmentationTarget {
90 default Collection<? extends AugmentationSchemaNode> getAvailableAugmentations() {
91 return filterEffectiveStatements(AugmentationSchemaNode.class);
96 * Bridge between {@link EffectiveStatementWithFlags} and {@link AddedByUsesAware}.
98 * @param <A> Argument type ({@link Void} if statement does not have argument.)
99 * @param <D> Class representing declared version of this statement.
101 public interface AddedByUsesMixin<A, D extends DeclaredStatement<A>>
102 extends EffectiveStatementWithFlags<A, D>, AddedByUsesAware {
104 default boolean isAddedByUses() {
105 return (flags() & FlagsBuilder.ADDED_BY_USES) != 0;
110 * Bridge between {@link EffectiveStatementWithFlags} and {@link ActionNodeContainer}.
112 * @param <A> Argument type ({@link Void} if statement does not have argument.)
113 * @param <D> Class representing declared version of this statement.
115 public interface ActionNodeContainerMixin<A, D extends DeclaredStatement<A>>
116 extends Mixin<A, D>, ActionNodeContainer {
118 default Collection<? extends ActionDefinition> getActions() {
119 return filterEffectiveStatements(ActionDefinition.class);
124 * Bridge between {@link EffectiveStatementWithFlags} and {@link NotificationNodeContainer}.
126 * @param <A> Argument type ({@link Void} if statement does not have argument.)
127 * @param <D> Class representing declared version of this statement.
129 public interface NotificationNodeContainerMixin<A, D extends DeclaredStatement<A>>
130 extends Mixin<A, D>, NotificationNodeContainer {
132 default Collection<? extends NotificationDefinition> getNotifications() {
133 return filterEffectiveStatements(NotificationDefinition.class);
138 * Bridge between {@link EffectiveStatementWithFlags} and {@link MustConstraintAware}.
140 * @param <A> Argument type ({@link Void} if statement does not have argument.)
141 * @param <D> Class representing declared version of this statement.
143 public interface MustConstraintMixin<A, D extends DeclaredStatement<A>> extends Mixin<A, D>, MustConstraintAware {
145 default Collection<? extends @NonNull MustDefinition> getMustConstraints() {
146 return filterEffectiveStatements(MustDefinition.class);
151 * Bridge between {@link EffectiveStatementWithFlags} and {@link CopyableNode}.
153 * @param <A> Argument type ({@link Void} if statement does not have argument.)
154 * @param <D> Class representing declared version of this statement.
156 public interface CopyableMixin<A, D extends DeclaredStatement<A>> extends AddedByUsesMixin<A, D>, CopyableNode {
158 default boolean isAugmenting() {
159 return (flags() & FlagsBuilder.AUGMENTING) != 0;
164 * Bridge between {@link EffectiveStatementWithFlags} and {@link DataNodeContainer}.
166 * @param <A> Argument type ({@link Void} if statement does not have argument.)
167 * @param <D> Class representing declared version of this statement.
169 public interface DataNodeContainerMixin<A, D extends DeclaredStatement<A>> extends DataNodeContainer, Mixin<A, D> {
171 default Collection<? extends TypeDefinition<?>> getTypeDefinitions() {
172 return filterTypeDefinitions(this);
176 default Collection<? extends DataSchemaNode> getChildNodes() {
177 return filterEffectiveStatements(DataSchemaNode.class);
181 default Collection<? extends GroupingDefinition> getGroupings() {
182 return filterEffectiveStatements(GroupingDefinition.class);
186 default Collection<? extends UsesNode> getUses() {
187 return filterEffectiveStatements(UsesNode.class);
192 * Bridge between {@link EffectiveStatementWithFlags} and {@link DataSchemaNode}.
194 * @param <A> Argument type ({@link Void} if statement does not have argument.)
195 * @param <D> Class representing declared version of this statement.
197 public interface DataSchemaNodeMixin<A, D extends DeclaredStatement<A>>
198 extends DataSchemaNode, CopyableMixin<A, D>, SchemaNodeMixin<A, D>, WhenConditionMixin<A, D> {
200 default Optional<Boolean> effectiveConfig() {
201 final int fl = flags() & FlagsBuilder.MASK_CONFIG;
203 case FlagsBuilder.CONFIG_FALSE:
204 return Optional.of(Boolean.FALSE);
205 case FlagsBuilder.CONFIG_TRUE:
206 return Optional.of(Boolean.TRUE);
207 case FlagsBuilder.CONFIG_UNDEF:
208 return Optional.empty();
210 throw new IllegalStateException("Unhandled effective config flags " + fl);
216 * Bridge between {@link EffectiveStatementWithFlags} and {@link DocumentedNode}.
218 * @param <A> Argument type ({@link Void} if statement does not have argument.)
219 * @param <D> Class representing declared version of this statement.
221 public interface DocumentedNodeMixin<A, D extends DeclaredStatement<A>> extends Mixin<A, D>, DocumentedNode {
223 * Bridge between {@link EffectiveStatementWithFlags} and
224 * {@link org.opendaylight.yangtools.yang.model.api.DocumentedNode.WithStatus}.
226 * @param <A> Argument type ({@link Void} if statement does not have argument.)
227 * @param <D> Class representing declared version of this statement.
229 interface WithStatus<A, D extends DeclaredStatement<A>>
230 extends EffectiveStatementWithFlags<A, D>, DocumentedNodeMixin<A, D>, DocumentedNode.WithStatus {
232 default Status getStatus() {
233 final int status = flags() & FlagsBuilder.MASK_STATUS;
235 case FlagsBuilder.STATUS_CURRENT:
236 return Status.CURRENT;
237 case FlagsBuilder.STATUS_DEPRECATED:
238 return Status.DEPRECATED;
239 case FlagsBuilder.STATUS_OBSOLETE:
240 return Status.OBSOLETE;
242 throw new IllegalStateException("Illegal status " + status);
248 default Optional<String> getDescription() {
249 return findFirstEffectiveSubstatementArgument(DescriptionEffectiveStatement.class);
253 default Optional<String> getReference() {
254 return findFirstEffectiveSubstatementArgument(ReferenceEffectiveStatement.class);
258 default Collection<? extends UnknownSchemaNode> getUnknownSchemaNodes() {
259 return filterEffectiveStatements(UnknownSchemaNode.class);
264 * Bridge between {@link EffectiveStatementWithFlags} and {@link ConstraintMetaDefinition}.
266 * @param <A> Argument type ({@link Void} if statement does not have argument.)
267 * @param <D> Class representing declared version of this statement.
269 public interface ConstraintMetaDefinitionMixin<A, D extends DeclaredStatement<A>> extends DocumentedNodeMixin<A, D>,
270 ConstraintMetaDefinition {
272 default Optional<String> getErrorAppTag() {
273 return findFirstEffectiveSubstatementArgument(ErrorAppTagEffectiveStatement.class);
277 default Optional<String> getErrorMessage() {
278 return findFirstEffectiveSubstatementArgument(ErrorMessageEffectiveStatement.class);
283 * Bridge between {@link EffectiveStatementWithFlags} and {@link MandatoryAware}.
285 * @param <A> Argument type ({@link Void} if statement does not have argument.)
286 * @param <D> Class representing declared version of this statement.
288 public interface MandatoryMixin<A, D extends DeclaredStatement<A>>
289 extends EffectiveStatementWithFlags<A, D>, MandatoryAware {
291 default boolean isMandatory() {
292 return (flags() & FlagsBuilder.MANDATORY) != 0;
297 * Bridge between {@link EffectiveStatementWithFlags} and {@code presence} statement.
299 * @param <A> Argument type ({@link Void} if statement does not have argument.)
300 * @param <D> Class representing declared version of this statement.
302 public interface PresenceMixin<A, D extends DeclaredStatement<A>> extends EffectiveStatementWithFlags<A, D> {
303 default boolean presence() {
304 return (flags() & FlagsBuilder.PRESENCE) != 0;
309 * Bridge between {@link EffectiveStatementWithFlags} and {@link SchemaNode}.
311 * @param <A> Argument type ({@link Void} if statement does not have argument.)
312 * @param <D> Class representing declared version of this statement.
314 public interface SchemaNodeMixin<A, D extends DeclaredStatement<A>>
315 extends DocumentedNodeMixin.WithStatus<A, D>, SchemaNode {
316 // FIXME: ditch all this complexity once we do not require SchemaPath
318 default QName getQName() {
319 return SchemaNodeDefaults.extractQName(pathObject());
324 default SchemaPath getPath() {
325 return SchemaNodeDefaults.extractPath(this, pathObject());
328 @NonNull Immutable pathObject();
332 * Bridge between {@link EffectiveStatementWithFlags} and {@link UnknownSchemaNode}.
334 * @param <A> Argument type ({@link Void} if statement does not have argument.)
335 * @param <D> Class representing declared version of this statement.
337 public interface UnknownSchemaNodeMixin<A, D extends DeclaredStatement<A>>
338 extends SchemaNodeMixin<A, D>, CopyableMixin<A, D>, UnknownSchemaNode {
341 default String getNodeParameter() {
342 return Strings.nullToEmpty(getDeclared().rawArgument());
347 * Bridge between {@link EffectiveStatementWithFlags} and {@code ordered-by} statement.
349 * @param <A> Argument type ({@link Void} if statement does not have argument.)
350 * @param <D> Class representing declared version of this statement.
352 public interface UserOrderedMixin<A, D extends DeclaredStatement<A>> extends EffectiveStatementWithFlags<A, D> {
353 default boolean userOrdered() {
354 return (flags() & FlagsBuilder.USER_ORDERED) != 0;
359 * Helper used to locate the effective {@code when} statement and exposing its argument as per
360 * {@link WhenConditionAware}.
362 * @param <A> Argument type ({@link Void} if statement does not have argument.)
363 * @param <D> Class representing declared version of this statement.
365 public interface WhenConditionMixin<A, D extends DeclaredStatement<A>> extends Mixin<A, D>, WhenConditionAware {
367 default Optional<QualifiedBound> getWhenCondition() {
368 return findFirstEffectiveSubstatementArgument(WhenEffectiveStatement.class);
373 * Helper bridge for operation containers ({@code input} and {@code output}).
375 * @param <D> Class representing declared version of this statement.
377 public interface OperationContainerMixin<D extends DeclaredStatement<QName>>
378 extends ContainerLike, DocumentedNodeMixin.WithStatus<QName, D>, DataNodeContainerMixin<QName, D>,
379 MustConstraintMixin<QName, D>, WhenConditionMixin<QName, D>, AugmentationTargetMixin<QName, D>,
380 SchemaNodeMixin<QName, D>, CopyableMixin<QName, D> {
382 default @NonNull QName argument() {
387 default Optional<ActionDefinition> findAction(final QName qname) {
388 return Optional.empty();
392 default Optional<NotificationDefinition> findNotification(final QName qname) {
393 return Optional.empty();
397 default Collection<? extends ActionDefinition> getActions() {
398 return ImmutableSet.of();
402 default Collection<? extends NotificationDefinition> getNotifications() {
403 return ImmutableSet.of();
407 default Optional<Boolean> effectiveConfig() {
408 return Optional.empty();
411 default String defaultToString() {
412 return MoreObjects.toStringHelper(this).add("qname", getQName()).toString();
417 * Helper bridge for {@code anydata} and {@code anyxml} opaque data.
419 * @param <D> Class representing declared version of this statement.
421 public interface OpaqueDataSchemaNodeMixin<D extends DeclaredStatement<QName>>
422 extends DerivableSchemaNode, DataSchemaNodeMixin<QName, D>, DocumentedNodeMixin.WithStatus<QName, D>,
423 MandatoryMixin<QName, D>, MustConstraintMixin<QName, D>, WhenConditionMixin<QName, D> {
425 default @NonNull QName argument() {
431 * Helper bridge for {@code rpc} and {@code action} operations.
433 * @param <D> Class representing declared version of this statement.
435 public interface OperationDefinitionMixin<D extends DeclaredStatement<QName>>
436 extends SchemaNodeMixin<QName, D>, OperationDefinition {
438 default @NonNull QName argument() {
443 default Collection<? extends @NonNull TypeDefinition<?>> getTypeDefinitions() {
444 return filterTypeDefinitions(this);
448 default Collection<? extends @NonNull GroupingDefinition> getGroupings() {
449 return filterEffectiveStatements(GroupingDefinition.class);
453 default InputSchemaNode getInput() {
454 return findAsContainer(this, InputEffectiveStatement.class, InputSchemaNode.class);
458 default OutputSchemaNode getOutput() {
459 return findAsContainer(this, OutputEffectiveStatement.class, OutputSchemaNode.class);
464 * Support interface for various mixins. Implementations are required to store 32bits worth of flags, which are
465 * globally assigned to sub-interfaces -- thus providing storage for many low-cardinality properties.
467 * @param <A> Argument type ({@link Void} if statement does not have argument.)
468 * @param <D> Class representing declared version of this statement.
470 public interface EffectiveStatementWithFlags<A, D extends DeclaredStatement<A>> extends Mixin<A, D> {
472 * Return flags associated with this statements. Flags can be built using {@link FlagsBuilder}.
474 * @return Flag field value (32 bits).
479 final class FlagsBuilder implements Mutable {
480 // We still have 23 flags remaining
481 static final int STATUS_CURRENT = 0x0001;
482 static final int STATUS_DEPRECATED = 0x0002;
483 static final int STATUS_OBSOLETE = 0x0003;
484 static final int MASK_STATUS = 0x0003;
486 static final int MANDATORY = 0x0004;
488 static final int AUGMENTING = 0x0010;
489 static final int ADDED_BY_USES = 0x0020;
490 private static final int MASK_HISTORY = 0x0030;
492 static final int USER_ORDERED = 0x0040;
493 static final int PRESENCE = 0x0080;
495 static final int CONFIG_UNDEF = 0x0100;
496 static final int CONFIG_FALSE = 0x0200;
497 static final int CONFIG_TRUE = 0x0300;
498 static final int MASK_CONFIG = CONFIG_TRUE;
502 public FlagsBuilder setConfiguration(final @Nullable Boolean config) {
504 if (config != null) {
505 fl = config ? CONFIG_TRUE : CONFIG_FALSE;
509 flags = flags & ~MASK_CONFIG | fl;
513 public FlagsBuilder setHistory(final CopyableNode history) {
514 flags = flags & ~MASK_HISTORY
515 | (history.isAugmenting() ? AUGMENTING : 0) | (history.isAddedByUses() ? ADDED_BY_USES : 0);
519 public FlagsBuilder setMandatory(final boolean mandatory) {
528 public FlagsBuilder setPresence(final boolean presence) {
537 public FlagsBuilder setStatus(final Status status) {
541 bits = STATUS_CURRENT;
544 bits = STATUS_DEPRECATED;
547 bits = STATUS_OBSOLETE;
550 throw new IllegalStateException("Unhandled status " + status);
553 flags = flags & ~MASK_STATUS | bits;
557 public FlagsBuilder setUserOrdered(final boolean userOrdered) {
559 flags |= USER_ORDERED;
561 flags &= ~USER_ORDERED;
566 public int toFlags() {
572 private EffectiveStatementMixins() {
575 static <T extends ContainerLike> T findAsContainer(final EffectiveStatement<?, ?> stmt,
576 final Class<? extends EffectiveStatement<QName, ?>> type, final Class<T> target) {
577 return target.cast(stmt.findFirstEffectiveSubstatement(type).get());
580 static Collection<? extends @NonNull TypeDefinition<?>> filterTypeDefinitions(final Mixin<?, ?> stmt) {
581 return Collections2.transform(stmt.filterEffectiveStatements(TypedefEffectiveStatement.class),
582 TypedefEffectiveStatement::getTypeDefinition);
585 public static int historyAndStatusFlags(final CopyableNode history,
586 final Collection<? extends EffectiveStatement<?, ?>> substatements) {
587 return new FlagsBuilder()
589 .setStatus(substatements.stream()
590 .filter(StatusEffectiveStatement.class::isInstance)
592 .map(stmt -> ((StatusEffectiveStatement) stmt).argument())
593 .orElse(Status.CURRENT))