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