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