bug 8029 added ref counts for physical locators.
[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.HwvtepDeviceInfo;
22 import org.opendaylight.ovsdb.hwvtepsouthbound.HwvtepSouthboundUtil;
23 import org.opendaylight.ovsdb.lib.notation.UUID;
24 import org.opendaylight.ovsdb.lib.operations.TransactionBuilder;
25 import org.opendaylight.ovsdb.lib.schema.typed.TyperUtils;
26 import org.opendaylight.ovsdb.schema.hardwarevtep.McastMacsRemote;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepGlobalAugmentation;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.LogicalSwitches;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.RemoteMcastMacs;
30 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
31 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
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             HwvtepDeviceInfo.DeviceData operationalMacOptional =
99                     getDeviceInfo().getDeviceOperData(RemoteMcastMacs.class, macIid);
100             McastMacsRemote mcastMacsRemote = TyperUtils.getTypedRowWrapper(transaction.getDatabaseSchema(),
101                     McastMacsRemote.class, null);
102             if (operationalMacOptional != null && operationalMacOptional.getUuid() != 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 = operationalMacOptional.getUuid();
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                 updateCurrentTxDeleteData(RemoteMcastMacs.class, macIid, mac);
111             } else {
112                 LOG.warn("Unable to delete remoteMcastMacs {} because it was not found in the operational store",
113                         mac.getMacEntryKey().getValue());
114             }
115     }
116
117     @Override
118     protected List<RemoteMcastMacs> getData(HwvtepGlobalAugmentation augmentation) {
119         return augmentation.getRemoteMcastMacs();
120     }
121
122     @Override
123     protected boolean areEqual(RemoteMcastMacs a, RemoteMcastMacs b) {
124         return a.getKey().equals(b.getKey()) && Objects.equals(a.getLocatorSet(), b.getLocatorSet());
125     }
126
127     @Override
128     protected boolean isRemoveCommand() {
129         return true;
130     }
131
132
133     @Override
134     public void onCommandSucceeded() {
135         //remove the refcounts of the deleted macs
136         for (MdsalUpdate mdsalUpdate : updates.get(getDeviceTransaction())) {
137             RemoteMcastMacs deletedMac = (RemoteMcastMacs) mdsalUpdate.getNewData();
138             InstanceIdentifier<RemoteMcastMacs> macIid = mdsalUpdate.getKey();
139             getDeviceInfo().removeRemoteMcast(
140                     (InstanceIdentifier<LogicalSwitches>) deletedMac.getLogicalSwitchRef().getValue(), macIid);
141         }
142     }
143 }