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.Collections;
15 import java.util.List;
17 import java.util.Map.Entry;
18 import java.util.Objects;
20 import org.opendaylight.controller.md.sal.binding.api.DataTreeModification;
21 import org.opendaylight.ovsdb.hwvtepsouthbound.HwvtepConnectionInstance;
22 import org.opendaylight.ovsdb.hwvtepsouthbound.HwvtepDeviceInfo;
23 import org.opendaylight.ovsdb.hwvtepsouthbound.HwvtepSouthboundUtil;
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.LogicalSwitch;
28 import org.opendaylight.ovsdb.schema.hardwarevtep.McastMacsLocal;
29 import org.opendaylight.ovsdb.schema.hardwarevtep.McastMacsRemote;
30 import org.opendaylight.ovsdb.schema.hardwarevtep.UcastMacsLocal;
31 import org.opendaylight.ovsdb.schema.hardwarevtep.UcastMacsRemote;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepGlobalAugmentation;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.LogicalSwitches;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.RemoteMcastMacs;
35 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
36 import org.opendaylight.yangtools.yang.binding.Identifiable;
37 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
41 import com.google.common.base.Optional;
43 public class LogicalSwitchRemoveCommand extends AbstractTransactCommand<LogicalSwitches, HwvtepGlobalAugmentation> {
44 private static final Logger LOG = LoggerFactory.getLogger(LogicalSwitchRemoveCommand.class);
46 public LogicalSwitchRemoveCommand(HwvtepOperationalState state,
47 Collection<DataTreeModification<Node>> changes) {
48 super(state, changes);
52 public void execute(TransactionBuilder transaction) {
53 Map<InstanceIdentifier<Node>, List<LogicalSwitches>> removeds =
54 extractRemoved(getChanges(),LogicalSwitches.class);
55 if (removeds != null) {
56 for (Entry<InstanceIdentifier<Node>, List<LogicalSwitches>> created: removeds.entrySet()) {
57 if (!HwvtepSouthboundUtil.isEmpty(created.getValue())) {
58 HwvtepConnectionInstance connectionInstance = getDeviceInfo().getConnectionInstance();
59 getDeviceInfo().scheduleTransaction(new TransactCommand() {
61 public void execute(TransactionBuilder transactionBuilder) {
62 HwvtepOperationalState operState = new HwvtepOperationalState(
63 connectionInstance.getDataBroker(), connectionInstance, Collections.EMPTY_LIST);
64 threadLocalOperationalState.set(operState);
65 threadLocalDeviceTransaction.set(transactionBuilder);
66 LOG.debug("Running delete logical switch in seperate tx {}", created.getKey());
67 removeLogicalSwitch(transactionBuilder, created.getKey(), created.getValue());
71 public void onSuccess(TransactionBuilder deviceTransaction) {
72 LogicalSwitchRemoveCommand.this.onSuccess(deviceTransaction);
76 public void onFailure(TransactionBuilder deviceTransaction) {
77 LogicalSwitchRemoveCommand.this.onFailure(deviceTransaction);
85 private void removeLogicalSwitch(final TransactionBuilder transaction,
86 final InstanceIdentifier<Node> nodeIid, final List<LogicalSwitches> lswitchList) {
87 for (LogicalSwitches lswitch: lswitchList) {
88 InstanceIdentifier<LogicalSwitches> lsKey = nodeIid.augmentation(HwvtepGlobalAugmentation.class).
89 child(LogicalSwitches.class, lswitch.getKey());
90 onConfigUpdate(transaction, nodeIid, lswitch, lsKey);
95 public void onConfigUpdate(final TransactionBuilder transaction,
96 final InstanceIdentifier<Node> nodeIid,
97 final LogicalSwitches lswitch,
98 final InstanceIdentifier lsKey,
99 final Object... extraData) {
100 processDependencies(null, transaction, nodeIid, lsKey, lswitch);
104 public void doDeviceTransaction(final TransactionBuilder transaction,
105 final InstanceIdentifier<Node> instanceIdentifier,
106 final LogicalSwitches lswitch,
107 final InstanceIdentifier lsKey,
108 final Object... extraData) {
109 LOG.debug("Removing logical switch named: {}", lswitch.getHwvtepNodeName().getValue());
110 HwvtepDeviceInfo.DeviceData deviceData = getOperationalState().getDeviceInfo().getDeviceOperData(
111 LogicalSwitches.class, lsKey);
112 LogicalSwitch logicalSwitch = TyperUtils.getTypedRowWrapper(transaction.getDatabaseSchema(), LogicalSwitch.class, null);
114 if (deviceData != null && deviceData.getUuid() != null) {
115 UUID logicalSwitchUuid = deviceData.getUuid();
116 transaction.add(op.delete(logicalSwitch.getSchema())
117 .where(logicalSwitch.getUuidColumn().getSchema().opEqual(logicalSwitchUuid)).build());
119 UcastMacsRemote ucastMacsRemote = TyperUtils.getTypedRowWrapper(transaction.getDatabaseSchema(),
120 UcastMacsRemote.class, null);
121 transaction.add(op.delete(ucastMacsRemote.getSchema())
122 .where(ucastMacsRemote.getLogicalSwitchColumn().getSchema().opEqual(logicalSwitchUuid)).build());
124 UcastMacsLocal ucastMacsLocal = TyperUtils.getTypedRowWrapper(transaction.getDatabaseSchema(),
125 UcastMacsLocal.class, null);
126 transaction.add(op.delete(ucastMacsLocal.getSchema())
127 .where(ucastMacsLocal.getLogicalSwitchColumn().getSchema().opEqual(logicalSwitchUuid)).build());
129 McastMacsRemote mcastMacsRemote = TyperUtils.getTypedRowWrapper(transaction.getDatabaseSchema(),
130 McastMacsRemote.class, null);
131 transaction.add(op.delete(mcastMacsRemote.getSchema())
132 .where(mcastMacsRemote.getLogicalSwitchColumn().getSchema().opEqual(logicalSwitchUuid)).build());
134 McastMacsLocal mcastMacsLocal = TyperUtils.getTypedRowWrapper(transaction.getDatabaseSchema(),
135 McastMacsLocal.class, null);
136 transaction.add(op.delete(mcastMacsLocal.getSchema())
137 .where(mcastMacsLocal.getLogicalSwitchColumn().getSchema().opEqual(logicalSwitchUuid)).build());
138 updateCurrentTxDeleteData(LogicalSwitches.class, lsKey, lswitch);
140 LOG.warn("Unable to delete logical switch {} because it was not found in the operational store",
141 lswitch.getHwvtepNodeName().getValue());
146 protected List<LogicalSwitches> getData(HwvtepGlobalAugmentation augmentation) {
147 return augmentation.getLogicalSwitches();
151 protected boolean areEqual(LogicalSwitches a , LogicalSwitches b) {
152 return a.getKey().equals(b.getKey()) && Objects.equals(a.getTunnelKey(), b.getTunnelKey());
156 protected boolean isRemoveCommand() {
161 public void onCommandSucceeded() {
162 if (getDeviceTransaction() == null || !updates.containsKey(getDeviceTransaction())) {
165 for (MdsalUpdate mdsalUpdate : updates.get(getDeviceTransaction())) {
166 getDeviceInfo().clearLogicalSwitchRefs((InstanceIdentifier<LogicalSwitches>) mdsalUpdate.getKey());