blob: 4f5f226cd12cd0dc667c819f0005da1dbd9c2124 [file] [log] [blame]
Charles Chanf17f66b2018-02-26 21:33:25 -08001/*
2 * Copyright 2018-present Open Networking Foundation
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 */
16
17package org.onosproject.segmentrouting;
18
19import com.google.common.collect.Lists;
20import com.google.common.collect.Maps;
21import com.google.common.collect.Sets;
22import org.easymock.EasyMock;
23import org.junit.Before;
24import org.junit.Test;
25import org.onlab.packet.VlanId;
26import org.onosproject.net.ConnectPoint;
27import org.onosproject.net.DefaultDevice;
28import org.onosproject.net.DefaultPort;
29import org.onosproject.net.Device;
30import org.onosproject.net.DeviceId;
31import org.onosproject.net.Port;
32import org.onosproject.net.PortNumber;
33import org.onosproject.net.device.DeviceService;
34import org.onosproject.net.intf.Interface;
35import org.onosproject.net.intf.InterfaceService;
36import org.onosproject.net.provider.ProviderId;
37import org.onosproject.segmentrouting.config.DeviceConfiguration;
38
39import java.util.List;
40import java.util.Set;
41
42import static org.easymock.EasyMock.anyObject;
43import static org.easymock.EasyMock.expect;
44import static org.easymock.EasyMock.replay;
45import static org.junit.Assert.*;
46
47public class RoutingRulePopulatorTest {
48 private RoutingRulePopulator rrp;
49 private SegmentRoutingManager srManager;
50 private InterfaceService interfaceService;
51 private DeviceService deviceService;
52
53 private final DeviceId devId1 = DeviceId.deviceId("of:1");
54 private final Device dev1 = new DefaultDevice(ProviderId.NONE, devId1, Device.Type.SWITCH,
55 null, null, null, null, null);
56
57 private final PortNumber p1 = PortNumber.portNumber(1);
58 private final PortNumber p2 = PortNumber.portNumber(2);
59 private final PortNumber p3 = PortNumber.portNumber(3);
60 private final PortNumber p4 = PortNumber.portNumber(4);
61 private final PortNumber p5 = PortNumber.portNumber(5);
62
63 private final VlanId v10 = VlanId.vlanId((short) 10);
64 private final VlanId v20 = VlanId.vlanId((short) 20);
65 private final VlanId vInt = SegmentRoutingManager.INTERNAL_VLAN;
66
67 private final Interface u10 = new Interface(null, new ConnectPoint(devId1, p1),
68 null, null, null, v10, null, null);
69 private final Interface t10 = new Interface(null, new ConnectPoint(devId1, p2),
70 null, null, null, null, Sets.newHashSet(v10), null);
71 private final Interface t10n20 = new Interface(null, new ConnectPoint(devId1, p3),
72 null, null, null, null, Sets.newHashSet(v10), v20);
73
74 @Before
75 public void setUp() throws Exception {
76 Set<Interface> interfaces = Sets.newHashSet(u10, t10, t10n20);
77 interfaceService = new MockInterfaceService(interfaces);
78 deviceService = EasyMock.createMock(DeviceService.class);
79 srManager = new MockSegmentRoutingManager(Maps.newHashMap());
80 srManager.deviceConfiguration = EasyMock.createMock(DeviceConfiguration.class);
81 srManager.interfaceService = interfaceService;
82 srManager.deviceService = deviceService;
83 rrp = new RoutingRulePopulator(srManager);
84 }
85
86 // All ports are enabled
87 @Test
88 public void testNoMoreEnabledPortCase1() throws Exception {
89 Port port1 = new DefaultPort(dev1, p1, true);
90 Port port2 = new DefaultPort(dev1, p2, true);
91 Port port3 = new DefaultPort(dev1, p3, true);
92 Port port4 = new DefaultPort(dev1, p4, true);
93 Port port5 = new DefaultPort(dev1, p5, true);
94 List<Port> ports = Lists.newArrayList(port1, port2, port3, port4, port5);
95
96 expect(deviceService.getPorts(anyObject(DeviceId.class))).andReturn(ports).anyTimes();
97 replay(deviceService);
98 assertFalse(rrp.noMoreEnabledPort(devId1, v10));
99 assertFalse(rrp.noMoreEnabledPort(devId1, v20));
100 assertFalse(rrp.noMoreEnabledPort(devId1, vInt));
101 }
102
103 // Disable port 1
104 @Test
105 public void testNoMoreEnabledPortCase2() throws Exception {
106 Port port1 = new DefaultPort(dev1, p1, false);
107 Port port2 = new DefaultPort(dev1, p2, true);
108 Port port3 = new DefaultPort(dev1, p3, true);
109 Port port4 = new DefaultPort(dev1, p4, true);
110 Port port5 = new DefaultPort(dev1, p5, true);
111 List<Port> ports = Lists.newArrayList(port1, port2, port3, port4, port5);
112
113 expect(deviceService.getPorts(anyObject(DeviceId.class))).andReturn(ports).anyTimes();
114 replay(deviceService);
115 assertFalse(rrp.noMoreEnabledPort(devId1, v10));
116 assertFalse(rrp.noMoreEnabledPort(devId1, v20));
117 assertFalse(rrp.noMoreEnabledPort(devId1, vInt));
118 }
119
120 // Disable port 1 and 3
121 @Test
122 public void testNoMoreEnabledPortCase3() throws Exception {
123 Port port1 = new DefaultPort(dev1, p1, false);
124 Port port2 = new DefaultPort(dev1, p2, true);
125 Port port3 = new DefaultPort(dev1, p3, false);
126 Port port4 = new DefaultPort(dev1, p4, true);
127 Port port5 = new DefaultPort(dev1, p5, true);
128 List<Port> ports = Lists.newArrayList(port1, port2, port3, port4, port5);
129
130 expect(deviceService.getPorts(anyObject(DeviceId.class))).andReturn(ports).anyTimes();
131 replay(deviceService);
132 assertFalse(rrp.noMoreEnabledPort(devId1, v10));
133 assertTrue(rrp.noMoreEnabledPort(devId1, v20));
134 assertFalse(rrp.noMoreEnabledPort(devId1, vInt));
135 }
136
137 // Disable port 1 to 3
138 @Test
139 public void testNoMoreEnabledPortCase4() throws Exception {
140 Port port1 = new DefaultPort(dev1, p1, false);
141 Port port2 = new DefaultPort(dev1, p2, false);
142 Port port3 = new DefaultPort(dev1, p3, false);
143 Port port4 = new DefaultPort(dev1, p4, true);
144 Port port5 = new DefaultPort(dev1, p5, true);
145 List<Port> ports = Lists.newArrayList(port1, port2, port3, port4, port5);
146
147 expect(deviceService.getPorts(anyObject(DeviceId.class))).andReturn(ports).anyTimes();
148 replay(deviceService);
149 assertTrue(rrp.noMoreEnabledPort(devId1, v10));
150 assertTrue(rrp.noMoreEnabledPort(devId1, v20));
151 assertFalse(rrp.noMoreEnabledPort(devId1, vInt));
152 }
153
154 // Disable port 1 to 4
155 @Test
156 public void testNoMoreEnabledPortCase5() throws Exception {
157 Port port1 = new DefaultPort(dev1, p1, false);
158 Port port2 = new DefaultPort(dev1, p2, false);
159 Port port3 = new DefaultPort(dev1, p3, false);
160 Port port4 = new DefaultPort(dev1, p4, false);
161 Port port5 = new DefaultPort(dev1, p5, true);
162 List<Port> ports = Lists.newArrayList(port1, port2, port3, port4, port5);
163
164 expect(deviceService.getPorts(anyObject(DeviceId.class))).andReturn(ports).anyTimes();
165 replay(deviceService);
166 assertTrue(rrp.noMoreEnabledPort(devId1, v10));
167 assertTrue(rrp.noMoreEnabledPort(devId1, v20));
168 assertFalse(rrp.noMoreEnabledPort(devId1, vInt));
169 }
170
171 // Disable all ports
172 @Test
173 public void testNoMoreEnabledPortCase6() throws Exception {
174 Port port1 = new DefaultPort(dev1, p1, false);
175 Port port2 = new DefaultPort(dev1, p2, false);
176 Port port3 = new DefaultPort(dev1, p3, false);
177 Port port4 = new DefaultPort(dev1, p4, false);
178 Port port5 = new DefaultPort(dev1, p5, false);
179 List<Port> ports = Lists.newArrayList(port1, port2, port3, port4, port5);
180
181 expect(deviceService.getPorts(anyObject(DeviceId.class))).andReturn(ports).anyTimes();
182 replay(deviceService);
183 assertTrue(rrp.noMoreEnabledPort(devId1, v10));
184 assertTrue(rrp.noMoreEnabledPort(devId1, v20));
185 assertTrue(rrp.noMoreEnabledPort(devId1, vInt));
186 }
187}