bug 8257 handling back to back ucast mac updates
[ovsdb.git] / hwvtepsouthbound / hwvtepsouthbound-impl / src / main / java / org / opendaylight / ovsdb / hwvtepsouthbound / transact / UcastMacsRemoteRemoveCommand.java
index 3126431b9696ce459e3570a9a74c50a8cfa39d31..2492e7cbb431936cd3b215ff3494c8d66bf9cb38 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2015, 2016 China Telecom Beijing Research Institute and others.  All rights reserved.
+ * Copyright (c) 2015, 2017 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,
@@ -10,15 +10,13 @@ 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.HwvtepDeviceInfo;
 import org.opendaylight.ovsdb.lib.notation.UUID;
 import org.opendaylight.ovsdb.lib.operations.TransactionBuilder;
 import org.opendaylight.ovsdb.lib.schema.typed.TyperUtils;
@@ -30,9 +28,7 @@ import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-import com.google.common.base.Optional;
-
-public class UcastMacsRemoteRemoveCommand extends AbstractTransactCommand {
+public class UcastMacsRemoteRemoveCommand extends AbstractTransactCommand<RemoteUcastMacs, HwvtepGlobalAugmentation> {
     private static final Logger LOG = LoggerFactory.getLogger(UcastMacsRemoteRemoveCommand.class);
 
     public UcastMacsRemoteRemoveCommand(HwvtepOperationalState state,
@@ -52,87 +48,61 @@ public class UcastMacsRemoteRemoveCommand extends AbstractTransactCommand {
         }
     }
 
-    private void removeUcastMacRemote(TransactionBuilder transaction,
-            InstanceIdentifier<Node> instanceIdentifier, List<RemoteUcastMacs> macList) {
+
+    private void removeUcastMacRemote(final TransactionBuilder transaction,
+                                      final InstanceIdentifier<Node> instanceIdentifier,
+                                      final List<RemoteUcastMacs> macList) {
         for (RemoteUcastMacs mac: macList) {
+            onConfigUpdate(transaction, instanceIdentifier, mac, null);
+        }
+    }
+
+    @Override
+    public void onConfigUpdate(final TransactionBuilder transaction,
+                               final InstanceIdentifier<Node> nodeIid,
+                               final RemoteUcastMacs remoteUcastMacs,
+                               final InstanceIdentifier macKey,
+                               final Object... extraData) {
+        InstanceIdentifier<RemoteUcastMacs> macIid = nodeIid.augmentation(HwvtepGlobalAugmentation.class).
+                child(RemoteUcastMacs.class, remoteUcastMacs.getKey());
+        processDependencies(null, transaction, nodeIid, macIid, remoteUcastMacs);
+    }
+
+    @Override
+    public void doDeviceTransaction(final TransactionBuilder transaction,
+                                    final InstanceIdentifier<Node> instanceIdentifier,
+                                    final RemoteUcastMacs mac,
+                                    final InstanceIdentifier macKey,
+                                    final Object... extraData) {
             LOG.debug("Removing remoteUcastMacs, mac address: {}", mac.getMacEntryKey().getValue());
-            Optional<RemoteUcastMacs> operationalMacOptional =
-                    getOperationalState().getRemoteUcastMacs(instanceIdentifier, mac.getKey());
+            InstanceIdentifier<RemoteUcastMacs> macIid = instanceIdentifier.augmentation(HwvtepGlobalAugmentation.class).
+                    child(RemoteUcastMacs.class, mac.getKey());
+            HwvtepDeviceInfo.DeviceData deviceData =
+                    getOperationalState().getDeviceInfo().getDeviceOperData(RemoteUcastMacs.class, macIid);
             UcastMacsRemote ucastMacsRemote = TyperUtils.getTypedRowWrapper(transaction.getDatabaseSchema(),
                     UcastMacsRemote.class, null);
-            if (operationalMacOptional.isPresent() && operationalMacOptional.get().getMacEntryUuid() != null) {
+            if (deviceData != null && deviceData.getUuid() != null) {
                 //when mac entry is deleted, its referenced locators are deleted automatically.
                 //locators in config DS is not deleted and need to be removed explicitly by user.
-                UUID macEntryUUID = new UUID(operationalMacOptional.get().getMacEntryUuid().getValue());
+                UUID macEntryUUID = deviceData.getUuid();
                 ucastMacsRemote.getUuidColumn().setData(macEntryUUID);
                 transaction.add(op.delete(ucastMacsRemote.getSchema()).
                         where(ucastMacsRemote.getUuidColumn().getSchema().opEqual(macEntryUUID)).build());
                 transaction.add(op.comment("UcastMacRemote: Deleting " + mac.getMacEntryKey().getValue()));
+                getOperationalState().getDeviceInfo().markKeyAsInTransit(RemoteUcastMacs.class, macKey);
             } else {
                 LOG.warn("Unable to delete remoteUcastMacs {} because it was not found in the operational store",
                         mac.getMacEntryKey().getValue());
             }
-        }
+            updateCurrentTxDeleteData(RemoteUcastMacs.class, macIid, mac);
     }
 
-    private Map<InstanceIdentifier<Node>, List<RemoteUcastMacs>> extractRemoved(
-            Collection<DataTreeModification<Node>> changes, Class<RemoteUcastMacs> class1) {
-        Map<InstanceIdentifier<Node>, List<RemoteUcastMacs>> result
-            = new HashMap<InstanceIdentifier<Node>, List<RemoteUcastMacs>>();
-        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 remoteUcastMacs belong to is removed, all remoteUcastMacs should be removed too.
-                Node removed = TransactUtils.getRemoved(mod);
-                if (removed != null) {
-                    List<RemoteUcastMacs> macListRemoved = null;
-                    if (removed.getAugmentation(HwvtepGlobalAugmentation.class) != null) {
-                        macListRemoved = removed.getAugmentation(HwvtepGlobalAugmentation.class).getRemoteUcastMacs();
-                    }
-                    if (macListRemoved != null) {
-                        result.put(key, macListRemoved);
-                    }
-                }
-                //If the node which remoteUcastMacs belong to is updated, and remoteUcastMacs 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<RemoteUcastMacs> macListUpdated = null;
-                    List<RemoteUcastMacs> macListBefore = null;
-                    HwvtepGlobalAugmentation hgUpdated = updated.getAugmentation(HwvtepGlobalAugmentation.class);
-                    if (hgUpdated != null) {
-                        macListUpdated = hgUpdated.getRemoteUcastMacs();
-                    }
-                    HwvtepGlobalAugmentation hgBefore = before.getAugmentation(HwvtepGlobalAugmentation.class);
-                    if (hgBefore != null) {
-                        macListBefore = hgBefore.getRemoteUcastMacs();
-                    }
-                    if (macListBefore != null) {
-                        List<RemoteUcastMacs> macListRemoved = new ArrayList<RemoteUcastMacs>();
-                        if (macListUpdated != null) {
-                            macListBefore.removeAll(macListUpdated);
-                        }
-                        //then exclude updated remoteUcastMacs
-                        for (RemoteUcastMacs macBefore: macListBefore) {
-                            int i = 0;
-                            for(; i < macListUpdated.size(); i++) {
-                                if (macBefore.getKey().equals(macListUpdated.get(i).getKey())) {
-                                    break;
-                                }
-                            }
-                            if (i == macListUpdated.size()) {
-                                macListRemoved.add(macBefore);
-                            }
-                        }
-                        if (!macListRemoved.isEmpty()) {
-                            result.put(key, macListRemoved);
-                        }
-                    }
-                }
-            }
-        }
-        return result;
+    protected List<RemoteUcastMacs> getData(HwvtepGlobalAugmentation augmentation) {
+        return augmentation.getRemoteUcastMacs();
+    }
+
+    @Override
+    protected boolean isRemoveCommand() {
+        return true;
     }
 }