Fix delete of Logical_Switch for the "referential integrity violation" failure.
[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.RemoteMcastMacs;
27 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
28 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31
32 public class McastMacsRemoteRemoveCommand extends AbstractTransactCommand<RemoteMcastMacs, HwvtepGlobalAugmentation> {
33     private static final Logger LOG = LoggerFactory.getLogger(McastMacsRemoteRemoveCommand.class);
34
35     public McastMacsRemoteRemoveCommand(final HwvtepOperationalState state,
36             final Collection<DataTreeModification<Node>> changes) {
37         super(state, changes);
38     }
39
40     @Override
41     public void execute(final TransactionBuilder transaction) {
42         Map<InstanceIdentifier<Node>, List<RemoteMcastMacs>> removeds =
43                 extractRemoved(getChanges(),RemoteMcastMacs.class);
44         if (!removeds.isEmpty()) {
45             for (Entry<InstanceIdentifier<Node>, List<RemoteMcastMacs>> removed:
46                 removeds.entrySet()) {
47                 removeMcastMacRemote(transaction,  removed.getKey(), removed.getValue());
48             }
49         }
50         //Remove the ones whose locator set got emptied
51         Map<InstanceIdentifier<Node>, List<RemoteMcastMacs>> updated =
52                 extractUpdated(getChanges(),RemoteMcastMacs.class);
53         if (!HwvtepSouthboundUtil.isEmptyMap(updated)) {
54             for (Entry<InstanceIdentifier<Node>, List<RemoteMcastMacs>> entry:
55                     updated.entrySet()) {
56                 List<RemoteMcastMacs> updatedList = entry.getValue();
57                 List<RemoteMcastMacs> tobeRemovedList = new ArrayList<>();
58                 if (!HwvtepSouthboundUtil.isEmpty(updatedList)) {
59                     for (RemoteMcastMacs mac: updatedList) {
60                         if (HwvtepSouthboundUtil.isEmpty(mac.getLocatorSet())) {
61                             tobeRemovedList.add(mac);
62                         }
63                     }
64                     removeMcastMacRemote(transaction, entry.getKey(), tobeRemovedList);
65                 }
66             }
67         }
68     }
69
70     private void removeMcastMacRemote(final TransactionBuilder transaction,
71                                       final InstanceIdentifier<Node> nodeIid, final List<RemoteMcastMacs> macList) {
72         for (RemoteMcastMacs mac : macList) {
73             InstanceIdentifier<RemoteMcastMacs> macKey = nodeIid.augmentation(HwvtepGlobalAugmentation.class)
74                     .child(RemoteMcastMacs.class, mac.key());
75             onConfigUpdate(transaction, nodeIid, mac, macKey);
76         }
77     }
78
79     @Override
80     public void onConfigUpdate(final TransactionBuilder transaction,
81                                final InstanceIdentifier<Node> nodeIid,
82                                final RemoteMcastMacs remoteMcastMac,
83                                final InstanceIdentifier macKey,
84                                final Object... extraData) {
85         processDependencies(EmptyDependencyGetter.INSTANCE, transaction, nodeIid, macKey, remoteMcastMac);
86     }
87
88     @Override
89     public void doDeviceTransaction(final TransactionBuilder transaction,
90                                     final InstanceIdentifier<Node> instanceIdentifier,
91                                     final RemoteMcastMacs mac,
92                                     final InstanceIdentifier macIid,
93                                     final Object... extraData) {
94         LOG.debug("Removing remoteMcastMacs, mac address: {}", mac.getMacEntryKey().getValue());
95         clearConfigData(RemoteMcastMacs.class, macIid);
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 isDeleteCmd() {
127         return true;
128     }
129
130 }