Add some Generator documentation
[mdsal.git] / binding / mdsal-binding-generator / src / main / java / org / opendaylight / mdsal / binding / generator / impl / reactor / NotificationServiceGenerator.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 static com.google.common.base.Verify.verify;
11 import static java.util.Objects.requireNonNull;
12
13 import java.util.List;
14 import org.opendaylight.mdsal.binding.model.api.AccessModifier;
15 import org.opendaylight.mdsal.binding.model.api.GeneratedType;
16 import org.opendaylight.mdsal.binding.model.api.type.builder.GeneratedTypeBuilder;
17 import org.opendaylight.mdsal.binding.model.api.type.builder.MethodSignatureBuilder;
18 import org.opendaylight.mdsal.binding.model.ri.BindingTypes;
19 import org.opendaylight.mdsal.binding.model.ri.Types;
20 import org.opendaylight.mdsal.binding.spec.naming.BindingMapping;
21 import org.opendaylight.yangtools.yang.model.api.DocumentedNode.WithStatus;
22 import org.opendaylight.yangtools.yang.model.api.Status;
23 import org.opendaylight.yangtools.yang.model.api.stmt.NotificationEffectiveStatement;
24
25 /**
26  * Aggregate service for top-level {@code notification} statements for a particular module. It does not handle nested
27  * (YANG 1.1) notifications.
28  */
29 // FIXME: eventually remove this generator
30 final class NotificationServiceGenerator extends AbstractImplicitGenerator {
31     private final List<NotificationGenerator> notifs;
32
33     NotificationServiceGenerator(final ModuleGenerator parent, final List<NotificationGenerator> notifs) {
34         super(parent);
35         this.notifs = requireNonNull(notifs);
36     }
37
38     @Override
39     String classSuffix() {
40         return BindingMapping.NOTIFICATION_LISTENER_SUFFIX;
41     }
42
43     @Override
44     GeneratedType createTypeImpl(final TypeBuilderFactory builderFactory) {
45         final GeneratedTypeBuilder builder = builderFactory.newGeneratedTypeBuilder(typeName());
46         // FIXME: MDSAL-496: mark this interface as deprecated
47         builder.addImplementsType(BindingTypes.NOTIFICATION_LISTENER);
48
49         for (NotificationGenerator gen : notifs) {
50             final MethodSignatureBuilder notificationMethod = builder.addMethod("on" + gen.assignedName())
51                 .setAccessModifier(AccessModifier.PUBLIC)
52                 .addParameter(gen.getGeneratedType(builderFactory), "notification")
53                 .setReturnType(Types.primitiveVoidType());
54
55             final NotificationEffectiveStatement stmt = gen.statement();
56             verify(stmt instanceof WithStatus, "Unexpected statement %s", stmt);
57             final WithStatus withStatus = (WithStatus) stmt;
58
59             annotateDeprecatedIfNecessary(withStatus, notificationMethod);
60             if (withStatus.getStatus() == Status.OBSOLETE) {
61                 notificationMethod.setDefault(true);
62             }
63
64             // FIXME: finish this up
65             // addComment(notificationMethod, notification);
66         }
67
68         return builder.build();
69     }
70 }