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.get();
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 if (removed.getTerminationPoint() != null) {
91 for (TerminationPoint tp : removed.getTerminationPoint()) {
92 HwvtepPhysicalPortAugmentation hppAugmentation =
93 tp.augmentation(HwvtepPhysicalPortAugmentation.class);
94 if (hppAugmentation != null) {
95 lswitchListRemoved.add(hppAugmentation);
99 if (!lswitchListRemoved.isEmpty()) {
100 result.put(key, lswitchListRemoved);
103 //If the node which physical ports belong to is updated, and physical ports may
104 //be created or updated or deleted, we need to get deleted ones.
105 Node updated = TransactUtils.getUpdated(mod);
106 Node before = mod.getDataBefore();
107 if (updated != null && before != null) {
108 List<HwvtepPhysicalPortAugmentation> portListUpdated = new ArrayList<>();
109 List<HwvtepPhysicalPortAugmentation> portListBefore = new ArrayList<>();
110 List<HwvtepPhysicalPortAugmentation> portListRemoved = new ArrayList<>();
111 if (updated.getTerminationPoint() != null) {
112 for (TerminationPoint tp : updated.getTerminationPoint()) {
113 HwvtepPhysicalPortAugmentation hppAugmentation =
114 tp.augmentation(HwvtepPhysicalPortAugmentation.class);
115 if (hppAugmentation != null) {
116 portListUpdated.add(hppAugmentation);
120 if (before.getTerminationPoint() != null) {
121 for (TerminationPoint tp : before.getTerminationPoint()) {
122 HwvtepPhysicalPortAugmentation hppAugmentation =
123 tp.augmentation(HwvtepPhysicalPortAugmentation.class);
124 if (hppAugmentation != null) {
125 portListBefore.add(hppAugmentation);
129 portListBefore.removeAll(portListUpdated);
130 //then exclude updated physical ports
131 for (HwvtepPhysicalPortAugmentation portBefore: portListBefore) {
133 for (; index < portListUpdated.size(); index++) {
134 if (portBefore.getHwvtepNodeName().equals(portListUpdated.get(index).getHwvtepNodeName())) {
138 if (index == portListUpdated.size()) {
139 portListRemoved.add(portBefore);
142 if (!portListRemoved.isEmpty()) {
143 result.put(key, portListRemoved);