Bump upstreams
[bgpcep.git] / pcep / tunnel / tunnel-provider / src / main / java / org / opendaylight / bgpcep / pcep / tunnel / provider / DestroyTunnelInstructionExecutor.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, 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
9 package org.opendaylight.bgpcep.pcep.tunnel.provider;
10
11 import com.google.common.util.concurrent.Futures;
12 import com.google.common.util.concurrent.ListenableFuture;
13 import com.google.common.util.concurrent.MoreExecutors;
14 import java.util.concurrent.ExecutionException;
15 import org.opendaylight.bgpcep.pcep.topology.spi.AbstractInstructionExecutor;
16 import org.opendaylight.bgpcep.programming.topology.TopologyProgrammingUtil;
17 import org.opendaylight.bgpcep.programming.tunnel.TunnelProgrammingUtil;
18 import org.opendaylight.mdsal.binding.api.DataBroker;
19 import org.opendaylight.mdsal.binding.api.ReadTransaction;
20 import org.opendaylight.mdsal.binding.api.RpcService;
21 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev220730.OperationResult;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev220730.RemoveLsp;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev220730.RemoveLspInputBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.tunnel.pcep.programming.rev181109.PcepDestroyTunnelInput;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.tunnel.pcep.rev181109.Link1;
27 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
28 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Link;
29 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
30 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
31 import org.opendaylight.yangtools.yang.common.RpcResult;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34
35 final class DestroyTunnelInstructionExecutor extends AbstractInstructionExecutor {
36     private static final Logger LOG = LoggerFactory.getLogger(DestroyTunnelInstructionExecutor.class);
37     private final PcepDestroyTunnelInput pcepDestroyTunnelInput;
38     private final DataBroker dataProvider;
39     private final RemoveLsp removeLsp;
40
41     DestroyTunnelInstructionExecutor(final PcepDestroyTunnelInput pcepDestroyTunnelInput, final DataBroker dataProvider,
42             final RpcService rpcService) {
43         super(pcepDestroyTunnelInput);
44         this.pcepDestroyTunnelInput = pcepDestroyTunnelInput;
45         this.dataProvider = dataProvider;
46         removeLsp = rpcService.getRpc(RemoveLsp.class);
47     }
48
49     @Override
50     protected ListenableFuture<OperationResult> invokeOperation() {
51         final InstanceIdentifier<Topology> tii = TopologyProgrammingUtil.topologyForInput(pcepDestroyTunnelInput);
52         final InstanceIdentifier<Link> lii = TunnelProgrammingUtil.linkIdentifier(tii, pcepDestroyTunnelInput);
53         try (ReadTransaction t = dataProvider.newReadOnlyTransaction()) {
54             final Node node;
55             final Link link;
56             try {
57                 // The link has to exist
58                 link = t.read(LogicalDatastoreType.OPERATIONAL, lii).get().orElseThrow();
59                 // The source node has to exist
60                 node = TunelProgrammingUtil.sourceNode(t, tii, link).orElseThrow();
61             } catch (final InterruptedException | ExecutionException e) {
62                 LOG.debug("Link or node does not exist.", e);
63                 return TunelProgrammingUtil.RESULT;
64             }
65             final RemoveLspInputBuilder ab = new RemoveLspInputBuilder();
66             ab.setName(link.augmentation(Link1.class).getSymbolicPathName());
67             ab.setNode(node.nonnullSupportingNode().values().iterator().next().key().getNodeRef());
68             return Futures.transform(removeLsp.invoke(ab.build()), RpcResult::getResult,
69                 MoreExecutors.directExecutor());
70         }
71     }
72 }