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