Refactor NBINotification & add ServiceListener tests
[transportpce.git] / nbinotifications / src / main / java / org / opendaylight / transportpce / nbinotifications / impl / NbiNotificationsImpl.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 com.google.common.util.concurrent.ListenableFuture;
11 import java.util.List;
12 import org.opendaylight.transportpce.common.converter.JsonStringConverter;
13 import org.opendaylight.transportpce.nbinotifications.consumer.Subscriber;
14 import org.opendaylight.transportpce.nbinotifications.serialization.NotificationAlarmServiceDeserializer;
15 import org.opendaylight.transportpce.nbinotifications.serialization.NotificationServiceDeserializer;
16 import org.opendaylight.yang.gen.v1.nbi.notifications.rev210628.GetNotificationsAlarmServiceInput;
17 import org.opendaylight.yang.gen.v1.nbi.notifications.rev210628.GetNotificationsAlarmServiceOutput;
18 import org.opendaylight.yang.gen.v1.nbi.notifications.rev210628.GetNotificationsAlarmServiceOutputBuilder;
19 import org.opendaylight.yang.gen.v1.nbi.notifications.rev210628.GetNotificationsServiceInput;
20 import org.opendaylight.yang.gen.v1.nbi.notifications.rev210628.GetNotificationsServiceOutput;
21 import org.opendaylight.yang.gen.v1.nbi.notifications.rev210628.GetNotificationsServiceOutputBuilder;
22 import org.opendaylight.yang.gen.v1.nbi.notifications.rev210628.NbiNotificationsService;
23 import org.opendaylight.yang.gen.v1.nbi.notifications.rev210628.get.notifications.alarm.service.output.NotificationAlarmService;
24 import org.opendaylight.yang.gen.v1.nbi.notifications.rev210628.get.notifications.service.output.NotificationService;
25 import org.opendaylight.yangtools.yang.common.RpcResult;
26 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29
30 public class NbiNotificationsImpl implements NbiNotificationsService {
31     private static final Logger LOG = LoggerFactory.getLogger(NbiNotificationsImpl.class);
32     private final JsonStringConverter<org.opendaylight.yang.gen.v1
33         .nbi.notifications.rev210628.NotificationService> converterService;
34     private final JsonStringConverter<org.opendaylight.yang.gen.v1
35             .nbi.notifications.rev210628.NotificationAlarmService> converterAlarmService;
36     private final String server;
37
38     public NbiNotificationsImpl(JsonStringConverter<org.opendaylight.yang.gen.v1
39             .nbi.notifications.rev210628.NotificationService> converterService,
40                                 JsonStringConverter<org.opendaylight.yang.gen.v1
41             .nbi.notifications.rev210628.NotificationAlarmService> converterAlarmService, String server) {
42         this.converterService = converterService;
43         this.converterAlarmService = converterAlarmService;
44         this.server = server;
45     }
46
47     @Override
48     public ListenableFuture<RpcResult<GetNotificationsServiceOutput>> getNotificationsService(
49             GetNotificationsServiceInput input) {
50         LOG.info("RPC getNotificationsService received");
51         if (input == null || input.getIdConsumer() == null || input.getGroupId() == null) {
52             LOG.warn("Missing mandatory params for input {}", input);
53             return RpcResultBuilder.success(new GetNotificationsServiceOutputBuilder().build()).buildFuture();
54         }
55         Subscriber<org.opendaylight.yang.gen.v1.nbi.notifications.rev210628.NotificationService,
56                 NotificationService> subscriber = new Subscriber<>(input.getIdConsumer(), input.getGroupId(), server,
57                 converterService, NotificationServiceDeserializer.class);
58         List<NotificationService> notificationServiceList = subscriber
59                 .subscribe(input.getConnectionType().getName(), NotificationService.QNAME);
60         GetNotificationsServiceOutputBuilder output = new GetNotificationsServiceOutputBuilder()
61                 .setNotificationService(notificationServiceList);
62         return RpcResultBuilder.success(output.build()).buildFuture();
63     }
64
65     @Override
66     public ListenableFuture<RpcResult<GetNotificationsAlarmServiceOutput>> getNotificationsAlarmService(
67             GetNotificationsAlarmServiceInput input) {
68         LOG.info("RPC getNotificationsAlarmService received");
69         if (input == null || input.getIdConsumer() == null || input.getGroupId() == null) {
70             LOG.warn("Missing mandatory params for input {}", input);
71             return RpcResultBuilder.success(new GetNotificationsAlarmServiceOutputBuilder().build()).buildFuture();
72         }
73         Subscriber<org.opendaylight.yang.gen.v1.nbi.notifications.rev210628.NotificationAlarmService,
74                 NotificationAlarmService> subscriber = new Subscriber<>(input.getIdConsumer(), input.getGroupId(),
75                 server, converterAlarmService, NotificationAlarmServiceDeserializer.class);
76         List<NotificationAlarmService> notificationAlarmServiceList = subscriber
77                 .subscribe("alarm" + input.getConnectionType().getName(), NotificationAlarmService.QNAME);
78         GetNotificationsAlarmServiceOutputBuilder output = new GetNotificationsAlarmServiceOutputBuilder()
79                 .setNotificationAlarmService(notificationAlarmServiceList);
80         return RpcResultBuilder.success(output.build()).buildFuture();
81     }
82 }