2 * Copyright © 2015, 2017 China Telecom Beijing Research Institute and others. All rights reserved.
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
8 package org.opendaylight.ovsdb.hwvtepsouthbound.transact;
10 import static org.opendaylight.ovsdb.lib.operations.Operations.op;
12 import java.util.ArrayList;
13 import java.util.Collection;
14 import java.util.HashMap;
15 import java.util.List;
17 import java.util.Map.Entry;
18 import java.util.Optional;
19 import org.opendaylight.mdsal.binding.api.DataObjectModification;
20 import org.opendaylight.mdsal.binding.api.DataTreeModification;
21 import org.opendaylight.ovsdb.lib.operations.TransactionBuilder;
22 import org.opendaylight.ovsdb.schema.hardwarevtep.PhysicalPort;
23 import org.opendaylight.ovsdb.utils.mdsal.utils.TransactionType;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepPhysicalPortAugmentation;
25 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
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.yangtools.yang.binding.InstanceIdentifier;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
31 public class PhysicalPortRemoveCommand extends AbstractTransactCommand {
32 private static final Logger LOG = LoggerFactory.getLogger(PhysicalPortRemoveCommand.class);
34 public PhysicalPortRemoveCommand(final HwvtepOperationalState state,
35 final Collection<DataTreeModification<Node>> changes) {
36 super(state, changes);
40 public void execute(final TransactionBuilder transaction) {
41 //TODO reuse from base class instead of extractRemovedPorts
42 Map<InstanceIdentifier<Node>, List<HwvtepPhysicalPortAugmentation>> removeds =
43 extractRemovedPorts(getChanges(), HwvtepPhysicalPortAugmentation.class);
44 if (!removeds.isEmpty()) {
45 for (Entry<InstanceIdentifier<Node>, List<HwvtepPhysicalPortAugmentation>> removed:
46 removeds.entrySet()) {
47 updatePhysicalPort(transaction, removed.getKey(), removed.getValue());
52 private void updatePhysicalPort(final TransactionBuilder transaction,
53 final InstanceIdentifier<Node> psNodeiid,
54 final List<HwvtepPhysicalPortAugmentation> listPort) {
55 for (HwvtepPhysicalPortAugmentation port : listPort) {
56 LOG.debug("Updating a physical port named: {}", port.getHwvtepNodeName().getValue());
57 Optional<HwvtepPhysicalPortAugmentation> operationalPhysicalPortOptional =
58 getOperationalState().getPhysicalPortAugmentation(psNodeiid, port.getHwvtepNodeName());
59 if (operationalPhysicalPortOptional.isPresent()) {
60 PhysicalPort physicalPort = transaction.getTypedRowWrapper(PhysicalPort.class);
61 physicalPort.setVlanBindings(new HashMap<>());
62 HwvtepPhysicalPortAugmentation updatedPhysicalPort = operationalPhysicalPortOptional.orElseThrow();
63 String existingPhysicalPortName = updatedPhysicalPort.getHwvtepNodeName().getValue();
64 PhysicalPort extraPhyscialPort = transaction.getTypedRowWrapper(PhysicalPort.class);
65 extraPhyscialPort.setName("");
66 LOG.trace("execute: updating physical port: {}", physicalPort);
67 transaction.add(op.update(physicalPort)
68 .where(extraPhyscialPort.getNameColumn().getSchema().opEqual(existingPhysicalPortName))
70 updateControllerTxHistory(TransactionType.UPDATE, physicalPort);
72 LOG.warn("Unable to update physical port {} because it was not found in the operational store, "
73 + "and thus we cannot retrieve its UUID", port.getHwvtepNodeName().getValue());
78 protected Map<InstanceIdentifier<Node>, List<HwvtepPhysicalPortAugmentation>> extractRemovedPorts(
79 final Collection<DataTreeModification<Node>> changes, final Class<HwvtepPhysicalPortAugmentation> class1) {
80 Map<InstanceIdentifier<Node>, List<HwvtepPhysicalPortAugmentation>> result = new HashMap<>();
81 if (changes != null && !changes.isEmpty()) {
82 for (DataTreeModification<Node> change : changes) {
83 final InstanceIdentifier<Node> key = change.getRootPath().getRootIdentifier();
84 final DataObjectModification<Node> mod = change.getRootNode();
85 //If the node which physical ports belong to is removed, all physical ports
86 //should be removed too.
87 Node removed = TransactUtils.getRemoved(mod);
88 if (removed != null) {
89 List<HwvtepPhysicalPortAugmentation> lswitchListRemoved = new ArrayList<>();
90 for (TerminationPoint tp : removed.nonnullTerminationPoint().values()) {
91 HwvtepPhysicalPortAugmentation hppAugmentation =
92 tp.augmentation(HwvtepPhysicalPortAugmentation.class);
93 if (hppAugmentation != null) {
94 lswitchListRemoved.add(hppAugmentation);
97 if (!lswitchListRemoved.isEmpty()) {
98 result.put(key, lswitchListRemoved);
101 //If the node which physical ports belong to is updated, and physical ports may
102 //be created or updated or deleted, we need to get deleted ones.
103 Node updated = TransactUtils.getUpdated(mod);
104 Node before = mod.getDataBefore();
105 if (updated != null && before != null) {
106 List<HwvtepPhysicalPortAugmentation> portListUpdated = new ArrayList<>();
107 List<HwvtepPhysicalPortAugmentation> portListBefore = new ArrayList<>();
108 List<HwvtepPhysicalPortAugmentation> portListRemoved = new ArrayList<>();
109 for (TerminationPoint tp : updated.nonnullTerminationPoint().values()) {
110 HwvtepPhysicalPortAugmentation hppAugmentation =
111 tp.augmentation(HwvtepPhysicalPortAugmentation.class);
112 if (hppAugmentation != null) {
113 portListUpdated.add(hppAugmentation);
116 for (TerminationPoint tp : before.nonnullTerminationPoint().values()) {
117 HwvtepPhysicalPortAugmentation hppAugmentation =
118 tp.augmentation(HwvtepPhysicalPortAugmentation.class);
119 if (hppAugmentation != null) {
120 portListBefore.add(hppAugmentation);
123 portListBefore.removeAll(portListUpdated);
124 //then exclude updated physical ports
125 for (HwvtepPhysicalPortAugmentation portBefore: portListBefore) {
127 for (; index < portListUpdated.size(); index++) {
128 if (portBefore.getHwvtepNodeName().equals(portListUpdated.get(index).getHwvtepNodeName())) {
132 if (index == portListUpdated.size()) {
133 portListRemoved.add(portBefore);
136 if (!portListRemoved.isEmpty()) {
137 result.put(key, portListRemoved);