c2d4a3827564d33e8e7a0b53dab15d853e536af2
[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     NotificationRuntimeType createRuntimeType(final GeneratedType type, final NotificationEffectiveStatement statement,
62             final List<RuntimeType> children, final List<AugmentRuntimeType> augments) {
63         return new DefaultNotificationRuntimeType(type, statement, children, augments);
64     }
65
66     @Override
67     void addAsGetterMethod(final GeneratedTypeBuilderBase<?> builder, final TypeBuilderFactory builderFactory) {
68         // Notifications are a distinct concept
69     }
70
71     private Type notificationType(final GeneratedTypeBuilder builder, final TypeBuilderFactory builderFactory) {
72         final AbstractCompositeGenerator<?, ?> parent = getParent();
73         if (parent instanceof ModuleGenerator) {
74             return BindingTypes.notification(builder);
75         }
76
77         final Type parentType = Type.of(parent.typeName());
78         if (parent instanceof ListGenerator) {
79             final KeyGenerator keyGen = ((ListGenerator) parent).keyGenerator();
80             if (keyGen != null) {
81                 return BindingTypes.keyedListNotification(builder, parentType, keyGen.getGeneratedType(builderFactory));
82             }
83         }
84         return BindingTypes.instanceNotification(builder, parentType);
85     }
86 }