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 java.util.ArrayList;
14 import java.util.Collection;
15 import java.util.HashMap;
16 import java.util.List;
18 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 import com.google.common.base.Optional;
35 public class PhysicalPortRemoveCommand extends AbstractTransactCommand {
36 private static final Logger LOG = LoggerFactory.getLogger(PhysicalPortRemoveCommand.class);
38 public PhysicalPortRemoveCommand(HwvtepOperationalState state,
39 Collection<DataTreeModification<Node>> changes) {
40 super(state, changes);
44 public void execute(TransactionBuilder transaction) {
45 //TODO reuse from base class instead of extractRemovedPorts
46 Map<InstanceIdentifier<Node>, List<HwvtepPhysicalPortAugmentation>> removeds =
47 extractRemovedPorts(getChanges(), HwvtepPhysicalPortAugmentation.class);
48 if (!removeds.isEmpty()) {
49 for (Entry<InstanceIdentifier<Node>, List<HwvtepPhysicalPortAugmentation>> removed:
50 removeds.entrySet()) {
51 updatePhysicalPort(transaction, removed.getKey(), removed.getValue());
56 private void updatePhysicalPort(final TransactionBuilder transaction,
57 final InstanceIdentifier<Node> psNodeiid,
58 final List<HwvtepPhysicalPortAugmentation> listPort) {
59 for (HwvtepPhysicalPortAugmentation port : listPort) {
60 LOG.debug("Updating a physical port named: {}", port.getHwvtepNodeName().getValue());
61 Optional<HwvtepPhysicalPortAugmentation> operationalPhysicalPortOptional =
62 getOperationalState().getPhysicalPortAugmentation(psNodeiid, port.getHwvtepNodeName());
63 if (operationalPhysicalPortOptional.isPresent()) {
64 PhysicalPort physicalPort = TyperUtils.getTypedRowWrapper(
65 transaction.getDatabaseSchema(),PhysicalPort.class);
66 physicalPort.setVlanBindings(new HashMap<>());
67 HwvtepPhysicalPortAugmentation updatedPhysicalPort = operationalPhysicalPortOptional.get();
68 String existingPhysicalPortName = updatedPhysicalPort.getHwvtepNodeName().getValue();
69 PhysicalPort extraPhyscialPort =
70 TyperUtils.getTypedRowWrapper(transaction.getDatabaseSchema(), PhysicalPort.class);
71 extraPhyscialPort.setName("");
72 LOG.trace("execute: updating physical port: {}", physicalPort);
73 transaction.add(op.update(physicalPort)
74 .where(extraPhyscialPort.getNameColumn().getSchema().opEqual(existingPhysicalPortName))
76 updateControllerTxHistory(TransactionType.UPDATE, physicalPort);
78 LOG.warn("Unable to update physical port {} because it was not found in the operational store, "
79 + "and thus we cannot retrieve its UUID", port.getHwvtepNodeName().getValue());
84 protected Map<InstanceIdentifier<Node>, List<HwvtepPhysicalPortAugmentation>> extractRemovedPorts(
85 Collection<DataTreeModification<Node>> changes, Class<HwvtepPhysicalPortAugmentation> class1) {
86 Map<InstanceIdentifier<Node>, List<HwvtepPhysicalPortAugmentation>> result = new HashMap<>();
87 if (changes != null && !changes.isEmpty()) {
88 for (DataTreeModification<Node> change : changes) {
89 final InstanceIdentifier<Node> key = change.getRootPath().getRootIdentifier();
90 final DataObjectModification<Node> mod = change.getRootNode();
91 //If the node which physical ports belong to is removed, all physical ports
92 //should be removed too.
93 Node removed = TransactUtils.getRemoved(mod);
94 if (removed != null) {
95 List<HwvtepPhysicalPortAugmentation> lswitchListRemoved = new ArrayList<>();
96 if (removed.getTerminationPoint() != null) {
97 for (TerminationPoint tp : removed.getTerminationPoint()) {
98 HwvtepPhysicalPortAugmentation hppAugmentation = tp.getAugmentation(HwvtepPhysicalPortAugmentation.class);
99 if (hppAugmentation != null) {
100 lswitchListRemoved.add(hppAugmentation);
104 if (!lswitchListRemoved.isEmpty()) {
105 result.put(key, lswitchListRemoved);
108 //If the node which physical ports belong to is updated, and physical ports may
109 //be created or updated or deleted, we need to get deleted ones.
110 Node updated = TransactUtils.getUpdated(mod);
111 Node before = mod.getDataBefore();
112 if (updated != null && before != null) {
113 List<HwvtepPhysicalPortAugmentation> portListUpdated = new ArrayList<>();
114 List<HwvtepPhysicalPortAugmentation> portListBefore = new ArrayList<>();
115 List<HwvtepPhysicalPortAugmentation> portListRemoved = new ArrayList<>();
116 if (updated.getTerminationPoint() != null) {
117 for (TerminationPoint tp : updated.getTerminationPoint()) {
118 HwvtepPhysicalPortAugmentation hppAugmentation = tp.getAugmentation(HwvtepPhysicalPortAugmentation.class);
119 if (hppAugmentation != null) {
120 portListUpdated.add(hppAugmentation);
124 if (before.getTerminationPoint() != null) {
125 for (TerminationPoint tp : before.getTerminationPoint()) {
126 HwvtepPhysicalPortAugmentation hppAugmentation = tp.getAugmentation(HwvtepPhysicalPortAugmentation.class);
127 if (hppAugmentation != null) {
128 portListBefore.add(hppAugmentation);
132 portListBefore.removeAll(portListUpdated);
133 //then exclude updated physical ports
134 for (HwvtepPhysicalPortAugmentation portBefore: portListBefore) {
136 for(; i < portListUpdated.size(); i++) {
137 if (portBefore.getHwvtepNodeName().equals(portListUpdated.get(i).getHwvtepNodeName())) {
141 if (i == portListUpdated.size()) {
142 portListRemoved.add(portBefore);
145 if (!portListRemoved.isEmpty()) {
146 result.put(key, portListRemoved);