Bump upstream dependencies to Ca
[transportpce.git] / tapi / src / main / java / org / opendaylight / transportpce / tapi / impl / rpc / DeleteConnectivityServiceImpl.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.tapi.impl.rpc;
9
10 import com.google.common.util.concurrent.ListenableFuture;
11 import java.util.ArrayList;
12 import java.util.HashMap;
13 import java.util.List;
14 import java.util.Map;
15 import java.util.NoSuchElementException;
16 import java.util.Optional;
17 import java.util.concurrent.ExecutionException;
18 import org.opendaylight.mdsal.binding.api.RpcService;
19 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
20 import org.opendaylight.transportpce.common.ResponseCodes;
21 import org.opendaylight.transportpce.common.network.NetworkTransactionService;
22 import org.opendaylight.transportpce.tapi.utils.TapiContext;
23 import org.opendaylight.yang.gen.v1.http.org.openroadm.common.service.types.rev230526.RpcActions;
24 import org.opendaylight.yang.gen.v1.http.org.openroadm.common.service.types.rev230526.sdnc.request.header.SdncRequestHeaderBuilder;
25 import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev230526.ServiceDelete;
26 import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev230526.ServiceDeleteInputBuilder;
27 import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev230526.ServiceDeleteOutput;
28 import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev230526.service.delete.input.ServiceDeleteReqInfo;
29 import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev230526.service.delete.input.ServiceDeleteReqInfoBuilder;
30 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.common.rev221121.Context;
31 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.common.rev221121.Uuid;
32 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.common.rev221121.global._class.Name;
33 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.common.rev221121.global._class.NameKey;
34 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.connectivity.rev221121.DeleteConnectivityService;
35 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.connectivity.rev221121.DeleteConnectivityServiceInput;
36 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.connectivity.rev221121.DeleteConnectivityServiceOutput;
37 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.connectivity.rev221121.DeleteConnectivityServiceOutputBuilder;
38 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.connectivity.rev221121.connectivity.context.ConnectivityService;
39 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.connectivity.rev221121.connectivity.context.ConnectivityServiceKey;
40 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.connectivity.rev221121.context.ConnectivityContext;
41 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
42 import org.opendaylight.yangtools.yang.common.ErrorType;
43 import org.opendaylight.yangtools.yang.common.RpcResult;
44 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
45 import org.slf4j.Logger;
46 import org.slf4j.LoggerFactory;
47
48 public class DeleteConnectivityServiceImpl implements DeleteConnectivityService {
49     private static final Logger LOG = LoggerFactory.getLogger(DeleteConnectivityServiceImpl.class);
50
51     private final RpcService rpcService;
52     private final TapiContext tapiContext;
53     private final NetworkTransactionService networkTransactionService;
54
55     public DeleteConnectivityServiceImpl(RpcService rpcService, TapiContext tapiContext,
56             NetworkTransactionService networkTransactionService) {
57         this.rpcService = rpcService;
58         this.tapiContext = tapiContext;
59         this.networkTransactionService = networkTransactionService;
60     }
61
62     @Override
63     public ListenableFuture<RpcResult<DeleteConnectivityServiceOutput>> invoke(DeleteConnectivityServiceInput input) {
64         List<String> serviceName = null;
65         if (input.getUuid() != null) {
66             try {
67                 serviceName = getNameFromUuid(input.getUuid(), "Service");
68             } catch (ExecutionException e) {
69                 LOG.error("Service {} to be deleted not found in the DataStore", e.getMessage());
70                 return RpcResultBuilder.<DeleteConnectivityServiceOutput>failed()
71                         .withError(ErrorType.RPC, "Failed to delete Service").buildFuture();
72             } catch (NoSuchElementException e) {
73                 LOG.error("Service {} to be deleted not found in the DataStore", e.getMessage());
74                 return RpcResultBuilder.<DeleteConnectivityServiceOutput>failed()
75                         .withError(ErrorType.RPC, "Failed to delete Service").buildFuture();
76             }
77             LOG.debug("The service {}, of name {} has been found in the DS", input.getUuid().toString(), serviceName);
78             try {
79                 Uuid serviceUuid = input.getUuid();
80                 this.tapiContext.deleteConnectivityService(serviceUuid);
81                 ListenableFuture<RpcResult<ServiceDeleteOutput>> output = rpcService.getRpc(ServiceDelete.class)
82                         .invoke(new ServiceDeleteInputBuilder()
83                                 .setServiceDeleteReqInfo(
84                                         new ServiceDeleteReqInfoBuilder().setServiceName(input.getUuid().getValue())
85                                                 .setTailRetention(ServiceDeleteReqInfo.TailRetention.No).build())
86                                 .setSdncRequestHeader(new SdncRequestHeaderBuilder().setRequestId("request-1")
87                                         .setNotificationUrl("notification url").setRequestSystemId("appname")
88                                         .setRpcAction(RpcActions.ServiceDelete).build())
89                                 .build());
90                 RpcResult<ServiceDeleteOutput> rpcResult = output.get();
91                 if (!rpcResult.getResult().getConfigurationResponseCommon().getResponseCode()
92                         .equals(ResponseCodes.RESPONSE_FAILED)) {
93                     LOG.info("Service is being deleted and devices are being rolled back");
94                     return RpcResultBuilder.success(new DeleteConnectivityServiceOutputBuilder().build()).buildFuture();
95                 }
96                 LOG.error("Failed to delete service. Deletion process failed");
97             } catch (InterruptedException | ExecutionException e) {
98                 LOG.error("Failed to delete service.", e);
99                 return RpcResultBuilder.<DeleteConnectivityServiceOutput>failed()
100                         .withError(ErrorType.RPC, "Failed to delete Service").buildFuture();
101             }
102         }
103         return RpcResultBuilder.<DeleteConnectivityServiceOutput>failed()
104                 .withError(ErrorType.RPC, "Failed to delete Service, service uuid in input is null").buildFuture();
105     }
106
107     public List<String> getNameFromUuid(Uuid uuid, String typeOfNode)
108             throws ExecutionException, NoSuchElementException {
109         Map<NameKey, Name> nameMap = new HashMap<>();
110         if ("Service".equals(typeOfNode)) {
111             ConnectivityService conServ = null;
112             InstanceIdentifier<ConnectivityService> nodeIID = InstanceIdentifier.builder(Context.class)
113                     .augmentation(
114                             org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.connectivity.rev221121.Context1.class)
115                     .child(ConnectivityContext.class).child(ConnectivityService.class, new ConnectivityServiceKey(uuid))
116                     .build();
117             ListenableFuture<Optional<ConnectivityService>> conServFuture = this.networkTransactionService
118                     .read(LogicalDatastoreType.OPERATIONAL, nodeIID);
119             try {
120                 conServ = conServFuture.get().orElseThrow();
121             } catch (InterruptedException e) {
122                 LOG.error("GetNamefromUuid Interrupt exception: Service not in Datastore, Interruption of the process");
123                 Thread.currentThread().interrupt();
124                 // TODO: investigate on how to throw Interrupted exception (generate a check
125                 // violation error)
126             } catch (ExecutionException e) {
127                 throw new ExecutionException("Unable to get from mdsal service: "
128                         + nodeIID.firstKeyOf(ConnectivityService.class).getUuid().getValue(), e);
129             } catch (NoSuchElementException e) {
130                 throw new NoSuchElementException("Unable to get from mdsal service: "
131                         + nodeIID.firstKeyOf(ConnectivityService.class).getUuid().getValue(), e);
132                 // return null;
133             }
134             nameMap = conServ.getName();
135         }
136
137         List<String> nameList = new ArrayList<>();
138         for (Map.Entry<NameKey, Name> entry : nameMap.entrySet()) {
139             nameList.add(entry.getValue().getValue());
140         }
141         LOG.debug("The service name of service {}, is {}", uuid.toString(), nameList.toString());
142         return nameList;
143     }
144
145 }