2 * Copyright (c) 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.Collection;
14 import java.util.List;
16 import java.util.Map.Entry;
17 import java.util.Objects;
19 import org.opendaylight.controller.md.sal.binding.api.DataTreeModification;
20 import org.opendaylight.ovsdb.hwvtepsouthbound.HwvtepDeviceInfo;
21 import org.opendaylight.ovsdb.hwvtepsouthbound.HwvtepSouthboundUtil;
22 import org.opendaylight.ovsdb.lib.notation.UUID;
23 import org.opendaylight.ovsdb.lib.operations.TransactionBuilder;
24 import org.opendaylight.ovsdb.lib.schema.typed.TyperUtils;
25 import org.opendaylight.ovsdb.schema.hardwarevtep.LogicalSwitch;
26 import org.opendaylight.ovsdb.schema.hardwarevtep.McastMacsLocal;
27 import org.opendaylight.ovsdb.schema.hardwarevtep.McastMacsRemote;
28 import org.opendaylight.ovsdb.schema.hardwarevtep.UcastMacsLocal;
29 import org.opendaylight.ovsdb.schema.hardwarevtep.UcastMacsRemote;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepGlobalAugmentation;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.LogicalSwitches;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.RemoteMcastMacs;
33 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
34 import org.opendaylight.yangtools.yang.binding.Identifiable;
35 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
39 import com.google.common.base.Optional;
41 public class LogicalSwitchRemoveCommand extends AbstractTransactCommand<LogicalSwitches, HwvtepGlobalAugmentation> {
42 private static final Logger LOG = LoggerFactory.getLogger(LogicalSwitchRemoveCommand.class);
44 public LogicalSwitchRemoveCommand(HwvtepOperationalState state,
45 Collection<DataTreeModification<Node>> changes) {
46 super(state, changes);
50 public void execute(TransactionBuilder transaction) {
51 Map<InstanceIdentifier<Node>, List<LogicalSwitches>> removeds =
52 extractRemoved(getChanges(),LogicalSwitches.class);
53 if (removeds != null) {
54 for (Entry<InstanceIdentifier<Node>, List<LogicalSwitches>> created: removeds.entrySet()) {
55 if (!HwvtepSouthboundUtil.isEmpty(created.getValue())) {
56 for (LogicalSwitches lswitch : created.getValue()) {
57 InstanceIdentifier<LogicalSwitches> lsKey = created.getKey().augmentation(
58 HwvtepGlobalAugmentation.class).child(LogicalSwitches.class, lswitch.getKey());
59 updateCurrentTxDeleteData(LogicalSwitches.class, lsKey, lswitch);
61 getOperationalState().getDeviceInfo().scheduleTransaction(new TransactCommand() {
63 public void execute(TransactionBuilder transactionBuilder) {
64 LOG.debug("Running delete logical switch in seperate tx {}", created.getKey());
65 removeLogicalSwitch(transactionBuilder, created.getKey(), created.getValue());
69 public void onConfigUpdate(TransactionBuilder transaction, InstanceIdentifier nodeIid,
70 Identifiable data, InstanceIdentifier key, Object... extraData) {
74 public void doDeviceTransaction(TransactionBuilder transaction, InstanceIdentifier nodeIid,
75 Identifiable data, InstanceIdentifier key, Object... extraData) {
83 private void removeLogicalSwitch(final TransactionBuilder transaction,
84 final InstanceIdentifier<Node> nodeIid, final List<LogicalSwitches> lswitchList) {
85 for (LogicalSwitches lswitch: lswitchList) {
86 InstanceIdentifier<LogicalSwitches> lsKey = nodeIid.augmentation(HwvtepGlobalAugmentation.class).
87 child(LogicalSwitches.class, lswitch.getKey());
88 onConfigUpdate(transaction, nodeIid, lswitch, lsKey);
93 public void onConfigUpdate(final TransactionBuilder transaction,
94 final InstanceIdentifier<Node> nodeIid,
95 final LogicalSwitches lswitch,
96 final InstanceIdentifier lsKey,
97 final Object... extraData) {
98 processDependencies(null, transaction, nodeIid, lsKey, lswitch);
102 public void doDeviceTransaction(final TransactionBuilder transaction,
103 final InstanceIdentifier<Node> instanceIdentifier,
104 final LogicalSwitches lswitch,
105 final InstanceIdentifier lsKey,
106 final Object... extraData) {
107 LOG.debug("Removing logical switch named: {}", lswitch.getHwvtepNodeName().getValue());
108 HwvtepDeviceInfo.DeviceData deviceData = getOperationalState().getDeviceInfo().getDeviceOperData(
109 LogicalSwitches.class, lsKey);
110 LogicalSwitch logicalSwitch = TyperUtils.getTypedRowWrapper(transaction.getDatabaseSchema(), LogicalSwitch.class, null);
112 if (deviceData != null && deviceData.getUuid() != null) {
113 UUID logicalSwitchUuid = deviceData.getUuid();
114 transaction.add(op.delete(logicalSwitch.getSchema())
115 .where(logicalSwitch.getUuidColumn().getSchema().opEqual(logicalSwitchUuid)).build());
117 UcastMacsRemote ucastMacsRemote = TyperUtils.getTypedRowWrapper(transaction.getDatabaseSchema(),
118 UcastMacsRemote.class, null);
119 transaction.add(op.delete(ucastMacsRemote.getSchema())
120 .where(ucastMacsRemote.getLogicalSwitchColumn().getSchema().opEqual(logicalSwitchUuid)).build());
122 UcastMacsLocal ucastMacsLocal = TyperUtils.getTypedRowWrapper(transaction.getDatabaseSchema(),
123 UcastMacsLocal.class, null);
124 transaction.add(op.delete(ucastMacsLocal.getSchema())
125 .where(ucastMacsLocal.getLogicalSwitchColumn().getSchema().opEqual(logicalSwitchUuid)).build());
127 McastMacsRemote mcastMacsRemote = TyperUtils.getTypedRowWrapper(transaction.getDatabaseSchema(),
128 McastMacsRemote.class, null);
129 transaction.add(op.delete(mcastMacsRemote.getSchema())
130 .where(mcastMacsRemote.getLogicalSwitchColumn().getSchema().opEqual(logicalSwitchUuid)).build());
132 McastMacsLocal mcastMacsLocal = TyperUtils.getTypedRowWrapper(transaction.getDatabaseSchema(),
133 McastMacsLocal.class, null);
134 transaction.add(op.delete(mcastMacsLocal.getSchema())
135 .where(mcastMacsLocal.getLogicalSwitchColumn().getSchema().opEqual(logicalSwitchUuid)).build());
136 getOperationalState().getDeviceInfo().markKeyAsInTransit(RemoteMcastMacs.class, lsKey);
138 LOG.warn("Unable to delete logical switch {} because it was not found in the operational store",
139 lswitch.getHwvtepNodeName().getValue());
144 protected List<LogicalSwitches> getData(HwvtepGlobalAugmentation augmentation) {
145 return augmentation.getLogicalSwitches();
149 protected boolean areEqual(LogicalSwitches a , LogicalSwitches b) {
150 return a.getKey().equals(b.getKey()) && Objects.equals(a.getTunnelKey(), b.getTunnelKey());
154 protected boolean isRemoveCommand() {