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