2 * Copyright (c) 2015 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.hwvtepsouthbound.HwvtepSouthboundMapper;
23 import org.opendaylight.ovsdb.lib.notation.Mutator;
24 import org.opendaylight.ovsdb.lib.notation.UUID;
25 import org.opendaylight.ovsdb.lib.operations.TransactionBuilder;
26 import org.opendaylight.ovsdb.lib.schema.typed.TyperUtils;
27 import org.opendaylight.ovsdb.schema.hardwarevtep.Global;
28 import org.opendaylight.ovsdb.schema.hardwarevtep.PhysicalPort;
29 import org.opendaylight.ovsdb.schema.hardwarevtep.PhysicalSwitch;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepPhysicalPortAugmentation;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.PhysicalSwitchAugmentation;
32 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
33 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.node.TerminationPoint;
34 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
38 import com.google.common.base.Optional;
39 import com.google.common.collect.Sets;
41 public class PhysicalPortRemoveCommand extends AbstractTransactCommand {
42 private static final Logger LOG = LoggerFactory.getLogger(PhysicalPortRemoveCommand.class);
44 public PhysicalPortRemoveCommand(HwvtepOperationalState state,
45 Collection<DataTreeModification<Node>> changes) {
46 super(state, changes);
50 public void execute(TransactionBuilder transaction) {
51 Map<InstanceIdentifier<Node>, List<HwvtepPhysicalPortAugmentation>> removeds =
52 extractRemoved(getChanges(),HwvtepPhysicalPortAugmentation.class);
53 if (!removeds.isEmpty()) {
54 for (Entry<InstanceIdentifier<Node>, List<HwvtepPhysicalPortAugmentation>> removed:
55 removeds.entrySet()) {
56 removePhysicalPort(transaction, removed.getKey(), removed.getValue());
61 private void removePhysicalPort(TransactionBuilder transaction,
62 InstanceIdentifier<Node> psNodeiid,
63 List<HwvtepPhysicalPortAugmentation> listPort) {
64 for (HwvtepPhysicalPortAugmentation port : listPort) {
65 LOG.debug("Removing a physical port named: {}", port.getHwvtepNodeName().getValue());
66 Optional<HwvtepPhysicalPortAugmentation> operationalPhysicalPortOptional =
67 getOperationalState().getPhysicalPortAugmentation(psNodeiid, port.getHwvtepNodeName());
68 PhysicalPort physicalPort = TyperUtils.getTypedRowWrapper(transaction.getDatabaseSchema(), PhysicalPort.class, null);
69 //get managing global node of physicalSwitchBelong
70 //InstanceIdentifier<?> globalNodeIid = physicalSwitchBelong.getManagedBy().getValue();
71 if (operationalPhysicalPortOptional.isPresent()) {
72 UUID physicalPortUuid = new UUID(operationalPhysicalPortOptional.get().getPhysicalPortUuid().getValue());
73 PhysicalSwitch physicalSwitch = TyperUtils.getTypedRowWrapper(transaction.getDatabaseSchema(),
74 PhysicalSwitch.class, null);
75 transaction.add(op.delete(physicalPort.getSchema())
76 .where(physicalPort.getUuidColumn().getSchema().opEqual(physicalPortUuid)).build());
77 transaction.add(op.comment("Physical Port: Deleting " + port.getHwvtepNodeName().getValue()));
78 transaction.add(op.mutate(physicalSwitch.getSchema())
79 .addMutation(physicalSwitch.getPortsColumn().getSchema(), Mutator.DELETE,
80 Sets.newHashSet(physicalPortUuid)));
81 transaction.add(op.comment("Physical Switch: Mutating " + port.getHwvtepNodeName().getValue() + " " + physicalPortUuid));
83 LOG.warn("Unable to delete logical switch {} because it was not found in the operational store, "
84 + "and thus we cannot retrieve its UUID", port.getHwvtepNodeName().getValue());
89 private Map<InstanceIdentifier<Node>, List<HwvtepPhysicalPortAugmentation>> extractRemoved(
90 Collection<DataTreeModification<Node>> changes, Class<HwvtepPhysicalPortAugmentation> class1) {
91 Map<InstanceIdentifier<Node>, List<HwvtepPhysicalPortAugmentation>> result
92 = new HashMap<InstanceIdentifier<Node>, List<HwvtepPhysicalPortAugmentation>>();
93 if (changes != null && !changes.isEmpty()) {
94 for (DataTreeModification<Node> change : changes) {
95 final InstanceIdentifier<Node> key = change.getRootPath().getRootIdentifier();
96 final DataObjectModification<Node> mod = change.getRootNode();
97 //If the node which physical ports belong to is removed, all physical ports
98 //should be removed too.
99 Node removed = TransactUtils.getRemoved(mod);
100 if (removed != null) {
101 List<HwvtepPhysicalPortAugmentation> lswitchListRemoved = new ArrayList<HwvtepPhysicalPortAugmentation>();
102 if (removed.getTerminationPoint() != null) {
103 for (TerminationPoint tp : removed.getTerminationPoint()) {
104 HwvtepPhysicalPortAugmentation hppAugmentation = tp.getAugmentation(HwvtepPhysicalPortAugmentation.class);
105 if (hppAugmentation != null) {
106 lswitchListRemoved.add(hppAugmentation);
110 if (!lswitchListRemoved.isEmpty()) {
111 result.put(key, lswitchListRemoved);
114 //If the node which physical ports belong to is updated, and physical ports may
115 //be created or updated or deleted, we need to get deleted ones.
116 Node updated = TransactUtils.getUpdated(mod);
117 Node before = mod.getDataBefore();
118 if (updated != null && before != null) {
119 List<HwvtepPhysicalPortAugmentation> portListUpdated = new ArrayList<HwvtepPhysicalPortAugmentation>();
120 List<HwvtepPhysicalPortAugmentation> portListBefore = new ArrayList<HwvtepPhysicalPortAugmentation>();
121 List<HwvtepPhysicalPortAugmentation> portListRemoved = new ArrayList<HwvtepPhysicalPortAugmentation>();
122 if (updated.getTerminationPoint() != null) {
123 for (TerminationPoint tp : updated.getTerminationPoint()) {
124 HwvtepPhysicalPortAugmentation hppAugmentation = tp.getAugmentation(HwvtepPhysicalPortAugmentation.class);
125 if (hppAugmentation != null) {
126 portListUpdated.add(hppAugmentation);
130 if (before.getTerminationPoint() != null) {
131 for (TerminationPoint tp : before.getTerminationPoint()) {
132 HwvtepPhysicalPortAugmentation hppAugmentation = tp.getAugmentation(HwvtepPhysicalPortAugmentation.class);
133 if (hppAugmentation != null) {
134 portListBefore.add(hppAugmentation);
138 portListBefore.removeAll(portListUpdated);
139 //then exclude updated physical ports
140 for (HwvtepPhysicalPortAugmentation portBefore: portListBefore) {
142 for(; i < portListUpdated.size(); i++) {
143 if (portBefore.getHwvtepNodeName().equals(portListUpdated.get(i).getHwvtepNodeName())) {
147 if (i == portListUpdated.size()) {
148 portListRemoved.add(portBefore);
151 if (!portListRemoved.isEmpty()) {
152 result.put(key, portListRemoved);