Added device transaction log cli
[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 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
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         if (removeds != null) {
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                             threadLocalOperationalState.set(operState);
62                             threadLocalDeviceTransaction.set(transactionBuilder);
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
82     private void removeLogicalSwitch(final TransactionBuilder transaction,
83                                      final InstanceIdentifier<Node> nodeIid, final List<LogicalSwitches> lswitchList) {
84         for (LogicalSwitches lswitch: lswitchList) {
85             InstanceIdentifier<LogicalSwitches> lsKey = nodeIid.augmentation(HwvtepGlobalAugmentation.class).
86                     child(LogicalSwitches.class, lswitch.getKey());
87             onConfigUpdate(transaction, nodeIid, lswitch, lsKey);
88         }
89     }
90
91     @Override
92     public void onConfigUpdate(final TransactionBuilder transaction,
93                                final InstanceIdentifier<Node> nodeIid,
94                                final LogicalSwitches lswitch,
95                                final InstanceIdentifier lsKey,
96                                final Object... extraData) {
97         processDependencies(null, transaction, nodeIid, lsKey, lswitch);
98     }
99
100     @Override
101     public void doDeviceTransaction(final TransactionBuilder transaction,
102                                     final InstanceIdentifier<Node> instanceIdentifier,
103                                     final LogicalSwitches lswitch,
104                                     final InstanceIdentifier lsKey,
105                                     final Object... extraData) {
106             LOG.debug("Removing logical switch named: {}", lswitch.getHwvtepNodeName().getValue());
107             HwvtepDeviceInfo.DeviceData deviceData  = getOperationalState().getDeviceInfo().getDeviceOperData(
108                     LogicalSwitches.class, lsKey);
109             LogicalSwitch logicalSwitch = TyperUtils.getTypedRowWrapper(transaction.getDatabaseSchema(), LogicalSwitch.class, null);
110
111             if (deviceData != null && deviceData.getUuid() != null) {
112                 UUID logicalSwitchUuid = deviceData.getUuid();
113                 transaction.add(op.delete(logicalSwitch.getSchema())
114                         .where(logicalSwitch.getUuidColumn().getSchema().opEqual(logicalSwitchUuid)).build());
115
116                 UcastMacsRemote ucastMacsRemote = TyperUtils.getTypedRowWrapper(transaction.getDatabaseSchema(),
117                         UcastMacsRemote.class, null);
118                 transaction.add(op.delete(ucastMacsRemote.getSchema())
119                         .where(ucastMacsRemote.getLogicalSwitchColumn().getSchema().opEqual(logicalSwitchUuid)).build());
120
121                 UcastMacsLocal ucastMacsLocal = TyperUtils.getTypedRowWrapper(transaction.getDatabaseSchema(),
122                         UcastMacsLocal.class, null);
123                 transaction.add(op.delete(ucastMacsLocal.getSchema())
124                         .where(ucastMacsLocal.getLogicalSwitchColumn().getSchema().opEqual(logicalSwitchUuid)).build());
125
126                 McastMacsRemote mcastMacsRemote = TyperUtils.getTypedRowWrapper(transaction.getDatabaseSchema(),
127                         McastMacsRemote.class, null);
128                 transaction.add(op.delete(mcastMacsRemote.getSchema())
129                         .where(mcastMacsRemote.getLogicalSwitchColumn().getSchema().opEqual(logicalSwitchUuid)).build());
130
131                 McastMacsLocal mcastMacsLocal = TyperUtils.getTypedRowWrapper(transaction.getDatabaseSchema(),
132                         McastMacsLocal.class, null);
133                 transaction.add(op.delete(mcastMacsLocal.getSchema())
134                         .where(mcastMacsLocal.getLogicalSwitchColumn().getSchema().opEqual(logicalSwitchUuid)).build());
135                 updateCurrentTxDeleteData(LogicalSwitches.class, lsKey, lswitch);
136                 updateControllerTxHistory(TransactionType.DELETE, lswitch);
137             } else {
138                 LOG.warn("Unable to delete logical switch {} because it was not found in the operational store",
139                         lswitch.getHwvtepNodeName().getValue());
140             }
141     }
142
143     @Override
144     protected List<LogicalSwitches> getData(HwvtepGlobalAugmentation augmentation) {
145         return augmentation.getLogicalSwitches();
146     }
147
148     @Override
149     protected boolean areEqual(LogicalSwitches a , LogicalSwitches b) {
150         return a.getKey().equals(b.getKey()) && Objects.equals(a.getTunnelKey(), b.getTunnelKey());
151     }
152
153     @Override
154     protected boolean isRemoveCommand() {
155         return true;
156     }
157
158     @Override
159     public void onCommandSucceeded() {
160         if (getDeviceTransaction() == null || !updates.containsKey(getDeviceTransaction())) {
161             return;
162         }
163         for (MdsalUpdate mdsalUpdate : updates.get(getDeviceTransaction())) {
164             getDeviceInfo().clearLogicalSwitchRefs((InstanceIdentifier<LogicalSwitches>) mdsalUpdate.getKey());
165         }
166     }
167 }