bug 8029 added ref counts for physical locators.
[ovsdb.git] / hwvtepsouthbound / hwvtepsouthbound-impl / src / main / java / org / opendaylight / ovsdb / hwvtepsouthbound / transact / UcastMacsRemoteRemoveCommand.java
1 /*
2  * Copyright (c) 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.List;
15 import java.util.Map;
16 import java.util.Map.Entry;
17
18 import org.opendaylight.controller.md.sal.binding.api.DataTreeModification;
19 import org.opendaylight.ovsdb.hwvtepsouthbound.HwvtepDeviceInfo;
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.UcastMacsRemote;
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.LogicalSwitches;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.RemoteUcastMacs;
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 UcastMacsRemoteRemoveCommand extends AbstractTransactCommand<RemoteUcastMacs, HwvtepGlobalAugmentation> {
33     private static final Logger LOG = LoggerFactory.getLogger(UcastMacsRemoteRemoveCommand.class);
34
35     public UcastMacsRemoteRemoveCommand(HwvtepOperationalState state,
36             Collection<DataTreeModification<Node>> changes) {
37         super(state, changes);
38     }
39
40     @Override
41     public void execute(TransactionBuilder transaction) {
42         Map<InstanceIdentifier<Node>, List<RemoteUcastMacs>> removeds =
43                 extractRemoved(getChanges(),RemoteUcastMacs.class);
44         if (!removeds.isEmpty()) {
45             for (Entry<InstanceIdentifier<Node>, List<RemoteUcastMacs>> removed:
46                 removeds.entrySet()) {
47                 removeUcastMacRemote(transaction,  removed.getKey(), removed.getValue());
48             }
49         }
50     }
51
52
53     private void removeUcastMacRemote(final TransactionBuilder transaction,
54                                       final InstanceIdentifier<Node> instanceIdentifier,
55                                       final List<RemoteUcastMacs> macList) {
56         for (RemoteUcastMacs mac: macList) {
57             onConfigUpdate(transaction, instanceIdentifier, mac, null);
58         }
59     }
60
61     @Override
62     public void onConfigUpdate(final TransactionBuilder transaction,
63                                final InstanceIdentifier<Node> nodeIid,
64                                final RemoteUcastMacs remoteUcastMacs,
65                                final InstanceIdentifier macKey,
66                                final Object... extraData) {
67         InstanceIdentifier<RemoteUcastMacs> macIid = nodeIid.augmentation(HwvtepGlobalAugmentation.class).
68                 child(RemoteUcastMacs.class, remoteUcastMacs.getKey());
69         processDependencies(null, transaction, nodeIid, macIid, remoteUcastMacs);
70     }
71
72     @Override
73     public void doDeviceTransaction(final TransactionBuilder transaction,
74                                     final InstanceIdentifier<Node> instanceIdentifier,
75                                     final RemoteUcastMacs mac,
76                                     final InstanceIdentifier macKey,
77                                     final Object... extraData) {
78             LOG.debug("Removing remoteUcastMacs, mac address: {}", mac.getMacEntryKey().getValue());
79             InstanceIdentifier<RemoteUcastMacs> macIid = instanceIdentifier.augmentation(HwvtepGlobalAugmentation.class).
80                     child(RemoteUcastMacs.class, mac.getKey());
81             HwvtepDeviceInfo.DeviceData deviceData =
82                     getOperationalState().getDeviceInfo().getDeviceOperData(RemoteUcastMacs.class, macIid);
83             UcastMacsRemote ucastMacsRemote = TyperUtils.getTypedRowWrapper(transaction.getDatabaseSchema(),
84                     UcastMacsRemote.class, null);
85             if (deviceData != null && deviceData.getUuid() != null) {
86                 //when mac entry is deleted, its referenced locators are deleted automatically.
87                 //locators in config DS is not deleted and need to be removed explicitly by user.
88                 UUID macEntryUUID = deviceData.getUuid();
89                 ucastMacsRemote.getUuidColumn().setData(macEntryUUID);
90                 transaction.add(op.delete(ucastMacsRemote.getSchema()).
91                         where(ucastMacsRemote.getUuidColumn().getSchema().opEqual(macEntryUUID)).build());
92                 transaction.add(op.comment("UcastMacRemote: Deleting " + mac.getMacEntryKey().getValue()));
93                 updateCurrentTxDeleteData(RemoteUcastMacs.class, macKey, mac);
94             } else {
95                 LOG.warn("Unable to delete remoteUcastMacs {} because it was not found in the operational store",
96                         mac.getMacEntryKey().getValue());
97             }
98     }
99
100     protected List<RemoteUcastMacs> getData(HwvtepGlobalAugmentation augmentation) {
101         return augmentation.getRemoteUcastMacs();
102     }
103
104     @Override
105     protected boolean isRemoveCommand() {
106         return true;
107     }
108
109     @Override
110     public void onCommandSucceeded() {
111         for (MdsalUpdate mdsalUpdate : updates.get(getDeviceTransaction())) {
112             RemoteUcastMacs deletedMac = (RemoteUcastMacs) mdsalUpdate.getNewData();
113             InstanceIdentifier<RemoteUcastMacs> macIid = mdsalUpdate.getKey();
114             getDeviceInfo().removeRemoteUcast(
115                     (InstanceIdentifier<LogicalSwitches>) deletedMac.getLogicalSwitchRef().getValue(), macIid);
116         }
117     }
118 }