Update MRI projects for Aluminium
[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.common.api.LogicalDatastoreType;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev200120.NetworkTopologyPcepService;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev200120.OperationResult;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev200120.RemoveLspInputBuilder;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.tunnel.pcep.programming.rev181109.PcepDestroyTunnelInput;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.tunnel.pcep.rev181109.Link1;
26 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
27 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Link;
28 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
29 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
30 import org.opendaylight.yangtools.yang.common.RpcResult;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33
34 final class DestroyTunnelInstructionExecutor extends AbstractInstructionExecutor {
35     private static final Logger LOG = LoggerFactory.getLogger(DestroyTunnelInstructionExecutor.class);
36     private final PcepDestroyTunnelInput pcepDestroyTunnelInput;
37     private final DataBroker dataProvider;
38     private final NetworkTopologyPcepService topologyService;
39
40     DestroyTunnelInstructionExecutor(final PcepDestroyTunnelInput pcepDestroyTunnelInput, final DataBroker dataProvider,
41             final NetworkTopologyPcepService topologyService) {
42         super(pcepDestroyTunnelInput);
43         this.pcepDestroyTunnelInput = pcepDestroyTunnelInput;
44         this.dataProvider = dataProvider;
45         this.topologyService = topologyService;
46     }
47
48     @Override
49     protected ListenableFuture<OperationResult> invokeOperation() {
50         final InstanceIdentifier<Topology> tii = TopologyProgrammingUtil.topologyForInput(this.pcepDestroyTunnelInput);
51         final InstanceIdentifier<Link> lii = TunnelProgrammingUtil.linkIdentifier(tii, this.pcepDestroyTunnelInput);
52         try (ReadTransaction t = this.dataProvider.newReadOnlyTransaction()) {
53             final Node node;
54             final Link link;
55             try {
56                 // The link has to exist
57                 link = t.read(LogicalDatastoreType.OPERATIONAL, lii).get().get();
58                 // The source node has to exist
59                 node = TunelProgrammingUtil.sourceNode(t, tii, link).get();
60             } catch (final InterruptedException | ExecutionException e) {
61                 LOG.debug("Link or node does not exist.", e);
62                 return TunelProgrammingUtil.RESULT;
63             }
64             final RemoveLspInputBuilder ab = new RemoveLspInputBuilder();
65             ab.setName(link.augmentation(Link1.class).getSymbolicPathName());
66             ab.setNode(node.nonnullSupportingNode().values().iterator().next().key().getNodeRef());
67             return Futures.transform(
68                     this.topologyService.removeLsp(ab.build()),
69                     RpcResult::getResult, MoreExecutors.directExecutor());
70         }
71     }
72 }