Bug 6185 - southbound system tests failing when
[ovsdb.git] / hwvtepsouthbound / hwvtepsouthbound-impl / src / main / java / org / opendaylight / ovsdb / hwvtepsouthbound / transact / LogicalSwitchUpdateCommand.java
index 730c936b5058b9d0662b48ca57af0984e3eb4460..905a788dcf3a8147ed40fe7d220ecaf2dab60dc9 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * 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,
@@ -13,18 +13,18 @@ import static org.opendaylight.ovsdb.lib.operations.Operations.op;
 import java.util.Collection;
 import java.util.HashMap;
 import java.util.HashSet;
+import java.util.List;
 import java.util.Map;
 import java.util.Map.Entry;
 import java.util.Set;
 
 import org.opendaylight.controller.md.sal.binding.api.DataObjectModification;
 import org.opendaylight.controller.md.sal.binding.api.DataTreeModification;
-import org.opendaylight.controller.md.sal.binding.api.DataObjectModification.ModificationType;
 import org.opendaylight.ovsdb.lib.operations.TransactionBuilder;
-import org.opendaylight.ovsdb.lib.schema.DatabaseSchema;
 import org.opendaylight.ovsdb.lib.schema.typed.TyperUtils;
 import org.opendaylight.ovsdb.schema.hardwarevtep.LogicalSwitch;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepLogicalSwitchAugmentation;
+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;
 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
 import org.slf4j.Logger;
@@ -42,84 +42,123 @@ public class LogicalSwitchUpdateCommand extends AbstractTransactCommand {
 
     @Override
     public void execute(TransactionBuilder transaction) {
-        Map<InstanceIdentifier<HwvtepLogicalSwitchAugmentation>, HwvtepLogicalSwitchAugmentation> created =
-                extractCreated(getChanges(),HwvtepLogicalSwitchAugmentation.class);
-        if(created != null) {
-            for(Entry<InstanceIdentifier<HwvtepLogicalSwitchAugmentation>, HwvtepLogicalSwitchAugmentation> logicalSwitchEntry:
-                created.entrySet()) {
-                updateLogicalSwitch(transaction,  logicalSwitchEntry.getKey(), logicalSwitchEntry.getValue());
+        Map<InstanceIdentifier<Node>, List<LogicalSwitches>> createds =
+                extractCreated(getChanges(),LogicalSwitches.class);
+        if (!createds.isEmpty()) {
+            for (Entry<InstanceIdentifier<Node>, List<LogicalSwitches>> created:
+                createds.entrySet()) {
+                updateLogicalSwitch(transaction,  created.getKey(), created.getValue());
+            }
+        }
+        Map<InstanceIdentifier<Node>, List<LogicalSwitches>> updateds =
+                extractUpdated(getChanges(),LogicalSwitches.class);
+        if (!updateds.isEmpty()) {
+            for (Entry<InstanceIdentifier<Node>, List<LogicalSwitches>> updated:
+                updateds.entrySet()) {
+                updateLogicalSwitch(transaction,  updated.getKey(), updated.getValue());
             }
         }
     }
 
-
     private void updateLogicalSwitch(TransactionBuilder transaction,
-            InstanceIdentifier<HwvtepLogicalSwitchAugmentation> iid, HwvtepLogicalSwitchAugmentation logicalSwitchAugmentation) {
-        LOG.debug("Creating a logical switch named: {}", logicalSwitchAugmentation.getHwvtepNodeName());
-        Optional<HwvtepLogicalSwitchAugmentation> operationalLogicalSwitchOptional =
-                getOperationalState().getLogicalSwitchAugmentation(iid);
-        DatabaseSchema dbSchema = transaction.getDatabaseSchema();
-        LogicalSwitch logicalSwitch = TyperUtils.getTypedRowWrapper(transaction.getDatabaseSchema(), LogicalSwitch.class);
-        if(!operationalLogicalSwitchOptional.isPresent()) {
-            setName(logicalSwitch, logicalSwitchAugmentation, operationalLogicalSwitchOptional);
-            setTunnelKey(logicalSwitch, logicalSwitchAugmentation, operationalLogicalSwitchOptional);
-            transaction.add(op.insert(logicalSwitch));
-        } else {
-            String existingLogicalSwitchName = operationalLogicalSwitchOptional.get().getHwvtepNodeName().getValue();
-            // Name is immutable, and so we *can't* update it.  So we use extraBridge for the schema stuff
-            LogicalSwitch extraLogicalSwitch = TyperUtils.getTypedRowWrapper(transaction.getDatabaseSchema(), LogicalSwitch.class);
-            extraLogicalSwitch.setName("");
-            transaction.add(op.update(logicalSwitch)
-                    .where(extraLogicalSwitch.getNameColumn().getSchema().opEqual(existingLogicalSwitchName))
-                    .build());
-            //stampInstanceIdentifier(transaction, iid.firstIdentifierOf(Node.class),existingBridgeName);
+            InstanceIdentifier<Node> instanceIdentifier, List<LogicalSwitches> lswitchList) {
+        for (LogicalSwitches lswitch: lswitchList) {
+            LOG.debug("Creating logcial switch named: {}", lswitch.getHwvtepNodeName());
+            Optional<LogicalSwitches> operationalSwitchOptional =
+                    getOperationalState().getLogicalSwitches(instanceIdentifier, lswitch.getKey());
+            LogicalSwitch logicalSwitch = TyperUtils.getTypedRowWrapper(transaction.getDatabaseSchema(), LogicalSwitch.class);
+            setDescription(logicalSwitch, lswitch);
+            setTunnelKey(logicalSwitch, lswitch);
+            if (!operationalSwitchOptional.isPresent()) {
+                setName(logicalSwitch, lswitch, operationalSwitchOptional);
+                LOG.trace("execute: creating LogicalSwitch entry: {}", logicalSwitch);
+                transaction.add(op.insert(logicalSwitch));
+                transaction.add(op.comment("Logical Switch: Creating " + lswitch.getHwvtepNodeName().getValue()));
+            } else {
+                LogicalSwitches updatedLSwitch = operationalSwitchOptional.get();
+                String existingLogicalSwitchName = updatedLSwitch.getHwvtepNodeName().getValue();
+                // Name is immutable, and so we *can't* update it.  So we use extraBridge for the schema stuff
+                LogicalSwitch extraLogicalSwitch = TyperUtils.getTypedRowWrapper(transaction.getDatabaseSchema(), LogicalSwitch.class);
+                extraLogicalSwitch.setName("");
+                LOG.trace("execute: updating LogicalSwitch entry: {}", logicalSwitch);
+                transaction.add(op.update(logicalSwitch)
+                        .where(extraLogicalSwitch.getNameColumn().getSchema().opEqual(existingLogicalSwitchName))
+                        .build());
+                transaction.add(op.comment("Logical Switch: Updating " + existingLogicalSwitchName));
+            }
         }
     }
 
-    private void setName(LogicalSwitch logicalSwitch, HwvtepLogicalSwitchAugmentation logicalSwitchAugmentation,
-            Optional<HwvtepLogicalSwitchAugmentation> operationalLogicalSwitchOptional) {
-        if(logicalSwitchAugmentation.getHwvtepNodeName() != null) {
-            logicalSwitch.setName(logicalSwitchAugmentation.getHwvtepNodeName().getValue());
-        } else if(operationalLogicalSwitchOptional.isPresent() && operationalLogicalSwitchOptional.get().getHwvtepNodeName() != null) {
-            logicalSwitch.setName(operationalLogicalSwitchOptional.get().getHwvtepNodeName().getValue());
+    private void setDescription(LogicalSwitch logicalSwitch, LogicalSwitches inputSwitch) {
+        if(inputSwitch.getHwvtepNodeDescription() != null) {
+            logicalSwitch.setDescription(inputSwitch.getHwvtepNodeDescription());
         }
     }
 
-    private void setTunnelKey(LogicalSwitch logicalSwitch, HwvtepLogicalSwitchAugmentation logicalSwitchAugmentation,
-            Optional<HwvtepLogicalSwitchAugmentation> operationalLogicalSwitchOptional) {
-        if(logicalSwitchAugmentation.getTunnelKey() != null) {
-            Set<Long> tunnel = new HashSet<Long>();
-            tunnel.add(Long.valueOf(logicalSwitchAugmentation.getTunnelKey()));
-            logicalSwitch.setTunnelKey(tunnel);
-        } else if(operationalLogicalSwitchOptional.isPresent() && operationalLogicalSwitchOptional.get().getTunnelKey() != null) {
+    private void setName(LogicalSwitch logicalSwitch, LogicalSwitches inputSwitch,
+            Optional<LogicalSwitches> inputSwitchOptional) {
+        if (inputSwitch.getHwvtepNodeName() != null) {
+            logicalSwitch.setName(inputSwitch.getHwvtepNodeName().getValue());
+        } else if (inputSwitchOptional.isPresent() && inputSwitchOptional.get().getHwvtepNodeName() != null) {
+            logicalSwitch.setName(inputSwitchOptional.get().getHwvtepNodeName().getValue());
+        }
+    }
+
+    private void setTunnelKey(LogicalSwitch logicalSwitch, LogicalSwitches inputSwitch) {
+        if (inputSwitch.getTunnelKey() != null) {
             Set<Long> tunnel = new HashSet<Long>();
-            tunnel.add(Long.valueOf(operationalLogicalSwitchOptional.get().getTunnelKey()));
+            tunnel.add(Long.valueOf(inputSwitch.getTunnelKey()));
             logicalSwitch.setTunnelKey(tunnel);
         }
     }
 
-    private Node getCreated(DataObjectModification<Node> mod) {
-        if((mod.getModificationType() == ModificationType.WRITE)
-                        && (mod.getDataBefore() == null)){
-            return mod.getDataAfter();
+    private Map<InstanceIdentifier<Node>, List<LogicalSwitches>> extractCreated(
+            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();
+                Node created = TransactUtils.getCreated(mod);
+                if (created != null) {
+                    List<LogicalSwitches> lswitchListUpdated = null;
+                    if (created.getAugmentation(HwvtepGlobalAugmentation.class) != null) {
+                        lswitchListUpdated = created.getAugmentation(HwvtepGlobalAugmentation.class).getLogicalSwitches();
+                    }
+                    if (lswitchListUpdated != null) {
+                        result.put(key, lswitchListUpdated);
+                    }
+                }
+            }
         }
-        return null;
+        return result;
     }
 
-    private Map<InstanceIdentifier<HwvtepLogicalSwitchAugmentation>, HwvtepLogicalSwitchAugmentation> extractCreated(
-            Collection<DataTreeModification<Node>> changes, Class<HwvtepLogicalSwitchAugmentation> class1) {
-        Map<InstanceIdentifier<HwvtepLogicalSwitchAugmentation>, HwvtepLogicalSwitchAugmentation> result
-            = new HashMap<InstanceIdentifier<HwvtepLogicalSwitchAugmentation>, HwvtepLogicalSwitchAugmentation>();
-        if(changes != null && !changes.isEmpty()) {
-            for(DataTreeModification<Node> change : changes) {
+    private Map<InstanceIdentifier<Node>, List<LogicalSwitches>> extractUpdated(
+            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();
-                Node created = getCreated(mod);
-                if(created != null) {
-                    HwvtepLogicalSwitchAugmentation logicalSwitch = created.getAugmentation(HwvtepLogicalSwitchAugmentation.class);
-                    InstanceIdentifier<HwvtepLogicalSwitchAugmentation> iid = change.getRootPath().getRootIdentifier().augmentation(HwvtepLogicalSwitchAugmentation.class);
-                    if(logicalSwitch != null) {
-                        result.put(iid, logicalSwitch);
+                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 (lswitchListUpdated != null) {
+                        if (lswitchListBefore != null) {
+                            lswitchListUpdated.removeAll(lswitchListBefore);
+                        }
+                        result.put(key, lswitchListUpdated);
                     }
                 }
             }