Make TransactionBuilder type-aware
[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 package org.opendaylight.ovsdb.hwvtepsouthbound.transact;
9
10 import static org.opendaylight.ovsdb.lib.operations.Operations.op;
11
12 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
13 import java.util.Collection;
14 import java.util.Collections;
15 import java.util.List;
16 import java.util.Map;
17 import java.util.Map.Entry;
18 import java.util.Objects;
19 import org.opendaylight.controller.md.sal.binding.api.DataTreeModification;
20 import org.opendaylight.ovsdb.hwvtepsouthbound.HwvtepConnectionInstance;
21 import org.opendaylight.ovsdb.hwvtepsouthbound.HwvtepDeviceInfo;
22 import org.opendaylight.ovsdb.hwvtepsouthbound.HwvtepSouthboundUtil;
23 import org.opendaylight.ovsdb.lib.notation.UUID;
24 import org.opendaylight.ovsdb.lib.operations.TransactionBuilder;
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.ovsdb.utils.mdsal.utils.TransactionType;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepGlobalAugmentation;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.LogicalSwitches;
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.InstanceIdentifier;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
37
38 public class LogicalSwitchRemoveCommand extends AbstractTransactCommand<LogicalSwitches, HwvtepGlobalAugmentation> {
39     private static final Logger LOG = LoggerFactory.getLogger(LogicalSwitchRemoveCommand.class);
40
41     public LogicalSwitchRemoveCommand(final HwvtepOperationalState state,
42             final Collection<DataTreeModification<Node>> changes) {
43         super(state, changes);
44     }
45
46     @Override
47     public void execute(final TransactionBuilder transaction) {
48         Map<InstanceIdentifier<Node>, List<LogicalSwitches>> removeds =
49                 extractRemoved(getChanges(),LogicalSwitches.class);
50
51         for (Entry<InstanceIdentifier<Node>, List<LogicalSwitches>> created: removeds.entrySet()) {
52             if (!HwvtepSouthboundUtil.isEmpty(created.getValue())) {
53                 HwvtepConnectionInstance connectionInstance = getDeviceInfo().getConnectionInstance();
54                 getDeviceInfo().scheduleTransaction(new TransactCommand() {
55                     @Override
56                     public void execute(final TransactionBuilder transactionBuilder) {
57                         HwvtepOperationalState operState = new HwvtepOperationalState(
58                                 connectionInstance.getDataBroker(), connectionInstance, Collections.EMPTY_LIST);
59                         hwvtepOperationalState = operState;
60                         deviceTransaction = deviceTransaction;
61                         LOG.debug("Running delete logical switch in seperate tx {}", created.getKey());
62                         removeLogicalSwitch(transactionBuilder, created.getKey(), created.getValue());
63                     }
64
65                     @Override
66                     public void onSuccess(final TransactionBuilder deviceTransaction) {
67                         LogicalSwitchRemoveCommand.this.onSuccess(deviceTransaction);
68                     }
69
70                     @Override
71                     public void onFailure(final TransactionBuilder deviceTransaction) {
72                         LogicalSwitchRemoveCommand.this.onFailure(deviceTransaction);
73                     }
74                 });
75             }
76         }
77     }
78
79     @SuppressFBWarnings(value = "UPM_UNCALLED_PRIVATE_METHOD",
80             justification = "https://github.com/spotbugs/spotbugs/issues/811")
81     private void removeLogicalSwitch(final TransactionBuilder transaction,
82                                      final InstanceIdentifier<Node> nodeIid, final List<LogicalSwitches> lswitchList) {
83         for (LogicalSwitches lswitch: lswitchList) {
84             InstanceIdentifier<LogicalSwitches> lsKey = nodeIid.augmentation(HwvtepGlobalAugmentation.class)
85                     .child(LogicalSwitches.class, lswitch.key());
86             onConfigUpdate(transaction, nodeIid, lswitch, lsKey);
87         }
88     }
89
90     @Override
91     public void onConfigUpdate(final TransactionBuilder transaction,
92                                final InstanceIdentifier<Node> nodeIid,
93                                final LogicalSwitches lswitch,
94                                final InstanceIdentifier lsKey,
95                                final Object... extraData) {
96         processDependencies(null, transaction, nodeIid, lsKey, lswitch);
97     }
98
99     @Override
100     public void doDeviceTransaction(final TransactionBuilder transaction,
101                                     final InstanceIdentifier<Node> instanceIdentifier,
102                                     final LogicalSwitches lswitch,
103                                     final InstanceIdentifier lsKey,
104                                     final Object... extraData) {
105         LOG.debug("Removing logical switch named: {}", lswitch.getHwvtepNodeName().getValue());
106         HwvtepDeviceInfo.DeviceData deviceData  = getOperationalState().getDeviceInfo().getDeviceOperData(
107                 LogicalSwitches.class, lsKey);
108         LogicalSwitch logicalSwitch = transaction.getTypedRowSchema(LogicalSwitch.class);
109
110         if (deviceData != null && deviceData.getUuid() != null) {
111             UUID logicalSwitchUuid = deviceData.getUuid();
112             transaction.add(op.delete(logicalSwitch.getSchema())
113                     .where(logicalSwitch.getUuidColumn().getSchema().opEqual(logicalSwitchUuid)).build());
114
115             UcastMacsRemote ucastMacsRemote = transaction.getTypedRowSchema(UcastMacsRemote.class);
116             transaction.add(op.delete(ucastMacsRemote.getSchema())
117                     .where(ucastMacsRemote.getLogicalSwitchColumn().getSchema().opEqual(logicalSwitchUuid)).build());
118
119             UcastMacsLocal ucastMacsLocal = transaction.getTypedRowSchema(UcastMacsLocal.class);
120             transaction.add(op.delete(ucastMacsLocal.getSchema())
121                     .where(ucastMacsLocal.getLogicalSwitchColumn().getSchema().opEqual(logicalSwitchUuid)).build());
122
123             McastMacsRemote mcastMacsRemote = transaction.getTypedRowSchema(McastMacsRemote.class);
124             transaction.add(op.delete(mcastMacsRemote.getSchema())
125                     .where(mcastMacsRemote.getLogicalSwitchColumn().getSchema().opEqual(logicalSwitchUuid)).build());
126
127             McastMacsLocal mcastMacsLocal = transaction.getTypedRowSchema(McastMacsLocal.class);
128             transaction.add(op.delete(mcastMacsLocal.getSchema())
129                     .where(mcastMacsLocal.getLogicalSwitchColumn().getSchema().opEqual(logicalSwitchUuid)).build());
130             updateCurrentTxDeleteData(LogicalSwitches.class, lsKey, lswitch);
131             updateControllerTxHistory(TransactionType.DELETE, lswitch);
132         } else {
133             LOG.warn("Unable to delete logical switch {} because it was not found in the operational store",
134                     lswitch.getHwvtepNodeName().getValue());
135         }
136     }
137
138     @Override
139     protected List<LogicalSwitches> getData(final HwvtepGlobalAugmentation augmentation) {
140         return augmentation.getLogicalSwitches();
141     }
142
143     @Override
144     protected boolean areEqual(final LogicalSwitches sw1, final LogicalSwitches sw2) {
145         return sw1.key().equals(sw2.key()) && Objects.equals(sw1.getTunnelKey(), sw2.getTunnelKey());
146     }
147
148     @Override
149     protected boolean isRemoveCommand() {
150         return true;
151     }
152
153     @Override
154     public void onCommandSucceeded() {
155         if (getDeviceTransaction() == null || !updates.containsKey(getDeviceTransaction())) {
156             return;
157         }
158         for (MdsalUpdate mdsalUpdate : updates.get(getDeviceTransaction())) {
159             getDeviceInfo().clearLogicalSwitchRefs(mdsalUpdate.getKey());
160         }
161     }
162 }