Bump MRI upstreams
[ovsdb.git] / hwvtepsouthbound / hwvtepsouthbound-impl / src / main / java / org / opendaylight / ovsdb / hwvtepsouthbound / transactions / md / HwvtepTunnelRemoveCommand.java
1 /*
2  * Copyright (c) 2016 Ericsson India Global Services Pvt Ltd. 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.ovsdb.hwvtepsouthbound.transactions.md;
10
11 import java.util.Collection;
12 import org.opendaylight.mdsal.binding.api.ReadWriteTransaction;
13 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
14 import org.opendaylight.ovsdb.hwvtepsouthbound.HwvtepConnectionInstance;
15 import org.opendaylight.ovsdb.hwvtepsouthbound.HwvtepSouthboundMapper;
16 import org.opendaylight.ovsdb.lib.message.TableUpdates;
17 import org.opendaylight.ovsdb.lib.notation.UUID;
18 import org.opendaylight.ovsdb.lib.schema.DatabaseSchema;
19 import org.opendaylight.ovsdb.lib.schema.typed.TyperUtils;
20 import org.opendaylight.ovsdb.schema.hardwarevtep.PhysicalLocator;
21 import org.opendaylight.ovsdb.schema.hardwarevtep.PhysicalSwitch;
22 import org.opendaylight.ovsdb.schema.hardwarevtep.Tunnel;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.physical._switch.attributes.Tunnels;
24 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
25 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.node.TerminationPoint;
26 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29
30 public final class HwvtepTunnelRemoveCommand extends AbstractTransactionCommand {
31     private static final Logger LOG = LoggerFactory.getLogger(HwvtepTunnelRemoveCommand.class);
32
33     Collection<Tunnel> deletedTunnelRows = null;
34
35     public HwvtepTunnelRemoveCommand(HwvtepConnectionInstance key, TableUpdates updates, DatabaseSchema dbSchema) {
36         super(key, updates, dbSchema);
37         try {
38             deletedTunnelRows = TyperUtils.extractRowsRemoved(Tunnel.class, getUpdates(), getDbSchema()).values();
39         } catch (IllegalArgumentException e) {
40             LOG.debug("Tunnel Table not supported on this HWVTEP device", e);
41         }
42     }
43
44     @Override
45     @SuppressWarnings("checkstyle:IllegalCatch")
46     public void execute(ReadWriteTransaction transaction) {
47         for (Tunnel tunnel : deletedTunnelRows) {
48             try {
49                 InstanceIdentifier<Tunnels> tunnelIid = getInstanceIdentifier(getOvsdbConnectionInstance(), tunnel);
50                 if (tunnelIid != null) {
51                     transaction.delete(LogicalDatastoreType.OPERATIONAL, tunnelIid);
52                     LOG.trace("Deleting tunnel {}", tunnelIid);
53                 }
54                 getOvsdbConnectionInstance().getDeviceInfo().removePhysicalSwitchForTunnel(tunnel.getUuid());
55             } catch (RuntimeException e) {
56                 LOG.warn("Failed to delete tunnel {}", tunnel, e);
57             }
58         }
59     }
60
61     private InstanceIdentifier<Tunnels> getInstanceIdentifier(HwvtepConnectionInstance client, Tunnel tunnel) {
62         InstanceIdentifier<Tunnels> result = null;
63
64         PhysicalSwitch phySwitch = client.getDeviceInfo().getPhysicalSwitchForTunnel(tunnel.getUuid());
65         if (phySwitch == null) {
66             //PhysicalSwitch has already been removed, nothing to do here
67             return null;
68         }
69         InstanceIdentifier<Node> psIid = HwvtepSouthboundMapper.createInstanceIdentifier(client, phySwitch);
70         PhysicalLocator plLocal = getPhysicalLocatorFromUUID(tunnel.getLocalColumn().getData());
71         PhysicalLocator plRemote = getPhysicalLocatorFromUUID(tunnel.getRemoteColumn().getData());
72         if (plLocal != null && plRemote != null) {
73             InstanceIdentifier<TerminationPoint> localTpPath = HwvtepSouthboundMapper.createInstanceIdentifier(
74                                                                 client.getInstanceIdentifier(), plLocal);
75             InstanceIdentifier<TerminationPoint> remoteTpPath = HwvtepSouthboundMapper.createInstanceIdentifier(
76                                                                 client.getInstanceIdentifier(), plRemote);
77             result = HwvtepSouthboundMapper.createInstanceIdentifier(psIid, localTpPath, remoteTpPath);
78         }
79         return result;
80     }
81
82     private PhysicalLocator getPhysicalLocatorFromUUID(UUID uuid) {
83         PhysicalLocator locator = getOvsdbConnectionInstance().getDeviceInfo().getPhysicalLocator(uuid);
84         if (locator == null) {
85             LOG.trace("Available PhysicalLocators: {}",
86                             getOvsdbConnectionInstance().getDeviceInfo().getPhysicalLocators());
87         }
88         return locator;
89     }
90 }