bug 8257 handling back to back mcast mac updates
[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
9 package org.opendaylight.ovsdb.hwvtepsouthbound.transact;
10
11 import static org.opendaylight.ovsdb.lib.operations.Operations.op;
12
13 import java.util.ArrayList;
14 import java.util.Collection;
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.HwvtepSouthboundUtil;
22 import org.opendaylight.ovsdb.lib.notation.UUID;
23 import org.opendaylight.ovsdb.lib.operations.TransactionBuilder;
24 import org.opendaylight.ovsdb.lib.schema.typed.TyperUtils;
25 import org.opendaylight.ovsdb.schema.hardwarevtep.McastMacsRemote;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepGlobalAugmentation;
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 import com.google.common.base.Optional;
34
35 public class McastMacsRemoteRemoveCommand extends AbstractTransactCommand<RemoteMcastMacs, HwvtepGlobalAugmentation> {
36     private static final Logger LOG = LoggerFactory.getLogger(McastMacsRemoteRemoveCommand.class);
37
38     public McastMacsRemoteRemoveCommand(HwvtepOperationalState state,
39             Collection<DataTreeModification<Node>> changes) {
40         super(state, changes);
41     }
42
43     @Override
44     public void execute(TransactionBuilder transaction) {
45         Map<InstanceIdentifier<Node>, List<RemoteMcastMacs>> removeds =
46                 extractRemoved(getChanges(),RemoteMcastMacs.class);
47         if (!removeds.isEmpty()) {
48             for (Entry<InstanceIdentifier<Node>, List<RemoteMcastMacs>> removed:
49                 removeds.entrySet()) {
50                 removeMcastMacRemote(transaction,  removed.getKey(), removed.getValue());
51             }
52         }
53         //Remove the ones whose locator set got emptied
54         Map<InstanceIdentifier<Node>, List<RemoteMcastMacs>> updated =
55                 extractUpdated(getChanges(),RemoteMcastMacs.class);
56         if (!HwvtepSouthboundUtil.isEmptyMap(updated)) {
57             for (Entry<InstanceIdentifier<Node>, List<RemoteMcastMacs>> entry:
58                     updated.entrySet()) {
59                 List<RemoteMcastMacs> updatedList = entry.getValue();
60                 List<RemoteMcastMacs> tobeRemovedList = new ArrayList<>();
61                 if (!HwvtepSouthboundUtil.isEmpty(updatedList)) {
62                     for (RemoteMcastMacs mac: updatedList) {
63                         if (HwvtepSouthboundUtil.isEmpty(mac.getLocatorSet())) {
64                             tobeRemovedList.add(mac);
65                         }
66                     }
67                     removeMcastMacRemote(transaction, entry.getKey(), tobeRemovedList);
68                 }
69             }
70         }
71     }
72
73     private void removeMcastMacRemote(final TransactionBuilder transaction,
74                                       final InstanceIdentifier<Node> nodeIid, final List<RemoteMcastMacs> macList) {
75         for (RemoteMcastMacs mac : macList) {
76             InstanceIdentifier<RemoteMcastMacs> macKey = nodeIid.augmentation(HwvtepGlobalAugmentation.class).
77                     child(RemoteMcastMacs.class, mac.getKey());
78             onConfigUpdate(transaction, nodeIid, mac, macKey);
79         }
80     }
81
82     @Override
83     public void onConfigUpdate(final TransactionBuilder transaction,
84                                final InstanceIdentifier<Node> nodeIid,
85                                final RemoteMcastMacs remoteMcastMac,
86                                final InstanceIdentifier macKey,
87                                final Object... extraData) {
88         processDependencies(null, transaction, nodeIid, macKey, remoteMcastMac);
89     }
90
91     @Override
92     public void doDeviceTransaction(final TransactionBuilder transaction,
93                                     final InstanceIdentifier<Node> instanceIdentifier,
94                                     final RemoteMcastMacs mac,
95                                     final InstanceIdentifier macIid,
96                                     final Object... extraData) {
97             LOG.debug("Removing remoteMcastMacs, mac address: {}", mac.getMacEntryKey().getValue());
98             Optional<RemoteMcastMacs> operationalMacOptional =
99                     getOperationalState().getRemoteMcastMacs(instanceIdentifier, mac.getKey());
100             McastMacsRemote mcastMacsRemote = TyperUtils.getTypedRowWrapper(transaction.getDatabaseSchema(),
101                     McastMacsRemote.class, null);
102             if (operationalMacOptional.isPresent() && operationalMacOptional.get().getMacEntryUuid() != null) {
103                 //when mac entry is deleted, its referenced locator set and locators are deleted automatically.
104                 //TODO: locator in config DS is not deleted
105                 UUID macEntryUUID = new UUID(operationalMacOptional.get().getMacEntryUuid().getValue());
106                 mcastMacsRemote.getUuidColumn().setData(macEntryUUID);
107                 transaction.add(op.delete(mcastMacsRemote.getSchema()).
108                         where(mcastMacsRemote.getUuidColumn().getSchema().opEqual(macEntryUUID)).build());
109                 transaction.add(op.comment("McastMacRemote: Deleting " + mac.getMacEntryKey().getValue()));
110                 getOperationalState().getDeviceInfo().markKeyAsInTransit(RemoteMcastMacs.class, macIid);
111             } else {
112                 LOG.warn("Unable to delete remoteMcastMacs {} because it was not found in the operational store",
113                         mac.getMacEntryKey().getValue());
114             }
115             updateCurrentTxDeleteData(RemoteMcastMacs.class, macIid, mac);
116     }
117
118     @Override
119     protected List<RemoteMcastMacs> getData(HwvtepGlobalAugmentation augmentation) {
120         return augmentation.getRemoteMcastMacs();
121     }
122
123     @Override
124     protected boolean areEqual(RemoteMcastMacs a, RemoteMcastMacs b) {
125         return a.getKey().equals(b.getKey()) && Objects.equals(a.getLocatorSet(), b.getLocatorSet());
126     }
127
128     @Override
129     protected boolean isRemoveCommand() {
130         return true;
131     }
132 }