Cleanup getQName() mixin interplay
[yangtools.git] / yang / yang-parser-rfc7950 / src / main / java / org / opendaylight / yangtools / yang / parser / rfc7950 / stmt / EffectiveStatementMixins.java
1 /*
2  * Copyright (c) 2020 PANTHEON.tech, s.r.o. 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;
9
10 import static com.google.common.base.Verify.verify;
11
12 import com.google.common.annotations.Beta;
13 import com.google.common.base.MoreObjects;
14 import com.google.common.collect.Collections2;
15 import com.google.common.collect.ImmutableList;
16 import com.google.common.collect.ImmutableSet;
17 import java.util.Collection;
18 import java.util.List;
19 import java.util.Optional;
20 import java.util.Set;
21 import org.eclipse.jdt.annotation.NonNull;
22 import org.eclipse.jdt.annotation.NonNullByDefault;
23 import org.opendaylight.yangtools.concepts.Mutable;
24 import org.opendaylight.yangtools.yang.common.QName;
25 import org.opendaylight.yangtools.yang.model.api.ActionDefinition;
26 import org.opendaylight.yangtools.yang.model.api.ActionNodeContainer;
27 import org.opendaylight.yangtools.yang.model.api.AddedByUsesAware;
28 import org.opendaylight.yangtools.yang.model.api.AugmentationSchemaNode;
29 import org.opendaylight.yangtools.yang.model.api.AugmentationTarget;
30 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
31 import org.opendaylight.yangtools.yang.model.api.CopyableNode;
32 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
33 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
34 import org.opendaylight.yangtools.yang.model.api.DerivableSchemaNode;
35 import org.opendaylight.yangtools.yang.model.api.DocumentedNode;
36 import org.opendaylight.yangtools.yang.model.api.GroupingDefinition;
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.RevisionAwareXPath;
44 import org.opendaylight.yangtools.yang.model.api.SchemaNode;
45 import org.opendaylight.yangtools.yang.model.api.Status;
46 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
47 import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
48 import org.opendaylight.yangtools.yang.model.api.UsesNode;
49 import org.opendaylight.yangtools.yang.model.api.WhenConditionAware;
50 import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement;
51 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
52 import org.opendaylight.yangtools.yang.model.api.stmt.DescriptionEffectiveStatement;
53 import org.opendaylight.yangtools.yang.model.api.stmt.InputEffectiveStatement;
54 import org.opendaylight.yangtools.yang.model.api.stmt.OutputEffectiveStatement;
55 import org.opendaylight.yangtools.yang.model.api.stmt.ReferenceEffectiveStatement;
56 import org.opendaylight.yangtools.yang.model.api.stmt.TypedefEffectiveStatement;
57 import org.opendaylight.yangtools.yang.model.api.stmt.WhenEffectiveStatement;
58 import org.opendaylight.yangtools.yang.parser.spi.meta.CopyHistory;
59 import org.opendaylight.yangtools.yang.parser.spi.meta.CopyType;
60
61 /**
62  * Mix-in interfaces providing services required by SchemaNode et al. These interfaces provide implementations, or
63  * implementation helpers based on default methods, so the correct behavior can be logically centralized.
64  */
65 @Beta
66 public final class EffectiveStatementMixins {
67     // Marker interface requiring all mixins to be derived from EffectiveStatement.
68     private interface Mixin<A, D extends DeclaredStatement<A>> extends EffectiveStatement<A, D> {
69         @SuppressWarnings("unchecked")
70         default <T> Collection<T> filterEffectiveStatements(final Class<T> type) {
71             // Yeah, this is not nice, but saves one transformation
72             return (Collection<T>) Collections2.filter(effectiveSubstatements(), type::isInstance);
73         }
74
75         // FIXME: YANGTOOLS-1068: eliminate this once we can return collections
76         default <T> List<T> filterEffectiveStatementsList(final Class<T> type) {
77             return ImmutableList.copyOf(filterEffectiveStatements(type));
78         }
79
80         // FIXME: YANGTOOLS-1068: eliminate this once we can return collections
81         default <T> Set<T> filterEffectiveStatementsSet(final Class<T> type) {
82             return ImmutableSet.copyOf(filterEffectiveStatements(type));
83         }
84     }
85
86     /**
87      * Bridge between {@link EffectiveStatement} and {@link AugmentationTarget}.
88      *
89      * @param <A> Argument type ({@link Void} if statement does not have argument.)
90      * @param <D> Class representing declared version of this statement.
91      */
92     public interface AugmentationTargetMixin<A, D extends DeclaredStatement<A>>
93             extends Mixin<A, D>, AugmentationTarget {
94         @Override
95         default Set<AugmentationSchemaNode> getAvailableAugmentations() {
96             return filterEffectiveStatementsSet(AugmentationSchemaNode.class);
97         }
98     }
99
100     /**
101      * Bridge between {@link EffectiveStatementWithFlags} and {@link AddedByUsesAware}.
102      *
103      * @param <A> Argument type ({@link Void} if statement does not have argument.)
104      * @param <D> Class representing declared version of this statement.
105      */
106     public interface AddedByUsesMixin<A, D extends DeclaredStatement<A>>
107             extends EffectiveStatementWithFlags<A, D>, AddedByUsesAware {
108         @Override
109         default boolean isAddedByUses() {
110             return (flags() & FlagsBuilder.ADDED_BY_USES) != 0;
111         }
112     }
113
114     /**
115      * Bridge between {@link EffectiveStatementWithFlags} and {@link ActionNodeContainer}.
116      *
117      * @param <A> Argument type ({@link Void} if statement does not have argument.)
118      * @param <D> Class representing declared version of this statement.
119      */
120     public interface ActionNodeContainerMixin<A, D extends DeclaredStatement<A>>
121             extends Mixin<A, D>, ActionNodeContainer {
122         @Override
123         default Set<ActionDefinition> getActions() {
124             return filterEffectiveStatementsSet(ActionDefinition.class);
125         }
126     }
127
128     /**
129      * Bridge between {@link EffectiveStatementWithFlags} and {@link NotificationNodeContainer}.
130      *
131      * @param <A> Argument type ({@link Void} if statement does not have argument.)
132      * @param <D> Class representing declared version of this statement.
133      */
134     public interface NotificationNodeContainerMixin<A, D extends DeclaredStatement<A>>
135             extends Mixin<A, D>, NotificationNodeContainer {
136         @Override
137         default Set<NotificationDefinition> getNotifications() {
138             return filterEffectiveStatementsSet(NotificationDefinition.class);
139         }
140     }
141
142     /**
143      * Bridge between {@link EffectiveStatementWithFlags} and {@link MustConstraintAware}.
144      *
145      * @param <A> Argument type ({@link Void} if statement does not have argument.)
146      * @param <D> Class representing declared version of this statement.
147      */
148     public interface MustConstraintMixin<A, D extends DeclaredStatement<A>> extends Mixin<A, D>, MustConstraintAware {
149         @Override
150         default Collection<MustDefinition> getMustConstraints() {
151             return filterEffectiveStatements(MustDefinition.class);
152         }
153     }
154
155     /**
156      * Bridge between {@link EffectiveStatementWithFlags} and {@link CopyableNode}.
157      *
158      * @param <A> Argument type ({@link Void} if statement does not have argument.)
159      * @param <D> Class representing declared version of this statement.
160      */
161     public interface CopyableMixin<A, D extends DeclaredStatement<A>> extends AddedByUsesMixin<A, D>, CopyableNode {
162         @Override
163         default boolean isAugmenting() {
164             return (flags() & FlagsBuilder.AUGMENTING) != 0;
165         }
166     }
167
168     /**
169      * Bridge between {@link EffectiveStatementWithFlags} and {@link DataNodeContainer}.
170      *
171      * @param <A> Argument type ({@link Void} if statement does not have argument.)
172      * @param <D> Class representing declared version of this statement.
173      */
174     public interface DataNodeContainerMixin<A, D extends DeclaredStatement<A>> extends DataNodeContainer, Mixin<A, D> {
175         @Override
176         default Set<TypeDefinition<?>> getTypeDefinitions() {
177             return filterTypeDefinitions(this);
178         }
179
180         @Override
181         default Collection<DataSchemaNode> getChildNodes() {
182             return filterEffectiveStatements(DataSchemaNode.class);
183         }
184
185         @Override
186         default Set<GroupingDefinition> getGroupings() {
187             return filterEffectiveStatementsSet(GroupingDefinition.class);
188         }
189
190         @Override
191         default Set<UsesNode> getUses() {
192             return filterEffectiveStatementsSet(UsesNode.class);
193         }
194     }
195
196     /**
197      * Bridge between {@link EffectiveStatementWithFlags} and {@link DataSchemaNode}.
198      *
199      * @param <A> Argument type ({@link Void} if statement does not have argument.)
200      * @param <D> Class representing declared version of this statement.
201      */
202     public interface DataSchemaNodeMixin<A, D extends DeclaredStatement<A>>
203             extends DataSchemaNode, CopyableMixin<A, D>, SchemaNodeMixin<A, D>, WhenConditionMixin<A, D> {
204         @Override
205         default boolean isConfiguration() {
206             return (flags() & FlagsBuilder.CONFIGURATION) != 0;
207         }
208     }
209
210     /**
211      * Bridge between {@link EffectiveStatementWithFlags} and {@link DocumentedNode}.
212      *
213      * @param <A> Argument type ({@link Void} if statement does not have argument.)
214      * @param <D> Class representing declared version of this statement.
215      */
216     public interface DocumentedNodeMixin<A, D extends DeclaredStatement<A>> extends Mixin<A, D>, DocumentedNode {
217         /**
218          * Bridge between {@link EffectiveStatementWithFlags} and
219          * {@link org.opendaylight.yangtools.yang.model.api.DocumentedNode.WithStatus}.
220          *
221          * @param <A> Argument type ({@link Void} if statement does not have argument.)
222          * @param <D> Class representing declared version of this statement.
223          */
224         interface WithStatus<A, D extends DeclaredStatement<A>>
225                 extends EffectiveStatementWithFlags<A, D>, DocumentedNodeMixin<A, D>, DocumentedNode.WithStatus {
226             @Override
227             default Status getStatus() {
228                 final int status = flags() & FlagsBuilder.MASK_STATUS;
229                 switch (status) {
230                     case FlagsBuilder.STATUS_CURRENT:
231                         return Status.CURRENT;
232                     case FlagsBuilder.STATUS_DEPRECATED:
233                         return Status.DEPRECATED;
234                     case FlagsBuilder.STATUS_OBSOLETE:
235                         return Status.OBSOLETE;
236                     default:
237                         throw new IllegalStateException("Illegal status " + status);
238                 }
239             }
240         }
241
242         @Override
243         default Optional<String> getDescription() {
244             return findFirstEffectiveSubstatementArgument(DescriptionEffectiveStatement.class);
245         }
246
247         @Override
248         default Optional<String> getReference() {
249             return findFirstEffectiveSubstatementArgument(ReferenceEffectiveStatement.class);
250         }
251
252         @Override
253         default List<UnknownSchemaNode> getUnknownSchemaNodes() {
254             return filterEffectiveStatementsList(UnknownSchemaNode.class);
255         }
256     }
257
258     /**
259      * Bridge between {@link EffectiveStatementWithFlags} and {@link MandatoryAware}.
260      *
261      * @param <A> Argument type ({@link Void} if statement does not have argument.)
262      * @param <D> Class representing declared version of this statement.
263      */
264     public interface MandatoryMixin<A, D extends DeclaredStatement<A>>
265             extends EffectiveStatementWithFlags<A, D>, MandatoryAware {
266         @Override
267         default boolean isMandatory() {
268             return (flags() & FlagsBuilder.MANDATORY) != 0;
269         }
270     }
271
272     /**
273      * Bridge between {@link EffectiveStatementWithFlags} and {@code presence} statement.
274      *
275      * @param <A> Argument type ({@link Void} if statement does not have argument.)
276      * @param <D> Class representing declared version of this statement.
277      */
278     public interface PresenceMixin<A, D extends DeclaredStatement<A>> extends EffectiveStatementWithFlags<A, D> {
279         default boolean presence() {
280             return (flags() & FlagsBuilder.PRESENCE) != 0;
281         }
282     }
283
284     /**
285      * Bridge between {@link EffectiveStatementWithFlags} and {@link SchemaNode}.
286      *
287      * @param <A> Argument type ({@link Void} if statement does not have argument.)
288      * @param <D> Class representing declared version of this statement.
289      */
290     public interface SchemaNodeMixin<A, D extends DeclaredStatement<A>>
291             extends DocumentedNodeMixin.WithStatus<A, D>, SchemaNode {
292         @Override
293         default QName getQName() {
294             return getPath().getLastComponent();
295         }
296     }
297
298     /**
299      * Bridge between {@link EffectiveStatementWithFlags} and {@code ordered-by} statement.
300      *
301      * @param <A> Argument type ({@link Void} if statement does not have argument.)
302      * @param <D> Class representing declared version of this statement.
303      */
304     public interface UserOrderedMixin<A, D extends DeclaredStatement<A>> extends EffectiveStatementWithFlags<A, D> {
305         default boolean userOrdered() {
306             return (flags() & FlagsBuilder.USER_ORDERED) != 0;
307         }
308     }
309
310     /**
311      * Helper used to locate the effective {@code when} statement and exposing its argument as per
312      * {@link WhenConditionAware}.
313      *
314      * @param <A> Argument type ({@link Void} if statement does not have argument.)
315      * @param <D> Class representing declared version of this statement.
316      */
317     public interface WhenConditionMixin<A, D extends DeclaredStatement<A>> extends Mixin<A, D>, WhenConditionAware {
318         @Override
319         default Optional<RevisionAwareXPath> getWhenCondition() {
320             return findFirstEffectiveSubstatementArgument(WhenEffectiveStatement.class);
321         }
322     }
323
324     /**
325      * Helper bridge for operation containers ({@code input} and {@code output}).
326      *
327      * @param <D> Class representing declared version of this statement.
328      */
329     public interface OperationContainerMixin<D extends DeclaredStatement<QName>>
330             extends ContainerSchemaNode, DocumentedNodeMixin.WithStatus<QName, D>, DataNodeContainerMixin<QName, D>,
331                     MustConstraintMixin<QName, D>, WhenConditionMixin<QName, D>, AugmentationTargetMixin<QName, D>,
332                     SchemaNodeMixin<QName, D>, CopyableMixin<QName, D> {
333         @Override
334         default @NonNull QName argument() {
335             return getQName();
336         }
337
338         @Override
339         default Optional<ActionDefinition> findAction(final QName qname) {
340             return Optional.empty();
341         }
342
343         @Override
344         default Optional<NotificationDefinition> findNotification(final QName qname) {
345             return Optional.empty();
346         }
347
348         @Override
349         default Set<ActionDefinition> getActions() {
350             return ImmutableSet.of();
351         }
352
353         @Override
354         default Set<NotificationDefinition> getNotifications() {
355             return ImmutableSet.of();
356         }
357
358         @Override
359         default boolean isConfiguration() {
360             return false;
361         }
362
363         @Override
364         default boolean isPresenceContainer() {
365             // FIXME: this should not really be here
366             return false;
367         }
368
369         default String defaultToString() {
370             return MoreObjects.toStringHelper(this).add("path", getPath()).toString();
371         }
372     }
373
374     /**
375      * Helper bridge for {@code anydata} and {@code anyxml} opaque data.
376      *
377      * @param <D> Class representing declared version of this statement.
378      */
379     public interface OpaqueDataSchemaNodeMixin<D extends DeclaredStatement<QName>>
380             extends DerivableSchemaNode, DataSchemaNodeMixin<QName, D>, DocumentedNodeMixin.WithStatus<QName, D>,
381                     MandatoryMixin<QName, D>, MustConstraintMixin<QName, D>, WhenConditionMixin<QName, D> {
382         @Override
383         default @NonNull QName argument() {
384             return getQName();
385         }
386     }
387
388     /**
389      * Helper bridge for {@code rpc} and {@code action} operations.
390      *
391      * @param <D> Class representing declared version of this statement.
392      */
393     public interface OperationDefinitionMixin<D extends DeclaredStatement<QName>>
394             extends SchemaNodeMixin<QName, D>, OperationDefinition {
395         @Override
396         default @NonNull QName argument() {
397             return getQName();
398         }
399
400         @Override
401         default Set<TypeDefinition<?>> getTypeDefinitions() {
402             return filterTypeDefinitions(this);
403         }
404
405         @Override
406         default Set<GroupingDefinition> getGroupings() {
407             return filterEffectiveStatementsSet(GroupingDefinition.class);
408         }
409
410         @Override
411         default ContainerSchemaNode getInput() {
412             return findAsContainer(this, InputEffectiveStatement.class);
413         }
414
415         @Override
416         default ContainerSchemaNode getOutput() {
417             return findAsContainer(this, OutputEffectiveStatement.class);
418         }
419     }
420
421     /**
422      * Support interface for various mixins. Implementations are required to store 32bits worth of flags, which are
423      * globally assigned to sub-interfaces -- thus providing storage for many low-cardinality properties.
424      *
425      * @param <A> Argument type ({@link Void} if statement does not have argument.)
426      * @param <D> Class representing declared version of this statement.
427      */
428     public interface EffectiveStatementWithFlags<A, D extends DeclaredStatement<A>> extends Mixin<A, D> {
429         /**
430          * Return flags assicated with this statements. Flags can be built using {@link FlagsBuilder}.
431          *
432          * @return Flag field value (32 bits).
433          */
434         int flags();
435
436         @NonNullByDefault
437         final class FlagsBuilder implements Mutable {
438             // We still have 24 flags remaining
439             static final int STATUS_CURRENT       = 0x0001;
440             static final int STATUS_DEPRECATED    = 0x0002;
441             static final int STATUS_OBSOLETE      = 0x0003;
442             static final int MASK_STATUS          = 0x0003;
443
444             static final int CONFIGURATION        = 0x0004;
445             static final int MANDATORY            = 0x0008;
446
447             static final int AUGMENTING           = 0x0010;
448             static final int ADDED_BY_USES        = 0x0020;
449             private static final int MASK_HISTORY = 0x0030;
450
451             static final int USER_ORDERED         = 0x0040;
452             static final int PRESENCE             = 0x0080;
453
454             private int flags;
455
456             public FlagsBuilder setConfiguration(final boolean config) {
457                 if (config) {
458                     flags |= CONFIGURATION;
459                 } else {
460                     flags &= ~CONFIGURATION;
461                 }
462                 return this;
463             }
464
465             public FlagsBuilder setHistory(final CopyHistory history) {
466                 int bits;
467                 if (history.contains(CopyType.ADDED_BY_USES_AUGMENTATION)) {
468                     bits = AUGMENTING | ADDED_BY_USES;
469                 } else {
470                     bits = 0;
471                     if (history.contains(CopyType.ADDED_BY_AUGMENTATION)) {
472                         bits |= AUGMENTING;
473                     }
474                     if (history.contains(CopyType.ADDED_BY_USES)) {
475                         bits |= ADDED_BY_USES;
476                     }
477                 }
478
479                 flags = flags & ~MASK_HISTORY | bits;
480                 return this;
481             }
482
483             public FlagsBuilder setMandatory(final boolean mandatory) {
484                 if (mandatory) {
485                     flags |= MANDATORY;
486                 } else {
487                     flags &= ~MANDATORY;
488                 }
489                 return this;
490             }
491
492             public FlagsBuilder setPresence(final boolean presence) {
493                 if (presence) {
494                     flags |= PRESENCE;
495                 } else {
496                     flags &= ~PRESENCE;
497                 }
498                 return this;
499             }
500
501             public FlagsBuilder setStatus(final Status status) {
502                 final int bits;
503                 switch (status) {
504                     case CURRENT:
505                         bits = STATUS_CURRENT;
506                         break;
507                     case DEPRECATED:
508                         bits = STATUS_DEPRECATED;
509                         break;
510                     case OBSOLETE:
511                         bits = STATUS_OBSOLETE;
512                         break;
513                     default:
514                         throw new IllegalStateException("Unhandled status " + status);
515                 }
516
517                 flags = flags & ~MASK_STATUS | bits;
518                 return this;
519             }
520
521             public FlagsBuilder setUserOrdered(final boolean userOrdered) {
522                 if (userOrdered) {
523                     flags |= USER_ORDERED;
524                 } else {
525                     flags &= ~USER_ORDERED;
526                 }
527                 return this;
528             }
529
530             public int toFlags() {
531                 return flags;
532             }
533         }
534     }
535
536     private EffectiveStatementMixins() {
537     }
538
539     static ContainerSchemaNode findAsContainer(final EffectiveStatement<?, ?> stmt,
540             final Class<? extends EffectiveStatement<QName, ?>> type) {
541         final EffectiveStatement<?, ?> statement = stmt.findFirstEffectiveSubstatement(type).get();
542         verify(statement instanceof ContainerSchemaNode, "Child statement %s is not a ContainerSchemaNode",
543             statement);
544         return (ContainerSchemaNode) statement;
545     }
546
547     static Set<TypeDefinition<?>> filterTypeDefinitions(final Mixin<?, ?> stmt) {
548         // TODO: the cast here is needed to work around Java 11 javac type inference issue
549         return (Set) stmt.effectiveSubstatements().stream()
550                 .filter(TypedefEffectiveStatement.class::isInstance)
551                 .map(typedef -> ((TypedefEffectiveStatement) typedef).getTypeDefinition())
552                 .collect(ImmutableSet.toImmutableSet());
553     }
554 }