Mahesh Poojary Huawei | 04897d5 | 2015-11-13 16:45:01 +0530 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Laboratory |
Mahesh Poojary Huawei | 04897d5 | 2015-11-13 16:45:01 +0530 | [diff] [blame] | 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
Ray Milkey | 6c1bac3 | 2015-11-13 14:40:40 -0800 | [diff] [blame] | 16 | package org.onosproject.vtnrsc; |
| 17 | |
Mahesh Poojary Huawei | 04897d5 | 2015-11-13 16:45:01 +0530 | [diff] [blame] | 18 | import static org.hamcrest.MatcherAssert.assertThat; |
| 19 | import static org.hamcrest.Matchers.is; |
| 20 | import static org.onlab.junit.ImmutableClassChecker.assertThatClassIsImmutable; |
| 21 | |
Phaneendra Manda | 329a127 | 2016-02-10 22:57:00 +0530 | [diff] [blame] | 22 | import java.util.LinkedList; |
| 23 | import java.util.List; |
| 24 | import java.util.Set; |
| 25 | |
| 26 | import org.junit.Test; |
| 27 | import org.onlab.packet.IPv4; |
| 28 | import org.onlab.packet.IpAddress; |
| 29 | import org.onosproject.net.PortNumber; |
| 30 | |
| 31 | import com.google.common.collect.Lists; |
| 32 | import com.google.common.testing.EqualsTester; |
| 33 | |
Mahesh Poojary Huawei | 04897d5 | 2015-11-13 16:45:01 +0530 | [diff] [blame] | 34 | /** |
| 35 | * Unit tests for DefaultPortChain class. |
| 36 | */ |
| 37 | public class DefaultPortChainTest { |
Phaneendra Manda | 329a127 | 2016-02-10 22:57:00 +0530 | [diff] [blame] | 38 | |
| 39 | final PortChainId portChainId = PortChainId.of("78888888-fc23-aeb6-f44b-56dc5e2fb3ae"); |
| 40 | final TenantId tenantId = TenantId.tenantId("1"); |
| 41 | final String name = "PortChain"; |
| 42 | final String description = "PortChain"; |
| 43 | final List<PortPairGroupId> portPairGroups = new LinkedList<PortPairGroupId>(); |
| 44 | final List<FlowClassifierId> flowClassifiers = new LinkedList<FlowClassifierId>(); |
| 45 | |
| 46 | private PortChain getPortChain() { |
| 47 | |
| 48 | portPairGroups.clear(); |
| 49 | flowClassifiers.clear(); |
| 50 | // create list of Port Pair Groups. |
| 51 | PortPairGroupId portPairGroupId = PortPairGroupId.of("73333333-fc23-aeb6-f44b-56dc5e2fb3ae"); |
| 52 | portPairGroups.add(portPairGroupId); |
| 53 | portPairGroupId = PortPairGroupId.of("73333333-fc23-aeb6-f44b-56dc5e2fb3af"); |
| 54 | portPairGroups.add(portPairGroupId); |
| 55 | // create list of Flow classifiers. |
| 56 | FlowClassifierId flowClassifierId = FlowClassifierId.of("74444444-fc23-aeb6-f44b-56dc5e2fb3ae"); |
| 57 | flowClassifiers.add(flowClassifierId); |
| 58 | flowClassifierId = FlowClassifierId.of("74444444-fc23-aeb6-f44b-56dc5e2fb3af"); |
| 59 | flowClassifiers.add(flowClassifierId); |
| 60 | |
| 61 | DefaultPortChain.Builder portChainBuilder = new DefaultPortChain.Builder(); |
| 62 | final PortChain portChain = portChainBuilder.setId(portChainId).setTenantId(tenantId).setName(name) |
| 63 | .setDescription(description).setPortPairGroups(portPairGroups).setFlowClassifiers(flowClassifiers) |
| 64 | .build(); |
| 65 | |
| 66 | return portChain; |
| 67 | } |
| 68 | |
Mahesh Poojary Huawei | 04897d5 | 2015-11-13 16:45:01 +0530 | [diff] [blame] | 69 | /** |
| 70 | * Checks that the DefaultPortChain class is immutable. |
| 71 | */ |
| 72 | @Test |
| 73 | public void testImmutability() { |
| 74 | assertThatClassIsImmutable(DefaultPortChain.class); |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * Checks the operation of equals() methods. |
| 79 | */ |
| 80 | @Test |
| 81 | public void testEquals() { |
Phaneendra Manda | 329a127 | 2016-02-10 22:57:00 +0530 | [diff] [blame] | 82 | |
Mahesh Poojary Huawei | 04897d5 | 2015-11-13 16:45:01 +0530 | [diff] [blame] | 83 | // Create same two port chain objects. |
Phaneendra Manda | 329a127 | 2016-02-10 22:57:00 +0530 | [diff] [blame] | 84 | final PortChain portChain1 = getPortChain(); |
| 85 | final PortChain samePortChain1 = getPortChain(); |
Mahesh Poojary Huawei | 04897d5 | 2015-11-13 16:45:01 +0530 | [diff] [blame] | 86 | |
| 87 | // Create different port chain object. |
| 88 | final PortChainId portChainId2 = PortChainId.of("79999999-fc23-aeb6-f44b-56dc5e2fb3ae"); |
| 89 | final TenantId tenantId2 = TenantId.tenantId("2"); |
| 90 | final String name2 = "PortChain2"; |
| 91 | final String description2 = "PortChain2"; |
| 92 | // create list of Port Pair Groups. |
| 93 | final List<PortPairGroupId> portPairGroups2 = new LinkedList<PortPairGroupId>(); |
Phaneendra Manda | 329a127 | 2016-02-10 22:57:00 +0530 | [diff] [blame] | 94 | PortPairGroupId portPairGroupId = PortPairGroupId.of("75555555-fc23-aeb6-f44b-56dc5e2fb3ae"); |
Mahesh Poojary Huawei | 04897d5 | 2015-11-13 16:45:01 +0530 | [diff] [blame] | 95 | portPairGroups2.add(portPairGroupId); |
| 96 | portPairGroupId = PortPairGroupId.of("75555555-fc23-aeb6-f44b-56dc5e2fb3af"); |
| 97 | portPairGroups2.add(portPairGroupId); |
| 98 | // create list of Flow classifiers. |
| 99 | final List<FlowClassifierId> flowClassifiers2 = new LinkedList<FlowClassifierId>(); |
Phaneendra Manda | 329a127 | 2016-02-10 22:57:00 +0530 | [diff] [blame] | 100 | FlowClassifierId flowClassifierId = FlowClassifierId.of("76666666-fc23-aeb6-f44b-56dc5e2fb3ae"); |
Mahesh Poojary Huawei | 04897d5 | 2015-11-13 16:45:01 +0530 | [diff] [blame] | 101 | flowClassifiers2.add(flowClassifierId); |
| 102 | flowClassifierId = FlowClassifierId.of("76666666-fc23-aeb6-f44b-56dc5e2fb3af"); |
| 103 | flowClassifiers2.add(flowClassifierId); |
| 104 | |
Phaneendra Manda | 329a127 | 2016-02-10 22:57:00 +0530 | [diff] [blame] | 105 | DefaultPortChain.Builder portChainBuilder = new DefaultPortChain.Builder(); |
Mahesh Poojary Huawei | 04897d5 | 2015-11-13 16:45:01 +0530 | [diff] [blame] | 106 | final PortChain portChain2 = portChainBuilder.setId(portChainId2).setTenantId(tenantId2).setName(name2) |
| 107 | .setDescription(description2).setPortPairGroups(portPairGroups2).setFlowClassifiers(flowClassifiers2) |
| 108 | .build(); |
| 109 | |
| 110 | new EqualsTester().addEqualityGroup(portChain1, samePortChain1).addEqualityGroup(portChain2).testEquals(); |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * Checks the construction of a DefaultPortChain object. |
| 115 | */ |
| 116 | @Test |
| 117 | public void testConstruction() { |
Mahesh Poojary Huawei | 04897d5 | 2015-11-13 16:45:01 +0530 | [diff] [blame] | 118 | |
Phaneendra Manda | 329a127 | 2016-02-10 22:57:00 +0530 | [diff] [blame] | 119 | final PortChain portChain = getPortChain(); |
Mahesh Poojary Huawei | 04897d5 | 2015-11-13 16:45:01 +0530 | [diff] [blame] | 120 | |
| 121 | assertThat(portChainId, is(portChain.portChainId())); |
| 122 | assertThat(tenantId, is(portChain.tenantId())); |
| 123 | assertThat(name, is(portChain.name())); |
| 124 | assertThat(description, is(portChain.description())); |
| 125 | assertThat(portPairGroups, is(portChain.portPairGroups())); |
| 126 | assertThat(flowClassifiers, is(portChain.flowClassifiers())); |
| 127 | } |
Phaneendra Manda | 329a127 | 2016-02-10 22:57:00 +0530 | [diff] [blame] | 128 | |
| 129 | /** |
| 130 | * Verifies the load balance data structures. |
| 131 | */ |
| 132 | @Test |
| 133 | public void testLoadBalanceIdMap() { |
| 134 | |
| 135 | final PortChain portChain = getPortChain(); |
| 136 | |
| 137 | final FiveTuple fiveTuple1 = DefaultFiveTuple.builder().setIpSrc(IpAddress.valueOf("1.1.1.1")) |
| 138 | .setIpDst(IpAddress.valueOf("2.2.2.2")) |
| 139 | .setPortSrc(PortNumber.portNumber(500)) |
| 140 | .setPortDst(PortNumber.portNumber(1000)) |
| 141 | .setProtocol(IPv4.PROTOCOL_TCP) |
| 142 | .build(); |
| 143 | |
| 144 | PortPairId portPairId = PortPairId.of("a4444444-4a56-2a6e-cd3a-9dee4e2ec345"); |
| 145 | |
| 146 | final LoadBalanceId id1 = LoadBalanceId.of((byte) 1); |
| 147 | |
| 148 | List<PortPairId> tempPath = Lists.newArrayList(); |
| 149 | tempPath.add(portPairId); |
| 150 | |
| 151 | portChain.addLoadBalancePath(fiveTuple1, id1, tempPath); |
| 152 | Set<FiveTuple> keys = portChain.getLoadBalanceIdMapKeys(); |
| 153 | List<PortPairId> path = portChain.getLoadBalancePath(fiveTuple1); |
| 154 | |
| 155 | assertThat(portChain.getLoadBalancePath(fiveTuple1), is(path)); |
| 156 | assertThat(portChain.getLoadBalancePath(id1), is(path)); |
| 157 | assertThat(portChain.getLoadBalanceId(fiveTuple1), is(id1)); |
| 158 | assertThat(keys.contains(fiveTuple1), is(true)); |
| 159 | assertThat(path.contains(portPairId), is(true)); |
| 160 | } |
Mahesh Poojary Huawei | 04897d5 | 2015-11-13 16:45:01 +0530 | [diff] [blame] | 161 | } |