Fixed termination point deletion from operational data store.
[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.SouthboundMapper;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbBridgeName;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.ConnectionInfo;
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.TerminationPoint;
27 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.node.TerminationPointKey;
28 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31
32 public class OvsdbPortRemoveCommand extends AbstractTransactionCommand {
33     private static final Logger LOG = LoggerFactory.getLogger(OvsdbPortRemoveCommand.class);
34
35     public OvsdbPortRemoveCommand(ConnectionInfo key, TableUpdates updates,
36             DatabaseSchema dbSchema) {
37         super(key, updates, dbSchema);
38     }
39
40     @Override
41     public void execute(ReadWriteTransaction transaction) {
42         String bridgeName = null;
43         String portName = null;
44         Collection<Port> portRemovedRows = TyperUtils.extractRowsRemoved(
45                 Port.class, getUpdates(), getDbSchema()).values();
46         Map<UUID,Bridge> bridgeUpdatedRows = TyperUtils.extractRowsUpdated(
47                 Bridge.class, getUpdates(), getDbSchema());
48         Map<UUID,Bridge> bridgeUpdatedOldRows = TyperUtils.extractRowsOld(
49                 Bridge.class, getUpdates(), getDbSchema());
50         for (Port port : portRemovedRows) {
51             for (UUID bridgeUUID : bridgeUpdatedOldRows.keySet()) {
52                 Bridge oldBridgeData = bridgeUpdatedOldRows.get(bridgeUUID);
53                 if (oldBridgeData.getPortsColumn().getData().contains(port.getUuidColumn().getData())) {
54                     Bridge updatedBridgeData = bridgeUpdatedRows.get(bridgeUUID);
55                     bridgeName = updatedBridgeData.getName();
56                     break;
57                 }
58             }
59             if (bridgeName == 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(getConnectionInfo(),
66                             new OvsdbBridgeName(bridgeName)).child(
67                             TerminationPoint.class,
68                             new TerminationPointKey(new TpId(portName)));
69             transaction.delete(LogicalDatastoreType.OPERATIONAL, nodePath);
70         }
71     }
72
73 }