1584c90d12d7ac45f6cf5506d06e05857c0ed71b
[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 {@link SchemaNode}.
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 SchemaNodeMixin<A, D extends DeclaredStatement<A>>
273             extends DocumentedNodeMixin.WithStatus<A, D>, SchemaNode {
274         @Override
275         default QName getQName() {
276             return getPath().getLastComponent();
277         }
278     }
279
280     /**
281      * Bridge between {@link EffectiveStatementWithFlags} and {@code ordered-by} statement.
282      *
283      * @param <A> Argument type ({@link Void} if statement does not have argument.)
284      * @param <D> Class representing declared version of this statement.
285      */
286     public interface UserOrderedMixin<A, D extends DeclaredStatement<A>> extends EffectiveStatementWithFlags<A, D> {
287         default boolean userOrdered() {
288             return (flags() & FlagsBuilder.USER_ORDERED) != 0;
289         }
290     }
291
292     /**
293      * Helper used to locate the effective {@code when} statement and exposing its argument as per
294      * {@link WhenConditionAware}.
295      *
296      * @param <A> Argument type ({@link Void} if statement does not have argument.)
297      * @param <D> Class representing declared version of this statement.
298      */
299     public interface WhenConditionMixin<A, D extends DeclaredStatement<A>> extends Mixin<A, D>, WhenConditionAware {
300         @Override
301         default Optional<RevisionAwareXPath> getWhenCondition() {
302             return findFirstEffectiveSubstatementArgument(WhenEffectiveStatement.class);
303         }
304     }
305
306     /**
307      * Support interface for various mixins. Implementations are required to store 32bits worth of flags, which are
308      * globally assigned to sub-interfaces -- thus providing storage for many low-cardinality properties.
309      *
310      * @param <A> Argument type ({@link Void} if statement does not have argument.)
311      * @param <D> Class representing declared version of this statement.
312      */
313     public interface EffectiveStatementWithFlags<A, D extends DeclaredStatement<A>> extends Mixin<A, D> {
314         /**
315          * Return flags assicated with this statements. Flags can be built using {@link FlagsBuilder}.
316          *
317          * @return Flag field value (32 bits).
318          */
319         int flags();
320
321         @NonNullByDefault
322         final class FlagsBuilder implements Mutable {
323             // We still have 25 flags remaining
324             static final int STATUS_CURRENT       = 0x0001;
325             static final int STATUS_DEPRECATED    = 0x0002;
326             static final int STATUS_OBSOLETE      = 0x0003;
327             static final int MASK_STATUS          = 0x0003;
328
329             static final int CONFIGURATION        = 0x0004;
330             static final int MANDATORY            = 0x0008;
331
332             static final int AUGMENTING           = 0x0010;
333             static final int ADDED_BY_USES        = 0x0020;
334             private static final int MASK_HISTORY = 0x0030;
335
336             static final int USER_ORDERED         = 0x0040;
337
338             private int flags;
339
340             public FlagsBuilder setConfiguration(final boolean config) {
341                 if (config) {
342                     flags |= CONFIGURATION;
343                 } else {
344                     flags &= ~CONFIGURATION;
345                 }
346                 return this;
347             }
348
349             public FlagsBuilder setHistory(final CopyHistory history) {
350                 int bits;
351                 if (history.contains(CopyType.ADDED_BY_USES_AUGMENTATION)) {
352                     bits = AUGMENTING | ADDED_BY_USES;
353                 } else {
354                     bits = 0;
355                     if (history.contains(CopyType.ADDED_BY_AUGMENTATION)) {
356                         bits |= AUGMENTING;
357                     }
358                     if (history.contains(CopyType.ADDED_BY_USES)) {
359                         bits |= ADDED_BY_USES;
360                     }
361                 }
362
363                 flags = flags & ~MASK_HISTORY | bits;
364                 return this;
365             }
366
367             public FlagsBuilder setMandatory(final boolean mandatory) {
368                 if (mandatory) {
369                     flags |= MANDATORY;
370                 } else {
371                     flags &= ~MANDATORY;
372                 }
373                 return this;
374             }
375
376             public FlagsBuilder setStatus(final Status status) {
377                 final int bits;
378                 switch (status) {
379                     case CURRENT:
380                         bits = STATUS_CURRENT;
381                         break;
382                     case DEPRECATED:
383                         bits = STATUS_DEPRECATED;
384                         break;
385                     case OBSOLETE:
386                         bits = STATUS_DEPRECATED;
387                         break;
388                     default:
389                         throw new IllegalStateException("Unhandled status " + status);
390                 }
391
392                 flags = flags & ~MASK_STATUS | bits;
393                 return this;
394             }
395
396             public FlagsBuilder setUserOrdered(final boolean userOrdered) {
397                 if (userOrdered) {
398                     flags |= USER_ORDERED;
399                 } else {
400                     flags &= ~USER_ORDERED;
401                 }
402                 return this;
403             }
404
405             public int toFlags() {
406                 return flags;
407             }
408         }
409     }
410 }