Refactor TAPI 2.4
[transportpce.git] / tapi / src / main / java / org / opendaylight / transportpce / tapi / impl / rpc / DeleteTapiLinkImpl.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.concurrent.ExecutionException;
12 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
13 import org.opendaylight.transportpce.common.network.NetworkTransactionService;
14 import org.opendaylight.transportpce.tapi.topology.TapiNetworkUtilsImpl;
15 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.tapinetworkutils.rev230728.DeleteTapiLink;
16 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.tapinetworkutils.rev230728.DeleteTapiLinkInput;
17 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.tapinetworkutils.rev230728.DeleteTapiLinkOutput;
18 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.tapinetworkutils.rev230728.DeleteTapiLinkOutputBuilder;
19 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.common.rev221121.Context;
20 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.topology.rev221121.Context1;
21 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.topology.rev221121.context.TopologyContext;
22 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.topology.rev221121.topology.Link;
23 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.topology.rev221121.topology.LinkKey;
24 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.topology.rev221121.topology.context.Topology;
25 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.topology.rev221121.topology.context.TopologyKey;
26 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
27 import org.opendaylight.yangtools.yang.common.ErrorType;
28 import org.opendaylight.yangtools.yang.common.RpcResult;
29 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32
33
34 public class DeleteTapiLinkImpl implements DeleteTapiLink {
35     private static final Logger LOG = LoggerFactory.getLogger(DeleteTapiLinkImpl.class);
36
37     private TapiNetworkUtilsImpl tapiNetworkUtilsImpl;
38     private NetworkTransactionService networkTransactionService;
39
40     public DeleteTapiLinkImpl(TapiNetworkUtilsImpl tapiNetworkUtilsImpl,
41             NetworkTransactionService networkTransactionService) {
42         this.tapiNetworkUtilsImpl = tapiNetworkUtilsImpl;
43         this.networkTransactionService = networkTransactionService;
44     }
45
46     @Override
47     public ListenableFuture<RpcResult<DeleteTapiLinkOutput>> invoke(DeleteTapiLinkInput input) {
48         // TODO: check if this IID is correct
49         // TODO --> need to check if the link exists in the topology
50         try {
51             InstanceIdentifier<Link> linkIID = InstanceIdentifier.builder(Context.class)
52                 .augmentation(Context1.class)
53                 .child(TopologyContext.class)
54                 .child(Topology.class, new TopologyKey(tapiNetworkUtilsImpl.getTapiTopoUuid()))
55                 .child(Link.class, new LinkKey(input.getUuid()))
56                 .build();
57             this.networkTransactionService.delete(LogicalDatastoreType.OPERATIONAL, linkIID);
58             this.networkTransactionService.commit().get();
59             LOG.info("TAPI link deleted successfully.");
60             return RpcResultBuilder.success(new DeleteTapiLinkOutputBuilder()
61                 .setResult("Link successfully deleted from tapi topology").build()).buildFuture();
62         } catch (InterruptedException | ExecutionException e) {
63             LOG.error("Failed to delete TAPI link", e);
64             return RpcResultBuilder.<DeleteTapiLinkOutput>failed()
65                 .withError(ErrorType.RPC, "Failed to delete link from topology")
66                 .buildFuture();
67         }
68     }
69
70 }