Rework AugmentRuntimeType and Choice/Case linkage
[mdsal.git] / binding / mdsal-binding-generator / src / main / java / org / opendaylight / mdsal / binding / generator / impl / reactor / NotificationGenerator.java
1 /*
2  * Copyright (c) 2021 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.mdsal.binding.generator.impl.reactor;
9
10 import java.util.List;
11 import org.opendaylight.mdsal.binding.generator.impl.rt.DefaultNotificationRuntimeType;
12 import org.opendaylight.mdsal.binding.model.api.GeneratedType;
13 import org.opendaylight.mdsal.binding.model.api.Type;
14 import org.opendaylight.mdsal.binding.model.api.type.builder.GeneratedTypeBuilder;
15 import org.opendaylight.mdsal.binding.model.api.type.builder.GeneratedTypeBuilderBase;
16 import org.opendaylight.mdsal.binding.model.ri.BindingTypes;
17 import org.opendaylight.mdsal.binding.runtime.api.AugmentRuntimeType;
18 import org.opendaylight.mdsal.binding.runtime.api.NotificationRuntimeType;
19 import org.opendaylight.mdsal.binding.runtime.api.RuntimeType;
20 import org.opendaylight.yangtools.yang.model.api.stmt.NotificationEffectiveStatement;
21 import org.opendaylight.yangtools.yang.model.util.SchemaInferenceStack;
22
23 /**
24  * Generator corresponding to a {@code notification} statement.
25  */
26 final class NotificationGenerator
27         extends CompositeSchemaTreeGenerator<NotificationEffectiveStatement, NotificationRuntimeType> {
28     NotificationGenerator(final NotificationEffectiveStatement statement,
29             final AbstractCompositeGenerator<?, ?> parent) {
30         super(statement, parent);
31     }
32
33     @Override
34     void pushToInference(final SchemaInferenceStack dataTree) {
35         dataTree.enterSchemaTree(statement().getIdentifier());
36     }
37
38     @Override
39     GeneratedType createTypeImpl(final TypeBuilderFactory builderFactory) {
40         final GeneratedTypeBuilder builder = builderFactory.newGeneratedTypeBuilder(typeName());
41
42         builder.addImplementsType(BindingTypes.DATA_OBJECT);
43         builder.addImplementsType(notificationType(builder, builderFactory));
44
45         addAugmentable(builder);
46         addUsesInterfaces(builder, builderFactory);
47
48         addConcreteInterfaceMethods(builder);
49         addGetterMethods(builder, builderFactory);
50
51         final ModuleGenerator module = currentModule();
52         module.addQNameConstant(builder, localName());
53
54         builderFactory.addCodegenInformation(module, statement(), builder);
55         annotateDeprecatedIfNecessary(builder);
56
57         return builder.build();
58     }
59
60     @Override
61     void addAsGetterMethod(final GeneratedTypeBuilderBase<?> builder, final TypeBuilderFactory builderFactory) {
62         // Notifications are a distinct concept
63     }
64
65     @Override
66     CompositeRuntimeTypeBuilder<NotificationEffectiveStatement, NotificationRuntimeType> createBuilder(
67             final NotificationEffectiveStatement statement) {
68         return new CompositeRuntimeTypeBuilder<>(statement) {
69             @Override
70             NotificationRuntimeType build(final GeneratedType type, final NotificationEffectiveStatement statement,
71                     final List<RuntimeType> children, final List<AugmentRuntimeType> augments) {
72                 return new DefaultNotificationRuntimeType(type, statement, children, augments);
73             }
74         };
75     }
76
77     private Type notificationType(final GeneratedTypeBuilder builder, final TypeBuilderFactory builderFactory) {
78         final AbstractCompositeGenerator<?, ?> parent = getParent();
79         if (parent instanceof ModuleGenerator) {
80             return BindingTypes.notification(builder);
81         }
82
83         final Type parentType = Type.of(parent.typeName());
84         if (parent instanceof ListGenerator) {
85             final KeyGenerator keyGen = ((ListGenerator) parent).keyGenerator();
86             if (keyGen != null) {
87                 return BindingTypes.keyedListNotification(builder, parentType, keyGen.getGeneratedType(builderFactory));
88             }
89         }
90         return BindingTypes.instanceNotification(builder, parentType);
91     }
92 }