bug 8257 handling back to back mcast mac updates
[ovsdb.git] / hwvtepsouthbound / hwvtepsouthbound-impl / src / main / java / org / opendaylight / ovsdb / hwvtepsouthbound / transact / UcastMacsLocalRemoveCommand.java
1 /*
2  * Copyright © 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
19 import org.opendaylight.controller.md.sal.binding.api.DataTreeModification;
20 import org.opendaylight.ovsdb.lib.notation.UUID;
21 import org.opendaylight.ovsdb.lib.operations.TransactionBuilder;
22 import org.opendaylight.ovsdb.lib.schema.typed.TyperUtils;
23 import org.opendaylight.ovsdb.schema.hardwarevtep.UcastMacsLocal;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepGlobalAugmentation;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.LocalUcastMacs;
26 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
27 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30
31 import com.google.common.base.Optional;
32
33 public class UcastMacsLocalRemoveCommand extends AbstractTransactCommand<LocalUcastMacs, HwvtepGlobalAugmentation> {
34     private static final Logger LOG = LoggerFactory.getLogger(UcastMacsLocalRemoveCommand.class);
35
36     public UcastMacsLocalRemoveCommand(HwvtepOperationalState state,
37             Collection<DataTreeModification<Node>> changes) {
38         super(state, changes);
39     }
40
41     @Override
42     public void execute(TransactionBuilder transaction) {
43         Map<InstanceIdentifier<Node>, List<LocalUcastMacs>> removeds =
44                 extractRemoved(getChanges(),LocalUcastMacs.class);
45         if (!removeds.isEmpty()) {
46             for (Entry<InstanceIdentifier<Node>, List<LocalUcastMacs>> removed:
47                 removeds.entrySet()) {
48                 removeUcastMacLocal(transaction,  removed.getKey(), removed.getValue());
49             }
50         }
51     }
52
53     private void removeUcastMacLocal(TransactionBuilder transaction,
54             InstanceIdentifier<Node> instanceIdentifier, List<LocalUcastMacs> macList) {
55         for (LocalUcastMacs mac: macList) {
56             LOG.debug("Removing remoteUcastMacs, mac address: {}", mac.getMacEntryKey().getValue());
57             Optional<LocalUcastMacs> operationalMacOptional =
58                     getOperationalState().getLocalUcastMacs(instanceIdentifier, mac.getKey());
59             UcastMacsLocal ucastMacsLocal = TyperUtils.getTypedRowWrapper(transaction.getDatabaseSchema(),
60                     UcastMacsLocal.class, null);
61             if (operationalMacOptional.isPresent() && operationalMacOptional.get().getMacEntryUuid() != null) {
62                 //when mac entry is deleted, its referenced locators are deleted automatically.
63                 //locators in config DS is not deleted and user need to be removed explicitly by user.
64                 UUID macEntryUUID = new UUID(operationalMacOptional.get().getMacEntryUuid().getValue());
65                 ucastMacsLocal.getUuidColumn().setData(macEntryUUID);
66                 transaction.add(op.delete(ucastMacsLocal.getSchema()).
67                         where(ucastMacsLocal.getUuidColumn().getSchema().opEqual(macEntryUUID)).build());
68                 transaction.add(op.comment("UcastMacLocal: Deleting " + mac.getMacEntryKey().getValue()));
69             } else {
70                 LOG.warn("Unable to delete remoteUcastMacs {} because it was not found in the operational store",
71                         mac.getMacEntryKey().getValue());
72             }
73         }
74     }
75
76     @Override
77     protected List<LocalUcastMacs> getData(HwvtepGlobalAugmentation augmentation) {
78         return augmentation.getLocalUcastMacs();
79     }
80
81     @Override
82     protected boolean cascadeDelete() {
83         return true;
84     }
85
86     @Override
87     protected UnMetDependencyGetter getDependencyGetter() {
88         return MAC_DEPENDENCY_GETTER;
89     }
90
91     static UnMetDependencyGetter MAC_DEPENDENCY_GETTER = new MacDependencyGetter();
92
93     public static class MacDependencyGetter extends UnMetDependencyGetter<LocalUcastMacs> {
94         @Override
95         public List<InstanceIdentifier<?>> getLogicalSwitchDependencies(LocalUcastMacs data) {
96             return Collections.singletonList(data.getLogicalSwitchRef().getValue());
97         }
98
99         @Override
100         public List<InstanceIdentifier<?>> getTerminationPointDependencies(LocalUcastMacs data) {
101             return Collections.emptyList();
102         }
103     }
104
105     @Override
106     protected boolean isRemoveCommand() {
107         return true;
108     }
109 }