blob: dc1d1aca51d6f44a1b4cee17d2d6339f1f20e074 [file] [log] [blame]
Mahesh Poojary Huawei04897d52015-11-13 16:45:01 +05301/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
Mahesh Poojary Huawei04897d52015-11-13 16:45:01 +05303 *
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 Milkey6c1bac32015-11-13 14:40:40 -080016package org.onosproject.vtnrsc;
17
Mahesh Poojary Huawei04897d52015-11-13 16:45:01 +053018import static org.hamcrest.MatcherAssert.assertThat;
19import static org.hamcrest.Matchers.is;
20import static org.onlab.junit.ImmutableClassChecker.assertThatClassIsImmutable;
21
Phaneendra Manda329a1272016-02-10 22:57:00 +053022import java.util.LinkedList;
23import java.util.List;
24import java.util.Set;
25
26import org.junit.Test;
27import org.onlab.packet.IPv4;
28import org.onlab.packet.IpAddress;
Phaneendra Mandaab7fdfa2016-04-19 01:24:09 +053029import org.onosproject.net.DeviceId;
Phaneendra Manda329a1272016-02-10 22:57:00 +053030import org.onosproject.net.PortNumber;
31
32import com.google.common.collect.Lists;
33import com.google.common.testing.EqualsTester;
34
Mahesh Poojary Huawei04897d52015-11-13 16:45:01 +053035/**
36 * Unit tests for DefaultPortChain class.
37 */
38public class DefaultPortChainTest {
Phaneendra Manda329a1272016-02-10 22:57:00 +053039
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 Huawei04897d52015-11-13 16:45:01 +053070 /**
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 Manda329a1272016-02-10 22:57:00 +053083
Mahesh Poojary Huawei04897d52015-11-13 16:45:01 +053084 // Create same two port chain objects.
Phaneendra Manda329a1272016-02-10 22:57:00 +053085 final PortChain portChain1 = getPortChain();
86 final PortChain samePortChain1 = getPortChain();
Mahesh Poojary Huawei04897d52015-11-13 16:45:01 +053087
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 Manda329a1272016-02-10 22:57:00 +053095 PortPairGroupId portPairGroupId = PortPairGroupId.of("75555555-fc23-aeb6-f44b-56dc5e2fb3ae");
Mahesh Poojary Huawei04897d52015-11-13 16:45:01 +053096 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 Manda329a1272016-02-10 22:57:00 +0530101 FlowClassifierId flowClassifierId = FlowClassifierId.of("76666666-fc23-aeb6-f44b-56dc5e2fb3ae");
Mahesh Poojary Huawei04897d52015-11-13 16:45:01 +0530102 flowClassifiers2.add(flowClassifierId);
103 flowClassifierId = FlowClassifierId.of("76666666-fc23-aeb6-f44b-56dc5e2fb3af");
104 flowClassifiers2.add(flowClassifierId);
105
Phaneendra Manda329a1272016-02-10 22:57:00 +0530106 DefaultPortChain.Builder portChainBuilder = new DefaultPortChain.Builder();
Mahesh Poojary Huawei04897d52015-11-13 16:45:01 +0530107 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 Huawei04897d52015-11-13 16:45:01 +0530119
Phaneendra Manda329a1272016-02-10 22:57:00 +0530120 final PortChain portChain = getPortChain();
Mahesh Poojary Huawei04897d52015-11-13 16:45:01 +0530121
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 Manda329a1272016-02-10 22:57:00 +0530129
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 Mandaab7fdfa2016-04-19 01:24:09 +0530162
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 Huawei04897d52015-11-13 16:45:01 +0530198}