Added device transaction log cli
[ovsdb.git] / hwvtepsouthbound / hwvtepsouthbound-impl / src / main / java / org / opendaylight / ovsdb / hwvtepsouthbound / transact / LogicalSwitchRemoveCommand.java
index 755d550d4146854841975776cae21cc976287dcd..619895b3058344ad29f0fc74dfcf52807f3bb914 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2015 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,19 +10,26 @@ 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.Collections;
 import java.util.List;
 import java.util.Map;
 import java.util.Map.Entry;
+import java.util.Objects;
 
-import org.opendaylight.controller.md.sal.binding.api.DataObjectModification;
 import org.opendaylight.controller.md.sal.binding.api.DataTreeModification;
+import org.opendaylight.ovsdb.hwvtepsouthbound.HwvtepConnectionInstance;
+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.LogicalSwitch;
+import org.opendaylight.ovsdb.schema.hardwarevtep.McastMacsLocal;
+import org.opendaylight.ovsdb.schema.hardwarevtep.McastMacsRemote;
+import org.opendaylight.ovsdb.schema.hardwarevtep.UcastMacsLocal;
+import org.opendaylight.ovsdb.schema.hardwarevtep.UcastMacsRemote;
+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.hwvtep.global.attributes.LogicalSwitches;
 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
@@ -30,9 +37,7 @@ import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-import com.google.common.base.Optional;
-
-public class LogicalSwitchRemoveCommand extends AbstractTransactCommand {
+public class LogicalSwitchRemoveCommand extends AbstractTransactCommand<LogicalSwitches, HwvtepGlobalAugmentation> {
     private static final Logger LOG = LoggerFactory.getLogger(LogicalSwitchRemoveCommand.class);
 
     public LogicalSwitchRemoveCommand(HwvtepOperationalState state,
@@ -44,92 +49,119 @@ public class LogicalSwitchRemoveCommand extends AbstractTransactCommand {
     public void execute(TransactionBuilder transaction) {
         Map<InstanceIdentifier<Node>, List<LogicalSwitches>> removeds =
                 extractRemoved(getChanges(),LogicalSwitches.class);
-        if (!removeds.isEmpty()) {
-            for (Entry<InstanceIdentifier<Node>, List<LogicalSwitches>> created:
-                removeds.entrySet()) {
-                removeLogicalSwitch(transaction,  created.getKey(), created.getValue());
+        if (removeds != null) {
+            for (Entry<InstanceIdentifier<Node>, List<LogicalSwitches>> created: removeds.entrySet()) {
+                if (!HwvtepSouthboundUtil.isEmpty(created.getValue())) {
+                    HwvtepConnectionInstance connectionInstance = getDeviceInfo().getConnectionInstance();
+                    getDeviceInfo().scheduleTransaction(new TransactCommand() {
+                        @Override
+                        public void execute(TransactionBuilder transactionBuilder) {
+                            HwvtepOperationalState operState = new HwvtepOperationalState(
+                                    connectionInstance.getDataBroker(), connectionInstance, Collections.EMPTY_LIST);
+                            threadLocalOperationalState.set(operState);
+                            threadLocalDeviceTransaction.set(transactionBuilder);
+                            LOG.debug("Running delete logical switch in seperate tx {}", created.getKey());
+                            removeLogicalSwitch(transactionBuilder, created.getKey(), created.getValue());
+                        }
+
+                        @Override
+                        public void onSuccess(TransactionBuilder deviceTransaction) {
+                            LogicalSwitchRemoveCommand.this.onSuccess(deviceTransaction);
+                        }
+
+                        @Override
+                        public void onFailure(TransactionBuilder deviceTransaction) {
+                            LogicalSwitchRemoveCommand.this.onFailure(deviceTransaction);
+                        }
+                    });
+                }
             }
         }
     }
 
-    private void removeLogicalSwitch(TransactionBuilder transaction,
-            InstanceIdentifier<Node> instanceIdentifier, List<LogicalSwitches> lswitchList) {
+    private void removeLogicalSwitch(final TransactionBuilder transaction,
+                                     final InstanceIdentifier<Node> nodeIid, final List<LogicalSwitches> lswitchList) {
         for (LogicalSwitches lswitch: lswitchList) {
-            LOG.debug("Removing logcial switch named: {}", lswitch.getHwvtepNodeName().getValue());
-            Optional<LogicalSwitches> operationalSwitchOptional =
-                    getOperationalState().getLogicalSwitches(instanceIdentifier, lswitch.getKey());
+            InstanceIdentifier<LogicalSwitches> lsKey = nodeIid.augmentation(HwvtepGlobalAugmentation.class).
+                    child(LogicalSwitches.class, lswitch.getKey());
+            onConfigUpdate(transaction, nodeIid, lswitch, lsKey);
+        }
+    }
+
+    @Override
+    public void onConfigUpdate(final TransactionBuilder transaction,
+                               final InstanceIdentifier<Node> nodeIid,
+                               final LogicalSwitches lswitch,
+                               final InstanceIdentifier lsKey,
+                               final Object... extraData) {
+        processDependencies(null, transaction, nodeIid, lsKey, lswitch);
+    }
+
+    @Override
+    public void doDeviceTransaction(final TransactionBuilder transaction,
+                                    final InstanceIdentifier<Node> instanceIdentifier,
+                                    final LogicalSwitches lswitch,
+                                    final InstanceIdentifier lsKey,
+                                    final Object... extraData) {
+            LOG.debug("Removing logical switch named: {}", lswitch.getHwvtepNodeName().getValue());
+            HwvtepDeviceInfo.DeviceData deviceData  = getOperationalState().getDeviceInfo().getDeviceOperData(
+                    LogicalSwitches.class, lsKey);
             LogicalSwitch logicalSwitch = TyperUtils.getTypedRowWrapper(transaction.getDatabaseSchema(), LogicalSwitch.class, null);
 
-            if (operationalSwitchOptional.isPresent() &&
-                    operationalSwitchOptional.get().getLogicalSwitchUuid() != null) {
-                UUID logicalSwitchUuid = new UUID(operationalSwitchOptional.get().getLogicalSwitchUuid().getValue());
+            if (deviceData != null && deviceData.getUuid() != null) {
+                UUID logicalSwitchUuid = deviceData.getUuid();
                 transaction.add(op.delete(logicalSwitch.getSchema())
                         .where(logicalSwitch.getUuidColumn().getSchema().opEqual(logicalSwitchUuid)).build());
-                transaction.add(op.comment("Logical Switch: Deleting " + lswitch.getHwvtepNodeName().getValue()));
+
+                UcastMacsRemote ucastMacsRemote = TyperUtils.getTypedRowWrapper(transaction.getDatabaseSchema(),
+                        UcastMacsRemote.class, null);
+                transaction.add(op.delete(ucastMacsRemote.getSchema())
+                        .where(ucastMacsRemote.getLogicalSwitchColumn().getSchema().opEqual(logicalSwitchUuid)).build());
+
+                UcastMacsLocal ucastMacsLocal = TyperUtils.getTypedRowWrapper(transaction.getDatabaseSchema(),
+                        UcastMacsLocal.class, null);
+                transaction.add(op.delete(ucastMacsLocal.getSchema())
+                        .where(ucastMacsLocal.getLogicalSwitchColumn().getSchema().opEqual(logicalSwitchUuid)).build());
+
+                McastMacsRemote mcastMacsRemote = TyperUtils.getTypedRowWrapper(transaction.getDatabaseSchema(),
+                        McastMacsRemote.class, null);
+                transaction.add(op.delete(mcastMacsRemote.getSchema())
+                        .where(mcastMacsRemote.getLogicalSwitchColumn().getSchema().opEqual(logicalSwitchUuid)).build());
+
+                McastMacsLocal mcastMacsLocal = TyperUtils.getTypedRowWrapper(transaction.getDatabaseSchema(),
+                        McastMacsLocal.class, null);
+                transaction.add(op.delete(mcastMacsLocal.getSchema())
+                        .where(mcastMacsLocal.getLogicalSwitchColumn().getSchema().opEqual(logicalSwitchUuid)).build());
+                updateCurrentTxDeleteData(LogicalSwitches.class, lsKey, lswitch);
+                updateControllerTxHistory(TransactionType.DELETE, lswitch);
             } else {
                 LOG.warn("Unable to delete logical switch {} because it was not found in the operational store",
                         lswitch.getHwvtepNodeName().getValue());
             }
-        }
     }
 
-    private Map<InstanceIdentifier<Node>, List<LogicalSwitches>> extractRemoved(
-            Collection<DataTreeModification<Node>> changes, Class<LogicalSwitches> class1) {
-        Map<InstanceIdentifier<Node>, List<LogicalSwitches>> result
-            = new HashMap<InstanceIdentifier<Node>, List<LogicalSwitches>>();
-        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 logical switches belong to is removed, all logical switches
-                //should be removed too.
-                Node removed = TransactUtils.getRemoved(mod);
-                if (removed != null) {
-                    List<LogicalSwitches> lswitchListRemoved = null;
-                    if (removed.getAugmentation(HwvtepGlobalAugmentation.class) != null) {
-                        lswitchListRemoved = removed.getAugmentation(HwvtepGlobalAugmentation.class).getLogicalSwitches();
-                    }
-                    if (lswitchListRemoved != null) {
-                        result.put(key, lswitchListRemoved);
-                    }
-                }
-                //If the node which logical switches belong to is updated, and logical switches 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<LogicalSwitches> lswitchListUpdated = null;
-                    List<LogicalSwitches> lswitchListBefore = null;
-                    if (updated.getAugmentation(HwvtepGlobalAugmentation.class) != null) {
-                        lswitchListUpdated = updated.getAugmentation(HwvtepGlobalAugmentation.class).getLogicalSwitches();
-                    }
-                    if (before.getAugmentation(HwvtepGlobalAugmentation.class) != null) {
-                        lswitchListBefore = before.getAugmentation(HwvtepGlobalAugmentation.class).getLogicalSwitches();
-                    }
-                    if (lswitchListBefore != null) {
-                        List<LogicalSwitches> lswitchListRemoved = new ArrayList<LogicalSwitches>();
-                        if (lswitchListUpdated != null) {
-                            lswitchListBefore.removeAll(lswitchListUpdated);
-                        }
-                        //then exclude updated ones
-                        for (LogicalSwitches lswitchBefore: lswitchListBefore) {
-                            int i = 0;
-                            for(; i < lswitchListUpdated.size(); i++) {
-                                if (lswitchBefore.getHwvtepNodeName().equals(lswitchListUpdated.get(i).getHwvtepNodeName())) {
-                                    break;
-                                }
-                            }
-                            if (i == lswitchListUpdated.size()) {
-                                lswitchListRemoved.add(lswitchBefore);
-                            }
-                        }
-                        if (!lswitchListRemoved.isEmpty()) {
-                            result.put(key, lswitchListRemoved);
-                        }
-                    }
-                }
-            }
+    @Override
+    protected List<LogicalSwitches> getData(HwvtepGlobalAugmentation augmentation) {
+        return augmentation.getLogicalSwitches();
+    }
+
+    @Override
+    protected boolean areEqual(LogicalSwitches a , LogicalSwitches b) {
+        return a.getKey().equals(b.getKey()) && Objects.equals(a.getTunnelKey(), b.getTunnelKey());
+    }
+
+    @Override
+    protected boolean isRemoveCommand() {
+        return true;
+    }
+
+    @Override
+    public void onCommandSucceeded() {
+        if (getDeviceTransaction() == null || !updates.containsKey(getDeviceTransaction())) {
+            return;
+        }
+        for (MdsalUpdate mdsalUpdate : updates.get(getDeviceTransaction())) {
+            getDeviceInfo().clearLogicalSwitchRefs((InstanceIdentifier<LogicalSwitches>) mdsalUpdate.getKey());
         }
-        return result;
     }
 }