blob: 27dd25aa93c76435d2c6e7137497f8c6d14357c6 [file] [log] [blame]
Mahesh Poojary Huawei04897d52015-11-13 16:45:01 +05301/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
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;
29import org.onosproject.net.PortNumber;
30
31import com.google.common.collect.Lists;
32import com.google.common.testing.EqualsTester;
33
Mahesh Poojary Huawei04897d52015-11-13 16:45:01 +053034/**
35 * Unit tests for DefaultPortChain class.
36 */
37public class DefaultPortChainTest {
Phaneendra Manda329a1272016-02-10 22:57:00 +053038
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 Huawei04897d52015-11-13 16:45:01 +053069 /**
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 Manda329a1272016-02-10 22:57:00 +053082
Mahesh Poojary Huawei04897d52015-11-13 16:45:01 +053083 // Create same two port chain objects.
Phaneendra Manda329a1272016-02-10 22:57:00 +053084 final PortChain portChain1 = getPortChain();
85 final PortChain samePortChain1 = getPortChain();
Mahesh Poojary Huawei04897d52015-11-13 16:45:01 +053086
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 Manda329a1272016-02-10 22:57:00 +053094 PortPairGroupId portPairGroupId = PortPairGroupId.of("75555555-fc23-aeb6-f44b-56dc5e2fb3ae");
Mahesh Poojary Huawei04897d52015-11-13 16:45:01 +053095 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 Manda329a1272016-02-10 22:57:00 +0530100 FlowClassifierId flowClassifierId = FlowClassifierId.of("76666666-fc23-aeb6-f44b-56dc5e2fb3ae");
Mahesh Poojary Huawei04897d52015-11-13 16:45:01 +0530101 flowClassifiers2.add(flowClassifierId);
102 flowClassifierId = FlowClassifierId.of("76666666-fc23-aeb6-f44b-56dc5e2fb3af");
103 flowClassifiers2.add(flowClassifierId);
104
Phaneendra Manda329a1272016-02-10 22:57:00 +0530105 DefaultPortChain.Builder portChainBuilder = new DefaultPortChain.Builder();
Mahesh Poojary Huawei04897d52015-11-13 16:45:01 +0530106 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 Huawei04897d52015-11-13 16:45:01 +0530118
Phaneendra Manda329a1272016-02-10 22:57:00 +0530119 final PortChain portChain = getPortChain();
Mahesh Poojary Huawei04897d52015-11-13 16:45:01 +0530120
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 Manda329a1272016-02-10 22:57:00 +0530128
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 Huawei04897d52015-11-13 16:45:01 +0530161}