Using MD-SAL .exists() API
[ovsdb.git] / hwvtepsouthbound / hwvtepsouthbound-impl / src / main / java / org / opendaylight / ovsdb / hwvtepsouthbound / transact / McastMacsRemoteRemoveCommand.java
index 0cf58a6301be70c9d50ee343cbea8fdc5ca6fc09..bf162ea45812733fbff5b2a9439467e0f1919561 100644 (file)
@@ -1,53 +1,46 @@
 /*
- * Copyright (c) 2015 China Telecom Beijing Research Institute and others.  All rights reserved.
+ * Copyright (c) 2015, 2016 China Telecom Beijing Research Institute and others.  All rights reserved.
  *
  * This program and the accompanying materials are made available under the
  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
  * and is available at http://www.eclipse.org/legal/epl-v10.html
  */
-
 package org.opendaylight.ovsdb.hwvtepsouthbound.transact;
 
 import static org.opendaylight.ovsdb.lib.operations.Operations.op;
 
 import java.util.ArrayList;
 import java.util.Collection;
-import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 import java.util.Map.Entry;
-
-import org.opendaylight.controller.md.sal.binding.api.DataObjectModification;
-import org.opendaylight.controller.md.sal.binding.api.DataTreeModification;
-import org.opendaylight.ovsdb.hwvtepsouthbound.HwvtepSouthboundConstants;
+import java.util.Objects;
+import org.opendaylight.mdsal.binding.api.DataTreeModification;
+import org.opendaylight.ovsdb.hwvtepsouthbound.HwvtepDeviceInfo;
+import org.opendaylight.ovsdb.hwvtepsouthbound.HwvtepSouthboundUtil;
 import org.opendaylight.ovsdb.lib.notation.UUID;
 import org.opendaylight.ovsdb.lib.operations.TransactionBuilder;
-import org.opendaylight.ovsdb.lib.schema.typed.TyperUtils;
 import org.opendaylight.ovsdb.schema.hardwarevtep.McastMacsRemote;
-import org.opendaylight.ovsdb.schema.hardwarevtep.PhysicalLocator;
-import org.opendaylight.ovsdb.schema.hardwarevtep.PhysicalLocatorSet;
+import org.opendaylight.ovsdb.utils.mdsal.utils.TransactionType;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepGlobalAugmentation;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepPhysicalLocatorAugmentation;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.RemoteMcastMacs;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.physical.locator.set.attributes.LocatorSet;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.RemoteMcastMacsKey;
 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
-import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.node.TerminationPoint;
 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-import com.google.common.base.Optional;
-
-public class McastMacsRemoteRemoveCommand extends AbstractTransactCommand {
-    private static final Logger LOG = LoggerFactory.getLogger(PhysicalPortRemoveCommand.class);
+public class McastMacsRemoteRemoveCommand
+        extends AbstractTransactCommand<RemoteMcastMacs, RemoteMcastMacsKey, HwvtepGlobalAugmentation> {
+    private static final Logger LOG = LoggerFactory.getLogger(McastMacsRemoteRemoveCommand.class);
 
-    public McastMacsRemoteRemoveCommand(HwvtepOperationalState state,
-            Collection<DataTreeModification<Node>> changes) {
+    public McastMacsRemoteRemoveCommand(final HwvtepOperationalState state,
+            final Collection<DataTreeModification<Node>> changes) {
         super(state, changes);
     }
 
     @Override
-    public void execute(TransactionBuilder transaction) {
+    public void execute(final TransactionBuilder transaction) {
         Map<InstanceIdentifier<Node>, List<RemoteMcastMacs>> removeds =
                 extractRemoved(getChanges(),RemoteMcastMacs.class);
         if (!removeds.isEmpty()) {
@@ -56,93 +49,104 @@ public class McastMacsRemoteRemoveCommand extends AbstractTransactCommand {
                 removeMcastMacRemote(transaction,  removed.getKey(), removed.getValue());
             }
         }
+        //Remove the ones whose locator set got emptied
+        Map<InstanceIdentifier<Node>, List<RemoteMcastMacs>> updated =
+                extractUpdated(getChanges(),RemoteMcastMacs.class);
+        if (!HwvtepSouthboundUtil.isEmptyMap(updated)) {
+            for (Entry<InstanceIdentifier<Node>, List<RemoteMcastMacs>> entry:
+                    updated.entrySet()) {
+                List<RemoteMcastMacs> updatedList = entry.getValue();
+                List<RemoteMcastMacs> tobeRemovedList = new ArrayList<>();
+                if (!HwvtepSouthboundUtil.isEmpty(updatedList)) {
+                    for (RemoteMcastMacs mac: updatedList) {
+                        if (HwvtepSouthboundUtil.isEmpty(mac.getLocatorSet())) {
+                            tobeRemovedList.add(mac);
+                        }
+                    }
+                    removeMcastMacRemote(transaction, entry.getKey(), tobeRemovedList);
+                }
+            }
+        }
     }
 
-    private void removeMcastMacRemote(TransactionBuilder transaction,
-            InstanceIdentifier<Node> instanceIdentifier, List<RemoteMcastMacs> macList) {
-        for (RemoteMcastMacs mac: macList) {
-            LOG.debug("Removing remoteMcastMacs, mac address: {}", mac.getMacEntryKey().getValue());
-            Optional<RemoteMcastMacs> operationalMacOptional =
-                    getOperationalState().getRemoteMcastMacs(instanceIdentifier, mac.getKey());
-            McastMacsRemote mcastMacsRemote = TyperUtils.getTypedRowWrapper(transaction.getDatabaseSchema(),
-                    McastMacsRemote.class, null);
-            if (operationalMacOptional.isPresent()) {
-                //when mac entry is deleted, its referenced locator set and locators are deleted automatically.
-                //TODO: locator in config DS is not deleted
-                String macString = null;
-                if (mac.getMacEntryKey().equals(HwvtepSouthboundConstants.UNKNOWN_DST_MAC)) {
-                    macString = HwvtepSouthboundConstants.UNKNOWN_DST_STRING;
-                } else {
-                    macString = mac.getMacEntryKey().getValue();
-                }
+    private void removeMcastMacRemote(final TransactionBuilder transaction,
+                                      final InstanceIdentifier<Node> nodeIid, final List<RemoteMcastMacs> macList) {
+        for (RemoteMcastMacs mac : macList) {
+            InstanceIdentifier<RemoteMcastMacs> macKey = nodeIid.augmentation(HwvtepGlobalAugmentation.class)
+                    .child(RemoteMcastMacs.class, mac.key());
+            onConfigUpdate(transaction, nodeIid, mac, macKey);
+        }
+    }
+
+    @Override
+    public void onConfigUpdate(final TransactionBuilder transaction,
+                               final InstanceIdentifier<Node> nodeIid,
+                               final RemoteMcastMacs remoteMcastMac,
+                               final InstanceIdentifier macKey,
+                               final Object... extraData) {
+        processDependencies(EmptyDependencyGetter.INSTANCE, transaction, nodeIid, macKey, remoteMcastMac);
+    }
+
+    @Override
+    public void doDeviceTransaction(final TransactionBuilder transaction,
+                                    final InstanceIdentifier<Node> instanceIdentifier,
+                                    final RemoteMcastMacs mac,
+                                    final InstanceIdentifier macIid,
+                                    final Object... extraData) {
+        String nodeId = instanceIdentifier.firstKeyOf(Node.class).getNodeId().getValue();
+        clearConfigData(RemoteMcastMacs.class, macIid);
+        long transactionId = getOperationalState().getTransactionId();
+        LOG.debug("Remove received for RemoteMcastMacs key: {} txId: {}", macIid, transactionId);
+        HwvtepDeviceInfo.DeviceData deviceData = getDeviceOpData(RemoteMcastMacs.class, macIid);
+        McastMacsRemote mcastMacsRemote = transaction.getTypedRowSchema(McastMacsRemote.class);
+        boolean deleted = false;
+        if (deviceData != null && deviceData.getData() != null && deviceData.getData() instanceof McastMacsRemote
+                && ((McastMacsRemote)deviceData.getData()).getLogicalSwitchColumn() != null) {
+            UUID logicalSwitchUid = ((McastMacsRemote)deviceData.getData()).getLogicalSwitchColumn().getData();
+            if (logicalSwitchUid != null) {
+                transaction.add(op.delete(mcastMacsRemote.getSchema())
+                        .where(mcastMacsRemote.getLogicalSwitchColumn().getSchema().opEqual(logicalSwitchUid)).build());
+                deleted = true;
+                updateCurrentTxDeleteData(RemoteMcastMacs.class, macIid, mac);
+                updateControllerTxHistory(TransactionType.DELETE, new StringBuilder(mcastMacsRemote.toString())
+                        .append(":  LS: ").append(logicalSwitchUid));
+                LOG.info("CONTROLLER - {} {} LS:{} Node:{}", TransactionType.DELETE,
+                    mcastMacsRemote, logicalSwitchUid, nodeId);
+
+            }
+        }
+        if (!deleted && deviceData != null) {
+            UUID macEntryUUID = deviceData.getUuid();
+            if (macEntryUUID != null) {
+                mcastMacsRemote.getUuidColumn().setData(macEntryUUID);
+                updateCurrentTxDeleteData(RemoteMcastMacs.class, macIid, mac);
                 transaction.add(op.delete(mcastMacsRemote.getSchema())
-                        .where(mcastMacsRemote.getMacColumn().getSchema().opEqual(macString)).build());
-                transaction.add(op.comment("Remote McastMacRemote: Deleting " + mac.getMacEntryKey().getValue()));
+                        .where(mcastMacsRemote.getUuidColumn().getSchema().opEqual(macEntryUUID)).build());
+                updateControllerTxHistory(TransactionType.DELETE, new StringBuilder(mcastMacsRemote.toString())
+                        .append(":  Mac : ").append(macEntryUUID));
+                LOG.info("CONTROLLER - {} {} Mac :{} Node:{}", TransactionType.DELETE,
+                    mcastMacsRemote, macEntryUUID, nodeId);
             } else {
-                LOG.warn("Unable to delete remoteMcastMacs {} because it was not found in the operational store",
-                        mac.getMacEntryKey().getValue());
+                LOG.error("Failed to delete remote mcast entry as it is not found in device {}", macIid);
+                getDeviceInfo().clearConfigData(RemoteMcastMacs.class, macIid);
+                return;
             }
         }
     }
 
-    private Map<InstanceIdentifier<Node>, List<RemoteMcastMacs>> extractRemoved(
-            Collection<DataTreeModification<Node>> changes, Class<RemoteMcastMacs> class1) {
-        Map<InstanceIdentifier<Node>, List<RemoteMcastMacs>> result
-            = new HashMap<InstanceIdentifier<Node>, List<RemoteMcastMacs>>();
-        if (changes != null && !changes.isEmpty()) {
-            for (DataTreeModification<Node> change : changes) {
-                final InstanceIdentifier<Node> key = change.getRootPath().getRootIdentifier();
-                final DataObjectModification<Node> mod = change.getRootNode();
-                //If the node which remoteMcastMacs belong to is removed, all remoteMcastMacs should be removed too.
-                Node removed = TransactUtils.getRemoved(mod);
-                if (removed != null) {
-                    List<RemoteMcastMacs> macListRemoved = null;
-                    if (removed.getAugmentation(HwvtepGlobalAugmentation.class) != null) {
-                        macListRemoved = removed.getAugmentation(HwvtepGlobalAugmentation.class).getRemoteMcastMacs();
-                    }
-                    if (macListRemoved != null) {
-                        result.put(key, macListRemoved);
-                    }
-                }
-                //If the node which remoteMcastMacs belong to is updated, and remoteMcastMacs may
-                //be created or updated or deleted, we need to get deleted ones.
-                Node updated = TransactUtils.getUpdated(mod);
-                Node before = mod.getDataBefore();
-                if (updated != null && before != null) {
-                    List<RemoteMcastMacs> macListUpdated = null;
-                    List<RemoteMcastMacs> macListBefore = null;
-                    HwvtepGlobalAugmentation hgUpdated = updated.getAugmentation(HwvtepGlobalAugmentation.class);
-                    if (hgUpdated != null) {
-                        macListUpdated = hgUpdated.getRemoteMcastMacs();
-                    }
-                    HwvtepGlobalAugmentation hgBefore = before.getAugmentation(HwvtepGlobalAugmentation.class);
-                    if (hgBefore != null) {
-                        macListBefore = hgBefore.getRemoteMcastMacs();
-                    }
-                    if (macListBefore != null) {
-                        List<RemoteMcastMacs> macListRemoved = new ArrayList<RemoteMcastMacs>();
-                        if (macListUpdated != null) {
-                            macListBefore.removeAll(macListUpdated);
-                        }
-                        //then exclude updated remoteMcastMacs
-                        for (RemoteMcastMacs macBefore: macListBefore) {
-                            int i = 0;
-                            for(; i < macListUpdated.size(); i++) {
-                                if (macBefore.getMacEntryKey().equals(macListUpdated.get(i).getMacEntryKey())) {
-                                    break;
-                                }
-                            }
-                            if (i == macListUpdated.size()) {
-                                macListRemoved.add(macBefore);
-                            }
-                        }
-                        if (!macListRemoved.isEmpty()) {
-                            result.put(key, macListRemoved);
-                        }
-                    }
-                }
-            }
-        }
-        return result;
+    @Override
+    protected Map<RemoteMcastMacsKey, RemoteMcastMacs> getData(final HwvtepGlobalAugmentation augmentation) {
+        return augmentation.getRemoteMcastMacs();
+    }
+
+    @Override
+    protected boolean areEqual(final RemoteMcastMacs macs1, final RemoteMcastMacs macs2) {
+        return macs1.key().equals(macs2.key()) && Objects.equals(macs1.getLocatorSet(), macs2.getLocatorSet());
     }
+
+    @Override
+    protected boolean isDeleteCmd() {
+        return true;
+    }
+
 }