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