e7d54e3183538bf3b7ebe28a39aae304edac0aaf
[hc2vpp.git] /
1 /*
2  * Copyright (c) 2016 Cisco and/or its affiliates.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at:
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package io.fd.honeycomb.v3po.notification.impl;
17
18 import io.fd.honeycomb.v3po.notification.NotificationCollector;
19 import java.util.Collection;
20 import javax.annotation.Nonnull;
21 import org.opendaylight.controller.md.sal.binding.api.NotificationPublishService;
22 import org.opendaylight.yangtools.yang.binding.Notification;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
25
26 /**
27  * Notification collector based on MD-SAL's {@link NotificationPublishService}.
28  */
29 public final class HoneycombNotificationCollector implements NotificationCollector, AutoCloseable {
30
31     private static final Logger LOG = LoggerFactory.getLogger(HoneycombNotificationCollector.class);
32
33     private final NotificationPublishService bindingDOMNotificationPublishServiceAdapter;
34     private final NotificationProducerRegistry notificationProducerRegistry;
35
36     public HoneycombNotificationCollector(
37         @Nonnull final NotificationPublishService bindingDOMNotificationPublishServiceAdapter,
38         @Nonnull final NotificationProducerRegistry notificationProducerRegistry) {
39         this.bindingDOMNotificationPublishServiceAdapter = bindingDOMNotificationPublishServiceAdapter;
40         this.notificationProducerRegistry = notificationProducerRegistry;
41     }
42
43     @Override
44     public void close() throws Exception {
45         LOG.trace("Closing");
46     }
47
48     @Override
49     public void onNotification(@Nonnull final Notification notification) {
50         LOG.debug("Notification: {} pushed into collector", notification.getClass().getSimpleName());
51         LOG.trace("Notification: {} pushed into collector", notification);
52         try {
53             bindingDOMNotificationPublishServiceAdapter.putNotification(notification);
54         } catch (InterruptedException e) {
55             LOG.warn("Interrupted", e);
56             Thread.currentThread().interrupt();
57             throw new RuntimeException(e);
58         }
59     }
60
61     @Override
62     @Nonnull
63     public Collection<Class<? extends Notification>> getNotificationTypes() {
64         return notificationProducerRegistry.getNotificationTypes();
65     }
66 }