Add new Maven module to manage NBI Notifications
[transportpce.git] / nbinotifications / src / main / java / org / opendaylight / transportpce / nbinotifications / impl / NbiNotificationsProvider.java
1 /*
2  * Copyright © 2020 Orange, Inc. 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.transportpce.nbinotifications.impl;
9
10 import java.util.HashMap;
11 import java.util.List;
12 import java.util.Map;
13 import org.opendaylight.mdsal.binding.api.NotificationService;
14 import org.opendaylight.mdsal.binding.api.RpcProviderService;
15 import org.opendaylight.mdsal.binding.dom.codec.spi.BindingDOMCodecServices;
16 import org.opendaylight.transportpce.common.converter.JsonStringConverter;
17 import org.opendaylight.transportpce.nbinotifications.listener.NbiNotificationsListenerImpl;
18 import org.opendaylight.transportpce.nbinotifications.producer.Publisher;
19 import org.opendaylight.yang.gen.v1.nbi.notifications.rev201130.NbiNotificationsListener;
20 import org.opendaylight.yang.gen.v1.nbi.notifications.rev201130.NbiNotificationsService;
21 import org.opendaylight.yangtools.concepts.ListenerRegistration;
22 import org.opendaylight.yangtools.concepts.ObjectRegistration;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
25
26 public class NbiNotificationsProvider {
27
28     private static final Logger LOG = LoggerFactory.getLogger(NbiNotificationsProvider.class);
29     private static Map<String, Publisher> publishersMap =  new HashMap<>();
30
31     private final RpcProviderService rpcService;
32     private ObjectRegistration<NbiNotificationsService> rpcRegistration;
33     private ListenerRegistration<NbiNotificationsListener> listenerRegistration;
34     private NotificationService notificationService;
35     private final JsonStringConverter<org.opendaylight.yang.gen.v1
36         .nbi.notifications.rev201130.NotificationService> converter;
37     private final String suscriberServer;
38
39
40     public NbiNotificationsProvider(List<String> topics,
41             String suscriberServer, String publisherServer,
42             RpcProviderService rpcProviderService, NotificationService notificationService,
43             BindingDOMCodecServices bindingDOMCodecServices) {
44         this.rpcService = rpcProviderService;
45         this.notificationService = notificationService;
46         converter =  new JsonStringConverter<>(bindingDOMCodecServices);
47         for (String topic: topics) {
48             LOG.info("Creating publisher for topic {}", topic);
49             publishersMap.put(topic, new Publisher(topic, publisherServer, converter));
50         }
51         this.suscriberServer = suscriberServer;
52     }
53
54     /**
55      * Method called when the blueprint container is created.
56      */
57     public void init() {
58         LOG.info("NbiNotificationsProvider Session Initiated");
59         rpcRegistration = rpcService.registerRpcImplementation(NbiNotificationsService.class,
60                 new NbiNotificationsImpl(converter, suscriberServer));
61         listenerRegistration = notificationService.registerNotificationListener(
62                 new NbiNotificationsListenerImpl(publishersMap));
63     }
64
65     /**
66      * Method called when the blueprint container is destroyed.
67      */
68     public void close() {
69         for (Publisher publisher : publishersMap.values()) {
70             publisher.close();
71         }
72         rpcRegistration.close();
73         listenerRegistration.close();
74         LOG.info("NbiNotificationsProvider Closed");
75     }
76
77 }