Make TransactionBuilder type-aware
[ovsdb.git] / hwvtepsouthbound / hwvtepsouthbound-impl / src / main / java / org / opendaylight / ovsdb / hwvtepsouthbound / transact / McastMacsRemoteRemoveCommand.java
1 /*
2  * Copyright (c) 2015, 2016 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.ArrayList;
13 import java.util.Collection;
14 import java.util.List;
15 import java.util.Map;
16 import java.util.Map.Entry;
17 import java.util.Objects;
18 import org.opendaylight.controller.md.sal.binding.api.DataTreeModification;
19 import org.opendaylight.ovsdb.hwvtepsouthbound.HwvtepDeviceInfo;
20 import org.opendaylight.ovsdb.hwvtepsouthbound.HwvtepSouthboundUtil;
21 import org.opendaylight.ovsdb.lib.notation.UUID;
22 import org.opendaylight.ovsdb.lib.operations.TransactionBuilder;
23 import org.opendaylight.ovsdb.schema.hardwarevtep.McastMacsRemote;
24 import org.opendaylight.ovsdb.utils.mdsal.utils.TransactionType;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepGlobalAugmentation;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.LogicalSwitches;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.RemoteMcastMacs;
28 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
29 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32
33 public class McastMacsRemoteRemoveCommand extends AbstractTransactCommand<RemoteMcastMacs, HwvtepGlobalAugmentation> {
34     private static final Logger LOG = LoggerFactory.getLogger(McastMacsRemoteRemoveCommand.class);
35
36     public McastMacsRemoteRemoveCommand(final HwvtepOperationalState state,
37             final Collection<DataTreeModification<Node>> changes) {
38         super(state, changes);
39     }
40
41     @Override
42     public void execute(final TransactionBuilder transaction) {
43         Map<InstanceIdentifier<Node>, List<RemoteMcastMacs>> removeds =
44                 extractRemoved(getChanges(),RemoteMcastMacs.class);
45         if (!removeds.isEmpty()) {
46             for (Entry<InstanceIdentifier<Node>, List<RemoteMcastMacs>> removed:
47                 removeds.entrySet()) {
48                 removeMcastMacRemote(transaction,  removed.getKey(), removed.getValue());
49             }
50         }
51         //Remove the ones whose locator set got emptied
52         Map<InstanceIdentifier<Node>, List<RemoteMcastMacs>> updated =
53                 extractUpdated(getChanges(),RemoteMcastMacs.class);
54         if (!HwvtepSouthboundUtil.isEmptyMap(updated)) {
55             for (Entry<InstanceIdentifier<Node>, List<RemoteMcastMacs>> entry:
56                     updated.entrySet()) {
57                 List<RemoteMcastMacs> updatedList = entry.getValue();
58                 List<RemoteMcastMacs> tobeRemovedList = new ArrayList<>();
59                 if (!HwvtepSouthboundUtil.isEmpty(updatedList)) {
60                     for (RemoteMcastMacs mac: updatedList) {
61                         if (HwvtepSouthboundUtil.isEmpty(mac.getLocatorSet())) {
62                             tobeRemovedList.add(mac);
63                         }
64                     }
65                     removeMcastMacRemote(transaction, entry.getKey(), tobeRemovedList);
66                 }
67             }
68         }
69     }
70
71     private void removeMcastMacRemote(final TransactionBuilder transaction,
72                                       final InstanceIdentifier<Node> nodeIid, final List<RemoteMcastMacs> macList) {
73         for (RemoteMcastMacs mac : macList) {
74             InstanceIdentifier<RemoteMcastMacs> macKey = nodeIid.augmentation(HwvtepGlobalAugmentation.class)
75                     .child(RemoteMcastMacs.class, mac.key());
76             onConfigUpdate(transaction, nodeIid, mac, macKey);
77         }
78     }
79
80     @Override
81     public void onConfigUpdate(final TransactionBuilder transaction,
82                                final InstanceIdentifier<Node> nodeIid,
83                                final RemoteMcastMacs remoteMcastMac,
84                                final InstanceIdentifier macKey,
85                                final Object... extraData) {
86         processDependencies(null, transaction, nodeIid, macKey, remoteMcastMac);
87     }
88
89     @Override
90     public void doDeviceTransaction(final TransactionBuilder transaction,
91                                     final InstanceIdentifier<Node> instanceIdentifier,
92                                     final RemoteMcastMacs mac,
93                                     final InstanceIdentifier macIid,
94                                     final Object... extraData) {
95         LOG.debug("Removing remoteMcastMacs, mac address: {}", mac.getMacEntryKey().getValue());
96         HwvtepDeviceInfo.DeviceData operationalMacOptional =
97                 getDeviceInfo().getDeviceOperData(RemoteMcastMacs.class, macIid);
98         McastMacsRemote mcastMacsRemote = transaction.getTypedRowSchema(McastMacsRemote.class);
99         if (operationalMacOptional != null && operationalMacOptional.getUuid() != null) {
100             //when mac entry is deleted, its referenced locator set and locators are deleted automatically.
101             //TODO: locator in config DS is not deleted
102             UUID macEntryUUID = operationalMacOptional.getUuid();
103             mcastMacsRemote.getUuidColumn().setData(macEntryUUID);
104             transaction.add(op.delete(mcastMacsRemote.getSchema())
105                     .where(mcastMacsRemote.getUuidColumn().getSchema().opEqual(macEntryUUID)).build());
106             transaction.add(op.comment("McastMacRemote: Deleting " + mac.getMacEntryKey().getValue()));
107             updateCurrentTxDeleteData(RemoteMcastMacs.class, macIid, mac);
108             updateControllerTxHistory(TransactionType.DELETE, mcastMacsRemote);
109         } else {
110             LOG.warn("Unable to delete remoteMcastMacs {} because it was not found in the operational store",
111                     mac.getMacEntryKey().getValue());
112         }
113     }
114
115     @Override
116     protected List<RemoteMcastMacs> getData(final HwvtepGlobalAugmentation augmentation) {
117         return augmentation.getRemoteMcastMacs();
118     }
119
120     @Override
121     protected boolean areEqual(final RemoteMcastMacs macs1, final RemoteMcastMacs macs2) {
122         return macs1.key().equals(macs2.key()) && Objects.equals(macs1.getLocatorSet(), macs2.getLocatorSet());
123     }
124
125     @Override
126     protected boolean isRemoveCommand() {
127         return true;
128     }
129
130
131     @Override
132     public void onCommandSucceeded() {
133         //remove the refcounts of the deleted macs
134         for (MdsalUpdate mdsalUpdate : updates.get(getDeviceTransaction())) {
135             RemoteMcastMacs deletedMac = (RemoteMcastMacs) mdsalUpdate.getNewData();
136             InstanceIdentifier<RemoteMcastMacs> macIid = mdsalUpdate.getKey();
137             getDeviceInfo().removeRemoteMcast(
138                     (InstanceIdentifier<LogicalSwitches>) deletedMac.getLogicalSwitchRef().getValue(), macIid);
139         }
140     }
141 }