MD-SAL API integration
[ovsdb.git] / hwvtepsouthbound / hwvtepsouthbound-impl / src / main / java / org / opendaylight / ovsdb / hwvtepsouthbound / transact / McastMacsLocalRemoveCommand.java
index fd5d5030fcdaee2047a6a61fd47df2d89793eaf0..05d6c89d6b0259a69214e651992bf7ffc5cc9c55 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2015 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,
@@ -10,18 +10,15 @@ 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 org.opendaylight.controller.md.sal.binding.api.DataObjectModification;
-import org.opendaylight.controller.md.sal.binding.api.DataTreeModification;
-import org.opendaylight.ovsdb.hwvtepsouthbound.HwvtepSouthboundConstants;
+import java.util.Optional;
+import org.opendaylight.mdsal.binding.api.DataTreeModification;
+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.McastMacsLocal;
 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.LocalMcastMacs;
@@ -30,18 +27,16 @@ import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-import com.google.common.base.Optional;
-
-public class McastMacsLocalRemoveCommand extends AbstractTransactCommand {
-    private static final Logger LOG = LoggerFactory.getLogger(PhysicalPortRemoveCommand.class);
+public class McastMacsLocalRemoveCommand extends AbstractTransactCommand<LocalMcastMacs, HwvtepGlobalAugmentation> {
+    private static final Logger LOG = LoggerFactory.getLogger(McastMacsLocalRemoveCommand.class);
 
-    public McastMacsLocalRemoveCommand(HwvtepOperationalState state,
-            Collection<DataTreeModification<Node>> changes) {
+    public McastMacsLocalRemoveCommand(final HwvtepOperationalState state,
+            final Collection<DataTreeModification<Node>> changes) {
         super(state, changes);
     }
 
     @Override
-    public void execute(TransactionBuilder transaction) {
+    public void execute(final TransactionBuilder transaction) {
         Map<InstanceIdentifier<Node>, List<LocalMcastMacs>> removeds =
                 extractRemoved(getChanges(),LocalMcastMacs.class);
         if (!removeds.isEmpty()) {
@@ -52,26 +47,21 @@ public class McastMacsLocalRemoveCommand extends AbstractTransactCommand {
         }
     }
 
-    private void removeMcastMacLocal(TransactionBuilder transaction,
-            InstanceIdentifier<Node> instanceIdentifier, List<LocalMcastMacs> macList) {
+    private void removeMcastMacLocal(final TransactionBuilder transaction,
+            final InstanceIdentifier<Node> instanceIdentifier, final List<LocalMcastMacs> macList) {
         for (LocalMcastMacs mac: macList) {
             LOG.debug("Removing localMcastMacs, mac address: {}", mac.getMacEntryKey().getValue());
             Optional<LocalMcastMacs> operationalMacOptional =
-                    getOperationalState().getLocalMcastMacs(instanceIdentifier, mac.getKey());
-            McastMacsLocal mcastMacsLocal = TyperUtils.getTypedRowWrapper(transaction.getDatabaseSchema(),
-                    McastMacsLocal.class, null);
-            if (operationalMacOptional.isPresent()) {
+                    getOperationalState().getLocalMcastMacs(instanceIdentifier, mac.key());
+            McastMacsLocal mcastMacsLocal = transaction.getTypedRowSchema(McastMacsLocal.class);
+            if (operationalMacOptional.isPresent() && operationalMacOptional.get().getMacEntryUuid() != null) {
                 //when mac entry is deleted, its referenced locator set and locators are deleted automatically.
                 //TODO: locator in config DS is not deleted
-                String macString = null;
-                if (mac.getMacEntryKey().equals(HwvtepSouthboundConstants.UNKNOWN_DST_MAC)) {
-                    macString = HwvtepSouthboundConstants.UNKNOWN_DST_STRING;
-                } else {
-                    macString = mac.getMacEntryKey().getValue();
-                }
+                UUID macEntryUUID = new UUID(operationalMacOptional.get().getMacEntryUuid().getValue());
+                mcastMacsLocal.getUuidColumn().setData(macEntryUUID);
                 transaction.add(op.delete(mcastMacsLocal.getSchema())
-                        .where(mcastMacsLocal.getMacColumn().getSchema().opEqual(macString)).build());
-                transaction.add(op.comment("Local McastMacLocal: Deleting " + mac.getMacEntryKey().getValue()));
+                        .where(mcastMacsLocal.getUuidColumn().getSchema().opEqual(macEntryUUID)).build());
+                transaction.add(op.comment("McastMacLocal: Deleting " + mac.getMacEntryKey().getValue()));
             } else {
                 LOG.warn("Unable to delete localMcastMacs {} because it was not found in the operational store",
                         mac.getMacEntryKey().getValue());
@@ -79,64 +69,37 @@ public class McastMacsLocalRemoveCommand extends AbstractTransactCommand {
         }
     }
 
-    private Map<InstanceIdentifier<Node>, List<LocalMcastMacs>> extractRemoved(
-            Collection<DataTreeModification<Node>> changes, Class<LocalMcastMacs> class1) {
-        Map<InstanceIdentifier<Node>, List<LocalMcastMacs>> result
-            = new HashMap<InstanceIdentifier<Node>, List<LocalMcastMacs>>();
-        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 localMcastMacs belong to is removed, all localMcastMacs should be removed too.
-                Node removed = TransactUtils.getRemoved(mod);
-                if (removed != null) {
-                    List<LocalMcastMacs> macListRemoved = null;
-                    if (removed.getAugmentation(HwvtepGlobalAugmentation.class) != null) {
-                        macListRemoved = removed.getAugmentation(HwvtepGlobalAugmentation.class).getLocalMcastMacs();
-                    }
-                    if (macListRemoved != null) {
-                        result.put(key, macListRemoved);
-                    }
-                }
-                //If the node which localMcastMacs belong to is updated, and localMcastMacs 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<LocalMcastMacs> macListUpdated = null;
-                    List<LocalMcastMacs> macListBefore = null;
-                    HwvtepGlobalAugmentation hgUpdated = updated.getAugmentation(HwvtepGlobalAugmentation.class);
-                    if (hgUpdated != null) {
-                        macListUpdated = hgUpdated.getLocalMcastMacs();
-                    }
-                    HwvtepGlobalAugmentation hgBefore = before.getAugmentation(HwvtepGlobalAugmentation.class);
-                    if (hgBefore != null) {
-                        macListBefore = hgBefore.getLocalMcastMacs();
-                    }
-                    if (macListBefore != null) {
-                        List<LocalMcastMacs> macListRemoved = new ArrayList<LocalMcastMacs>();
-                        if (macListUpdated != null) {
-                            macListBefore.removeAll(macListUpdated);
-                        }
-                        //then exclude updated localMcastMacs
-                        for (LocalMcastMacs macBefore: macListBefore) {
-                            int i = 0;
-                            for(; i < macListUpdated.size(); i++) {
-                                if (macBefore.getMacEntryKey().equals(macListUpdated.get(i).getMacEntryKey())) {
-                                    break;
-                                }
-                            }
-                            if (i == macListUpdated.size()) {
-                                macListRemoved.add(macBefore);
-                            }
-                        }
-                        if (!macListRemoved.isEmpty()) {
-                            result.put(key, macListRemoved);
-                        }
-                    }
-                }
-            }
+    @Override
+    protected List<LocalMcastMacs> getData(final HwvtepGlobalAugmentation augmentation) {
+        return augmentation.getLocalMcastMacs();
+    }
+
+    @Override
+    protected boolean cascadeDelete() {
+        return true;
+    }
+
+    @Override
+    protected UnMetDependencyGetter getDependencyGetter() {
+        return MAC_DEPENDENCY_GETTER;
+    }
+
+    static UnMetDependencyGetter MAC_DEPENDENCY_GETTER = new MacDependencyGetter();
+
+    public static class MacDependencyGetter extends UnMetDependencyGetter<LocalMcastMacs> {
+        @Override
+        public List<InstanceIdentifier<?>> getLogicalSwitchDependencies(final LocalMcastMacs data) {
+            return Collections.singletonList(data.getLogicalSwitchRef().getValue());
         }
-        return result;
+
+        @Override
+        public List<InstanceIdentifier<?>> getTerminationPointDependencies(final LocalMcastMacs data) {
+            return Collections.emptyList();
+        }
+    }
+
+    @Override
+    protected boolean isDeleteCmd() {
+        return true;
     }
 }