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