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