Make TransactionBuilder type-aware
[ovsdb.git] / hwvtepsouthbound / hwvtepsouthbound-impl / src / main / java / org / opendaylight / ovsdb / hwvtepsouthbound / transact / UcastMacsRemoteRemoveCommand.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 java.util.Collection;
13 import java.util.List;
14 import java.util.Map;
15 import java.util.Map.Entry;
16 import org.opendaylight.controller.md.sal.binding.api.DataTreeModification;
17 import org.opendaylight.ovsdb.hwvtepsouthbound.HwvtepDeviceInfo;
18 import org.opendaylight.ovsdb.lib.notation.UUID;
19 import org.opendaylight.ovsdb.lib.operations.TransactionBuilder;
20 import org.opendaylight.ovsdb.schema.hardwarevtep.UcastMacsRemote;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepGlobalAugmentation;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.LogicalSwitches;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.RemoteUcastMacs;
24 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
25 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
28
29 public class UcastMacsRemoteRemoveCommand extends AbstractTransactCommand<RemoteUcastMacs, HwvtepGlobalAugmentation> {
30     private static final Logger LOG = LoggerFactory.getLogger(UcastMacsRemoteRemoveCommand.class);
31
32     public UcastMacsRemoteRemoveCommand(final HwvtepOperationalState state,
33             final Collection<DataTreeModification<Node>> changes) {
34         super(state, changes);
35     }
36
37     @Override
38     public void execute(final TransactionBuilder transaction) {
39         Map<InstanceIdentifier<Node>, List<RemoteUcastMacs>> removeds =
40                 extractRemoved(getChanges(),RemoteUcastMacs.class);
41         if (!removeds.isEmpty()) {
42             for (Entry<InstanceIdentifier<Node>, List<RemoteUcastMacs>> removed:
43                 removeds.entrySet()) {
44                 removeUcastMacRemote(transaction,  removed.getKey(), removed.getValue());
45             }
46         }
47     }
48
49
50     private void removeUcastMacRemote(final TransactionBuilder transaction,
51                                       final InstanceIdentifier<Node> instanceIdentifier,
52                                       final List<RemoteUcastMacs> macList) {
53         for (RemoteUcastMacs mac: macList) {
54             onConfigUpdate(transaction, instanceIdentifier, mac, null);
55         }
56     }
57
58     @Override
59     public void onConfigUpdate(final TransactionBuilder transaction,
60                                final InstanceIdentifier<Node> nodeIid,
61                                final RemoteUcastMacs remoteUcastMacs,
62                                final InstanceIdentifier macKey,
63                                final Object... extraData) {
64         InstanceIdentifier<RemoteUcastMacs> macIid = nodeIid.augmentation(HwvtepGlobalAugmentation.class)
65                 .child(RemoteUcastMacs.class, remoteUcastMacs.key());
66         processDependencies(null, transaction, nodeIid, macIid, remoteUcastMacs);
67     }
68
69     @Override
70     public void doDeviceTransaction(final TransactionBuilder transaction,
71                                     final InstanceIdentifier<Node> instanceIdentifier,
72                                     final RemoteUcastMacs mac,
73                                     final InstanceIdentifier macKey,
74                                     final Object... extraData) {
75         LOG.debug("Removing remoteUcastMacs, mac address: {}", mac.getMacEntryKey().getValue());
76         InstanceIdentifier<RemoteUcastMacs> macIid = instanceIdentifier.augmentation(HwvtepGlobalAugmentation.class)
77                 .child(RemoteUcastMacs.class, mac.key());
78         HwvtepDeviceInfo.DeviceData deviceData =
79                 getOperationalState().getDeviceInfo().getDeviceOperData(RemoteUcastMacs.class, macIid);
80         UcastMacsRemote ucastMacsRemote = transaction.getTypedRowSchema(UcastMacsRemote.class);
81         if (deviceData != null && deviceData.getUuid() != null) {
82             //when mac entry is deleted, its referenced locators are deleted automatically.
83             //locators in config DS is not deleted and need to be removed explicitly by user.
84             UUID macEntryUUID = deviceData.getUuid();
85             ucastMacsRemote.getUuidColumn().setData(macEntryUUID);
86             transaction.add(op.delete(ucastMacsRemote.getSchema())
87                     .where(ucastMacsRemote.getUuidColumn().getSchema().opEqual(macEntryUUID)).build());
88             transaction.add(op.comment("UcastMacRemote: Deleting " + mac.getMacEntryKey().getValue()));
89             updateCurrentTxDeleteData(RemoteUcastMacs.class, macKey, mac);
90         } else {
91             LOG.warn("Unable to delete remoteUcastMacs {} because it was not found in the operational store",
92                     mac.getMacEntryKey().getValue());
93         }
94     }
95
96     @Override
97     protected List<RemoteUcastMacs> getData(final HwvtepGlobalAugmentation augmentation) {
98         return augmentation.getRemoteUcastMacs();
99     }
100
101     @Override
102     protected boolean isRemoveCommand() {
103         return true;
104     }
105
106     @Override
107     public void onCommandSucceeded() {
108         for (MdsalUpdate mdsalUpdate : updates.get(getDeviceTransaction())) {
109             RemoteUcastMacs deletedMac = (RemoteUcastMacs) mdsalUpdate.getNewData();
110             InstanceIdentifier<RemoteUcastMacs> macIid = mdsalUpdate.getKey();
111             getDeviceInfo().removeRemoteUcast(
112                     (InstanceIdentifier<LogicalSwitches>) deletedMac.getLogicalSwitchRef().getValue(), macIid);
113         }
114     }
115 }