Use IetfYangUtils to canonize addresses
[netvirt.git] / elanmanager / impl / src / main / java / org / opendaylight / netvirt / elan / l2gw / listeners / LocalUcastMacListener.java
index 273c257e92e971a08ea03e71ca96307f53225754..899917ec8dde88b0da41ebd3b8908317bf65440a 100644 (file)
@@ -12,7 +12,6 @@ import java.util.Collection;
 import java.util.Collections;
 import java.util.HashMap;
 import java.util.HashSet;
-import java.util.Locale;
 import java.util.Map;
 import java.util.Set;
 import java.util.function.Predicate;
@@ -28,7 +27,7 @@ import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
 import org.opendaylight.genius.infra.ManagedNewTransactionRunner;
 import org.opendaylight.genius.infra.ManagedNewTransactionRunnerImpl;
 import org.opendaylight.genius.utils.batching.ResourceBatchingManager;
-import org.opendaylight.genius.utils.hwvtep.HwvtepHACache;
+import org.opendaylight.genius.utils.hwvtep.HwvtepNodeHACache;
 import org.opendaylight.genius.utils.hwvtep.HwvtepSouthboundUtils;
 import org.opendaylight.infrautils.jobcoordinator.JobCoordinator;
 import org.opendaylight.infrautils.utils.concurrent.ListenableFutures;
@@ -38,6 +37,7 @@ import org.opendaylight.netvirt.elan.l2gw.ha.listeners.HAOpClusteredListener;
 import org.opendaylight.netvirt.elan.l2gw.utils.ElanL2GatewayUtils;
 import org.opendaylight.netvirt.elanmanager.utils.ElanL2GwCacheUtils;
 import org.opendaylight.netvirt.neutronvpn.api.l2gw.L2GatewayDevice;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.IetfYangUtil;
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.MacAddress;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.instances.ElanInstance;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepGlobalAugmentation;
@@ -46,41 +46,40 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hw
 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.DataObject;
 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 @Singleton
 public class LocalUcastMacListener extends ChildListener<Node, LocalUcastMacs, String>
         implements ClusteredDataTreeChangeListener<Node> {
 
+    private static final Logger LOG = LoggerFactory.getLogger(LocalUcastMacListener.class);
     public static final String NODE_CHECK = "physical";
 
     private static final Predicate<InstanceIdentifier<Node>> IS_PS_NODE_IID =
         (iid) -> iid.firstKeyOf(Node.class).getNodeId().getValue().contains(NODE_CHECK);
 
-    private static final Predicate<InstanceIdentifier<Node>> IS_NOT_HA_CHILD =
-        (iid) -> !HwvtepHACache.getInstance().isHAEnabledDevice(iid)
-                && !iid.firstKeyOf(Node.class).getNodeId().getValue().contains(HwvtepHAUtil.PHYSICALSWITCH);
-
-    private static final Predicate<InstanceIdentifier<Node>> IS_HA_CHILD =
-        (iid) -> HwvtepHACache.getInstance().isHAEnabledDevice(iid);
-
     private final ManagedNewTransactionRunner txRunner;
     private final ElanL2GatewayUtils elanL2GatewayUtils;
     private final HAOpClusteredListener haOpClusteredListener;
     private final JobCoordinator jobCoordinator;
     private final ElanInstanceCache elanInstanceCache;
+    private final HwvtepNodeHACache hwvtepNodeHACache;
 
     @Inject
     public LocalUcastMacListener(final DataBroker dataBroker,
                                  final HAOpClusteredListener haOpClusteredListener,
                                  final ElanL2GatewayUtils elanL2GatewayUtils,
                                  final JobCoordinator jobCoordinator,
-                                 final ElanInstanceCache elanInstanceCache) {
+                                 final ElanInstanceCache elanInstanceCache,
+                                 final HwvtepNodeHACache hwvtepNodeHACache) {
         super(dataBroker, false);
         this.txRunner = new ManagedNewTransactionRunnerImpl(dataBroker);
         this.elanL2GatewayUtils = elanL2GatewayUtils;
         this.haOpClusteredListener = haOpClusteredListener;
         this.jobCoordinator = jobCoordinator;
         this.elanInstanceCache = elanInstanceCache;
+        this.hwvtepNodeHACache = hwvtepNodeHACache;
     }
 
     @Override
@@ -92,7 +91,7 @@ public class LocalUcastMacListener extends ChildListener<Node, LocalUcastMacs, S
 
     @Override
     protected boolean proceed(final InstanceIdentifier<Node> parent) {
-        return IS_NOT_HA_CHILD.test(parent);
+        return isNotHAChild(parent);
     }
 
     protected String getElanName(final LocalUcastMacs mac) {
@@ -100,8 +99,7 @@ public class LocalUcastMacListener extends ChildListener<Node, LocalUcastMacs, S
     }
 
     @Override
-    protected String getGroup(final InstanceIdentifier<LocalUcastMacs> childIid,
-                              final LocalUcastMacs localUcastMacs) {
+    protected String getGroup(final LocalUcastMacs localUcastMacs) {
         return getElanName(localUcastMacs);
     }
 
@@ -114,9 +112,9 @@ public class LocalUcastMacListener extends ChildListener<Node, LocalUcastMacs, S
 
     public void removed(final InstanceIdentifier<LocalUcastMacs> identifier, final LocalUcastMacs macRemoved) {
         String hwvtepNodeId = identifier.firstKeyOf(Node.class).getNodeId().getValue();
-        String macAddress = macRemoved.getMacEntryKey().getValue().toLowerCase(Locale.getDefault());
+        MacAddress macAddress = IetfYangUtil.INSTANCE.canonizeMacAddress(macRemoved.getMacEntryKey());
 
-        LOG.trace("LocalUcastMacs {} removed from {}", macAddress, hwvtepNodeId);
+        LOG.trace("LocalUcastMacs {} removed from {}", macAddress.getValue(), hwvtepNodeId);
 
         ResourceBatchingManager.getInstance().delete(ResourceBatchingManager.ShardResource.CONFIG_TOPOLOGY,
                 identifier);
@@ -136,9 +134,9 @@ public class LocalUcastMacListener extends ChildListener<Node, LocalUcastMacs, S
                 elanL2GwDevice.removeUcastLocalMac(macRemoved);
                 ElanInstance elanInstance = elanInstanceCache.get(elanName).orNull();
                 elanL2GatewayUtils.unInstallL2GwUcastMacFromL2gwDevices(elanName, elanL2GwDevice,
-                        Collections.singletonList(new MacAddress(macAddress.toLowerCase(Locale.getDefault()))));
+                        Collections.singletonList(macAddress));
                 elanL2GatewayUtils.unInstallL2GwUcastMacFromElanDpns(elanInstance, elanL2GwDevice,
-                        Collections.singletonList(new MacAddress(macAddress.toLowerCase(Locale.getDefault()))));
+                        Collections.singletonList(macAddress));
                 return null;
             });
     }
@@ -148,7 +146,7 @@ public class LocalUcastMacListener extends ChildListener<Node, LocalUcastMacs, S
                 identifier, macAdded);
 
         String hwvtepNodeId = identifier.firstKeyOf(Node.class).getNodeId().getValue();
-        String macAddress = macAdded.getMacEntryKey().getValue().toLowerCase(Locale.getDefault());
+        String macAddress = IetfYangUtil.INSTANCE.canonizeMacAddress(macAdded.getMacEntryKey()).getValue();
         String elanName = getElanName(macAdded);
 
         LOG.trace("LocalUcastMacs {} added to {}", macAddress, hwvtepNodeId);
@@ -169,8 +167,7 @@ public class LocalUcastMacListener extends ChildListener<Node, LocalUcastMacs, S
                 }
 
                 elanL2GwDevice.addUcastLocalMac(macAdded);
-                elanL2GatewayUtils.installL2GwUcastMacInElan(elan, elanL2GwDevice,
-                        macAddress.toLowerCase(), macAdded, null);
+                elanL2GatewayUtils.installL2GwUcastMacInElan(elan, elanL2GwDevice, macAddress, macAdded, null);
                 return null;
             });
     }
@@ -193,7 +190,7 @@ public class LocalUcastMacListener extends ChildListener<Node, LocalUcastMacs, S
                     LocalUcastMacs mac = afterMac != null ? afterMac : (LocalUcastMacs)childMod.getDataBefore();
                     InstanceIdentifier<LocalUcastMacs> iid = parentIid
                         .augmentation(HwvtepGlobalAugmentation.class)
-                        .child(LocalUcastMacs.class, mac.getKey());
+                        .child(LocalUcastMacs.class, mac.key());
                     result.put(iid, (DataObjectModification<LocalUcastMacs>) childMod);
                 });
         }
@@ -208,7 +205,7 @@ public class LocalUcastMacListener extends ChildListener<Node, LocalUcastMacs, S
         }
         ListenableFutures.addErrorLogging(txRunner.callWithNewReadWriteTransactionAndSubmit(tx -> {
             haOpClusteredListener.onGlobalNodeAdd(nodeIid, modification.getRootNode().getDataAfter(), tx);
-            if (!IS_HA_CHILD.test(nodeIid)) {
+            if (!isHAChild(nodeIid)) {
                 LOG.trace("On parent add {}", nodeIid);
                 Node operNode = modification.getRootNode().getDataAfter();
                 Set<LocalUcastMacs> configMacs =
@@ -222,12 +219,12 @@ public class LocalUcastMacListener extends ChildListener<Node, LocalUcastMacs, S
 
     InstanceIdentifier<LocalUcastMacs> getMacIid(InstanceIdentifier<Node> nodeIid, LocalUcastMacs mac) {
         return nodeIid.augmentation(HwvtepGlobalAugmentation.class)
-                .child(LocalUcastMacs.class, mac.getKey());
+                .child(LocalUcastMacs.class, mac.key());
     }
 
-    private Set<LocalUcastMacs> getMacs(@Nullable Node node) {
+    private static Set<LocalUcastMacs> getMacs(@Nullable Node node) {
         if (node != null) {
-            HwvtepGlobalAugmentation augmentation = node.getAugmentation(HwvtepGlobalAugmentation.class);
+            HwvtepGlobalAugmentation augmentation = node.augmentation(HwvtepGlobalAugmentation.class);
             if (augmentation != null && augmentation.getLocalUcastMacs() != null) {
                 return new HashSet<>(augmentation.getLocalUcastMacs());
             }
@@ -248,4 +245,13 @@ public class LocalUcastMacListener extends ChildListener<Node, LocalUcastMacs, S
         return HwvtepSouthboundUtils.createHwvtepTopologyInstanceIdentifier()
                 .child(Node.class);
     }
+
+    private boolean isNotHAChild(InstanceIdentifier<Node> nodeId) {
+        return !hwvtepNodeHACache.isHAEnabledDevice(nodeId)
+                && !nodeId.firstKeyOf(Node.class).getNodeId().getValue().contains(HwvtepHAUtil.PHYSICALSWITCH);
+    }
+
+    private boolean isHAChild(InstanceIdentifier<Node> nodeId) {
+        return hwvtepNodeHACache.isHAEnabledDevice(nodeId);
+    }
 }