bug 6579 checking the depency before transaction
[ovsdb.git] / hwvtepsouthbound / hwvtepsouthbound-impl / src / main / java / org / opendaylight / ovsdb / hwvtepsouthbound / transact / McastMacsRemoteUpdateCommand.java
index 1afc244f50538c578a945b3ba6093b345e8a1ec5..30e40ad66197c6ef42d77758dac29d7a5381afa0 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,
@@ -8,36 +8,39 @@
 
 package org.opendaylight.ovsdb.hwvtepsouthbound.transact;
 
-import static org.opendaylight.ovsdb.lib.operations.Operations.op;
-
-import java.util.Collection;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
-import java.util.Map.Entry;
-
+import com.google.common.base.Optional;
+import com.google.common.collect.Lists;
 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 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.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.Uuid;
 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.HwvtepNodeName;
 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.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.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;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Map.Entry;
+
+import static org.opendaylight.ovsdb.lib.operations.Operations.op;
 
-public class McastMacsRemoteUpdateCommand extends AbstractTransactCommand {
-    private static final Logger LOG = LoggerFactory.getLogger(PhysicalPortRemoveCommand.class);
+public class McastMacsRemoteUpdateCommand extends AbstractTransactCommand<RemoteMcastMacs> {
+    private static final Logger LOG = LoggerFactory.getLogger(McastMacsRemoteUpdateCommand.class);
+    private static final McastMacUnMetDependencyGetter MCAST_MAC_DATA_VALIDATOR = new McastMacUnMetDependencyGetter();
 
     public McastMacsRemoteUpdateCommand(HwvtepOperationalState state,
             Collection<DataTreeModification<Node>> changes) {
@@ -67,40 +70,67 @@ public class McastMacsRemoteUpdateCommand extends AbstractTransactCommand {
     private void updateMcastMacRemote(TransactionBuilder transaction,
             InstanceIdentifier<Node> instanceIdentifier, List<RemoteMcastMacs> macList) {
         for (RemoteMcastMacs mac: macList) {
+            onConfigUpdate(transaction, instanceIdentifier, mac, null);
+        }
+    }
+
+    @Override
+    public void onConfigUpdate(TransactionBuilder transaction,
+                                  InstanceIdentifier<Node> nodeIid,
+                                  RemoteMcastMacs remoteMcastMac,
+                                  InstanceIdentifier macKey,
+                                  Object... extraData) {
+        InstanceIdentifier<RemoteMcastMacs> macIid = nodeIid.augmentation(HwvtepGlobalAugmentation.class).
+                child(RemoteMcastMacs.class, remoteMcastMac.getKey());
+        processDependencies(MCAST_MAC_DATA_VALIDATOR, transaction, nodeIid, macIid, remoteMcastMac);
+    }
+
+    @Override
+    public void doDeviceTransaction(TransactionBuilder transaction,
+                                       InstanceIdentifier<Node> instanceIdentifier,
+                                       RemoteMcastMacs mac,
+                                       InstanceIdentifier macKey,
+                                       Object... extraData) {
             LOG.debug("Creating remoteMcastMacs, mac address: {}", mac.getMacEntryKey().getValue());
             Optional<RemoteMcastMacs> operationalMacOptional =
                     getOperationalState().getRemoteMcastMacs(instanceIdentifier, mac.getKey());
             McastMacsRemote mcastMacsRemote = TyperUtils.getTypedRowWrapper(transaction.getDatabaseSchema(), McastMacsRemote.class);
             setIpAddress(mcastMacsRemote, mac);
             setLocatorSet(transaction, mcastMacsRemote, mac);
-            setLogicalSwitch(instanceIdentifier, mcastMacsRemote, mac);
+            setLogicalSwitch(mcastMacsRemote, mac);
             if (!operationalMacOptional.isPresent()) {
                 setMac(mcastMacsRemote, mac, operationalMacOptional);
+                LOG.trace("execute: create RemoteMcastMac entry: {}", mcastMacsRemote);
                 transaction.add(op.insert(mcastMacsRemote));
-            } else {
-                RemoteMcastMacs updatedMac = operationalMacOptional.get();
-                String existingMac = updatedMac.getMacEntryKey().getValue();
-                McastMacsRemote extraMac = TyperUtils.getTypedRowWrapper(transaction.getDatabaseSchema(), McastMacsRemote.class);
-                extraMac.setMac("");;
+                transaction.add(op.comment("McastMacRemote: Creating " + mac.getMacEntryKey().getValue()));
+            } else if (operationalMacOptional.get().getMacEntryUuid() != null) {
+                UUID macEntryUUID = new UUID(operationalMacOptional.get().getMacEntryUuid().getValue());
+                McastMacsRemote extraMac = TyperUtils.getTypedRowWrapper(transaction.getDatabaseSchema(),
+                                McastMacsRemote.class, null);
+                extraMac.getUuidColumn().setData(macEntryUUID);
+                LOG.trace("execute: update RemoteMcastMac entry: {}", mcastMacsRemote);
                 transaction.add(op.update(mcastMacsRemote)
-                        .where(extraMac.getMacColumn().getSchema().opEqual(existingMac))
+                        .where(extraMac.getUuidColumn().getSchema().opEqual(macEntryUUID))
                         .build());
+                transaction.add(op.comment("McastMacRemote: Updating " + macEntryUUID));
+            } else {
+                LOG.warn("Unable to update remoteMcastMacs {} because uuid not found in the operational store",
+                                mac.getMacEntryKey().getValue());
             }
-        }
     }
 
-    private void setLogicalSwitch(InstanceIdentifier<Node> iid, McastMacsRemote mcastMacsRemote, RemoteMcastMacs inputMac) {
+    private void setLogicalSwitch(McastMacsRemote mcastMacsRemote, RemoteMcastMacs inputMac) {
         if (inputMac.getLogicalSwitchRef() != null) {
-            HwvtepNodeName lswitchName = new HwvtepNodeName(inputMac.getLogicalSwitchRef().getValue());
+            @SuppressWarnings("unchecked")
+            InstanceIdentifier<LogicalSwitches> lswitchIid = (InstanceIdentifier<LogicalSwitches>) inputMac.getLogicalSwitchRef().getValue();
             Optional<LogicalSwitches> operationalSwitchOptional =
-                    getOperationalState().getLogicalSwitches(iid, new LogicalSwitchesKey(lswitchName));
+                    getOperationalState().getLogicalSwitches(lswitchIid);
             if (operationalSwitchOptional.isPresent()) {
                 Uuid logicalSwitchUuid = operationalSwitchOptional.get().getLogicalSwitchUuid();
                 UUID logicalSwitchUUID = new UUID(logicalSwitchUuid.getValue());
                 mcastMacsRemote.setLogicalSwitch(logicalSwitchUUID);
             } else {
-                LOG.warn("Create or update remoteMcastMac: NO logical switch named {} found in operational datastore!",
-                        lswitchName);
+                mcastMacsRemote.setLogicalSwitch(TransactUtils.getLogicalSwitchUUID(lswitchIid));
             }
         }
     }
@@ -187,4 +217,25 @@ public class McastMacsRemoteUpdateCommand extends AbstractTransactCommand {
         }
         return result;
     }
+
+    static class McastMacUnMetDependencyGetter extends UnMetDependencyGetter<RemoteMcastMacs> {
+
+        public List<InstanceIdentifier<?>> getLogicalSwitchDependencies(RemoteMcastMacs data) {
+            if (data == null) {
+                return Collections.EMPTY_LIST;
+            }
+            return Lists.newArrayList(data.getLogicalSwitchRef().getValue());
+        }
+
+        public List<InstanceIdentifier<?>> getTerminationPointDependencies(RemoteMcastMacs data) {
+            if (data == null || HwvtepSouthboundUtil.isEmpty(data.getLocatorSet())) {
+                return Collections.EMPTY_LIST;
+            }
+            List<InstanceIdentifier<?>> locators = new ArrayList<>();
+            for (LocatorSet locator: data.getLocatorSet()) {
+                locators.add(locator.getLocatorRef().getValue());
+            }
+            return locators;
+        }
+    }
 }