Merge "Bug 3660 - Missing flow for match ARP."
[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.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;
44
45 @RunWith(MockitoJUnitRunner.class)
46 public class FlowRegistryKeyFactoryTest {
47
48     private static final Logger LOG = LoggerFactory.getLogger(FlowRegistryKeyFactoryTest.class);
49
50
51     private static final FlowsStatisticsUpdateBuilder FLOWS_STATISTICS_UPDATE_BUILDER = new FlowsStatisticsUpdateBuilder();
52     @Mock
53     private DeviceContext deviceContext;
54     @Mock
55     private DeviceState deviceState;
56
57
58     @Before
59     public void setup() {
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));
66
67             MatchBuilder matchBuilder = new MatchBuilder();
68
69             EthernetMatchBuilder ethernetMatchBuilder = new EthernetMatchBuilder();
70
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());
75
76             EthernetDestinationBuilder ethernetDestinationBuilder = new EthernetDestinationBuilder();
77             ethernetDestinationBuilder.setAddress(new MacAddress("00:00:00:0" + i + ":00:00"));
78             ethernetMatchBuilder.setEthernetDestination(ethernetDestinationBuilder.build());
79
80             matchBuilder.setEthernetMatch(ethernetMatchBuilder.build());
81
82             flowAndStatisticsMapListBuilder.setMatch(matchBuilder.build());
83             flowAndStatisticsMapListList.add(flowAndStatisticsMapListBuilder.build());
84         }
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);
88     }
89
90     @Test
91     public void testEquals() throws Exception {
92         FlowsStatisticsUpdate flowStats = FLOWS_STATISTICS_UPDATE_BUILDER.build();
93
94         HashSet<FlowRegistryKey> flowRegistryKeys = new HashSet<>();
95         for (FlowAndStatisticsMapList item : flowStats.getFlowAndStatisticsMapList()) {
96             flowRegistryKeys.add(FlowRegistryKeyFactory.create(item));
97             flowRegistryKeys.add(FlowRegistryKeyFactory.create(item));
98         }
99         assertEquals(3, flowRegistryKeys.size());
100     }
101
102     @Test
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())
109                 .setPriority(2)
110                 .setTableId((short) 0);
111
112         FlowRegistryKey flow1Hash = FlowRegistryKeyFactory.create(flow1Builder.build());
113         LOG.info("flowHash1: {}", flow1Hash.hashCode());
114
115
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());
121
122         FlowRegistryKey flow2Hash = FlowRegistryKeyFactory.create(flow2Builder.build());
123         LOG.info("flowHash2: {}", flow2Hash.hashCode());
124
125         Assert.assertNotSame(flow1Hash, flow2Hash);
126     }
127
128     @Test
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())
135                 .setPriority(2)
136                 .setTableId((short) 0);
137
138         FlowBuilder fb1 = new FlowBuilder(flow1Builder.build());
139         fb1.setTableId(null);
140         try {
141             FlowRegistryKeyFactory.create(fb1.build());
142             Assert.fail("hash creation should have failed because of NPE");
143         } catch (Exception e) {
144             // expected
145             Assert.assertEquals("flow tableId must not be null", e.getMessage());
146         }
147
148         FlowBuilder fb2 = new FlowBuilder(flow1Builder.build());
149         fb2.setPriority(null);
150         try {
151             FlowRegistryKeyFactory.create(fb2.build());
152         } catch (Exception e) {
153             // not expected
154             Assert.fail("no exception was expected while hash was creating.");
155         }
156
157         FlowBuilder fb3 = new FlowBuilder(flow1Builder.build());
158         fb3.setCookie(null);
159         FlowRegistryKey flowRegistryKey = FlowRegistryKeyFactory.create(fb3.build());
160         Assert.assertNotNull(flowRegistryKey.getCookie());
161         Assert.assertEquals(OFConstants.DEFAULT_COOKIE, flowRegistryKey.getCookie());
162     }
163
164     @Test
165     public void testGetHash() throws Exception {
166         FlowsStatisticsUpdate flowStats = FLOWS_STATISTICS_UPDATE_BUILDER.build();
167
168         for (FlowAndStatisticsMapList item : flowStats.getFlowAndStatisticsMapList()) {
169             FlowRegistryKey flowRegistryKey = FlowRegistryKeyFactory.create(item);
170             FlowRegistryKey lastHash = null;
171             if (null != lastHash) {
172                 assertNotEquals(lastHash, flowRegistryKey);
173             } else {
174                 lastHash = flowRegistryKey;
175             }
176         }
177     }
178 }