713093640dd82b3427200127f6ffae56736e1f9b
[ovsdb.git] / southbound / southbound-impl / src / main / java / org / opendaylight / ovsdb / southbound / transactions / md / OvsdbPortRemoveCommand.java
1 /*
2  * Copyright (c) 2015 Intel Corp. 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.southbound.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.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.openvswitch.Bridge;
21 import org.opendaylight.ovsdb.schema.openvswitch.Port;
22 import org.opendaylight.ovsdb.southbound.OvsdbConnectionInstance;
23 import org.opendaylight.ovsdb.southbound.SouthboundMapper;
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 OvsdbPortRemoveCommand extends AbstractTransactionCommand {
32     private static final Logger LOG = LoggerFactory.getLogger(OvsdbPortRemoveCommand.class);
33
34     public OvsdbPortRemoveCommand(OvsdbConnectionInstance key, TableUpdates updates,
35             DatabaseSchema dbSchema) {
36         super(key, updates, dbSchema);
37     }
38
39     @Override
40     public void execute(ReadWriteTransaction transaction) {
41         String portName = null;
42         Collection<Port> portRemovedRows = TyperUtils.extractRowsRemoved(
43                 Port.class, getUpdates(), getDbSchema()).values();
44         Map<UUID,Bridge> bridgeUpdatedRows = TyperUtils.extractRowsUpdated(
45                 Bridge.class, getUpdates(), getDbSchema());
46         Map<UUID,Bridge> bridgeUpdatedOldRows = TyperUtils.extractRowsOld(
47                 Bridge.class, getUpdates(), getDbSchema());
48         for (Port port : portRemovedRows) {
49             Bridge updatedBridgeData = null;
50             for (UUID bridgeUUID : bridgeUpdatedOldRows.keySet()) {
51                 Bridge oldBridgeData = bridgeUpdatedOldRows.get(bridgeUUID);
52                 if (oldBridgeData.getPortsColumn() != null
53                         && oldBridgeData.getPortsColumn().getData().contains(port.getUuidColumn().getData())
54                         && (! bridgeUpdatedRows.isEmpty())) {
55                     updatedBridgeData = bridgeUpdatedRows.get(bridgeUUID);
56                     break;
57                 }
58             }
59             if (updatedBridgeData == null) {
60                 LOG.warn("Bridge not found for port {}",port);
61                 continue;
62             }
63             portName = port.getName();
64             final InstanceIdentifier<TerminationPoint> nodePath = SouthboundMapper
65                     .createInstanceIdentifier(getOvsdbConnectionInstance(),
66                             updatedBridgeData).child(
67                             TerminationPoint.class,
68                             new TerminationPointKey(new TpId(portName)));
69             transaction.delete(LogicalDatastoreType.OPERATIONAL, nodePath);
70         }
71     }
72
73 }