NPE Exception while processing Interfaces
[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 package org.opendaylight.ovsdb.southbound.transactions.md;
9
10 import java.util.Map;
11 import java.util.Map.Entry;
12 import org.opendaylight.controller.md.sal.binding.api.ReadWriteTransaction;
13 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
14 import org.opendaylight.ovsdb.lib.message.TableUpdates;
15 import org.opendaylight.ovsdb.lib.notation.UUID;
16 import org.opendaylight.ovsdb.lib.schema.DatabaseSchema;
17 import org.opendaylight.ovsdb.lib.schema.typed.TyperUtils;
18 import org.opendaylight.ovsdb.schema.openvswitch.Bridge;
19 import org.opendaylight.ovsdb.schema.openvswitch.Port;
20 import org.opendaylight.ovsdb.southbound.InstanceIdentifierCodec;
21 import org.opendaylight.ovsdb.southbound.OvsdbConnectionInstance;
22 import org.opendaylight.ovsdb.southbound.SouthboundMapper;
23 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.TpId;
24 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.node.TerminationPoint;
25 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.node.TerminationPointKey;
26 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29
30 public class OvsdbPortRemoveCommand extends AbstractTransactionCommand {
31     private static final Logger LOG = LoggerFactory.getLogger(OvsdbPortRemoveCommand.class);
32
33     private final InstanceIdentifierCodec instanceIdentifierCodec;
34
35     public OvsdbPortRemoveCommand(final InstanceIdentifierCodec instanceIdentifierCodec,
36             final OvsdbConnectionInstance key, final TableUpdates updates, final DatabaseSchema dbSchema) {
37         super(key, updates, dbSchema);
38         this.instanceIdentifierCodec = instanceIdentifierCodec;
39     }
40
41     @Override
42     public void execute(final ReadWriteTransaction transaction) {
43         Map<UUID, Port> portRemovedRows = TyperUtils.extractRowsRemoved(
44             Port.class, getUpdates(), getDbSchema());
45         Map<UUID, Port> portUpdatedRows = TyperUtils.extractRowsUpdated(
46                 Port.class, getUpdates(), getDbSchema());
47         Map<UUID,Bridge> bridgeUpdatedRows = TyperUtils.extractRowsUpdated(
48                 Bridge.class, getUpdates(), getDbSchema());
49         Map<UUID,Bridge> bridgeUpdatedOldRows = TyperUtils.extractRowsOld(
50                 Bridge.class, getUpdates(), getDbSchema());
51         for (Entry<UUID, Port> portRemoved: portRemovedRows.entrySet()) {
52             final UUID portUuid = portRemoved.getKey();
53             final Port port = portRemoved.getValue();
54             final String portName = port.getName();
55             boolean isPortInUpdatedRows = portUpdatedRows.values()
56                 .stream().anyMatch(updatedPort -> portName.equals(updatedPort.getName()));
57             if (isPortInUpdatedRows) {
58                 LOG.debug("port {} present in updated rows, skipping delete", portName);
59                 continue;
60             }
61             Bridge bridgeData = null;
62             for (Entry<UUID, Bridge> entry : bridgeUpdatedOldRows.entrySet()) {
63                 UUID bridgeUuid = entry.getKey();
64                 Bridge oldBridgeData = entry.getValue();
65                 if (oldBridgeData.getPortsColumn() != null
66                         && oldBridgeData.getPortsColumn().getData().contains(port.getUuidColumn().getData())) {
67                     // We have a match, try updated bridge rows first...
68                     bridgeData = bridgeUpdatedRows.get(bridgeUuid);
69                     if (bridgeData == null) {
70                         // ... and fall back to old data
71                         bridgeData = oldBridgeData;
72                     }
73                     break;
74                 }
75             }
76             if (bridgeData == null) {
77                 LOG.warn("Bridge not found for port {}", port);
78                 continue;
79             }
80             final InstanceIdentifier<TerminationPoint> nodePath = SouthboundMapper.createInstanceIdentifier(
81                 instanceIdentifierCodec, getOvsdbConnectionInstance(), bridgeData)
82                     .child(TerminationPoint.class, new TerminationPointKey(new TpId(portName)));
83             transaction.delete(LogicalDatastoreType.OPERATIONAL, nodePath);
84             // Remove from OvsdbConnection Instance cache
85             getOvsdbConnectionInstance().removePort(portUuid);
86             getOvsdbConnectionInstance().removePortInterface(portName);
87         }
88     }
89 }