FlowHash renamed to FlowRegistryKey
[openflowplugin.git] / openflowplugin-impl / src / test / java / org / opendaylight / openflowplugin / impl / registry / flow / FlowRegistryKeyFactoryTest.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.openflowplugin.impl.registry.flow;
10
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.FlowRegistryKey;
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 FlowRegistryKeyFactoryTest {
45
46     private static final Logger LOG = LoggerFactory.getLogger(FlowRegistryKeyFactoryTest.class);
47
48
49     private static final FlowsStatisticsUpdateBuilder FLOWS_STATISTICS_UPDATE_BUILDER = new FlowsStatisticsUpdateBuilder();
50     @Mock
51     private DeviceContext deviceContext;
52     @Mock
53     private DeviceState deviceState;
54
55
56     @Before
57     public void setup() {
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));
64
65             MatchBuilder matchBuilder = new MatchBuilder();
66
67             EthernetMatchBuilder ethernetMatchBuilder = new EthernetMatchBuilder();
68
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());
73
74             EthernetDestinationBuilder ethernetDestinationBuilder = new EthernetDestinationBuilder();
75             ethernetDestinationBuilder.setAddress(new MacAddress("00:00:00:0" + i + ":00:00"));
76             ethernetMatchBuilder.setEthernetDestination(ethernetDestinationBuilder.build());
77
78             matchBuilder.setEthernetMatch(ethernetMatchBuilder.build());
79
80             flowAndStatisticsMapListBuilder.setMatch(matchBuilder.build());
81             flowAndStatisticsMapListList.add(flowAndStatisticsMapListBuilder.build());
82         }
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);
86     }
87
88     @Test
89     public void testEquals() throws Exception {
90         FlowsStatisticsUpdate flowStats = FLOWS_STATISTICS_UPDATE_BUILDER.build();
91
92         HashSet<FlowRegistryKey> flowRegistryKeys = new HashSet<>();
93         for (FlowAndStatisticsMapList item : flowStats.getFlowAndStatisticsMapList()) {
94             flowRegistryKeys.add(FlowHashFactory.create(item, OFConstants.OFP_VERSION_1_3));
95             flowRegistryKeys.add(FlowHashFactory.create(item, OFConstants.OFP_VERSION_1_3));
96         }
97         assertEquals(3, flowRegistryKeys.size());
98     }
99
100     @Test
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())
107                 .setPriority(2)
108                 .setTableId((short) 0);
109
110         FlowRegistryKey flow1Hash = FlowHashFactory.create(flow1Builder.build(), OFConstants.OFP_VERSION_1_3);
111         LOG.info("flowHash1: {}", flow1Hash.hashCode());
112
113
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());
119
120         FlowRegistryKey flow2Hash = FlowHashFactory.create(flow2Builder.build(), OFConstants.OFP_VERSION_1_3);
121         LOG.info("flowHash2: {}", flow2Hash.hashCode());
122
123         Assert.assertNotSame(flow1Hash, flow2Hash);
124     }
125
126     @Test
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())
133                 .setPriority(2)
134                 .setTableId((short) 0);
135
136         FlowBuilder fb1 = new FlowBuilder(flow1Builder.build());
137         fb1.setTableId(null);
138         try {
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) {
142             // expected
143             Assert.assertEquals("flow tableId must not be null", e.getMessage());
144         }
145
146         FlowBuilder fb2 = new FlowBuilder(flow1Builder.build());
147         fb2.setPriority(null);
148         try {
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) {
152             // expected
153             Assert.assertEquals("flow priority must not be null", e.getMessage());
154         }
155
156         FlowBuilder fb3 = new FlowBuilder(flow1Builder.build());
157         fb3.setCookie(null);
158         FlowRegistryKey flowRegistryKey = FlowHashFactory.create(fb3.build(), OFConstants.OFP_VERSION_1_3);
159         Assert.assertNotNull(flowRegistryKey.getCookie());
160         Assert.assertEquals(OFConstants.DEFAULT_COOKIE, flowRegistryKey.getCookie());
161     }
162
163     @Test
164     public void testGetHash() throws Exception {
165         FlowsStatisticsUpdate flowStats = FLOWS_STATISTICS_UPDATE_BUILDER.build();
166
167         for (FlowAndStatisticsMapList item : flowStats.getFlowAndStatisticsMapList()) {
168             FlowRegistryKey flowRegistryKey = FlowHashFactory.create(item, OFConstants.OFP_VERSION_1_3);
169             FlowRegistryKey lastHash = null;
170             if (null != lastHash) {
171                 assertNotEquals(lastHash, flowRegistryKey);
172             } else {
173                 lastHash = flowRegistryKey;
174             }
175         }
176     }
177 }