Update MRI projects for Aluminium
[ovsdb.git] / hwvtepsouthbound / hwvtepsouthbound-impl / src / main / java / org / opendaylight / ovsdb / hwvtepsouthbound / transact / LogicalSwitchUpdateCommand.java
index bbde50e671ea8840f8779e72a957c13f1c2876e8..fa1cb1bafbbbe2f1943890e214e40eac6cccef12 100644 (file)
@@ -1,55 +1,48 @@
 /*
- * Copyright (c) 2015, 2016 China Telecom Beijing Research Institute and others.  All rights reserved.
+ * Copyright © 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,
  * and is available at http://www.eclipse.org/legal/epl-v10.html
  */
-
 package org.opendaylight.ovsdb.hwvtepsouthbound.transact;
 
+import static org.opendaylight.ovsdb.hwvtepsouthbound.HwvtepSouthboundUtil.schemaMismatchLog;
 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.Objects;
 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.mdsal.binding.api.DataTreeModification;
+import org.opendaylight.ovsdb.hwvtepsouthbound.HwvtepDeviceInfo;
+import org.opendaylight.ovsdb.lib.error.SchemaVersionMismatchException;
+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.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.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.LogicalSwitchesKey;
 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;
 import org.slf4j.LoggerFactory;
 
-import com.google.common.base.Optional;
-
-public class LogicalSwitchUpdateCommand extends AbstractTransactCommand {
+public class LogicalSwitchUpdateCommand
+        extends AbstractTransactCommand<LogicalSwitches, LogicalSwitchesKey, HwvtepGlobalAugmentation> {
     private static final Logger LOG = LoggerFactory.getLogger(LogicalSwitchUpdateCommand.class);
 
-    public LogicalSwitchUpdateCommand(HwvtepOperationalState state,
-            Collection<DataTreeModification<Node>> changes) {
+    public LogicalSwitchUpdateCommand(final HwvtepOperationalState state,
+            final Collection<DataTreeModification<Node>> changes) {
         super(state, changes);
     }
 
     @Override
-    public void execute(TransactionBuilder transaction) {
-        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());
-            }
-        }
+    public void execute(final TransactionBuilder transaction) {
         Map<InstanceIdentifier<Node>, List<LogicalSwitches>> updateds =
                 extractUpdated(getChanges(),LogicalSwitches.class);
         if (!updateds.isEmpty()) {
@@ -60,109 +53,99 @@ public class LogicalSwitchUpdateCommand extends AbstractTransactCommand {
         }
     }
 
-    private void updateLogicalSwitch(TransactionBuilder transaction,
-            InstanceIdentifier<Node> instanceIdentifier, List<LogicalSwitches> lswitchList) {
+    public void updateLogicalSwitch(final TransactionBuilder transaction,
+                                     final InstanceIdentifier<Node> nodeIid, final 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).withId(TransactUtils.getLogicalSwitchId(lswitch)));
-                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));
-            }
+            InstanceIdentifier<LogicalSwitches> lsKey = nodeIid.augmentation(HwvtepGlobalAugmentation.class)
+                    .child(LogicalSwitches.class, lswitch.key());
+            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(EmptyDependencyGetter.INSTANCE, 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("Creating logical switch named: {}", lswitch.getHwvtepNodeName());
+        final HwvtepDeviceInfo.DeviceData operationalSwitchOptional =
+                getDeviceInfo().getDeviceOperData(LogicalSwitches.class, lsKey);
+        LogicalSwitch logicalSwitch = transaction.getTypedRowWrapper(LogicalSwitch.class);
+        setDescription(logicalSwitch, lswitch);
+        setTunnelKey(logicalSwitch, lswitch);
+        setReplicationMode(logicalSwitch, lswitch);
+        if (operationalSwitchOptional == null) {
+            setName(logicalSwitch, lswitch);
+            LOG.trace("execute: creating LogicalSwitch entry: {}", logicalSwitch);
+            transaction.add(op.insert(logicalSwitch).withId(TransactUtils.getLogicalSwitchId(lswitch)));
+            transaction.add(op.comment("Logical Switch: Creating " + lswitch.getHwvtepNodeName().getValue()));
+            UUID lsUuid = new UUID(TransactUtils.getLogicalSwitchId(lswitch));
+            updateCurrentTxData(LogicalSwitches.class, lsKey, lsUuid, lswitch);
+            updateControllerTxHistory(TransactionType.ADD, logicalSwitch);
+        } else {
+            String existingLogicalSwitchName = lswitch.getHwvtepNodeName().getValue();
+            // Name is immutable, and so we *can't* update it.  So we use extraBridge for the schema stuff
+            LogicalSwitch extraLogicalSwitch = transaction.getTypedRowWrapper(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));
+            updateControllerTxHistory(TransactionType.UPDATE, logicalSwitch);
         }
     }
 
-    private void setDescription(LogicalSwitch logicalSwitch, LogicalSwitches inputSwitch) {
-        if(inputSwitch.getHwvtepNodeDescription() != null) {
+    private static void setDescription(final LogicalSwitch logicalSwitch, final LogicalSwitches inputSwitch) {
+        if (inputSwitch.getHwvtepNodeDescription() != null) {
             logicalSwitch.setDescription(inputSwitch.getHwvtepNodeDescription());
         }
     }
 
-    private void setName(LogicalSwitch logicalSwitch, LogicalSwitches inputSwitch,
-            Optional<LogicalSwitches> inputSwitchOptional) {
+    private static void setName(final LogicalSwitch logicalSwitch, final LogicalSwitches inputSwitch) {
         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) {
+    private static void setTunnelKey(final LogicalSwitch logicalSwitch, final LogicalSwitches inputSwitch) {
         if (inputSwitch.getTunnelKey() != null) {
-            Set<Long> tunnel = new HashSet<Long>();
+            Set<Long> tunnel = new HashSet<>();
             tunnel.add(Long.valueOf(inputSwitch.getTunnelKey()));
             logicalSwitch.setTunnelKey(tunnel);
         }
     }
 
-    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);
-                    }
-                }
+    private static void setReplicationMode(final LogicalSwitch logicalSwitch, final LogicalSwitches inputSwitch) {
+        if (inputSwitch.getReplicationMode() != null) {
+            Set<String> mode = new HashSet<>();
+            mode.add(inputSwitch.getReplicationMode());
+            try {
+                logicalSwitch.setReplicationMode(mode);
+            }
+            catch (SchemaVersionMismatchException e) {
+                schemaMismatchLog("replication_mode", "Logical_Switch", e);
             }
         }
-        return result;
     }
 
-    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 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);
-                    }
-                }
-            }
-        }
-        return result;
+    @Override
+    protected Map<LogicalSwitchesKey, LogicalSwitches> getData(final HwvtepGlobalAugmentation augmentation) {
+        return augmentation.getLogicalSwitches();
+    }
+
+    @Override
+    protected boolean areEqual(final LogicalSwitches sw1, final LogicalSwitches sw2) {
+        return sw1.key().equals(sw2.key()) && Objects.equals(sw1.getTunnelKey(), sw2.getTunnelKey());
     }
 }