Merge topic 'Li-proposal'
[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 org.junit.Before;
19 import org.junit.Test;
20 import org.junit.runner.RunWith;
21 import org.mockito.Mock;
22 import org.mockito.Mockito;
23 import org.mockito.runners.MockitoJUnitRunner;
24 import org.opendaylight.openflowplugin.api.OFConstants;
25 import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
26 import org.opendaylight.openflowplugin.api.openflow.device.DeviceState;
27 import org.opendaylight.openflowplugin.api.openflow.registry.flow.FlowHash;
28 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev100924.MacAddress;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.FlowsStatisticsUpdate;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.FlowsStatisticsUpdateBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.flow.and.statistics.map.list.FlowAndStatisticsMapList;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.flow.and.statistics.map.list.FlowAndStatisticsMapListBuilder;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.FlowCookie;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.MatchBuilder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.ethernet.match.fields.EthernetDestinationBuilder;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.ethernet.match.fields.EthernetSourceBuilder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.EthernetMatchBuilder;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
40 @RunWith(MockitoJUnitRunner.class)
41 public class FlowHashFactoryTest {
42
43     private static final Logger LOG = LoggerFactory.getLogger(FlowHashFactoryTest.class);
44
45
46     private static final FlowsStatisticsUpdateBuilder FLOWS_STATISTICS_UPDATE_BUILDER = new FlowsStatisticsUpdateBuilder();
47     @Mock
48     private DeviceContext deviceContext;
49     @Mock
50     private DeviceState deviceState;
51
52
53     @Before
54     public void setup() {
55         List<FlowAndStatisticsMapList> flowAndStatisticsMapListList = new ArrayList();
56         for (int i = 1; i < 4; i++) {
57             FlowAndStatisticsMapListBuilder flowAndStatisticsMapListBuilder = new FlowAndStatisticsMapListBuilder();
58             flowAndStatisticsMapListBuilder.setPriority(i);
59             flowAndStatisticsMapListBuilder.setTableId((short) i);
60             flowAndStatisticsMapListBuilder.setCookie(new FlowCookie(BigInteger.TEN));
61
62             MatchBuilder matchBuilder = new MatchBuilder();
63
64             EthernetMatchBuilder ethernetMatchBuilder = new EthernetMatchBuilder();
65
66             EthernetSourceBuilder ethernetSourceBuilder = new EthernetSourceBuilder();
67             MacAddress macAddress = new MacAddress("00:00:00:00:00:0" + i);
68             ethernetSourceBuilder.setAddress(macAddress);
69             ethernetMatchBuilder.setEthernetSource(ethernetSourceBuilder.build());
70
71             EthernetDestinationBuilder ethernetDestinationBuilder = new EthernetDestinationBuilder();
72             ethernetDestinationBuilder.setAddress(new MacAddress("00:00:00:0" + i + ":00:00"));
73             ethernetMatchBuilder.setEthernetDestination(ethernetDestinationBuilder.build());
74
75             matchBuilder.setEthernetMatch(ethernetMatchBuilder.build());
76
77             flowAndStatisticsMapListBuilder.setMatch(matchBuilder.build());
78             flowAndStatisticsMapListList.add(flowAndStatisticsMapListBuilder.build());
79         }
80         FLOWS_STATISTICS_UPDATE_BUILDER.setFlowAndStatisticsMapList(flowAndStatisticsMapListList);
81         Mockito.when(deviceContext.getDeviceState()).thenReturn(deviceState);
82         Mockito.when(deviceState.getVersion()).thenReturn(OFConstants.OFP_VERSION_1_3);
83     }
84
85     @Test
86     public void testEquals() throws Exception {
87         FlowsStatisticsUpdate flowStats = FLOWS_STATISTICS_UPDATE_BUILDER.build();
88
89         HashSet<FlowHash> flowHashs = new HashSet();
90         for (FlowAndStatisticsMapList item : flowStats.getFlowAndStatisticsMapList()) {
91             FlowHash flowHash = FlowHashFactory.create(item, deviceContext);
92             flowHashs.add(flowHash);
93             flowHashs.add(flowHash);
94         }
95         assertEquals(3, flowHashs.size());
96     }
97
98     @Test
99     public void testGetHash() throws Exception {
100         FlowsStatisticsUpdate flowStats = FLOWS_STATISTICS_UPDATE_BUILDER.build();
101
102         for (FlowAndStatisticsMapList item : flowStats.getFlowAndStatisticsMapList()) {
103             FlowHash flowHash = FlowHashFactory.create(item, deviceContext);
104             FlowHash lastHash = null;
105             if (null != lastHash) {
106                 assertNotEquals(lastHash, flowHash);
107             } else {
108                 lastHash = flowHash;
109             }
110         }
111     }
112 }