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 org.junit.Assert;
19 import org.junit.Before;
20 import org.junit.Test;
21 import org.junit.runner.RunWith;
22 import org.mockito.Mock;
23 import org.mockito.Mockito;
24 import org.mockito.runners.MockitoJUnitRunner;
25 import org.opendaylight.openflowplugin.api.OFConstants;
26 import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
27 import org.opendaylight.openflowplugin.api.openflow.device.DeviceState;
28 import org.opendaylight.openflowplugin.api.openflow.registry.flow.FlowRegistryKey;
29 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Prefix;
30 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev100924.MacAddress;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowBuilder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.FlowsStatisticsUpdate;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.FlowsStatisticsUpdateBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.flow.and.statistics.map.list.FlowAndStatisticsMapList;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.flow.and.statistics.map.list.FlowAndStatisticsMapListBuilder;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.FlowCookie;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.MatchBuilder;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.ethernet.match.fields.EthernetDestinationBuilder;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.ethernet.match.fields.EthernetSourceBuilder;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.EthernetMatchBuilder;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._3.match.Ipv4MatchBuilder;
42 import org.slf4j.Logger;
43 import org.slf4j.LoggerFactory;
45 @RunWith(MockitoJUnitRunner.class)
46 public class FlowRegistryKeyFactoryTest {
48 private static final Logger LOG = LoggerFactory.getLogger(FlowRegistryKeyFactoryTest.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<FlowRegistryKey> flowRegistryKeys = new HashSet<>();
95 for (FlowAndStatisticsMapList item : flowStats.getFlowAndStatisticsMapList()) {
96 flowRegistryKeys.add(FlowRegistryKeyFactory.create(item));
97 flowRegistryKeys.add(FlowRegistryKeyFactory.create(item));
99 assertEquals(3, flowRegistryKeys.size());
103 public void testGetHash2() throws Exception {
104 MatchBuilder match1Builder = new MatchBuilder().setLayer3Match(new Ipv4MatchBuilder()
105 .setIpv4Destination(new Ipv4Prefix("10.0.1.157/32")).build());
106 FlowBuilder flow1Builder = new FlowBuilder()
107 .setCookie(new FlowCookie(BigInteger.valueOf(483)))
108 .setMatch(match1Builder.build())
110 .setTableId((short) 0);
112 FlowRegistryKey flow1Hash = FlowRegistryKeyFactory.create(flow1Builder.build());
113 LOG.info("flowHash1: {}", flow1Hash.hashCode());
116 MatchBuilder match2Builder = new MatchBuilder().setLayer3Match(new Ipv4MatchBuilder()
117 .setIpv4Destination(new Ipv4Prefix("10.0.0.242/32")).build());
118 FlowBuilder flow2Builder = new FlowBuilder(flow1Builder.build())
119 .setCookie(new FlowCookie(BigInteger.valueOf(148)))
120 .setMatch(match2Builder.build());
122 FlowRegistryKey flow2Hash = FlowRegistryKeyFactory.create(flow2Builder.build());
123 LOG.info("flowHash2: {}", flow2Hash.hashCode());
125 Assert.assertNotSame(flow1Hash, flow2Hash);
129 public void testGetHashNPE() throws Exception {
130 MatchBuilder match1Builder = new MatchBuilder().setLayer3Match(new Ipv4MatchBuilder()
131 .setIpv4Destination(new Ipv4Prefix("10.0.1.157/32")).build());
132 FlowBuilder flow1Builder = new FlowBuilder()
133 .setCookie(new FlowCookie(BigInteger.valueOf(483)))
134 .setMatch(match1Builder.build())
136 .setTableId((short) 0);
138 FlowBuilder fb1 = new FlowBuilder(flow1Builder.build());
139 fb1.setTableId(null);
141 FlowRegistryKeyFactory.create(fb1.build());
142 Assert.fail("hash creation should have failed because of NPE");
143 } catch (Exception e) {
145 Assert.assertEquals("flow tableId must not be null", e.getMessage());
148 FlowBuilder fb2 = new FlowBuilder(flow1Builder.build());
149 fb2.setPriority(null);
151 FlowRegistryKeyFactory.create(fb2.build());
152 } catch (Exception e) {
154 Assert.fail("no exception was expected while hash was creating.");
157 FlowBuilder fb3 = new FlowBuilder(flow1Builder.build());
159 FlowRegistryKey flowRegistryKey = FlowRegistryKeyFactory.create(fb3.build());
160 Assert.assertNotNull(flowRegistryKey.getCookie());
161 Assert.assertEquals(OFConstants.DEFAULT_COOKIE, flowRegistryKey.getCookie());
165 public void testGetHash() throws Exception {
166 FlowsStatisticsUpdate flowStats = FLOWS_STATISTICS_UPDATE_BUILDER.build();
168 for (FlowAndStatisticsMapList item : flowStats.getFlowAndStatisticsMapList()) {
169 FlowRegistryKey flowRegistryKey = FlowRegistryKeyFactory.create(item);
170 FlowRegistryKey lastHash = null;
171 if (null != lastHash) {
172 assertNotEquals(lastHash, flowRegistryKey);
174 lastHash = flowRegistryKey;