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
9 package org.opendaylight.ovsdb.hwvtepsouthbound.transact;
11 import static org.opendaylight.ovsdb.lib.operations.Operations.op;
13 import com.google.common.base.Optional;
14 import java.util.ArrayList;
15 import java.util.Collection;
16 import java.util.HashMap;
17 import java.util.List;
19 import java.util.Map.Entry;
20 import org.opendaylight.controller.md.sal.binding.api.DataObjectModification;
21 import org.opendaylight.controller.md.sal.binding.api.DataTreeModification;
22 import org.opendaylight.ovsdb.lib.operations.TransactionBuilder;
23 import org.opendaylight.ovsdb.lib.schema.typed.TyperUtils;
24 import org.opendaylight.ovsdb.schema.hardwarevtep.PhysicalPort;
25 import org.opendaylight.ovsdb.utils.mdsal.utils.TransactionType;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepPhysicalPortAugmentation;
27 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
28 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.node.TerminationPoint;
29 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
33 public class PhysicalPortRemoveCommand extends AbstractTransactCommand {
34 private static final Logger LOG = LoggerFactory.getLogger(PhysicalPortRemoveCommand.class);
36 public PhysicalPortRemoveCommand(HwvtepOperationalState state,
37 Collection<DataTreeModification<Node>> changes) {
38 super(state, changes);
42 public void execute(TransactionBuilder transaction) {
43 //TODO reuse from base class instead of extractRemovedPorts
44 Map<InstanceIdentifier<Node>, List<HwvtepPhysicalPortAugmentation>> removeds =
45 extractRemovedPorts(getChanges(), HwvtepPhysicalPortAugmentation.class);
46 if (!removeds.isEmpty()) {
47 for (Entry<InstanceIdentifier<Node>, List<HwvtepPhysicalPortAugmentation>> removed:
48 removeds.entrySet()) {
49 updatePhysicalPort(transaction, removed.getKey(), removed.getValue());
54 private void updatePhysicalPort(final TransactionBuilder transaction,
55 final InstanceIdentifier<Node> psNodeiid,
56 final List<HwvtepPhysicalPortAugmentation> listPort) {
57 for (HwvtepPhysicalPortAugmentation port : listPort) {
58 LOG.debug("Updating a physical port named: {}", port.getHwvtepNodeName().getValue());
59 Optional<HwvtepPhysicalPortAugmentation> operationalPhysicalPortOptional =
60 getOperationalState().getPhysicalPortAugmentation(psNodeiid, port.getHwvtepNodeName());
61 if (operationalPhysicalPortOptional.isPresent()) {
62 PhysicalPort physicalPort = TyperUtils.getTypedRowWrapper(
63 transaction.getDatabaseSchema(),PhysicalPort.class);
64 physicalPort.setVlanBindings(new HashMap<>());
65 HwvtepPhysicalPortAugmentation updatedPhysicalPort = operationalPhysicalPortOptional.get();
66 String existingPhysicalPortName = updatedPhysicalPort.getHwvtepNodeName().getValue();
67 PhysicalPort extraPhyscialPort =
68 TyperUtils.getTypedRowWrapper(transaction.getDatabaseSchema(), PhysicalPort.class);
69 extraPhyscialPort.setName("");
70 LOG.trace("execute: updating physical port: {}", physicalPort);
71 transaction.add(op.update(physicalPort)
72 .where(extraPhyscialPort.getNameColumn().getSchema().opEqual(existingPhysicalPortName))
74 updateControllerTxHistory(TransactionType.UPDATE, physicalPort);
76 LOG.warn("Unable to update physical port {} because it was not found in the operational store, "
77 + "and thus we cannot retrieve its UUID", port.getHwvtepNodeName().getValue());
82 protected Map<InstanceIdentifier<Node>, List<HwvtepPhysicalPortAugmentation>> extractRemovedPorts(
83 Collection<DataTreeModification<Node>> changes, Class<HwvtepPhysicalPortAugmentation> class1) {
84 Map<InstanceIdentifier<Node>, List<HwvtepPhysicalPortAugmentation>> result = new HashMap<>();
85 if (changes != null && !changes.isEmpty()) {
86 for (DataTreeModification<Node> change : changes) {
87 final InstanceIdentifier<Node> key = change.getRootPath().getRootIdentifier();
88 final DataObjectModification<Node> mod = change.getRootNode();
89 //If the node which physical ports belong to is removed, all physical ports
90 //should be removed too.
91 Node removed = TransactUtils.getRemoved(mod);
92 if (removed != null) {
93 List<HwvtepPhysicalPortAugmentation> lswitchListRemoved = new ArrayList<>();
94 if (removed.getTerminationPoint() != null) {
95 for (TerminationPoint tp : removed.getTerminationPoint()) {
96 HwvtepPhysicalPortAugmentation hppAugmentation =
97 tp.augmentation(HwvtepPhysicalPortAugmentation.class);
98 if (hppAugmentation != null) {
99 lswitchListRemoved.add(hppAugmentation);
103 if (!lswitchListRemoved.isEmpty()) {
104 result.put(key, lswitchListRemoved);
107 //If the node which physical ports belong to is updated, and physical ports may
108 //be created or updated or deleted, we need to get deleted ones.
109 Node updated = TransactUtils.getUpdated(mod);
110 Node before = mod.getDataBefore();
111 if (updated != null && before != null) {
112 List<HwvtepPhysicalPortAugmentation> portListUpdated = new ArrayList<>();
113 List<HwvtepPhysicalPortAugmentation> portListBefore = new ArrayList<>();
114 List<HwvtepPhysicalPortAugmentation> portListRemoved = new ArrayList<>();
115 if (updated.getTerminationPoint() != null) {
116 for (TerminationPoint tp : updated.getTerminationPoint()) {
117 HwvtepPhysicalPortAugmentation hppAugmentation =
118 tp.augmentation(HwvtepPhysicalPortAugmentation.class);
119 if (hppAugmentation != null) {
120 portListUpdated.add(hppAugmentation);
124 if (before.getTerminationPoint() != null) {
125 for (TerminationPoint tp : before.getTerminationPoint()) {
126 HwvtepPhysicalPortAugmentation hppAugmentation =
127 tp.augmentation(HwvtepPhysicalPortAugmentation.class);
128 if (hppAugmentation != null) {
129 portListBefore.add(hppAugmentation);
133 portListBefore.removeAll(portListUpdated);
134 //then exclude updated physical ports
135 for (HwvtepPhysicalPortAugmentation portBefore: portListBefore) {
137 for (; index < portListUpdated.size(); index++) {
138 if (portBefore.getHwvtepNodeName().equals(portListUpdated.get(index).getHwvtepNodeName())) {
142 if (index == portListUpdated.size()) {
143 portListRemoved.add(portBefore);
146 if (!portListRemoved.isEmpty()) {
147 result.put(key, portListRemoved);