0a00ece93bd293de46e11e9a70b79c1e04eda7d5
[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
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.junit.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.DeviceInfo;
28 import org.opendaylight.openflowplugin.api.openflow.device.DeviceState;
29 import org.opendaylight.openflowplugin.api.openflow.registry.flow.FlowRegistryKey;
30 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Prefix;
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.match.layer._3.match.Ipv4MatchBuilder;
39 import org.opendaylight.yangtools.yang.common.Uint16;
40 import org.opendaylight.yangtools.yang.common.Uint8;
41 import org.slf4j.Logger;
42 import org.slf4j.LoggerFactory;
43
44 @RunWith(MockitoJUnitRunner.class)
45 public class FlowRegistryKeyFactoryTest {
46
47     private static final Logger LOG = LoggerFactory.getLogger(FlowRegistryKeyFactoryTest.class);
48
49
50     private static final FlowsStatisticsUpdateBuilder FLOWS_STATISTICS_UPDATE_BUILDER =
51             new FlowsStatisticsUpdateBuilder();
52     @Mock
53     private DeviceContext deviceContext;
54     @Mock
55     private DeviceState deviceState;
56     @Mock
57     private DeviceInfo deviceInfo;
58
59
60     @Before
61     public void setup() {
62         List<FlowAndStatisticsMapList> flowAndStatisticsMapListList = new ArrayList<>();
63         for (int i = 1; i < 4; i++) {
64             flowAndStatisticsMapListList.add(TestFlowHelper.createFlowAndStatisticsMapListBuilder(i).build());
65         }
66         FLOWS_STATISTICS_UPDATE_BUILDER.setFlowAndStatisticsMapList(flowAndStatisticsMapListList);
67         Mockito.when(deviceInfo.getVersion()).thenReturn(OFConstants.OFP_VERSION_1_3);
68     }
69
70     @Test
71     public void testEquals() {
72         FlowsStatisticsUpdate flowStats = FLOWS_STATISTICS_UPDATE_BUILDER.build();
73
74         HashSet<FlowRegistryKey> flowRegistryKeys = new HashSet<>();
75         for (FlowAndStatisticsMapList item : flowStats.getFlowAndStatisticsMapList()) {
76             final FlowRegistryKey key1 = FlowRegistryKeyFactory.create(deviceInfo.getVersion(), item);
77             final FlowRegistryKey key2 = FlowRegistryKeyFactory.create(deviceInfo.getVersion(), item);
78             flowRegistryKeys.add(key1);
79             flowRegistryKeys.add(key1);
80             flowRegistryKeys.add(key2);
81         }
82         assertEquals(3, flowRegistryKeys.size());
83     }
84
85     @Test
86     public void testEqualsNegative() {
87         final FlowAndStatisticsMapList flowStatisticsMapList1 =
88                 TestFlowHelper.createFlowAndStatisticsMapListBuilder(1).build();
89         final FlowRegistryKey key1 = FlowRegistryKeyFactory.create(deviceInfo.getVersion(), flowStatisticsMapList1);
90
91         FlowRegistryKey key2;
92         FlowAndStatisticsMapListBuilder flowStatisticsMapListBld2;
93
94         // different priority
95         flowStatisticsMapListBld2 = new FlowAndStatisticsMapListBuilder(flowStatisticsMapList1);
96         flowStatisticsMapListBld2.setPriority(flowStatisticsMapListBld2.getPriority().toJava() + 1);
97         key2 = FlowRegistryKeyFactory.create(deviceInfo.getVersion(), flowStatisticsMapListBld2.build());
98         Assert.assertFalse(key1.equals(key2));
99
100         // different match
101         flowStatisticsMapListBld2 = new FlowAndStatisticsMapListBuilder(flowStatisticsMapList1);
102         flowStatisticsMapListBld2.setMatch(new MatchBuilder().build());
103         key2 = FlowRegistryKeyFactory.create(deviceInfo.getVersion(), flowStatisticsMapListBld2.build());
104         Assert.assertFalse(key1.equals(key2));
105
106         // different tableId
107         flowStatisticsMapListBld2 = new FlowAndStatisticsMapListBuilder(flowStatisticsMapList1);
108         flowStatisticsMapListBld2.setTableId((short) (flowStatisticsMapListBld2.getTableId().toJava() + 1));
109         key2 = FlowRegistryKeyFactory.create(deviceInfo.getVersion(), flowStatisticsMapListBld2.build());
110         Assert.assertFalse(key1.equals(key2));
111
112         Assert.assertFalse(key1.equals(null));
113     }
114
115     @Test
116     public void testGetHash2() {
117         MatchBuilder match1Builder = new MatchBuilder().setLayer3Match(new Ipv4MatchBuilder()
118                 .setIpv4Destination(new Ipv4Prefix("10.0.1.157/32")).build());
119         FlowBuilder flow1Builder = new FlowBuilder()
120                 .setCookie(new FlowCookie(BigInteger.valueOf(483)))
121                 .setMatch(match1Builder.build())
122                 .setPriority(2)
123                 .setTableId((short) 0);
124
125         FlowRegistryKey flow1Hash = FlowRegistryKeyFactory.create(deviceInfo.getVersion(), flow1Builder.build());
126         LOG.info("flowHash1: {}", flow1Hash.hashCode());
127
128
129         MatchBuilder match2Builder = new MatchBuilder().setLayer3Match(new Ipv4MatchBuilder()
130                 .setIpv4Destination(new Ipv4Prefix("10.0.0.242/32")).build());
131         FlowBuilder flow2Builder = new FlowBuilder(flow1Builder.build())
132                 .setCookie(new FlowCookie(BigInteger.valueOf(148)))
133                 .setMatch(match2Builder.build());
134
135         FlowRegistryKey flow2Hash = FlowRegistryKeyFactory.create(deviceInfo.getVersion(), flow2Builder.build());
136         LOG.info("flowHash2: {}", flow2Hash.hashCode());
137
138         Assert.assertNotSame(flow1Hash, flow2Hash);
139     }
140
141     @Test
142     public void testGetHashNPE() {
143         MatchBuilder match1Builder = new MatchBuilder().setLayer3Match(new Ipv4MatchBuilder()
144                 .setIpv4Destination(new Ipv4Prefix("10.0.1.157/32")).build());
145         FlowBuilder flow1Builder = new FlowBuilder()
146                 .setCookie(new FlowCookie(BigInteger.valueOf(483)))
147                 .setMatch(match1Builder.build())
148                 .setPriority(2)
149                 .setTableId((short) 0);
150
151         FlowBuilder fb1 = new FlowBuilder(flow1Builder.build());
152         fb1.setTableId((Uint8) null);
153         try {
154             FlowRegistryKeyFactory.create(deviceInfo.getVersion(), fb1.build());
155             Assert.fail("hash creation should have failed because of NPE");
156         } catch (NullPointerException e) {
157             // expected
158             Assert.assertEquals("flow tableId must not be null", e.getMessage());
159         }
160
161         FlowBuilder fb2 = new FlowBuilder(flow1Builder.build());
162         fb2.setPriority((Uint16) null);
163         try {
164             FlowRegistryKeyFactory.create(deviceInfo.getVersion(), fb2.build());
165         } catch (NullPointerException e) {
166             // not expected
167             Assert.fail("no exception was expected while hash was creating.");
168         }
169
170         FlowBuilder fb3 = new FlowBuilder(flow1Builder.build());
171         fb3.setCookie(null);
172         FlowRegistryKey flowRegistryKey = FlowRegistryKeyFactory.create(deviceInfo.getVersion(), fb3.build());
173         Assert.assertNotNull(flowRegistryKey.getCookie());
174         Assert.assertEquals(OFConstants.DEFAULT_COOKIE, flowRegistryKey.getCookie());
175     }
176
177     @Test
178     public void testGetHash() {
179         FlowsStatisticsUpdate flowStats = FLOWS_STATISTICS_UPDATE_BUILDER.build();
180
181         for (FlowAndStatisticsMapList item : flowStats.getFlowAndStatisticsMapList()) {
182             FlowRegistryKey flowRegistryKey = FlowRegistryKeyFactory.create(deviceInfo.getVersion(), item);
183             FlowRegistryKey lastHash = null;
184             if (null != lastHash) {
185                 assertNotEquals(lastHash, flowRegistryKey);
186             } else {
187                 lastHash = flowRegistryKey;
188             }
189         }
190     }
191 }