820ba9fdec9086f7a57defc4ee8050b85320855e
[ovsdb.git] / hwvtepsouthbound / hwvtepsouthbound-impl / src / main / java / org / opendaylight / ovsdb / hwvtepsouthbound / transactions / md / HwvtepPhysicalPortRemoveCommand.java
1 /*
2  * Copyright (c) 2015 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 java.util.Map;
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.hwvtepsouthbound.events.PortEvent;
18 import org.opendaylight.ovsdb.lib.message.TableUpdates;
19 import org.opendaylight.ovsdb.lib.notation.UUID;
20 import org.opendaylight.ovsdb.lib.schema.DatabaseSchema;
21 import org.opendaylight.ovsdb.lib.schema.typed.TyperUtils;
22 import org.opendaylight.ovsdb.schema.hardwarevtep.PhysicalPort;
23 import org.opendaylight.ovsdb.schema.hardwarevtep.PhysicalSwitch;
24 import org.opendaylight.ovsdb.utils.mdsal.utils.TransactionType;
25 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.TpId;
26 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
27 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.node.TerminationPoint;
28 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.node.TerminationPointKey;
29 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32
33 public class HwvtepPhysicalPortRemoveCommand extends AbstractTransactionCommand {
34
35     private static final Logger LOG = LoggerFactory.getLogger(HwvtepPhysicalPortRemoveCommand.class);
36
37     public HwvtepPhysicalPortRemoveCommand(HwvtepConnectionInstance key, TableUpdates updates,
38             DatabaseSchema dbSchema) {
39         super(key, updates, dbSchema);
40     }
41
42     @Override
43     public void execute(ReadWriteTransaction transaction) {
44         Collection<PhysicalPort> deletedPortRows =
45                 TyperUtils.extractRowsRemoved(PhysicalPort.class, getUpdates(), getDbSchema()).values();
46         Map<UUID, PhysicalSwitch> updatedPSRows =
47                 TyperUtils.extractRowsUpdated(PhysicalSwitch.class, getUpdates(), getDbSchema());
48         Map<UUID, PhysicalSwitch> oldPSRows =
49                 TyperUtils.extractRowsOld(PhysicalSwitch.class, getUpdates(), getDbSchema());
50         for (PhysicalPort port : deletedPortRows) {
51             PhysicalSwitch updatedPSwitchData = null;
52             for (Map.Entry<UUID, PhysicalSwitch> updatedEntry : updatedPSRows.entrySet()) {
53                 UUID switchUUID = updatedEntry.getKey();
54                 PhysicalSwitch oldPSwitchData = oldPSRows.get(switchUUID);
55                 if (oldPSwitchData.getPortsColumn() != null
56                         && oldPSwitchData.getPortsColumn().getData().contains(port.getUuidColumn().getData())
57                         && !updatedPSRows.isEmpty()) {
58                     updatedPSwitchData = updatedEntry.getValue();
59                     break;
60                 }
61             }
62             if (updatedPSwitchData == null) {
63                 LOG.warn("PhysicalSwitch not found for port {}", port);
64             } else {
65                 String portName = port.getName();
66                 final InstanceIdentifier<TerminationPoint> nodePath =
67                         HwvtepSouthboundMapper.createInstanceIdentifier(getOvsdbConnectionInstance(),
68                                 updatedPSwitchData).child(TerminationPoint.class,
69                                 new TerminationPointKey(new TpId(portName)));
70                 transaction.delete(LogicalDatastoreType.OPERATIONAL, nodePath);
71                 addToDeviceUpdate(TransactionType.DELETE,
72                         new PortEvent(port, nodePath.firstKeyOf(Node.class).getNodeId()));
73                 getDeviceInfo().clearDeviceOperData(TerminationPoint.class, nodePath);
74             }
75         }
76     }
77 }