BUG5764: Hwvtep tunnel update/delete not reflected correctly
[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
13 import org.opendaylight.controller.md.sal.binding.api.ReadWriteTransaction;
14 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
15 import org.opendaylight.ovsdb.hwvtepsouthbound.HwvtepConnectionInstance;
16 import org.opendaylight.ovsdb.hwvtepsouthbound.HwvtepSouthboundMapper;
17 import org.opendaylight.ovsdb.lib.message.TableUpdates;
18 import org.opendaylight.ovsdb.lib.notation.UUID;
19 import org.opendaylight.ovsdb.lib.schema.DatabaseSchema;
20 import org.opendaylight.ovsdb.lib.schema.typed.TyperUtils;
21 import org.opendaylight.ovsdb.schema.hardwarevtep.PhysicalLocator;
22 import org.opendaylight.ovsdb.schema.hardwarevtep.PhysicalSwitch;
23 import org.opendaylight.ovsdb.schema.hardwarevtep.Tunnel;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.physical._switch.attributes.Tunnels;
25 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
26 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.node.TerminationPoint;
27 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30
31 public class HwvtepTunnelRemoveCommand extends AbstractTransactionCommand {
32
33     private static final Logger LOG = LoggerFactory.getLogger(HwvtepTunnelRemoveCommand.class);
34     Collection<Tunnel> deletedTunnelRows = null;
35
36     public HwvtepTunnelRemoveCommand(HwvtepConnectionInstance key, TableUpdates updates, DatabaseSchema dbSchema) {
37         super(key, updates, dbSchema);
38         try {
39             deletedTunnelRows = TyperUtils.extractRowsRemoved(Tunnel.class, getUpdates(), getDbSchema()).values();
40         } catch (IllegalArgumentException e) {
41             LOG.debug("Tunnel Table not supported on this HWVTEP device", e.getMessage());
42         }
43     }
44
45     @Override
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 (Exception 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 pSwitch = client.getDeviceInfo().getPhysicalSwitchForTunnel(tunnel.getUuid());
65         if(pSwitch == null){
66             //PhysicalSwitch has already been removed, nothing to do here
67             return null;
68         }
69         InstanceIdentifier<Node> psIid = HwvtepSouthboundMapper.createInstanceIdentifier(client, pSwitch);
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 pLoc = getOvsdbConnectionInstance().getDeviceInfo().getPhysicalLocator(uuid);
84         if(pLoc == null) {
85             LOG.trace("Available PhysicalLocators: ",
86                             getOvsdbConnectionInstance().getDeviceInfo().getPhysicalLocators());
87         }
88         return pLoc;
89     }
90
91 }