Modernize ListGenerator
[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.binding.contract.StatementNamespace;
21 import org.opendaylight.yangtools.yang.model.api.stmt.NotificationEffectiveStatement;
22 import org.opendaylight.yangtools.yang.model.util.SchemaInferenceStack;
23
24 /**
25  * Generator corresponding to a {@code notification} statement.
26  */
27 final class NotificationGenerator
28         extends CompositeSchemaTreeGenerator<NotificationEffectiveStatement, NotificationRuntimeType> {
29     NotificationGenerator(final NotificationEffectiveStatement statement,
30             final AbstractCompositeGenerator<?, ?> parent) {
31         super(statement, parent);
32     }
33
34     @Override
35     StatementNamespace namespace() {
36         return StatementNamespace.NOTIFICATION;
37     }
38
39     @Override
40     void pushToInference(final SchemaInferenceStack dataTree) {
41         dataTree.enterSchemaTree(statement().argument());
42     }
43
44     @Override
45     GeneratedType createTypeImpl(final TypeBuilderFactory builderFactory) {
46         final GeneratedTypeBuilder builder = builderFactory.newGeneratedTypeBuilder(typeName());
47
48         builder.addImplementsType(BindingTypes.DATA_OBJECT);
49         builder.addImplementsType(notificationType(builder, builderFactory));
50
51         addAugmentable(builder);
52         addUsesInterfaces(builder, builderFactory);
53
54         addConcreteInterfaceMethods(builder);
55         addGetterMethods(builder, builderFactory);
56
57         final ModuleGenerator module = currentModule();
58         module.addQNameConstant(builder, localName());
59
60         builderFactory.addCodegenInformation(module, statement(), builder);
61         annotateDeprecatedIfNecessary(builder);
62
63         return builder.build();
64     }
65
66     @Override
67     void addAsGetterMethod(final GeneratedTypeBuilderBase<?> builder, final TypeBuilderFactory builderFactory) {
68         // Notifications are a distinct concept
69     }
70
71     @Override
72     CompositeRuntimeTypeBuilder<NotificationEffectiveStatement, NotificationRuntimeType> createBuilder(
73             final NotificationEffectiveStatement statement) {
74         return new CompositeRuntimeTypeBuilder<>(statement) {
75             @Override
76             NotificationRuntimeType build(final GeneratedType type, final NotificationEffectiveStatement statement,
77                     final List<RuntimeType> children, final List<AugmentRuntimeType> augments) {
78                 return new DefaultNotificationRuntimeType(type, statement, children, augments);
79             }
80         };
81     }
82
83     private Type notificationType(final GeneratedTypeBuilder builder, final TypeBuilderFactory builderFactory) {
84         final AbstractCompositeGenerator<?, ?> parent = getParent();
85         if (parent instanceof ModuleGenerator) {
86             return BindingTypes.notification(builder);
87         }
88
89         final Type parentType = Type.of(parent.typeName());
90         if (parent instanceof ListGenerator listGen) {
91             final KeyGenerator keyGen = listGen.keyGenerator();
92             if (keyGen != null) {
93                 return BindingTypes.keyedListNotification(builder, parentType, keyGen.getGeneratedType(builderFactory));
94             }
95         }
96         return BindingTypes.instanceNotification(builder, parentType);
97     }
98 }