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;
13 import java.math.BigInteger;
14 import java.util.ArrayList;
15 import java.util.HashSet;
16 import java.util.List;
17 import org.junit.Assert;
18 import org.junit.Before;
19 import org.junit.Test;
20 import org.junit.runner.RunWith;
21 import org.mockito.Mock;
22 import org.mockito.Mockito;
23 import org.mockito.runners.MockitoJUnitRunner;
24 import org.opendaylight.openflowplugin.api.OFConstants;
25 import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
26 import org.opendaylight.openflowplugin.api.openflow.device.DeviceState;
27 import org.opendaylight.openflowplugin.api.openflow.registry.flow.FlowHash;
28 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Prefix;
29 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev100924.MacAddress;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.FlowsStatisticsUpdate;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.FlowsStatisticsUpdateBuilder;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.flow.and.statistics.map.list.FlowAndStatisticsMapList;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.flow.and.statistics.map.list.FlowAndStatisticsMapListBuilder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.FlowCookie;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.MatchBuilder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.ethernet.match.fields.EthernetDestinationBuilder;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.ethernet.match.fields.EthernetSourceBuilder;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.EthernetMatchBuilder;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._3.match.Ipv4MatchBuilder;
41 import org.slf4j.Logger;
42 import org.slf4j.LoggerFactory;
43 @RunWith(MockitoJUnitRunner.class)
44 public class FlowHashFactoryTest {
46 private static final Logger LOG = LoggerFactory.getLogger(FlowHashFactoryTest.class);
49 private static final FlowsStatisticsUpdateBuilder FLOWS_STATISTICS_UPDATE_BUILDER = new FlowsStatisticsUpdateBuilder();
51 private DeviceContext deviceContext;
53 private DeviceState deviceState;
58 List<FlowAndStatisticsMapList> flowAndStatisticsMapListList = new ArrayList<>();
59 for (int i = 1; i < 4; i++) {
60 FlowAndStatisticsMapListBuilder flowAndStatisticsMapListBuilder = new FlowAndStatisticsMapListBuilder();
61 flowAndStatisticsMapListBuilder.setPriority(i);
62 flowAndStatisticsMapListBuilder.setTableId((short) i);
63 flowAndStatisticsMapListBuilder.setCookie(new FlowCookie(BigInteger.TEN));
65 MatchBuilder matchBuilder = new MatchBuilder();
67 EthernetMatchBuilder ethernetMatchBuilder = new EthernetMatchBuilder();
69 EthernetSourceBuilder ethernetSourceBuilder = new EthernetSourceBuilder();
70 MacAddress macAddress = new MacAddress("00:00:00:00:00:0" + i);
71 ethernetSourceBuilder.setAddress(macAddress);
72 ethernetMatchBuilder.setEthernetSource(ethernetSourceBuilder.build());
74 EthernetDestinationBuilder ethernetDestinationBuilder = new EthernetDestinationBuilder();
75 ethernetDestinationBuilder.setAddress(new MacAddress("00:00:00:0" + i + ":00:00"));
76 ethernetMatchBuilder.setEthernetDestination(ethernetDestinationBuilder.build());
78 matchBuilder.setEthernetMatch(ethernetMatchBuilder.build());
80 flowAndStatisticsMapListBuilder.setMatch(matchBuilder.build());
81 flowAndStatisticsMapListList.add(flowAndStatisticsMapListBuilder.build());
83 FLOWS_STATISTICS_UPDATE_BUILDER.setFlowAndStatisticsMapList(flowAndStatisticsMapListList);
84 Mockito.when(deviceContext.getDeviceState()).thenReturn(deviceState);
85 Mockito.when(deviceState.getVersion()).thenReturn(OFConstants.OFP_VERSION_1_3);
89 public void testEquals() throws Exception {
90 FlowsStatisticsUpdate flowStats = FLOWS_STATISTICS_UPDATE_BUILDER.build();
92 HashSet<FlowHash> flowHashs = new HashSet<>();
93 for (FlowAndStatisticsMapList item : flowStats.getFlowAndStatisticsMapList()) {
94 flowHashs.add(FlowHashFactory.create(item, OFConstants.OFP_VERSION_1_3));
95 flowHashs.add(FlowHashFactory.create(item, OFConstants.OFP_VERSION_1_3));
97 assertEquals(3, flowHashs.size());
101 public void testGetHash2() throws Exception {
102 MatchBuilder match1Builder = new MatchBuilder().setLayer3Match(new Ipv4MatchBuilder()
103 .setIpv4Destination(new Ipv4Prefix("10.0.1.157/32")).build());
104 FlowBuilder flow1Builder = new FlowBuilder()
105 .setCookie(new FlowCookie(BigInteger.valueOf(483)))
106 .setMatch(match1Builder.build())
108 .setTableId((short) 0);
110 FlowHash flow1Hash = FlowHashFactory.create(flow1Builder.build(), OFConstants.OFP_VERSION_1_3);
111 LOG.info("flowHash1: {}", flow1Hash.hashCode());
114 MatchBuilder match2Builder = new MatchBuilder().setLayer3Match(new Ipv4MatchBuilder()
115 .setIpv4Destination(new Ipv4Prefix("10.0.0.242/32")).build());
116 FlowBuilder flow2Builder = new FlowBuilder(flow1Builder.build())
117 .setCookie(new FlowCookie(BigInteger.valueOf(148)))
118 .setMatch(match2Builder.build());
120 FlowHash flow2Hash = FlowHashFactory.create(flow2Builder.build(), OFConstants.OFP_VERSION_1_3);
121 LOG.info("flowHash2: {}", flow2Hash.hashCode());
123 Assert.assertNotSame(flow1Hash, flow2Hash);
127 public void testGetHashNPE() throws Exception {
128 MatchBuilder match1Builder = new MatchBuilder().setLayer3Match(new Ipv4MatchBuilder()
129 .setIpv4Destination(new Ipv4Prefix("10.0.1.157/32")).build());
130 FlowBuilder flow1Builder = new FlowBuilder()
131 .setCookie(new FlowCookie(BigInteger.valueOf(483)))
132 .setMatch(match1Builder.build())
134 .setTableId((short) 0);
136 FlowBuilder fb1 = new FlowBuilder(flow1Builder.build());
137 fb1.setTableId(null);
139 FlowHashFactory.create(fb1.build(), OFConstants.OFP_VERSION_1_3);
140 Assert.fail("hash creation should have failed because of NPE");
141 } catch (Exception e) {
143 Assert.assertEquals("flow tableId must not be null", e.getMessage());
146 FlowBuilder fb2 = new FlowBuilder(flow1Builder.build());
147 fb2.setPriority(null);
149 FlowHashFactory.create(fb2.build(), OFConstants.OFP_VERSION_1_3);
150 Assert.fail("hash creation should have failed because of NPE");
151 } catch (Exception e) {
153 Assert.assertEquals("flow priority must not be null", e.getMessage());
156 FlowBuilder fb3 = new FlowBuilder(flow1Builder.build());
158 FlowHash flowHash = FlowHashFactory.create(fb3.build(), OFConstants.OFP_VERSION_1_3);
159 Assert.assertNotNull(flowHash.getCookie());
160 Assert.assertEquals(OFConstants.DEFAULT_COOKIE, flowHash.getCookie());
164 public void testGetHash() throws Exception {
165 FlowsStatisticsUpdate flowStats = FLOWS_STATISTICS_UPDATE_BUILDER.build();
167 for (FlowAndStatisticsMapList item : flowStats.getFlowAndStatisticsMapList()) {
168 FlowHash flowHash = FlowHashFactory.create(item, OFConstants.OFP_VERSION_1_3);
169 FlowHash lastHash = null;
170 if (null != lastHash) {
171 assertNotEquals(lastHash, flowHash);