blob: c8afeef48a18f60de61e8e73c0dd4cef8baebc26 [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);
Saurav Das9bf49582018-08-13 15:34:26 -070065 private VlanId vInt;
Charles Chanf17f66b2018-02-26 21:33:25 -080066
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;
Saurav Das9bf49582018-08-13 15:34:26 -070083 vInt = srManager.getDefaultInternalVlan();
Charles Chanf17f66b2018-02-26 21:33:25 -080084 rrp = new RoutingRulePopulator(srManager);
85 }
86
87 // All ports are enabled
88 @Test
89 public void testNoMoreEnabledPortCase1() throws Exception {
90 Port port1 = new DefaultPort(dev1, p1, true);
91 Port port2 = new DefaultPort(dev1, p2, true);
92 Port port3 = new DefaultPort(dev1, p3, true);
93 Port port4 = new DefaultPort(dev1, p4, true);
94 Port port5 = new DefaultPort(dev1, p5, true);
95 List<Port> ports = Lists.newArrayList(port1, port2, port3, port4, port5);
96
97 expect(deviceService.getPorts(anyObject(DeviceId.class))).andReturn(ports).anyTimes();
98 replay(deviceService);
99 assertFalse(rrp.noMoreEnabledPort(devId1, v10));
100 assertFalse(rrp.noMoreEnabledPort(devId1, v20));
101 assertFalse(rrp.noMoreEnabledPort(devId1, vInt));
102 }
103
104 // Disable port 1
105 @Test
106 public void testNoMoreEnabledPortCase2() throws Exception {
107 Port port1 = new DefaultPort(dev1, p1, false);
108 Port port2 = new DefaultPort(dev1, p2, true);
109 Port port3 = new DefaultPort(dev1, p3, true);
110 Port port4 = new DefaultPort(dev1, p4, true);
111 Port port5 = new DefaultPort(dev1, p5, true);
112 List<Port> ports = Lists.newArrayList(port1, port2, port3, port4, port5);
113
114 expect(deviceService.getPorts(anyObject(DeviceId.class))).andReturn(ports).anyTimes();
115 replay(deviceService);
116 assertFalse(rrp.noMoreEnabledPort(devId1, v10));
117 assertFalse(rrp.noMoreEnabledPort(devId1, v20));
118 assertFalse(rrp.noMoreEnabledPort(devId1, vInt));
119 }
120
121 // Disable port 1 and 3
122 @Test
123 public void testNoMoreEnabledPortCase3() throws Exception {
124 Port port1 = new DefaultPort(dev1, p1, false);
125 Port port2 = new DefaultPort(dev1, p2, true);
126 Port port3 = new DefaultPort(dev1, p3, false);
127 Port port4 = new DefaultPort(dev1, p4, true);
128 Port port5 = new DefaultPort(dev1, p5, true);
129 List<Port> ports = Lists.newArrayList(port1, port2, port3, port4, port5);
130
131 expect(deviceService.getPorts(anyObject(DeviceId.class))).andReturn(ports).anyTimes();
132 replay(deviceService);
133 assertFalse(rrp.noMoreEnabledPort(devId1, v10));
134 assertTrue(rrp.noMoreEnabledPort(devId1, v20));
135 assertFalse(rrp.noMoreEnabledPort(devId1, vInt));
136 }
137
138 // Disable port 1 to 3
139 @Test
140 public void testNoMoreEnabledPortCase4() throws Exception {
141 Port port1 = new DefaultPort(dev1, p1, false);
142 Port port2 = new DefaultPort(dev1, p2, false);
143 Port port3 = new DefaultPort(dev1, p3, false);
144 Port port4 = new DefaultPort(dev1, p4, true);
145 Port port5 = new DefaultPort(dev1, p5, true);
146 List<Port> ports = Lists.newArrayList(port1, port2, port3, port4, port5);
147
148 expect(deviceService.getPorts(anyObject(DeviceId.class))).andReturn(ports).anyTimes();
149 replay(deviceService);
150 assertTrue(rrp.noMoreEnabledPort(devId1, v10));
151 assertTrue(rrp.noMoreEnabledPort(devId1, v20));
152 assertFalse(rrp.noMoreEnabledPort(devId1, vInt));
153 }
154
155 // Disable port 1 to 4
156 @Test
157 public void testNoMoreEnabledPortCase5() throws Exception {
158 Port port1 = new DefaultPort(dev1, p1, false);
159 Port port2 = new DefaultPort(dev1, p2, false);
160 Port port3 = new DefaultPort(dev1, p3, false);
161 Port port4 = new DefaultPort(dev1, p4, false);
162 Port port5 = new DefaultPort(dev1, p5, true);
163 List<Port> ports = Lists.newArrayList(port1, port2, port3, port4, port5);
164
165 expect(deviceService.getPorts(anyObject(DeviceId.class))).andReturn(ports).anyTimes();
166 replay(deviceService);
167 assertTrue(rrp.noMoreEnabledPort(devId1, v10));
168 assertTrue(rrp.noMoreEnabledPort(devId1, v20));
169 assertFalse(rrp.noMoreEnabledPort(devId1, vInt));
170 }
171
172 // Disable all ports
173 @Test
174 public void testNoMoreEnabledPortCase6() throws Exception {
175 Port port1 = new DefaultPort(dev1, p1, false);
176 Port port2 = new DefaultPort(dev1, p2, false);
177 Port port3 = new DefaultPort(dev1, p3, false);
178 Port port4 = new DefaultPort(dev1, p4, false);
179 Port port5 = new DefaultPort(dev1, p5, false);
180 List<Port> ports = Lists.newArrayList(port1, port2, port3, port4, port5);
181
182 expect(deviceService.getPorts(anyObject(DeviceId.class))).andReturn(ports).anyTimes();
183 replay(deviceService);
184 assertTrue(rrp.noMoreEnabledPort(devId1, v10));
185 assertTrue(rrp.noMoreEnabledPort(devId1, v20));
186 assertTrue(rrp.noMoreEnabledPort(devId1, vInt));
187 }
188}