4a9440cbccb9adbe3990f6a44ea09a50201765a9
[hc2vpp.git] /
1 package org.opendaylight.yang.gen.v1.urn.honeycomb.params.xml.ns.yang.notification.impl.rev160601;
2
3 import io.fd.honeycomb.v3po.notification.NotificationCollector;
4 import io.fd.honeycomb.v3po.notification.NotificationProducer;
5 import io.fd.honeycomb.v3po.notification.impl.HoneycombNotificationCollector;
6 import io.fd.honeycomb.v3po.notification.impl.NotificationProducerRegistry;
7 import io.fd.honeycomb.v3po.notification.impl.NotificationProducerTracker;
8 import java.util.Collection;
9 import org.opendaylight.controller.md.sal.binding.impl.BindingDOMNotificationPublishServiceAdapter;
10 import org.opendaylight.controller.md.sal.binding.impl.BindingToNormalizedNodeCodec;
11 import org.opendaylight.controller.md.sal.dom.broker.impl.DOMNotificationRouter;
12 import org.opendaylight.yangtools.yang.binding.Notification;
13
14 public class HoneycombNotificationManagerModule extends org.opendaylight.yang.gen.v1.urn.honeycomb.params.xml.ns.yang.notification.impl.rev160601.AbstractHoneycombNotificationManagerModule {
15
16     public HoneycombNotificationManagerModule(org.opendaylight.controller.config.api.ModuleIdentifier identifier, org.opendaylight.controller.config.api.DependencyResolver dependencyResolver) {
17         super(identifier, dependencyResolver);
18     }
19
20     public HoneycombNotificationManagerModule(org.opendaylight.controller.config.api.ModuleIdentifier identifier, org.opendaylight.controller.config.api.DependencyResolver dependencyResolver, org.opendaylight.yang.gen.v1.urn.honeycomb.params.xml.ns.yang.notification.impl.rev160601.HoneycombNotificationManagerModule oldModule, java.lang.AutoCloseable oldInstance) {
21         super(identifier, dependencyResolver, oldModule, oldInstance);
22     }
23
24     @Override
25     public void customValidation() {
26         // add custom validation form module attributes here.
27     }
28
29     @Override
30     public java.lang.AutoCloseable createInstance() {
31         final DOMNotificationRouter notificationRouter = getDomNotificationServiceDependency();
32
33         // Create the registry to keep track of what's registered
34         final NotificationProducerRegistry notificationProducerRegistry =
35             new NotificationProducerRegistry(getNotificationProducersDependency());
36
37         // Create BA version of notification service (implementation is free from ODL)
38         final BindingToNormalizedNodeCodec codec = getRuntimeMappingCodecDependency();
39         final BindingDOMNotificationPublishServiceAdapter bindingDOMNotificationPublishServiceAdapter =
40             new BindingDOMNotificationPublishServiceAdapter(codec, notificationRouter);
41
42         // Create Collector on top of BA notification service and registry
43         final HoneycombNotificationCollector honeycombNotificationCollector =
44             new HoneycombNotificationCollector(bindingDOMNotificationPublishServiceAdapter, notificationProducerRegistry);
45
46         // Create tracker, responsible for starting and stopping registered notification producers whenever necessary
47         final NotificationProducerTracker notificationProducerTracker =
48             new NotificationProducerTracker(notificationProducerRegistry, honeycombNotificationCollector,
49                 notificationRouter);
50
51         // TODO wire with restconf
52         // DOMNotificationService is already provided by DOMBroker injected into RESTCONF, however RESTCONF
53         // only supports data-change notification, nothing else. So currently its impossible.
54
55         return new CloseableCollector(honeycombNotificationCollector, () -> {
56             // Close all resources in order opposite to instantiation
57             notificationProducerTracker.close();
58             honeycombNotificationCollector.close();
59             bindingDOMNotificationPublishServiceAdapter.close();
60             // notificationProducerRegistry; no close, it's just a collection
61         });
62     }
63
64     /**
65      * NotificationCollector wrapper in which close method execution can be injected
66      */
67     private class CloseableCollector implements AutoCloseable, NotificationCollector, NotificationProducer {
68
69         private final HoneycombNotificationCollector honeycombNotificationCollector;
70         private final AutoCloseable resources;
71
72         CloseableCollector(final HoneycombNotificationCollector honeycombNotificationCollector,
73                            final AutoCloseable resources) {
74             this.honeycombNotificationCollector = honeycombNotificationCollector;
75             this.resources = resources;
76         }
77
78         @Override
79         public void close() throws Exception {
80             resources.close();
81         }
82
83         @Override
84         public void onNotification(final Notification notification) {
85             honeycombNotificationCollector.onNotification(notification);
86         }
87
88         @Override
89         public Collection<Class<? extends Notification>> getNotificationTypes() {
90             return honeycombNotificationCollector.getNotificationTypes();
91         }
92     }
93 }