--- /dev/null
+/*
+ * Copyright (c) 2015 Cisco Systems, Inc. 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.groupbasedpolicy.renderer.ofoverlay.equivalence;
+
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+import java.math.BigInteger;
+import java.util.Arrays;
+import java.util.List;
+
+import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.Flow;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.FlowCookie;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.FlowModFlags;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.Instructions;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.Match;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.Instruction;
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Test;
+
+public class FlowEquivalenceTest {
+
+ private FlowEquivalence equivalence;
+
+ private Flow flowA;
+ private Flow flowB;
+
+ private FlowCookie cookie;
+ private FlowCookie cookieMask;
+ private FlowModFlags flowModFlags;
+ private Instructions instructions;
+ private Instruction instruction;
+ private List<Instruction> instructionList;
+ private Match match;
+
+ private long bufferId = 5L;
+ private String containerName = "containerName";
+ private String flowName = "flowName";
+ private Long outgroup = 5L;
+ private BigInteger outport = BigInteger.TEN;
+ private Integer priority = Integer.valueOf(5);
+ private Short tableId = 5;
+ private Boolean barrier = Boolean.FALSE;
+ private Boolean installHW = Boolean.FALSE;
+ private Boolean strict = Boolean.FALSE;
+
+ @Before
+ public void initialise() {
+ equivalence = new FlowEquivalence();
+
+ flowA = mock(Flow.class);
+ flowB = mock(Flow.class);
+ when(flowA.getBufferId()).thenReturn(bufferId);
+ when(flowA.getContainerName()).thenReturn(containerName);
+ cookie = mock(FlowCookie.class);
+ when(flowA.getCookie()).thenReturn(cookie);
+ cookieMask = mock(FlowCookie.class);
+ when(flowA.getCookieMask()).thenReturn(cookieMask);
+ flowModFlags = mock(FlowModFlags.class);
+ when(flowA.getFlags()).thenReturn(flowModFlags);
+ when(flowA.getFlowName()).thenReturn(flowName);
+
+ instruction = mock(Instruction.class);
+ instructions = mock(Instructions.class);
+ instructionList = Arrays.asList(instruction);
+ when(instructions.getInstruction()).thenReturn(instructionList);
+ when(flowA.getInstructions()).thenReturn(instructions);
+
+ match = mock(Match.class);
+ when(flowA.getMatch()).thenReturn(match);
+ when(flowA.getOutGroup()).thenReturn(outgroup);
+ when(flowA.getOutPort()).thenReturn(outport);
+ when(flowA.getPriority()).thenReturn(priority);
+ when(flowA.getTableId()).thenReturn(tableId);
+ when(flowA.isBarrier()).thenReturn(barrier);
+ when(flowA.isInstallHw()).thenReturn(installHW);
+ when(flowA.isStrict()).thenReturn(strict);
+ }
+
+ @Test
+ public void doEquivalentDoHashTest() {
+ Assert.assertTrue(equivalence.doEquivalent(flowA, flowA));
+ Assert.assertEquals(equivalence.doHash(flowA), equivalence.doHash(flowA));
+
+ when(flowB.getBufferId()).thenReturn(8L);
+ Assert.assertFalse(equivalence.doEquivalent(flowA, flowB));
+ Assert.assertNotEquals(equivalence.doHash(flowA), equivalence.doHash(flowB));
+
+ when(flowB.getBufferId()).thenReturn(bufferId);
+ when(flowB.getContainerName()).thenReturn("otherName");
+ Assert.assertFalse(equivalence.doEquivalent(flowA, flowB));
+ Assert.assertNotEquals(equivalence.doHash(flowA), equivalence.doHash(flowB));
+
+ when(flowB.getContainerName()).thenReturn(containerName);
+ when(flowB.getCookie()).thenReturn(mock(FlowCookie.class));
+ Assert.assertFalse(equivalence.doEquivalent(flowA, flowB));
+ Assert.assertNotEquals(equivalence.doHash(flowA), equivalence.doHash(flowB));
+
+ when(flowB.getCookie()).thenReturn(cookie);
+ when(flowB.getCookieMask()).thenReturn(mock(FlowCookie.class));
+ Assert.assertFalse(equivalence.doEquivalent(flowA, flowB));
+ Assert.assertNotEquals(equivalence.doHash(flowA), equivalence.doHash(flowB));
+
+ when(flowB.getCookieMask()).thenReturn(cookieMask);
+ when(flowB.getFlags()).thenReturn(mock(FlowModFlags.class));
+ Assert.assertFalse(equivalence.doEquivalent(flowA, flowB));
+ Assert.assertNotEquals(equivalence.doHash(flowA), equivalence.doHash(flowB));
+
+ when(flowB.getFlags()).thenReturn(flowModFlags);
+ when(flowB.getFlowName()).thenReturn("otherName");
+ Assert.assertFalse(equivalence.doEquivalent(flowA, flowB));
+ Assert.assertNotEquals(equivalence.doHash(flowA), equivalence.doHash(flowB));
+
+ when(flowB.getFlowName()).thenReturn(flowName);
+ Instruction instructionOther = mock(Instruction.class);
+ Instructions instructionsOther = mock(Instructions.class);
+ List<Instruction> instructionListOther = Arrays.asList(instructionOther);
+ when(instructions.getInstruction()).thenReturn(instructionListOther);
+ when(flowB.getInstructions()).thenReturn(instructionsOther);
+ Assert.assertFalse(equivalence.doEquivalent(flowA, flowB));
+ Assert.assertNotEquals(equivalence.doHash(flowA), equivalence.doHash(flowB));
+
+ when(flowB.getInstructions()).thenReturn(instructions);
+ when(flowB.getMatch()).thenReturn(null);
+ Assert.assertFalse(equivalence.doEquivalent(flowA, flowB));
+ Assert.assertNotEquals(equivalence.doHash(flowA), equivalence.doHash(flowB));
+
+ when(flowB.getMatch()).thenReturn(match);
+ when(flowB.getOutGroup()).thenReturn(8L);
+ Assert.assertFalse(equivalence.doEquivalent(flowA, flowB));
+ Assert.assertNotEquals(equivalence.doHash(flowA), equivalence.doHash(flowB));
+
+ when(flowB.getOutGroup()).thenReturn(outgroup);
+ when(flowB.getOutPort()).thenReturn(BigInteger.ONE);
+ Assert.assertFalse(equivalence.doEquivalent(flowA, flowB));
+ Assert.assertNotEquals(equivalence.doHash(flowA), equivalence.doHash(flowB));
+
+ when(flowB.getOutPort()).thenReturn(outport);
+ when(flowB.getPriority()).thenReturn(8);
+ Assert.assertFalse(equivalence.doEquivalent(flowA, flowB));
+ Assert.assertNotEquals(equivalence.doHash(flowA), equivalence.doHash(flowB));
+
+ when(flowB.getPriority()).thenReturn(priority);
+ when(flowB.getTableId()).thenReturn((short) 8);
+ Assert.assertFalse(equivalence.doEquivalent(flowA, flowB));
+ Assert.assertNotEquals(equivalence.doHash(flowA), equivalence.doHash(flowB));
+
+ when(flowB.getTableId()).thenReturn(tableId);
+ when(flowB.isBarrier()).thenReturn(Boolean.TRUE);
+ Assert.assertFalse(equivalence.doEquivalent(flowA, flowB));
+ Assert.assertNotEquals(equivalence.doHash(flowA), equivalence.doHash(flowB));
+
+ when(flowB.isBarrier()).thenReturn(barrier);
+ when(flowB.isInstallHw()).thenReturn(Boolean.TRUE);
+ Assert.assertFalse(equivalence.doEquivalent(flowA, flowB));
+ Assert.assertNotEquals(equivalence.doHash(flowA), equivalence.doHash(flowB));
+
+ when(flowB.isInstallHw()).thenReturn(installHW);
+ when(flowB.isStrict()).thenReturn(Boolean.TRUE);
+ Assert.assertFalse(equivalence.doEquivalent(flowA, flowB));
+ Assert.assertNotEquals(equivalence.doHash(flowA), equivalence.doHash(flowB));
+
+ when(flowB.isStrict()).thenReturn(strict);
+ Assert.assertTrue(equivalence.doEquivalent(flowA, flowB));
+ Assert.assertEquals(equivalence.doHash(flowA), equivalence.doHash(flowB));
+ }
+}
--- /dev/null
+/*
+ * Copyright (c) 2015 Cisco Systems, Inc. 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.groupbasedpolicy.renderer.ofoverlay.equivalence;
+
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+import java.util.Arrays;
+import java.util.List;
+
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Test;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.Match;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorId;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.EthernetMatch;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.Icmpv4Match;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.Icmpv6Match;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.IpMatch;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.Layer3Match;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.Layer4Match;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.Metadata;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.ProtocolMatchFields;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.TcpFlagMatch;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.Tunnel;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.VlanMatch;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.general.rev140714.GeneralAugMatchNodesNodeTableFlow;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.general.rev140714.general.extension.list.grouping.ExtensionList;
+
+public class MatchEquivalenceTest {
+
+ private MatchEquivalence equivalence;
+
+ private Match matchA;
+ private Match matchB;
+
+ private EthernetMatch ethernetMatch;
+ private Icmpv4Match icmpv4Match;
+ private Icmpv6Match icmpv6Match;
+ private NodeConnectorId inPhyPort;
+ private NodeConnectorId inPort;
+ private IpMatch ipMatch;
+ private Layer3Match layer3Match;
+ private Layer4Match layer4Match;
+ private Metadata metadata;
+ private ProtocolMatchFields protocolMatchFields;
+ private TcpFlagMatch tcpFlagMatch;
+ private Tunnel tunnel;
+ private VlanMatch vlanMatch;
+
+ private GeneralAugMatchNodesNodeTableFlow generalAugMatchA;
+ private GeneralAugMatchNodesNodeTableFlow generalAugMatchB;
+ private ExtensionList extensionListA;
+ private ExtensionList extensionListB;
+ private List<ExtensionList> setA;
+ private List<ExtensionList> setB;
+
+ @Before
+ public void initialise() {
+ equivalence = new MatchEquivalence();
+
+ matchA = mock(Match.class);
+ matchB = mock(Match.class);
+
+ ethernetMatch = mock(EthernetMatch.class);
+ when(matchA.getEthernetMatch()).thenReturn(ethernetMatch);
+ icmpv4Match = mock(Icmpv4Match.class);
+ when(matchA.getIcmpv4Match()).thenReturn(icmpv4Match);
+ icmpv6Match = mock(Icmpv6Match.class);
+ when(matchA.getIcmpv6Match()).thenReturn(icmpv6Match);
+ inPhyPort = mock(NodeConnectorId.class);
+ when(matchA.getInPhyPort()).thenReturn(inPhyPort);
+ inPort = mock(NodeConnectorId.class);
+ when(matchA.getInPort()).thenReturn(inPort);
+ ipMatch = mock(IpMatch.class);
+ when(matchA.getIpMatch()).thenReturn(ipMatch);
+ layer3Match = mock(Layer3Match.class);
+ when(matchA.getLayer3Match()).thenReturn(layer3Match);
+ layer4Match = mock(Layer4Match.class);
+ when(matchA.getLayer4Match()).thenReturn(layer4Match);
+ metadata = mock(Metadata.class);
+ when(matchA.getMetadata()).thenReturn(metadata);
+ protocolMatchFields = mock(ProtocolMatchFields.class);
+ when(matchA.getProtocolMatchFields()).thenReturn(protocolMatchFields);
+ tcpFlagMatch = mock(TcpFlagMatch.class);
+ when(matchA.getTcpFlagMatch()).thenReturn(tcpFlagMatch);
+ tunnel = mock(Tunnel.class);
+ when(matchA.getTunnel()).thenReturn(tunnel);
+ vlanMatch = mock(VlanMatch.class);
+ when(matchA.getVlanMatch()).thenReturn(vlanMatch);
+
+ generalAugMatchA = mock(GeneralAugMatchNodesNodeTableFlow.class);
+ when(matchA.getAugmentation(GeneralAugMatchNodesNodeTableFlow.class)).thenReturn(generalAugMatchA);
+ generalAugMatchB = mock(GeneralAugMatchNodesNodeTableFlow.class);
+ extensionListA = mock(ExtensionList.class);
+ extensionListB = mock(ExtensionList.class);
+ setA = Arrays.asList(extensionListA);
+ setB = Arrays.asList(extensionListB);
+ when(generalAugMatchA.getExtensionList()).thenReturn(setA);
+ }
+
+ @Test
+ public void doEquivalentdoHashTest() {
+ Assert.assertTrue(equivalence.doEquivalent(matchA, matchA));
+ Assert.assertEquals(equivalence.doHash(matchA), equivalence.doHash(matchA));
+
+ when(matchB.getEthernetMatch()).thenReturn(mock(EthernetMatch.class));
+ Assert.assertFalse(equivalence.doEquivalent(matchA, matchB));
+ Assert.assertNotEquals(equivalence.doHash(matchA), equivalence.doHash(matchB));
+
+ when(matchB.getEthernetMatch()).thenReturn(ethernetMatch);
+ when(matchB.getIcmpv4Match()).thenReturn(mock(Icmpv4Match.class));
+ Assert.assertFalse(equivalence.doEquivalent(matchA, matchB));
+ Assert.assertNotEquals(equivalence.doHash(matchA), equivalence.doHash(matchB));
+
+ when(matchB.getIcmpv4Match()).thenReturn(icmpv4Match);
+ when(matchB.getIcmpv6Match()).thenReturn(mock(Icmpv6Match.class));
+ Assert.assertFalse(equivalence.doEquivalent(matchA, matchB));
+ Assert.assertNotEquals(equivalence.doHash(matchA), equivalence.doHash(matchB));
+
+ when(matchB.getIcmpv6Match()).thenReturn(icmpv6Match);
+ when(matchB.getInPhyPort()).thenReturn(mock(NodeConnectorId.class));
+ Assert.assertFalse(equivalence.doEquivalent(matchA, matchB));
+ Assert.assertNotEquals(equivalence.doHash(matchA), equivalence.doHash(matchB));
+
+ when(matchB.getInPhyPort()).thenReturn(inPhyPort);
+ when(matchB.getInPort()).thenReturn(mock(NodeConnectorId.class));
+ Assert.assertFalse(equivalence.doEquivalent(matchA, matchB));
+ Assert.assertNotEquals(equivalence.doHash(matchA), equivalence.doHash(matchB));
+
+ when(matchB.getInPort()).thenReturn(inPort);
+ when(matchB.getIpMatch()).thenReturn(mock(IpMatch.class));
+ Assert.assertFalse(equivalence.doEquivalent(matchA, matchB));
+ Assert.assertNotEquals(equivalence.doHash(matchA), equivalence.doHash(matchB));
+
+ when(matchB.getIpMatch()).thenReturn(ipMatch);
+ when(matchB.getLayer3Match()).thenReturn(mock(Layer3Match.class));
+ Assert.assertFalse(equivalence.doEquivalent(matchA, matchB));
+ Assert.assertNotEquals(equivalence.doHash(matchA), equivalence.doHash(matchB));
+
+ when(matchB.getLayer3Match()).thenReturn(layer3Match);
+ when(matchB.getLayer4Match()).thenReturn(mock(Layer4Match.class));
+ Assert.assertFalse(equivalence.doEquivalent(matchA, matchB));
+ Assert.assertNotEquals(equivalence.doHash(matchA), equivalence.doHash(matchB));
+
+ when(matchB.getLayer4Match()).thenReturn(layer4Match);
+ when(matchB.getMetadata()).thenReturn(mock(Metadata.class));
+ Assert.assertFalse(equivalence.doEquivalent(matchA, matchB));
+ Assert.assertNotEquals(equivalence.doHash(matchA), equivalence.doHash(matchB));
+
+ when(matchB.getMetadata()).thenReturn(metadata);
+ when(matchB.getProtocolMatchFields()).thenReturn(mock(ProtocolMatchFields.class));
+ Assert.assertFalse(equivalence.doEquivalent(matchA, matchB));
+ Assert.assertNotEquals(equivalence.doHash(matchA), equivalence.doHash(matchB));
+
+ when(matchB.getProtocolMatchFields()).thenReturn(protocolMatchFields);
+ when(matchB.getTcpFlagMatch()).thenReturn(mock(TcpFlagMatch.class));
+ Assert.assertFalse(equivalence.doEquivalent(matchA, matchB));
+ Assert.assertNotEquals(equivalence.doHash(matchA), equivalence.doHash(matchB));
+
+ when(matchB.getTcpFlagMatch()).thenReturn(tcpFlagMatch);
+ when(matchB.getTunnel()).thenReturn(mock(Tunnel.class));
+ Assert.assertFalse(equivalence.doEquivalent(matchA, matchB));
+ Assert.assertNotEquals(equivalence.doHash(matchA), equivalence.doHash(matchB));
+
+ when(matchB.getTunnel()).thenReturn(tunnel);
+ when(matchB.getVlanMatch()).thenReturn(mock(VlanMatch.class));
+ Assert.assertFalse(equivalence.doEquivalent(matchA, matchB));
+ Assert.assertNotEquals(equivalence.doHash(matchA), equivalence.doHash(matchB));
+
+ when(matchB.getVlanMatch()).thenReturn(vlanMatch);
+ Assert.assertFalse(equivalence.doEquivalent(matchA, matchB));
+ Assert.assertNotEquals(equivalence.doHash(matchA), equivalence.doHash(matchB));
+
+ when(generalAugMatchB.getExtensionList()).thenReturn(setB);
+ when(matchB.getAugmentation(GeneralAugMatchNodesNodeTableFlow.class)).thenReturn(generalAugMatchB);
+ Assert.assertFalse(equivalence.doEquivalent(matchA, matchB));
+ Assert.assertNotEquals(equivalence.doHash(matchA), equivalence.doHash(matchB));
+
+ when(matchA.getAugmentation(GeneralAugMatchNodesNodeTableFlow.class)).thenReturn(null);
+ Assert.assertFalse(equivalence.doEquivalent(matchA, matchB));
+ Assert.assertNotEquals(equivalence.doHash(matchA), equivalence.doHash(matchB));
+
+ when(matchB.getAugmentation(GeneralAugMatchNodesNodeTableFlow.class)).thenReturn(null);
+ Assert.assertTrue(equivalence.doEquivalent(matchA, matchB));
+ Assert.assertEquals(equivalence.doHash(matchA), equivalence.doHash(matchB));
+
+ when(matchA.getAugmentation(GeneralAugMatchNodesNodeTableFlow.class)).thenReturn(generalAugMatchA);
+ when(matchB.getAugmentation(GeneralAugMatchNodesNodeTableFlow.class)).thenReturn(generalAugMatchB);
+ when(generalAugMatchB.getExtensionList()).thenReturn(setA);
+ Assert.assertTrue(equivalence.doEquivalent(matchA, matchB));
+ Assert.assertEquals(equivalence.doHash(matchA), equivalence.doHash(matchB));
+ }
+}