blob: 9ac553dc3b49b4f04cd0f37cd606d2bbb14c28e3 [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;
Charles Chanf17f66b2018-02-26 21:33:25 -080037
38import java.util.List;
39import java.util.Set;
40
41import static org.easymock.EasyMock.anyObject;
42import static org.easymock.EasyMock.expect;
43import static org.easymock.EasyMock.replay;
44import static org.junit.Assert.*;
45
46public class RoutingRulePopulatorTest {
47 private RoutingRulePopulator rrp;
48 private SegmentRoutingManager srManager;
49 private InterfaceService interfaceService;
50 private DeviceService deviceService;
51
52 private final DeviceId devId1 = DeviceId.deviceId("of:1");
53 private final Device dev1 = new DefaultDevice(ProviderId.NONE, devId1, Device.Type.SWITCH,
54 null, null, null, null, null);
55
56 private final PortNumber p1 = PortNumber.portNumber(1);
57 private final PortNumber p2 = PortNumber.portNumber(2);
58 private final PortNumber p3 = PortNumber.portNumber(3);
59 private final PortNumber p4 = PortNumber.portNumber(4);
60 private final PortNumber p5 = PortNumber.portNumber(5);
61
62 private final VlanId v10 = VlanId.vlanId((short) 10);
63 private final VlanId v20 = VlanId.vlanId((short) 20);
Saurav Das9bf49582018-08-13 15:34:26 -070064 private VlanId vInt;
Charles Chanf17f66b2018-02-26 21:33:25 -080065
66 private final Interface u10 = new Interface(null, new ConnectPoint(devId1, p1),
67 null, null, null, v10, null, null);
68 private final Interface t10 = new Interface(null, new ConnectPoint(devId1, p2),
69 null, null, null, null, Sets.newHashSet(v10), null);
70 private final Interface t10n20 = new Interface(null, new ConnectPoint(devId1, p3),
71 null, null, null, null, Sets.newHashSet(v10), v20);
72
73 @Before
74 public void setUp() throws Exception {
75 Set<Interface> interfaces = Sets.newHashSet(u10, t10, t10n20);
76 interfaceService = new MockInterfaceService(interfaces);
77 deviceService = EasyMock.createMock(DeviceService.class);
Charles Chanfab61472021-06-14 23:31:23 -070078 srManager = new MockSegmentRoutingManager(Maps.newHashMap(), Maps.newHashMap());
Charles Chanf17f66b2018-02-26 21:33:25 -080079 srManager.deviceConfiguration = EasyMock.createMock(DeviceConfiguration.class);
80 srManager.interfaceService = interfaceService;
81 srManager.deviceService = deviceService;
Saurav Das9bf49582018-08-13 15:34:26 -070082 vInt = srManager.getDefaultInternalVlan();
Charles Chanf17f66b2018-02-26 21:33:25 -080083 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}