Refactor nbinotifications
[transportpce.git] / nbinotifications / src / main / java / org / opendaylight / transportpce / nbinotifications / impl / rpc / GetNotificationSubscriptionServiceDetailsImpl.java
1 /*
2  * Copyright © 2024 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.rpc;
9
10 import com.google.common.util.concurrent.ListenableFuture;
11 import org.opendaylight.transportpce.nbinotifications.impl.NbiNotificationsProvider;
12 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.common.rev221121.Uuid;
13 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.notification.rev221121.GetNotificationSubscriptionServiceDetails;
14 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.notification.rev221121.GetNotificationSubscriptionServiceDetailsInput;
15 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.notification.rev221121.GetNotificationSubscriptionServiceDetailsOutput;
16 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.notification.rev221121.GetNotificationSubscriptionServiceDetailsOutputBuilder;
17 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.notification.rev221121.context.NotificationContext;
18 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.notification.rev221121.get.notification.subscription.service.details.output.SubscriptionServiceBuilder;
19 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.notification.rev221121.notification.context.NotifSubscriptionKey;
20 import org.opendaylight.yangtools.yang.common.ErrorType;
21 import org.opendaylight.yangtools.yang.common.RpcResult;
22 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
25
26
27 public class GetNotificationSubscriptionServiceDetailsImpl implements GetNotificationSubscriptionServiceDetails {
28     private static final Logger LOG = LoggerFactory.getLogger(GetNotificationSubscriptionServiceDetailsImpl.class);
29
30     private NbiNotificationsProvider nbiNotifications;
31
32     public GetNotificationSubscriptionServiceDetailsImpl(NbiNotificationsProvider nbiNotifications) {
33         this.nbiNotifications = nbiNotifications;
34     }
35
36     @Override
37     public ListenableFuture<RpcResult<GetNotificationSubscriptionServiceDetailsOutput>> invoke(
38             GetNotificationSubscriptionServiceDetailsInput input) {
39         if (input == null || input.getUuid() == null) {
40             LOG.warn("Missing mandatory params for input {}", input);
41             return RpcResultBuilder.<GetNotificationSubscriptionServiceDetailsOutput>failed()
42                 .withError(ErrorType.RPC, "Missing input parameters")
43                 .buildFuture();
44         }
45         Uuid notifSubsUuid = input.getUuid();
46         NotificationContext notificationContext = nbiNotifications.getNotificationContext();
47         if (notificationContext == null) {
48             return RpcResultBuilder.<GetNotificationSubscriptionServiceDetailsOutput>failed()
49                 .withError(ErrorType.APPLICATION, "Notification context is empty")
50                 .buildFuture();
51         }
52         var subsc = notificationContext.getNotifSubscription();
53         if (subsc == null) {
54             return RpcResultBuilder
55                 .success(
56                     new GetNotificationSubscriptionServiceDetailsOutputBuilder()
57                         .setSubscriptionService(new SubscriptionServiceBuilder().build())
58                         .build())
59                 .buildFuture();
60         }
61         if (subsc.containsKey(new NotifSubscriptionKey(notifSubsUuid))) {
62             return RpcResultBuilder
63                 .success(
64                     new GetNotificationSubscriptionServiceDetailsOutputBuilder()
65                         .setSubscriptionService(
66                             new SubscriptionServiceBuilder(subsc.get(new NotifSubscriptionKey(notifSubsUuid))).build())
67                         .build())
68                 .buildFuture();
69         }
70         return RpcResultBuilder.<GetNotificationSubscriptionServiceDetailsOutput>failed()
71             .withError(ErrorType.APPLICATION, "Notification subscription service doesnt exist")
72             .buildFuture();
73
74     }
75
76 }