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; |
Phaneendra Manda | ab7fdfa | 2016-04-19 01:24:09 +0530 | [diff] [blame] | 29 | import org.onosproject.net.DeviceId; |
Phaneendra Manda | 329a127 | 2016-02-10 22:57:00 +0530 | [diff] [blame] | 30 | import org.onosproject.net.PortNumber; |
| 31 | |
| 32 | import com.google.common.collect.Lists; |
| 33 | import com.google.common.testing.EqualsTester; |
| 34 | |
Mahesh Poojary Huawei | 04897d5 | 2015-11-13 16:45:01 +0530 | [diff] [blame] | 35 | /** |
| 36 | * Unit tests for DefaultPortChain class. |
| 37 | */ |
| 38 | public class DefaultPortChainTest { |
Phaneendra Manda | 329a127 | 2016-02-10 22:57:00 +0530 | [diff] [blame] | 39 | |
| 40 | final PortChainId portChainId = PortChainId.of("78888888-fc23-aeb6-f44b-56dc5e2fb3ae"); |
| 41 | final TenantId tenantId = TenantId.tenantId("1"); |
| 42 | final String name = "PortChain"; |
| 43 | final String description = "PortChain"; |
| 44 | final List<PortPairGroupId> portPairGroups = new LinkedList<PortPairGroupId>(); |
| 45 | final List<FlowClassifierId> flowClassifiers = new LinkedList<FlowClassifierId>(); |
| 46 | |
| 47 | private PortChain getPortChain() { |
| 48 | |
| 49 | portPairGroups.clear(); |
| 50 | flowClassifiers.clear(); |
| 51 | // create list of Port Pair Groups. |
| 52 | PortPairGroupId portPairGroupId = PortPairGroupId.of("73333333-fc23-aeb6-f44b-56dc5e2fb3ae"); |
| 53 | portPairGroups.add(portPairGroupId); |
| 54 | portPairGroupId = PortPairGroupId.of("73333333-fc23-aeb6-f44b-56dc5e2fb3af"); |
| 55 | portPairGroups.add(portPairGroupId); |
| 56 | // create list of Flow classifiers. |
| 57 | FlowClassifierId flowClassifierId = FlowClassifierId.of("74444444-fc23-aeb6-f44b-56dc5e2fb3ae"); |
| 58 | flowClassifiers.add(flowClassifierId); |
| 59 | flowClassifierId = FlowClassifierId.of("74444444-fc23-aeb6-f44b-56dc5e2fb3af"); |
| 60 | flowClassifiers.add(flowClassifierId); |
| 61 | |
| 62 | DefaultPortChain.Builder portChainBuilder = new DefaultPortChain.Builder(); |
| 63 | final PortChain portChain = portChainBuilder.setId(portChainId).setTenantId(tenantId).setName(name) |
| 64 | .setDescription(description).setPortPairGroups(portPairGroups).setFlowClassifiers(flowClassifiers) |
| 65 | .build(); |
| 66 | |
| 67 | return portChain; |
| 68 | } |
| 69 | |
Mahesh Poojary Huawei | 04897d5 | 2015-11-13 16:45:01 +0530 | [diff] [blame] | 70 | /** |
| 71 | * Checks that the DefaultPortChain class is immutable. |
| 72 | */ |
| 73 | @Test |
| 74 | public void testImmutability() { |
| 75 | assertThatClassIsImmutable(DefaultPortChain.class); |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * Checks the operation of equals() methods. |
| 80 | */ |
| 81 | @Test |
| 82 | public void testEquals() { |
Phaneendra Manda | 329a127 | 2016-02-10 22:57:00 +0530 | [diff] [blame] | 83 | |
Mahesh Poojary Huawei | 04897d5 | 2015-11-13 16:45:01 +0530 | [diff] [blame] | 84 | // Create same two port chain objects. |
Phaneendra Manda | 329a127 | 2016-02-10 22:57:00 +0530 | [diff] [blame] | 85 | final PortChain portChain1 = getPortChain(); |
| 86 | final PortChain samePortChain1 = getPortChain(); |
Mahesh Poojary Huawei | 04897d5 | 2015-11-13 16:45:01 +0530 | [diff] [blame] | 87 | |
| 88 | // Create different port chain object. |
| 89 | final PortChainId portChainId2 = PortChainId.of("79999999-fc23-aeb6-f44b-56dc5e2fb3ae"); |
| 90 | final TenantId tenantId2 = TenantId.tenantId("2"); |
| 91 | final String name2 = "PortChain2"; |
| 92 | final String description2 = "PortChain2"; |
| 93 | // create list of Port Pair Groups. |
| 94 | final List<PortPairGroupId> portPairGroups2 = new LinkedList<PortPairGroupId>(); |
Phaneendra Manda | 329a127 | 2016-02-10 22:57:00 +0530 | [diff] [blame] | 95 | PortPairGroupId portPairGroupId = PortPairGroupId.of("75555555-fc23-aeb6-f44b-56dc5e2fb3ae"); |
Mahesh Poojary Huawei | 04897d5 | 2015-11-13 16:45:01 +0530 | [diff] [blame] | 96 | portPairGroups2.add(portPairGroupId); |
| 97 | portPairGroupId = PortPairGroupId.of("75555555-fc23-aeb6-f44b-56dc5e2fb3af"); |
| 98 | portPairGroups2.add(portPairGroupId); |
| 99 | // create list of Flow classifiers. |
| 100 | final List<FlowClassifierId> flowClassifiers2 = new LinkedList<FlowClassifierId>(); |
Phaneendra Manda | 329a127 | 2016-02-10 22:57:00 +0530 | [diff] [blame] | 101 | FlowClassifierId flowClassifierId = FlowClassifierId.of("76666666-fc23-aeb6-f44b-56dc5e2fb3ae"); |
Mahesh Poojary Huawei | 04897d5 | 2015-11-13 16:45:01 +0530 | [diff] [blame] | 102 | flowClassifiers2.add(flowClassifierId); |
| 103 | flowClassifierId = FlowClassifierId.of("76666666-fc23-aeb6-f44b-56dc5e2fb3af"); |
| 104 | flowClassifiers2.add(flowClassifierId); |
| 105 | |
Phaneendra Manda | 329a127 | 2016-02-10 22:57:00 +0530 | [diff] [blame] | 106 | DefaultPortChain.Builder portChainBuilder = new DefaultPortChain.Builder(); |
Mahesh Poojary Huawei | 04897d5 | 2015-11-13 16:45:01 +0530 | [diff] [blame] | 107 | final PortChain portChain2 = portChainBuilder.setId(portChainId2).setTenantId(tenantId2).setName(name2) |
| 108 | .setDescription(description2).setPortPairGroups(portPairGroups2).setFlowClassifiers(flowClassifiers2) |
| 109 | .build(); |
| 110 | |
| 111 | new EqualsTester().addEqualityGroup(portChain1, samePortChain1).addEqualityGroup(portChain2).testEquals(); |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | * Checks the construction of a DefaultPortChain object. |
| 116 | */ |
| 117 | @Test |
| 118 | public void testConstruction() { |
Mahesh Poojary Huawei | 04897d5 | 2015-11-13 16:45:01 +0530 | [diff] [blame] | 119 | |
Phaneendra Manda | 329a127 | 2016-02-10 22:57:00 +0530 | [diff] [blame] | 120 | final PortChain portChain = getPortChain(); |
Mahesh Poojary Huawei | 04897d5 | 2015-11-13 16:45:01 +0530 | [diff] [blame] | 121 | |
| 122 | assertThat(portChainId, is(portChain.portChainId())); |
| 123 | assertThat(tenantId, is(portChain.tenantId())); |
| 124 | assertThat(name, is(portChain.name())); |
| 125 | assertThat(description, is(portChain.description())); |
| 126 | assertThat(portPairGroups, is(portChain.portPairGroups())); |
| 127 | assertThat(flowClassifiers, is(portChain.flowClassifiers())); |
| 128 | } |
Phaneendra Manda | 329a127 | 2016-02-10 22:57:00 +0530 | [diff] [blame] | 129 | |
| 130 | /** |
| 131 | * Verifies the load balance data structures. |
| 132 | */ |
| 133 | @Test |
| 134 | public void testLoadBalanceIdMap() { |
| 135 | |
| 136 | final PortChain portChain = getPortChain(); |
| 137 | |
| 138 | final FiveTuple fiveTuple1 = DefaultFiveTuple.builder().setIpSrc(IpAddress.valueOf("1.1.1.1")) |
| 139 | .setIpDst(IpAddress.valueOf("2.2.2.2")) |
| 140 | .setPortSrc(PortNumber.portNumber(500)) |
| 141 | .setPortDst(PortNumber.portNumber(1000)) |
| 142 | .setProtocol(IPv4.PROTOCOL_TCP) |
| 143 | .build(); |
| 144 | |
| 145 | PortPairId portPairId = PortPairId.of("a4444444-4a56-2a6e-cd3a-9dee4e2ec345"); |
| 146 | |
| 147 | final LoadBalanceId id1 = LoadBalanceId.of((byte) 1); |
| 148 | |
| 149 | List<PortPairId> tempPath = Lists.newArrayList(); |
| 150 | tempPath.add(portPairId); |
| 151 | |
| 152 | portChain.addLoadBalancePath(fiveTuple1, id1, tempPath); |
| 153 | Set<FiveTuple> keys = portChain.getLoadBalanceIdMapKeys(); |
| 154 | List<PortPairId> path = portChain.getLoadBalancePath(fiveTuple1); |
| 155 | |
| 156 | assertThat(portChain.getLoadBalancePath(fiveTuple1), is(path)); |
| 157 | assertThat(portChain.getLoadBalancePath(id1), is(path)); |
| 158 | assertThat(portChain.getLoadBalanceId(fiveTuple1), is(id1)); |
| 159 | assertThat(keys.contains(fiveTuple1), is(true)); |
| 160 | assertThat(path.contains(portPairId), is(true)); |
| 161 | } |
Phaneendra Manda | ab7fdfa | 2016-04-19 01:24:09 +0530 | [diff] [blame] | 162 | |
| 163 | /** |
| 164 | * Verifies sfc classifiers. |
| 165 | */ |
| 166 | @Test |
| 167 | public void testSfcClassifier() { |
| 168 | final PortChain portChain = getPortChain(); |
| 169 | |
| 170 | final LoadBalanceId id1 = LoadBalanceId.of((byte) 1); |
| 171 | List<DeviceId> classifierList = Lists.newArrayList(); |
| 172 | DeviceId deviceId1 = DeviceId.deviceId("of:000000001"); |
| 173 | classifierList.add(deviceId1); |
| 174 | DeviceId deviceId2 = DeviceId.deviceId("of:000000002"); |
| 175 | classifierList.add(deviceId2); |
| 176 | portChain.addSfcClassifiers(id1, classifierList); |
| 177 | |
| 178 | assertThat(portChain.getSfcClassifiers(id1).contains(deviceId1), is(true)); |
| 179 | } |
| 180 | |
| 181 | /** |
| 182 | * Verifies sfc forwarders. |
| 183 | */ |
| 184 | @Test |
| 185 | public void testSfcForwarder() { |
| 186 | final PortChain portChain = getPortChain(); |
| 187 | |
| 188 | final LoadBalanceId id1 = LoadBalanceId.of((byte) 1); |
| 189 | List<DeviceId> forwarderList = Lists.newArrayList(); |
| 190 | DeviceId deviceId1 = DeviceId.deviceId("of:000000001"); |
| 191 | forwarderList.add(deviceId1); |
| 192 | DeviceId deviceId2 = DeviceId.deviceId("of:000000002"); |
| 193 | forwarderList.add(deviceId2); |
| 194 | portChain.addSfcForwarders(id1, forwarderList); |
| 195 | |
| 196 | assertThat(portChain.getSfcForwarders(id1).contains(deviceId1), is(true)); |
| 197 | } |
Mahesh Poojary Huawei | 04897d5 | 2015-11-13 16:45:01 +0530 | [diff] [blame] | 198 | } |