2 * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
9 package org.opendaylight.openflowplugin.impl.registry.flow;
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertNotEquals;
14 import java.math.BigInteger;
15 import java.util.ArrayList;
16 import java.util.HashSet;
17 import java.util.List;
18 import junit.framework.Assert;
19 import org.junit.Before;
20 import org.junit.Ignore;
21 import org.junit.Test;
22 import org.junit.runner.RunWith;
23 import org.mockito.Mock;
24 import org.mockito.Mockito;
25 import org.mockito.runners.MockitoJUnitRunner;
26 import org.opendaylight.openflowplugin.api.OFConstants;
27 import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
28 import org.opendaylight.openflowplugin.api.openflow.device.DeviceState;
29 import org.opendaylight.openflowplugin.api.openflow.registry.flow.FlowHash;
30 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Prefix;
31 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev100924.MacAddress;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowBuilder;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.FlowsStatisticsUpdate;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.FlowsStatisticsUpdateBuilder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.flow.and.statistics.map.list.FlowAndStatisticsMapList;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.flow.and.statistics.map.list.FlowAndStatisticsMapListBuilder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.FlowCookie;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.MatchBuilder;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.ethernet.match.fields.EthernetDestinationBuilder;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.ethernet.match.fields.EthernetSourceBuilder;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.EthernetMatchBuilder;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._3.match.Ipv4MatchBuilder;
43 import org.slf4j.Logger;
44 import org.slf4j.LoggerFactory;
45 @RunWith(MockitoJUnitRunner.class)
46 public class FlowHashFactoryTest {
48 private static final Logger LOG = LoggerFactory.getLogger(FlowHashFactoryTest.class);
51 private static final FlowsStatisticsUpdateBuilder FLOWS_STATISTICS_UPDATE_BUILDER = new FlowsStatisticsUpdateBuilder();
53 private DeviceContext deviceContext;
55 private DeviceState deviceState;
60 List<FlowAndStatisticsMapList> flowAndStatisticsMapListList = new ArrayList();
61 for (int i = 1; i < 4; i++) {
62 FlowAndStatisticsMapListBuilder flowAndStatisticsMapListBuilder = new FlowAndStatisticsMapListBuilder();
63 flowAndStatisticsMapListBuilder.setPriority(i);
64 flowAndStatisticsMapListBuilder.setTableId((short) i);
65 flowAndStatisticsMapListBuilder.setCookie(new FlowCookie(BigInteger.TEN));
67 MatchBuilder matchBuilder = new MatchBuilder();
69 EthernetMatchBuilder ethernetMatchBuilder = new EthernetMatchBuilder();
71 EthernetSourceBuilder ethernetSourceBuilder = new EthernetSourceBuilder();
72 MacAddress macAddress = new MacAddress("00:00:00:00:00:0" + i);
73 ethernetSourceBuilder.setAddress(macAddress);
74 ethernetMatchBuilder.setEthernetSource(ethernetSourceBuilder.build());
76 EthernetDestinationBuilder ethernetDestinationBuilder = new EthernetDestinationBuilder();
77 ethernetDestinationBuilder.setAddress(new MacAddress("00:00:00:0" + i + ":00:00"));
78 ethernetMatchBuilder.setEthernetDestination(ethernetDestinationBuilder.build());
80 matchBuilder.setEthernetMatch(ethernetMatchBuilder.build());
82 flowAndStatisticsMapListBuilder.setMatch(matchBuilder.build());
83 flowAndStatisticsMapListList.add(flowAndStatisticsMapListBuilder.build());
85 FLOWS_STATISTICS_UPDATE_BUILDER.setFlowAndStatisticsMapList(flowAndStatisticsMapListList);
86 Mockito.when(deviceContext.getDeviceState()).thenReturn(deviceState);
87 Mockito.when(deviceState.getVersion()).thenReturn(OFConstants.OFP_VERSION_1_3);
91 public void testEquals() throws Exception {
92 FlowsStatisticsUpdate flowStats = FLOWS_STATISTICS_UPDATE_BUILDER.build();
94 HashSet<FlowHash> flowHashs = new HashSet();
95 for (FlowAndStatisticsMapList item : flowStats.getFlowAndStatisticsMapList()) {
96 flowHashs.add(FlowHashFactory.create(item, deviceContext));
97 flowHashs.add(FlowHashFactory.create(item, deviceContext));
99 assertEquals(3, flowHashs.size());
104 public void testGetHash2() throws Exception {
105 MatchBuilder match1Builder = new MatchBuilder().setLayer3Match(new Ipv4MatchBuilder()
106 .setIpv4Destination(new Ipv4Prefix("10.0.1.157/32")).build());
107 FlowBuilder flow1Builder = new FlowBuilder()
108 .setCookie(new FlowCookie(BigInteger.valueOf(483)))
109 .setMatch(match1Builder.build())
111 .setTableId((short) 0);
113 FlowHash flow1Hash = FlowHashFactory.create(flow1Builder.build(), deviceContext);
114 LOG.info("flowHash1: {}", flow1Hash.hashCode());
117 MatchBuilder match2Builder = new MatchBuilder().setLayer3Match(new Ipv4MatchBuilder()
118 .setIpv4Destination(new Ipv4Prefix("10.0.0.242/32")).build());
119 FlowBuilder flow2Builder = new FlowBuilder(flow1Builder.build())
120 .setCookie(new FlowCookie(BigInteger.valueOf(148)))
121 .setMatch(match2Builder.build());
123 FlowHash flow2Hash = FlowHashFactory.create(flow2Builder.build(), deviceContext);
124 LOG.info("flowHash2: {}", flow2Hash.hashCode());
126 Assert.assertNotSame(flow1Hash, flow2Hash);
130 public void testGetHash() throws Exception {
131 FlowsStatisticsUpdate flowStats = FLOWS_STATISTICS_UPDATE_BUILDER.build();
133 for (FlowAndStatisticsMapList item : flowStats.getFlowAndStatisticsMapList()) {
134 FlowHash flowHash = FlowHashFactory.create(item, deviceContext);
135 FlowHash lastHash = null;
136 if (null != lastHash) {
137 assertNotEquals(lastHash, flowHash);