10d8fc7dbd9f2536c1c2e0bc45824c11e12ebdd7
[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 org.opendaylight.bgpcep.pcep.topology.spi.AbstractInstructionExecutor;
15 import org.opendaylight.bgpcep.programming.topology.TopologyProgrammingUtil;
16 import org.opendaylight.bgpcep.programming.tunnel.TunnelProgrammingUtil;
17 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
18 import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
19 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
20 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev171025.NetworkTopologyPcepService;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev171025.OperationResult;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev171025.RemoveLspInputBuilder;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev171025.RemoveLspOutput;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.tunnel.pcep.programming.rev131030.PcepDestroyTunnelInput;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.tunnel.pcep.rev130820.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 NetworkTopologyPcepService topologyService;
40
41     DestroyTunnelInstructionExecutor(final PcepDestroyTunnelInput pcepDestroyTunnelInput, final DataBroker dataProvider,
42         final NetworkTopologyPcepService topologyService) {
43         super(pcepDestroyTunnelInput);
44         this.pcepDestroyTunnelInput = pcepDestroyTunnelInput;
45         this.dataProvider = dataProvider;
46         this.topologyService = topologyService;
47     }
48
49     @Override
50     protected ListenableFuture<OperationResult> invokeOperation() {
51         final InstanceIdentifier<Topology> tii = TopologyProgrammingUtil.topologyForInput(this.pcepDestroyTunnelInput);
52         final InstanceIdentifier<Link> lii = TunnelProgrammingUtil.linkIdentifier(tii, this.pcepDestroyTunnelInput);
53         try (final ReadOnlyTransaction t = this.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).checkedGet().get();
59                 // The source node has to exist
60                 node = TunelProgrammingUtil.sourceNode(t, tii, link).get();
61             } catch (IllegalStateException | ReadFailedException 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.getAugmentation(Link1.class).getSymbolicPathName());
67             ab.setNode(node.getSupportingNode().get(0).getKey().getNodeRef());
68             return Futures.transform(
69                 (ListenableFuture<RpcResult<RemoveLspOutput>>) this.topologyService.removeLsp(ab.build()),
70                 RpcResult::getResult, MoreExecutors.directExecutor());
71         }
72     }
73 }