Refactor nbinotifications
[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(
45             NetworkTransactionService networkTransactionService, 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")
58                     .buildFuture();
59             }
60             Uuid notifSubsUuid = input.getUuid();
61             InstanceIdentifier<NotifSubscription> notifSubscriptionIID = InstanceIdentifier.builder(Context.class)
62                 .augmentation(Context1.class)
63                 .child(NotificationContext.class)
64                 .child(NotifSubscription.class, new NotifSubscriptionKey(notifSubsUuid))
65                 .build();
66             Optional<NotifSubscription> optionalNotifSub = this.networkTransactionService
67                 .read(LogicalDatastoreType.OPERATIONAL, notifSubscriptionIID)
68                 .get();
69             if (optionalNotifSub.isEmpty()) {
70                 return RpcResultBuilder.<DeleteNotificationSubscriptionServiceOutput>failed()
71                     .withError(ErrorType.APPLICATION, "Notification subscription doesnt exist")
72                     .buildFuture();
73             }
74             NotifSubscription notifSubscription = optionalNotifSub.orElseThrow();
75             this.networkTransactionService.delete(LogicalDatastoreType.OPERATIONAL, notifSubscriptionIID);
76             this.networkTransactionService.commit().get();
77             for (Map.Entry<SubscriptionFilterKey, SubscriptionFilter> sfEntry :
78                     notifSubscription.getSubscriptionFilter().entrySet()) {
79                 for (Uuid objectUuid:sfEntry.getValue().getRequestedObjectIdentifier()) {
80                     this.topicManager.deleteTapiTopic(objectUuid.getValue());
81                 }
82             }
83             return RpcResultBuilder
84                 .success(new DeleteNotificationSubscriptionServiceOutputBuilder().build())
85                 .buildFuture();
86         } catch (InterruptedException | ExecutionException | NoSuchElementException e) {
87             LOG.error("Failed to delete Notification subscription service", e);
88         }
89         return RpcResultBuilder.<DeleteNotificationSubscriptionServiceOutput>failed()
90             .withError(ErrorType.APPLICATION, "Failed to delete notification subscription service")
91             .buildFuture();
92     }
93 }