Merge "Avoid exceptions with IPv6 subnets"
[ovsdb.git] / hwvtepsouthbound / hwvtepsouthbound-impl / src / main / java / org / opendaylight / ovsdb / hwvtepsouthbound / transactions / md / PhysicalPortRemoveCommand.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
14 import org.opendaylight.controller.md.sal.binding.api.ReadWriteTransaction;
15 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
16 import org.opendaylight.ovsdb.hwvtepsouthbound.HwvtepConnectionInstance;
17 import org.opendaylight.ovsdb.hwvtepsouthbound.HwvtepSouthboundMapper;
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.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.TpId;
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.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.node.TerminationPointKey;
27 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30
31 public class PhysicalPortRemoveCommand extends AbstractTransactionCommand {
32
33     private static final Logger LOG = LoggerFactory.getLogger(PhysicalPortRemoveCommand.class);
34
35     public PhysicalPortRemoveCommand(HwvtepConnectionInstance key, TableUpdates updates,
36             DatabaseSchema dbSchema) {
37         super(key, updates, dbSchema);
38     }
39
40     @Override
41     public void execute(ReadWriteTransaction transaction) {
42         String portName = null;
43         Collection<PhysicalPort> deletedPortRows = TyperUtils.extractRowsRemoved(PhysicalPort.class, getUpdates(),getDbSchema()).values();
44         Map<UUID, PhysicalSwitch> updatedPSRows = TyperUtils.extractRowsUpdated(PhysicalSwitch.class, getUpdates(),getDbSchema());
45         Map<UUID, PhysicalSwitch> oldPSRows = TyperUtils.extractRowsOld(PhysicalSwitch.class, getUpdates(),getDbSchema());
46         if(deletedPortRows != null && !deletedPortRows.isEmpty()) {
47             for (PhysicalPort pPort : deletedPortRows) {
48                 PhysicalSwitch updatedPSwitchData = null;
49                 for(UUID pSwitchUUID: updatedPSRows.keySet()) {
50                     PhysicalSwitch oldPSwitchData = oldPSRows.get(pSwitchUUID);
51                     if(oldPSwitchData.getPortsColumn() != null
52                             && oldPSwitchData.getPortsColumn().getData().contains(pPort.getUuidColumn().getData())
53                             && (!updatedPSRows.isEmpty())) {
54                         updatedPSwitchData = updatedPSRows.get(pSwitchUUID);
55                         break;
56                     }
57                 }
58                 if(updatedPSwitchData == null) {
59                     LOG.warn("PhysicalSwitch not found for port {}", pPort);
60                     continue;
61                 }
62                 portName = pPort.getName();
63                 final InstanceIdentifier<TerminationPoint> nodePath = HwvtepSouthboundMapper
64                         .createInstanceIdentifier(getOvsdbConnectionInstance(), 
65                                 updatedPSwitchData).child(
66                                 TerminationPoint.class,
67                                 new TerminationPointKey(new TpId(portName)));
68                 transaction.delete(LogicalDatastoreType.OPERATIONAL, nodePath);
69             }
70         }
71     }
72
73 }