Merge "bug 3126 - shift to new notification service"
[openflowplugin.git] / openflowplugin-impl / src / test / java / org / opendaylight / openflowplugin / impl / registry / flow / FlowHashFactoryTest.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 junit.framework.Assert;
19 import org.junit.Before;
20 import org.junit.Ignore;
21 import org.junit.Test;
22 import org.junit.runner.RunWith;
23 import org.mockito.Mock;
24 import org.mockito.Mockito;
25 import org.mockito.runners.MockitoJUnitRunner;
26 import org.opendaylight.openflowplugin.api.OFConstants;
27 import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
28 import org.opendaylight.openflowplugin.api.openflow.device.DeviceState;
29 import org.opendaylight.openflowplugin.api.openflow.registry.flow.FlowHash;
30 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Prefix;
31 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev100924.MacAddress;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowBuilder;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.FlowsStatisticsUpdate;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.FlowsStatisticsUpdateBuilder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.flow.and.statistics.map.list.FlowAndStatisticsMapList;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.flow.and.statistics.map.list.FlowAndStatisticsMapListBuilder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.FlowCookie;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.MatchBuilder;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.ethernet.match.fields.EthernetDestinationBuilder;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.ethernet.match.fields.EthernetSourceBuilder;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.EthernetMatchBuilder;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._3.match.Ipv4MatchBuilder;
43 import org.slf4j.Logger;
44 import org.slf4j.LoggerFactory;
45 @RunWith(MockitoJUnitRunner.class)
46 public class FlowHashFactoryTest {
47
48     private static final Logger LOG = LoggerFactory.getLogger(FlowHashFactoryTest.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<FlowHash> flowHashs = new HashSet();
95         for (FlowAndStatisticsMapList item : flowStats.getFlowAndStatisticsMapList()) {
96             flowHashs.add(FlowHashFactory.create(item, deviceContext));
97             flowHashs.add(FlowHashFactory.create(item, deviceContext));
98         }
99         assertEquals(3, flowHashs.size());
100     }
101
102     @Test
103     @Ignore
104     public void testGetHash2() throws Exception {
105         MatchBuilder match1Builder = new MatchBuilder().setLayer3Match(new Ipv4MatchBuilder()
106                 .setIpv4Destination(new Ipv4Prefix("10.0.1.157/32")).build());
107         FlowBuilder flow1Builder = new FlowBuilder()
108                 .setCookie(new FlowCookie(BigInteger.valueOf(483)))
109                 .setMatch(match1Builder.build())
110                 .setPriority(2)
111                 .setTableId((short) 0);
112
113         FlowHash flow1Hash = FlowHashFactory.create(flow1Builder.build(), deviceContext);
114         LOG.info("flowHash1: {}", flow1Hash.hashCode());
115
116
117         MatchBuilder match2Builder = new MatchBuilder().setLayer3Match(new Ipv4MatchBuilder()
118                 .setIpv4Destination(new Ipv4Prefix("10.0.0.242/32")).build());
119         FlowBuilder flow2Builder = new FlowBuilder(flow1Builder.build())
120                 .setCookie(new FlowCookie(BigInteger.valueOf(148)))
121                 .setMatch(match2Builder.build());
122
123         FlowHash flow2Hash = FlowHashFactory.create(flow2Builder.build(), deviceContext);
124         LOG.info("flowHash2: {}", flow2Hash.hashCode());
125
126         Assert.assertNotSame(flow1Hash, flow2Hash);
127     }
128
129     @Test
130     public void testGetHash() throws Exception {
131         FlowsStatisticsUpdate flowStats = FLOWS_STATISTICS_UPDATE_BUILDER.build();
132
133         for (FlowAndStatisticsMapList item : flowStats.getFlowAndStatisticsMapList()) {
134             FlowHash flowHash = FlowHashFactory.create(item, deviceContext);
135             FlowHash lastHash = null;
136             if (null != lastHash) {
137                 assertNotEquals(lastHash, flowHash);
138             } else {
139                 lastHash = flowHash;
140             }
141         }
142     }
143 }