441aa735f2a60cb9214670bea744243c1df3756d
[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.mdsal.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         clearConfigData(RemoteMcastMacs.class, macIid);
95         long transactionId = getOperationalState().getTransactionId();
96         LOG.debug("Remove received for RemoteMcastMacs key: {} txId: {}", macIid, transactionId);
97         HwvtepDeviceInfo.DeviceData deviceData = getDeviceOpData(RemoteMcastMacs.class, macIid);
98         McastMacsRemote mcastMacsRemote = transaction.getTypedRowSchema(McastMacsRemote.class);
99         boolean deleted = false;
100         if (deviceData != null && deviceData.getData() != null && deviceData.getData() instanceof McastMacsRemote
101                 && ((McastMacsRemote)deviceData.getData()).getLogicalSwitchColumn() != null) {
102             UUID logicalSwitchUid = ((McastMacsRemote)deviceData.getData()).getLogicalSwitchColumn().getData();
103             if (logicalSwitchUid != null) {
104                 transaction.add(op.delete(mcastMacsRemote.getSchema())
105                         .where(mcastMacsRemote.getLogicalSwitchColumn().getSchema().opEqual(logicalSwitchUid)).build());
106                 deleted = true;
107                 updateCurrentTxDeleteData(RemoteMcastMacs.class, macIid, mac);
108                 updateControllerTxHistory(TransactionType.DELETE, new StringBuilder(mcastMacsRemote.toString())
109                         .append(":  LS: ").append(logicalSwitchUid));
110                 LOG.info("CONTROLLER - {} {} LS:{}", TransactionType.DELETE, mcastMacsRemote, logicalSwitchUid);
111             }
112         }
113         if (!deleted && deviceData != null) {
114             UUID macEntryUUID = deviceData.getUuid();
115             if (macEntryUUID != null) {
116                 mcastMacsRemote.getUuidColumn().setData(macEntryUUID);
117                 updateCurrentTxDeleteData(RemoteMcastMacs.class, macIid, mac);
118                 transaction.add(op.delete(mcastMacsRemote.getSchema())
119                         .where(mcastMacsRemote.getUuidColumn().getSchema().opEqual(macEntryUUID)).build());
120                 updateControllerTxHistory(TransactionType.DELETE, new StringBuilder(mcastMacsRemote.toString())
121                         .append(":  Mac : ").append(macEntryUUID));
122                 LOG.info("CONTROLLER - {} {} Mac :{}", TransactionType.DELETE, mcastMacsRemote, macEntryUUID);
123             } else {
124                 LOG.error("Failed to delete remote mcast entry as it is not found in device {}", macIid);
125                 getDeviceInfo().clearConfigData(RemoteMcastMacs.class, macIid);
126                 return;
127             }
128         }
129     }
130
131     @Override
132     protected List<RemoteMcastMacs> getData(final HwvtepGlobalAugmentation augmentation) {
133         return augmentation.getRemoteMcastMacs();
134     }
135
136     @Override
137     protected boolean areEqual(final RemoteMcastMacs macs1, final RemoteMcastMacs macs2) {
138         return macs1.key().equals(macs2.key()) && Objects.equals(macs1.getLocatorSet(), macs2.getLocatorSet());
139     }
140
141     @Override
142     protected boolean isDeleteCmd() {
143         return true;
144     }
145
146 }