Bump upstream dependencies to Ca
[transportpce.git] / nbinotifications / src / main / java / org / opendaylight / transportpce / nbinotifications / impl / rpc / DeleteNotificationSubscriptionServiceImpl.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 java.util.Map;
12 import java.util.NoSuchElementException;
13 import java.util.Optional;
14 import java.util.concurrent.ExecutionException;
15 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
16 import org.opendaylight.transportpce.common.network.NetworkTransactionService;
17 import org.opendaylight.transportpce.nbinotifications.utils.TopicManager;
18 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.common.rev221121.Context;
19 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.common.rev221121.Uuid;
20 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.notification.rev221121.Context1;
21 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.notification.rev221121.DeleteNotificationSubscriptionService;
22 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.notification.rev221121.DeleteNotificationSubscriptionServiceInput;
23 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.notification.rev221121.DeleteNotificationSubscriptionServiceOutput;
24 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.notification.rev221121.DeleteNotificationSubscriptionServiceOutputBuilder;
25 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.notification.rev221121.context.NotificationContext;
26 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.notification.rev221121.notification.context.NotifSubscription;
27 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.notification.rev221121.notification.context.NotifSubscriptionKey;
28 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.notification.rev221121.notification.subscription.service.SubscriptionFilter;
29 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.notification.rev221121.notification.subscription.service.SubscriptionFilterKey;
30 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
31 import org.opendaylight.yangtools.yang.common.ErrorType;
32 import org.opendaylight.yangtools.yang.common.RpcResult;
33 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36
37
38 public class DeleteNotificationSubscriptionServiceImpl implements DeleteNotificationSubscriptionService {
39     private static final Logger LOG = LoggerFactory.getLogger(DeleteNotificationSubscriptionServiceImpl.class);
40
41     private final NetworkTransactionService networkTransactionService;
42     private final TopicManager topicManager;
43
44     public DeleteNotificationSubscriptionServiceImpl(NetworkTransactionService networkTransactionService,
45             TopicManager topicManager) {
46         this.networkTransactionService = networkTransactionService;
47         this.topicManager = topicManager;
48     }
49
50     @Override
51     public ListenableFuture<RpcResult<DeleteNotificationSubscriptionServiceOutput>> invoke(
52             DeleteNotificationSubscriptionServiceInput input) {
53         try {
54             if (input == null || input.getUuid() == null) {
55                 LOG.warn("Missing mandatory params for input {}", input);
56                 return RpcResultBuilder.<DeleteNotificationSubscriptionServiceOutput>failed()
57                     .withError(ErrorType.RPC, "Missing input parameters").buildFuture();
58             }
59             Uuid notifSubsUuid = input.getUuid();
60             InstanceIdentifier<NotifSubscription> notifSubscriptionIID = InstanceIdentifier.builder(Context.class)
61                 .augmentation(Context1.class).child(NotificationContext.class).child(NotifSubscription.class,
62                     new NotifSubscriptionKey(notifSubsUuid)).build();
63             Optional<NotifSubscription> optionalNotifSub = this.networkTransactionService.read(
64                 LogicalDatastoreType.OPERATIONAL, notifSubscriptionIID).get();
65
66             if (optionalNotifSub.isEmpty()) {
67                 return RpcResultBuilder.<DeleteNotificationSubscriptionServiceOutput>failed()
68                     .withError(ErrorType.APPLICATION,
69                         "Notification subscription doesnt exist").buildFuture();
70             }
71             NotifSubscription notifSubscription = optionalNotifSub.orElseThrow();
72             this.networkTransactionService.delete(LogicalDatastoreType.OPERATIONAL, notifSubscriptionIID);
73             this.networkTransactionService.commit().get();
74             for (Map.Entry<SubscriptionFilterKey, SubscriptionFilter> sfEntry : notifSubscription
75                     .getSubscriptionFilter().entrySet()) {
76                 for (Uuid objectUuid:sfEntry.getValue().getRequestedObjectIdentifier()) {
77                     this.topicManager.deleteTapiTopic(objectUuid.getValue());
78                 }
79             }
80 //            for (Uuid objectUuid:notifSubscription.getSubscriptionFilter().getRequestedObjectIdentifier()) {
81 //                this.topicManager.deleteTapiTopic(objectUuid.getValue());
82 //            }
83             return RpcResultBuilder.success(new DeleteNotificationSubscriptionServiceOutputBuilder().build())
84                 .buildFuture();
85         } catch (InterruptedException | ExecutionException | NoSuchElementException e) {
86             LOG.error("Failed to delete Notification subscription service", e);
87         }
88         return RpcResultBuilder.<DeleteNotificationSubscriptionServiceOutput>failed()
89             .withError(ErrorType.APPLICATION,
90                 "Failed to delete notification subscription service").buildFuture();
91     }
92
93 }