Refactor ContainerEffectiveStatementImpl
[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 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;
17 import java.util.Set;
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;
51
52 /**
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.
55  */
56 @Beta
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);
64         }
65
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));
69         }
70
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));
74         }
75     }
76
77     /**
78      * Bridge between {@link EffectiveStatement} and {@link AugmentationTarget}.
79      *
80      * @param <A> Argument type ({@link Void} if statement does not have argument.)
81      * @param <D> Class representing declared version of this statement.
82      */
83     public interface AugmentationTargetMixin<A, D extends DeclaredStatement<A>>
84             extends Mixin<A, D>, AugmentationTarget {
85         @Override
86         default Set<AugmentationSchemaNode> getAvailableAugmentations() {
87             return filterEffectiveStatementsSet(AugmentationSchemaNode.class);
88         }
89     }
90
91     /**
92      * Bridge between {@link EffectiveStatementWithFlags} and {@link AddedByUsesAware}.
93      *
94      * @param <A> Argument type ({@link Void} if statement does not have argument.)
95      * @param <D> Class representing declared version of this statement.
96      */
97     public interface AddedByUsesMixin<A, D extends DeclaredStatement<A>>
98             extends EffectiveStatementWithFlags<A, D>, AddedByUsesAware {
99         @Override
100         default boolean isAddedByUses() {
101             return (flags() & FlagsBuilder.ADDED_BY_USES) != 0;
102         }
103     }
104
105     /**
106      * Bridge between {@link EffectiveStatementWithFlags} and {@link ActionNodeContainer}.
107      *
108      * @param <A> Argument type ({@link Void} if statement does not have argument.)
109      * @param <D> Class representing declared version of this statement.
110      */
111     public interface ActionNodeContainerMixin<A, D extends DeclaredStatement<A>>
112             extends Mixin<A, D>, ActionNodeContainer {
113         @Override
114         default Set<ActionDefinition> getActions() {
115             return filterEffectiveStatementsSet(ActionDefinition.class);
116         }
117     }
118
119     /**
120      * Bridge between {@link EffectiveStatementWithFlags} and {@link NotificationNodeContainer}.
121      *
122      * @param <A> Argument type ({@link Void} if statement does not have argument.)
123      * @param <D> Class representing declared version of this statement.
124      */
125     public interface NotificationNodeContainerMixin<A, D extends DeclaredStatement<A>>
126             extends Mixin<A, D>, NotificationNodeContainer {
127         @Override
128         default Set<NotificationDefinition> getNotifications() {
129             return filterEffectiveStatementsSet(NotificationDefinition.class);
130         }
131     }
132
133     /**
134      * Bridge between {@link EffectiveStatementWithFlags} and {@link MustConstraintAware}.
135      *
136      * @param <A> Argument type ({@link Void} if statement does not have argument.)
137      * @param <D> Class representing declared version of this statement.
138      */
139     public interface MustConstraintMixin<A, D extends DeclaredStatement<A>> extends Mixin<A, D>, MustConstraintAware {
140         @Override
141         default Collection<MustDefinition> getMustConstraints() {
142             return filterEffectiveStatements(MustDefinition.class);
143         }
144     }
145
146     /**
147      * Bridge between {@link EffectiveStatementWithFlags} and {@link CopyableNode}.
148      *
149      * @param <A> Argument type ({@link Void} if statement does not have argument.)
150      * @param <D> Class representing declared version of this statement.
151      */
152     public interface CopyableMixin<A, D extends DeclaredStatement<A>> extends AddedByUsesMixin<A, D>, CopyableNode {
153         @Override
154         default boolean isAugmenting() {
155             return (flags() & FlagsBuilder.AUGMENTING) != 0;
156         }
157     }
158
159     /**
160      * Bridge between {@link EffectiveStatementWithFlags} and {@link DataNodeContainer}.
161      *
162      * @param <A> Argument type ({@link Void} if statement does not have argument.)
163      * @param <D> Class representing declared version of this statement.
164      */
165     public interface DataNodeContainerMixin<A, D extends DeclaredStatement<A>> extends DataNodeContainer, Mixin<A, D> {
166         @Override
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());
172         }
173
174         @Override
175         default Collection<DataSchemaNode> getChildNodes() {
176             return filterEffectiveStatements(DataSchemaNode.class);
177         }
178
179         @Override
180         default Set<GroupingDefinition> getGroupings() {
181             return filterEffectiveStatementsSet(GroupingDefinition.class);
182         }
183
184         @Override
185         default Set<UsesNode> getUses() {
186             return filterEffectiveStatementsSet(UsesNode.class);
187         }
188     }
189
190     /**
191      * Bridge between {@link EffectiveStatementWithFlags} and {@link DataSchemaNode}.
192      *
193      * @param <A> Argument type ({@link Void} if statement does not have argument.)
194      * @param <D> Class representing declared version of this statement.
195      */
196     public interface DataSchemaNodeMixin<A, D extends DeclaredStatement<A>>
197             extends DataSchemaNode, CopyableMixin<A, D>, SchemaNodeMixin<A, D>, WhenConditionMixin<A, D> {
198         @Override
199         default boolean isConfiguration() {
200             return (flags() & FlagsBuilder.CONFIGURATION) != 0;
201         }
202     }
203
204     /**
205      * Bridge between {@link EffectiveStatementWithFlags} and {@link DocumentedNode}.
206      *
207      * @param <A> Argument type ({@link Void} if statement does not have argument.)
208      * @param <D> Class representing declared version of this statement.
209      */
210     public interface DocumentedNodeMixin<A, D extends DeclaredStatement<A>> extends Mixin<A, D>, DocumentedNode {
211         /**
212          * Bridge between {@link EffectiveStatementWithFlags} and
213          * {@link org.opendaylight.yangtools.yang.model.api.DocumentedNode.WithStatus}.
214          *
215          * @param <A> Argument type ({@link Void} if statement does not have argument.)
216          * @param <D> Class representing declared version of this statement.
217          */
218         interface WithStatus<A, D extends DeclaredStatement<A>>
219                 extends EffectiveStatementWithFlags<A, D>, DocumentedNodeMixin<A, D>, DocumentedNode.WithStatus {
220             @Override
221             default Status getStatus() {
222                 final int status = flags() & FlagsBuilder.MASK_STATUS;
223                 switch (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;
230                     default:
231                         throw new IllegalStateException("Illegal status " + status);
232                 }
233             }
234         }
235
236         @Override
237         default Optional<String> getDescription() {
238             return findFirstEffectiveSubstatementArgument(DescriptionEffectiveStatement.class);
239         }
240
241         @Override
242         default Optional<String> getReference() {
243             return findFirstEffectiveSubstatementArgument(ReferenceEffectiveStatement.class);
244         }
245
246         @Override
247         default List<UnknownSchemaNode> getUnknownSchemaNodes() {
248             return filterEffectiveStatementsList(UnknownSchemaNode.class);
249         }
250     }
251
252     /**
253      * Bridge between {@link EffectiveStatementWithFlags} and {@link MandatoryAware}.
254      *
255      * @param <A> Argument type ({@link Void} if statement does not have argument.)
256      * @param <D> Class representing declared version of this statement.
257      */
258     public interface MandatoryMixin<A, D extends DeclaredStatement<A>>
259             extends EffectiveStatementWithFlags<A, D>, MandatoryAware {
260         @Override
261         default boolean isMandatory() {
262             return (flags() & FlagsBuilder.MANDATORY) != 0;
263         }
264     }
265
266     /**
267      * Bridge between {@link EffectiveStatementWithFlags} and {@code presence} statement.
268      *
269      * @param <A> Argument type ({@link Void} if statement does not have argument.)
270      * @param <D> Class representing declared version of this statement.
271      */
272     public interface PresenceMixin<A, D extends DeclaredStatement<A>> extends EffectiveStatementWithFlags<A, D> {
273         default boolean presence() {
274             return (flags() & FlagsBuilder.PRESENCE) != 0;
275         }
276     }
277
278     /**
279      * Bridge between {@link EffectiveStatementWithFlags} and {@link SchemaNode}.
280      *
281      * @param <A> Argument type ({@link Void} if statement does not have argument.)
282      * @param <D> Class representing declared version of this statement.
283      */
284     public interface SchemaNodeMixin<A, D extends DeclaredStatement<A>>
285             extends DocumentedNodeMixin.WithStatus<A, D>, SchemaNode {
286         @Override
287         default QName getQName() {
288             return getPath().getLastComponent();
289         }
290     }
291
292     /**
293      * Bridge between {@link EffectiveStatementWithFlags} and {@code ordered-by} statement.
294      *
295      * @param <A> Argument type ({@link Void} if statement does not have argument.)
296      * @param <D> Class representing declared version of this statement.
297      */
298     public interface UserOrderedMixin<A, D extends DeclaredStatement<A>> extends EffectiveStatementWithFlags<A, D> {
299         default boolean userOrdered() {
300             return (flags() & FlagsBuilder.USER_ORDERED) != 0;
301         }
302     }
303
304     /**
305      * Helper used to locate the effective {@code when} statement and exposing its argument as per
306      * {@link WhenConditionAware}.
307      *
308      * @param <A> Argument type ({@link Void} if statement does not have argument.)
309      * @param <D> Class representing declared version of this statement.
310      */
311     public interface WhenConditionMixin<A, D extends DeclaredStatement<A>> extends Mixin<A, D>, WhenConditionAware {
312         @Override
313         default Optional<RevisionAwareXPath> getWhenCondition() {
314             return findFirstEffectiveSubstatementArgument(WhenEffectiveStatement.class);
315         }
316     }
317
318     /**
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.
321      *
322      * @param <A> Argument type ({@link Void} if statement does not have argument.)
323      * @param <D> Class representing declared version of this statement.
324      */
325     public interface EffectiveStatementWithFlags<A, D extends DeclaredStatement<A>> extends Mixin<A, D> {
326         /**
327          * Return flags assicated with this statements. Flags can be built using {@link FlagsBuilder}.
328          *
329          * @return Flag field value (32 bits).
330          */
331         int flags();
332
333         @NonNullByDefault
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;
340
341             static final int CONFIGURATION        = 0x0004;
342             static final int MANDATORY            = 0x0008;
343
344             static final int AUGMENTING           = 0x0010;
345             static final int ADDED_BY_USES        = 0x0020;
346             private static final int MASK_HISTORY = 0x0030;
347
348             static final int USER_ORDERED         = 0x0040;
349             static final int PRESENCE             = 0x0080;
350
351             private int flags;
352
353             public FlagsBuilder setConfiguration(final boolean config) {
354                 if (config) {
355                     flags |= CONFIGURATION;
356                 } else {
357                     flags &= ~CONFIGURATION;
358                 }
359                 return this;
360             }
361
362             public FlagsBuilder setHistory(final CopyHistory history) {
363                 int bits;
364                 if (history.contains(CopyType.ADDED_BY_USES_AUGMENTATION)) {
365                     bits = AUGMENTING | ADDED_BY_USES;
366                 } else {
367                     bits = 0;
368                     if (history.contains(CopyType.ADDED_BY_AUGMENTATION)) {
369                         bits |= AUGMENTING;
370                     }
371                     if (history.contains(CopyType.ADDED_BY_USES)) {
372                         bits |= ADDED_BY_USES;
373                     }
374                 }
375
376                 flags = flags & ~MASK_HISTORY | bits;
377                 return this;
378             }
379
380             public FlagsBuilder setMandatory(final boolean mandatory) {
381                 if (mandatory) {
382                     flags |= MANDATORY;
383                 } else {
384                     flags &= ~MANDATORY;
385                 }
386                 return this;
387             }
388
389             public FlagsBuilder setPresence(final boolean presence) {
390                 if (presence) {
391                     flags |= PRESENCE;
392                 } else {
393                     flags &= ~PRESENCE;
394                 }
395                 return this;
396             }
397
398             public FlagsBuilder setStatus(final Status status) {
399                 final int bits;
400                 switch (status) {
401                     case CURRENT:
402                         bits = STATUS_CURRENT;
403                         break;
404                     case DEPRECATED:
405                         bits = STATUS_DEPRECATED;
406                         break;
407                     case OBSOLETE:
408                         bits = STATUS_DEPRECATED;
409                         break;
410                     default:
411                         throw new IllegalStateException("Unhandled status " + status);
412                 }
413
414                 flags = flags & ~MASK_STATUS | bits;
415                 return this;
416             }
417
418             public FlagsBuilder setUserOrdered(final boolean userOrdered) {
419                 if (userOrdered) {
420                     flags |= USER_ORDERED;
421                 } else {
422                     flags &= ~USER_ORDERED;
423                 }
424                 return this;
425             }
426
427             public int toFlags() {
428                 return flags;
429             }
430         }
431     }
432 }