f09ca6a1a87d26e481fd223cda562a5a64a62489
[ovsdb.git] / hwvtepsouthbound / hwvtepsouthbound-impl / src / main / java / org / opendaylight / ovsdb / hwvtepsouthbound / transact / LogicalSwitchRemoveCommand.java
1 /*
2  * Copyright (c) 2015, 2017 China Telecom Beijing Research Institute and others.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.ovsdb.hwvtepsouthbound.transact;
10
11 import static org.opendaylight.ovsdb.lib.operations.Operations.op;
12
13 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
14 import java.util.Collection;
15 import java.util.Collections;
16 import java.util.List;
17 import java.util.Map;
18 import java.util.Map.Entry;
19 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.ovsdb.utils.mdsal.utils.TransactionType;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepGlobalAugmentation;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.LogicalSwitches;
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.InstanceIdentifier;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
39
40 public class LogicalSwitchRemoveCommand extends AbstractTransactCommand<LogicalSwitches, HwvtepGlobalAugmentation> {
41     private static final Logger LOG = LoggerFactory.getLogger(LogicalSwitchRemoveCommand.class);
42
43     public LogicalSwitchRemoveCommand(HwvtepOperationalState state,
44             Collection<DataTreeModification<Node>> changes) {
45         super(state, changes);
46     }
47
48     @Override
49     public void execute(TransactionBuilder transaction) {
50         Map<InstanceIdentifier<Node>, List<LogicalSwitches>> removeds =
51                 extractRemoved(getChanges(),LogicalSwitches.class);
52
53         for (Entry<InstanceIdentifier<Node>, List<LogicalSwitches>> created: removeds.entrySet()) {
54             if (!HwvtepSouthboundUtil.isEmpty(created.getValue())) {
55                 HwvtepConnectionInstance connectionInstance = getDeviceInfo().getConnectionInstance();
56                 getDeviceInfo().scheduleTransaction(new TransactCommand() {
57                     @Override
58                     public void execute(TransactionBuilder transactionBuilder) {
59                         HwvtepOperationalState operState = new HwvtepOperationalState(
60                                 connectionInstance.getDataBroker(), connectionInstance, Collections.EMPTY_LIST);
61                         hwvtepOperationalState = operState;
62                         deviceTransaction = deviceTransaction;
63                         LOG.debug("Running delete logical switch in seperate tx {}", created.getKey());
64                         removeLogicalSwitch(transactionBuilder, created.getKey(), created.getValue());
65                     }
66
67                     @Override
68                     public void onSuccess(TransactionBuilder deviceTransaction) {
69                         LogicalSwitchRemoveCommand.this.onSuccess(deviceTransaction);
70                     }
71
72                     @Override
73                     public void onFailure(TransactionBuilder deviceTransaction) {
74                         LogicalSwitchRemoveCommand.this.onFailure(deviceTransaction);
75                     }
76                 });
77             }
78         }
79     }
80
81     @SuppressFBWarnings(value = "UPM_UNCALLED_PRIVATE_METHOD",
82             justification = "https://github.com/spotbugs/spotbugs/issues/811")
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.key());
88             onConfigUpdate(transaction, nodeIid, lswitch, lsKey);
89         }
90     }
91
92     @Override
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);
99     }
100
101     @Override
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(),
111                 LogicalSwitch.class, null);
112
113         if (deviceData != null && deviceData.getUuid() != null) {
114             UUID logicalSwitchUuid = deviceData.getUuid();
115             transaction.add(op.delete(logicalSwitch.getSchema())
116                     .where(logicalSwitch.getUuidColumn().getSchema().opEqual(logicalSwitchUuid)).build());
117
118             UcastMacsRemote ucastMacsRemote = TyperUtils.getTypedRowWrapper(transaction.getDatabaseSchema(),
119                     UcastMacsRemote.class, null);
120             transaction.add(op.delete(ucastMacsRemote.getSchema())
121                     .where(ucastMacsRemote.getLogicalSwitchColumn().getSchema().opEqual(logicalSwitchUuid)).build());
122
123             UcastMacsLocal ucastMacsLocal = TyperUtils.getTypedRowWrapper(transaction.getDatabaseSchema(),
124                     UcastMacsLocal.class, null);
125             transaction.add(op.delete(ucastMacsLocal.getSchema())
126                     .where(ucastMacsLocal.getLogicalSwitchColumn().getSchema().opEqual(logicalSwitchUuid)).build());
127
128             McastMacsRemote mcastMacsRemote = TyperUtils.getTypedRowWrapper(transaction.getDatabaseSchema(),
129                     McastMacsRemote.class, null);
130             transaction.add(op.delete(mcastMacsRemote.getSchema())
131                     .where(mcastMacsRemote.getLogicalSwitchColumn().getSchema().opEqual(logicalSwitchUuid)).build());
132
133             McastMacsLocal mcastMacsLocal = TyperUtils.getTypedRowWrapper(transaction.getDatabaseSchema(),
134                     McastMacsLocal.class, null);
135             transaction.add(op.delete(mcastMacsLocal.getSchema())
136                     .where(mcastMacsLocal.getLogicalSwitchColumn().getSchema().opEqual(logicalSwitchUuid)).build());
137             updateCurrentTxDeleteData(LogicalSwitches.class, lsKey, lswitch);
138             updateControllerTxHistory(TransactionType.DELETE, lswitch);
139         } else {
140             LOG.warn("Unable to delete logical switch {} because it was not found in the operational store",
141                     lswitch.getHwvtepNodeName().getValue());
142         }
143     }
144
145     @Override
146     protected List<LogicalSwitches> getData(HwvtepGlobalAugmentation augmentation) {
147         return augmentation.getLogicalSwitches();
148     }
149
150     @Override
151     protected boolean areEqual(LogicalSwitches sw1, LogicalSwitches sw2) {
152         return sw1.key().equals(sw2.key()) && Objects.equals(sw1.getTunnelKey(), sw2.getTunnelKey());
153     }
154
155     @Override
156     protected boolean isRemoveCommand() {
157         return true;
158     }
159
160     @Override
161     public void onCommandSucceeded() {
162         if (getDeviceTransaction() == null || !updates.containsKey(getDeviceTransaction())) {
163             return;
164         }
165         for (MdsalUpdate mdsalUpdate : updates.get(getDeviceTransaction())) {
166             getDeviceInfo().clearLogicalSwitchRefs(mdsalUpdate.getKey());
167         }
168     }
169 }