87ebcdb9a601eb53d0e7c40eed85f1267bcf7ad7
[mdsal.git] / dom / mdsal-dom-broker / src / main / java / org / opendaylight / mdsal / dom / broker / OSGiDOMNotificationRouter.java
1 /*
2  * Copyright (c) 2020 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.dom.broker;
9
10 import com.google.common.util.concurrent.ListenableFuture;
11 import java.util.Collection;
12 import java.util.concurrent.TimeUnit;
13 import org.opendaylight.mdsal.dom.api.DOMNotification;
14 import org.opendaylight.mdsal.dom.api.DOMNotificationListener;
15 import org.opendaylight.mdsal.dom.api.DOMNotificationPublishService;
16 import org.opendaylight.mdsal.dom.api.DOMNotificationService;
17 import org.opendaylight.mdsal.dom.spi.DOMNotificationSubscriptionListener;
18 import org.opendaylight.mdsal.dom.spi.DOMNotificationSubscriptionListenerRegistry;
19 import org.opendaylight.yangtools.concepts.ListenerRegistration;
20 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
21 import org.osgi.service.component.annotations.Activate;
22 import org.osgi.service.component.annotations.Component;
23 import org.osgi.service.component.annotations.Deactivate;
24 import org.osgi.service.metatype.annotations.AttributeDefinition;
25 import org.osgi.service.metatype.annotations.Designate;
26 import org.osgi.service.metatype.annotations.ObjectClassDefinition;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29
30 @Component(immediate = true, configurationPid = "org.opendaylight.mdsal.dom.notification", service = {
31         DOMNotificationService.class, DOMNotificationPublishService.class,
32         DOMNotificationSubscriptionListenerRegistry.class
33 })
34 @Designate(ocd = OSGiDOMNotificationRouter.Config.class)
35 public final class OSGiDOMNotificationRouter implements DOMNotificationService, DOMNotificationPublishService,
36         DOMNotificationSubscriptionListenerRegistry {
37     @ObjectClassDefinition()
38     public @interface Config {
39         @AttributeDefinition(name = "notification-queue-depth")
40         int queueDepth() default 65536;
41         @AttributeDefinition(name = "notification-queue-spin")
42         long spinTime() default 0;
43         @AttributeDefinition(name = "notification-queue-park")
44         long parkTime() default 0;
45     }
46
47     private static final Logger LOG = LoggerFactory.getLogger(OSGiDOMNotificationRouter.class);
48
49     private DOMNotificationRouter router;
50
51     @Activate
52     void activate(final Config config) {
53         router = DOMNotificationRouter.create(config.queueDepth(), config.spinTime(), config.parkTime(),
54             TimeUnit.MILLISECONDS);
55         LOG.info("DOM Notification Router started");
56     }
57
58     @Deactivate
59     void deactivate() {
60         router.close();
61         router = null;
62         LOG.info("DOM Notification Router stopped");
63     }
64
65     @Override
66     public <L extends DOMNotificationSubscriptionListener> ListenerRegistration<L> registerSubscriptionListener(
67             final L listener) {
68         return router.registerSubscriptionListener(listener);
69     }
70
71     @Override
72     public ListenableFuture<? extends Object> putNotification(final DOMNotification notification)
73             throws InterruptedException {
74         return router.putNotification(notification);
75     }
76
77     @Override
78     public ListenableFuture<? extends Object> offerNotification(final DOMNotification notification) {
79         return router.offerNotification(notification);
80     }
81
82     @Override
83     public ListenableFuture<? extends Object> offerNotification(final DOMNotification notification,
84             final long timeout, final TimeUnit unit) throws InterruptedException {
85         return router.offerNotification(notification, timeout, unit);
86     }
87
88     @Override
89     public <T extends DOMNotificationListener> ListenerRegistration<T> registerNotificationListener(final T listener,
90             final Collection<SchemaPath> types) {
91         return router.registerNotificationListener(listener, types);
92     }
93
94     @Override
95     public <T extends DOMNotificationListener> ListenerRegistration<T> registerNotificationListener(final T listener,
96             final SchemaPath... types) {
97         return router.registerNotificationListener(listener, types);
98     }
99 }