blob: 60163eb01ea5e45b61877a616718a24c2d725309 [file] [log] [blame]
Andreas Pantelopoulos5e7be3d2017-10-23 12:18:25 -07001/*
2 * Copyright 2016-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.fasterxml.jackson.databind.JsonNode;
20import com.fasterxml.jackson.databind.ObjectMapper;
21import com.google.common.collect.Lists;
22import com.google.common.collect.Sets;
23import org.junit.Before;
24import org.junit.Test;
25import org.onlab.packet.IpAddress;
26import org.onlab.packet.IpPrefix;
27import org.onlab.packet.MacAddress;
28import org.onlab.packet.MplsLabel;
29import org.onlab.packet.VlanId;
30import org.onosproject.TestApplicationId;
31import org.onosproject.core.ApplicationId;
32import org.onosproject.net.ConnectPoint;
33import org.onosproject.net.DefaultAnnotations;
34import org.onosproject.net.DefaultHost;
35import org.onosproject.net.Device;
36import org.onosproject.net.DeviceId;
37import org.onosproject.net.Host;
38import org.onosproject.net.HostId;
39import org.onosproject.net.HostLocation;
40import org.onosproject.net.PortNumber;
41import org.onosproject.net.config.Config;
42import org.onosproject.net.config.ConfigApplyDelegate;
43import org.onosproject.net.device.DeviceService;
44import org.onosproject.net.host.InterfaceIpAddress;
45import org.onosproject.net.intf.Interface;
46import org.onosproject.net.intf.InterfaceService;
47import org.onosproject.net.provider.ProviderId;
48import org.onosproject.segmentrouting.config.PwaasConfig;
49import org.onosproject.segmentrouting.pwaas.DefaultL2Tunnel;
50import org.onosproject.segmentrouting.pwaas.DefaultL2TunnelDescription;
51import org.onosproject.segmentrouting.pwaas.DefaultL2TunnelPolicy;
52import org.onosproject.segmentrouting.pwaas.L2Mode;
53
54import java.io.InputStream;
55import java.util.Set;
56
57import static org.hamcrest.Matchers.is;
58import static org.junit.Assert.*;
59
60/**
61 * Unit tests for class {@link PwaasConfig}.
62 */
63public class PwaasConfigTest {
64
65 private static final String TUNNEL_ID_1 = "1";
66 private static final String TUNNEL_ID_2 = "20";
67 private static final String NOT_PRESENT_TUNNEL_ID = "2";
68 private static final ConnectPoint INGRESS_1 = ConnectPoint.deviceConnectPoint("of:0000000000000001/1");
69 private static final ConnectPoint INGRESS_2 = ConnectPoint.deviceConnectPoint("of:0000000000000001/1");
70 private static final ConnectPoint EGRESS_1 = ConnectPoint.deviceConnectPoint("of:0000000000000002/1");
71 private static final ConnectPoint EGRESS_2 = ConnectPoint.deviceConnectPoint("of:0000000000000002/1");
72 private static final VlanId INGRESS_INNER_TAG_1 = VlanId.vlanId("10");
73 private static final VlanId INGRESS_INNER_TAG_2 = VlanId.vlanId("100");
74 private static final VlanId INGRESS_OUTER_TAG_1 = VlanId.vlanId("20");
75 private static final VlanId INGRESS_OUTER_TAG_2 = VlanId.vlanId("200");
76 private static final VlanId EGRESS_INNER_TAG_1 = VlanId.vlanId("10");
77 private static final VlanId EGRESS_INNER_TAG_2 = VlanId.vlanId("100");
78 private static final VlanId EGRESS_OUTER_TAG_1 = VlanId.vlanId("21");
79 private static final VlanId EGRESS_OUTER_TAG_2 = VlanId.vlanId("210");
80 private static final String MODE_1 = "RAW";
81 private static final String MODE_2 = "RAW";
82 private static final VlanId SD_TAG_1 = VlanId.NONE;
83 private static final VlanId SD_TAG_2 = VlanId.NONE;
84 private static final MplsLabel PW_LABEL_1 = MplsLabel.mplsLabel("255");
85 private static final MplsLabel PW_LABEL_2 = MplsLabel.mplsLabel("1255");
86
87 /*
88 * Configuration below copied from host handler test.
89 */
90
91 // Host Mac, VLAN
92 private static final ProviderId PROVIDER_ID = ProviderId.NONE;
93 private static final MacAddress HOST_MAC = MacAddress.valueOf("00:00:00:00:00:01");
94 private static final VlanId HOST_VLAN_UNTAGGED = VlanId.NONE;
95 private static final HostId HOST_ID_UNTAGGED = HostId.hostId(HOST_MAC, HOST_VLAN_UNTAGGED);
96 private static final VlanId HOST_VLAN_TAGGED = VlanId.vlanId((short) 20);
97 private static final HostId HOST_ID_TAGGED = HostId.hostId(HOST_MAC, HOST_VLAN_TAGGED);
98 // Host IP
99 private static final IpAddress HOST_IP11 = IpAddress.valueOf("10.0.1.1");
100 private static final IpAddress HOST_IP21 = IpAddress.valueOf("10.0.2.1");
101 private static final IpAddress HOST_IP12 = IpAddress.valueOf("10.0.1.2");
102 private static final IpAddress HOST_IP13 = IpAddress.valueOf("10.0.1.3");
103 private static final IpAddress HOST_IP14 = IpAddress.valueOf("10.0.1.4");
104 private static final IpAddress HOST_IP32 = IpAddress.valueOf("10.0.3.2");
105 // Device
106 private static final DeviceId DEV1 = DeviceId.deviceId("of:0000000000000001");
107 private static final DeviceId DEV2 = DeviceId.deviceId("of:0000000000000002");
108 private static final DeviceId DEV3 = DeviceId.deviceId("of:0000000000000003");
109 private static final DeviceId DEV4 = DeviceId.deviceId("of:0000000000000004");
110 private static final DeviceId DEV5 = DeviceId.deviceId("of:0000000000000005");
111 private static final DeviceId DEV6 = DeviceId.deviceId("of:0000000000000006");
112 // Port
113 private static final PortNumber P1 = PortNumber.portNumber(1);
114 private static final PortNumber P2 = PortNumber.portNumber(2);
115 private static final PortNumber P3 = PortNumber.portNumber(3);
116 private static final PortNumber P9 = PortNumber.portNumber(9);
117 // Connect Point
118 private static final ConnectPoint CP11 = new ConnectPoint(DEV1, P1);
119 private static final HostLocation HOST_LOC11 = new HostLocation(CP11, 0);
120 private static final ConnectPoint CP12 = new ConnectPoint(DEV1, P2);
121 private static final HostLocation HOST_LOC12 = new HostLocation(CP12, 0);
122 private static final ConnectPoint CP13 = new ConnectPoint(DEV1, P3);
123 private static final HostLocation HOST_LOC13 = new HostLocation(CP13, 0);
124 private static final ConnectPoint CP21 = new ConnectPoint(DEV2, P1);
125 private static final HostLocation HOST_LOC21 = new HostLocation(CP21, 0);
126 private static final ConnectPoint CP22 = new ConnectPoint(DEV2, P2);
127 private static final HostLocation HOST_LOC22 = new HostLocation(CP22, 0);
128 // Connect Point for dual-homed host failover
129 private static final ConnectPoint CP31 = new ConnectPoint(DEV3, P1);
130 private static final HostLocation HOST_LOC31 = new HostLocation(CP31, 0);
131 private static final ConnectPoint CP32 = new ConnectPoint(DEV3, P2);
132 private static final HostLocation HOST_LOC32 = new HostLocation(CP32, 0);
133 private static final ConnectPoint CP41 = new ConnectPoint(DEV4, P1);
134 private static final HostLocation HOST_LOC41 = new HostLocation(CP41, 0);
135 private static final ConnectPoint CP39 = new ConnectPoint(DEV3, P9);
136 private static final ConnectPoint CP49 = new ConnectPoint(DEV4, P9);
137 // Conenct Point for mastership test
138 private static final ConnectPoint CP51 = new ConnectPoint(DEV5, P1);
139 private static final HostLocation HOST_LOC51 = new HostLocation(CP51, 0);
140 private static final ConnectPoint CP61 = new ConnectPoint(DEV6, P1);
141 private static final HostLocation HOST_LOC61 = new HostLocation(CP61, 0);
142 // Interface VLAN
143 private static final VlanId INTF_VLAN_UNTAGGED = VlanId.vlanId((short) 10);
144 private static final Set<VlanId> INTF_VLAN_TAGGED = Sets.newHashSet(VlanId.vlanId((short) 20));
145 private static final VlanId INTF_VLAN_NATIVE = VlanId.vlanId((short) 30);
146 private static final Set<VlanId> INTF_VLAN_PAIR = Sets.newHashSet(VlanId.vlanId((short) 10),
147 VlanId.vlanId((short) 20), VlanId.vlanId((short) 30));
148 private static final VlanId INTF_VLAN_OTHER = VlanId.vlanId((short) 40);
149 // Interface subnet
150 private static final IpPrefix INTF_PREFIX1 = IpPrefix.valueOf("10.0.1.254/24");
151 private static final IpPrefix INTF_PREFIX2 = IpPrefix.valueOf("10.0.2.254/24");
152 private static final IpPrefix INTF_PREFIX3 = IpPrefix.valueOf("10.0.3.254/24");
153 private static final InterfaceIpAddress INTF_IP1 =
154 new InterfaceIpAddress(INTF_PREFIX1.address(), INTF_PREFIX1);
155 private static final InterfaceIpAddress INTF_IP2 =
156 new InterfaceIpAddress(INTF_PREFIX2.address(), INTF_PREFIX2);
157 private static final InterfaceIpAddress INTF_IP3 =
158 new InterfaceIpAddress(INTF_PREFIX3.address(), INTF_PREFIX3);
159 // Interfaces
160 private static final Interface INTF11 =
161 new Interface(null, CP11, Lists.newArrayList(INTF_IP1), MacAddress.NONE, null,
162 INTF_VLAN_UNTAGGED, null, null);
163 private static final Interface INTF12 =
164 new Interface(null, CP12, Lists.newArrayList(INTF_IP1), MacAddress.NONE, null,
165 INTF_VLAN_UNTAGGED, null, null);
166 private static final Interface INTF13 =
167 new Interface(null, CP13, Lists.newArrayList(INTF_IP2), MacAddress.NONE, null,
168 null, INTF_VLAN_TAGGED, INTF_VLAN_NATIVE);
169 private static final Interface INTF21 =
170 new Interface(null, CP21, Lists.newArrayList(INTF_IP1), MacAddress.NONE, null,
171 INTF_VLAN_UNTAGGED, null, null);
172 private static final Interface INTF22 =
173 new Interface(null, CP22, Lists.newArrayList(INTF_IP1), MacAddress.NONE, null,
174 INTF_VLAN_UNTAGGED, null, null);
175 private static final Interface INTF31 =
176 new Interface(null, CP31, Lists.newArrayList(INTF_IP1), MacAddress.NONE, null,
177 INTF_VLAN_UNTAGGED, null, null);
178 private static final Interface INTF32 =
179 new Interface(null, CP32, Lists.newArrayList(INTF_IP3), MacAddress.NONE, null,
180 INTF_VLAN_OTHER, null, null);
181 private static final Interface INTF39 =
182 new Interface(null, CP39, Lists.newArrayList(INTF_IP1), MacAddress.NONE, null,
183 null, INTF_VLAN_PAIR, null);
184 private static final Interface INTF41 =
185 new Interface(null, CP41, Lists.newArrayList(INTF_IP1), MacAddress.NONE, null,
186 INTF_VLAN_UNTAGGED, null, null);
187 private static final Interface INTF49 =
188 new Interface(null, CP49, Lists.newArrayList(INTF_IP1), MacAddress.NONE, null,
189 null, INTF_VLAN_PAIR, null);
190 // Host
191 private static final Host HOST1 = new DefaultHost(PROVIDER_ID, HOST_ID_UNTAGGED, HOST_MAC,
192 HOST_VLAN_UNTAGGED,
193 Sets.newHashSet(HOST_LOC11, HOST_LOC21),
194 Sets.newHashSet(HOST_IP11),
195 false);
196
197 // A set of hosts
198 private static final Set<Host> HOSTS = Sets.newHashSet(HOST1);
199 // A set of devices of which we have mastership
200 private static final Set<DeviceId> LOCAL_DEVICES = Sets.newHashSet(DEV1, DEV2, DEV3, DEV4);
201 // A set of interfaces
202 private static final Set<Interface> INTERFACES = Sets.newHashSet(INTF11, INTF12, INTF13, INTF21,
203 INTF22, INTF31, INTF32, INTF39, INTF41, INTF49);
204
205 private PwaasConfig config;
206 private PwaasConfig invalidConfigVlan;
207 private PwaasConfig invalidConfigMode;
208 private PwaasConfig invalidConfigLabel;
209 private PwaasConfig invalidConfigConflictingVlan;
210
211 @Before
212 public void setUp() throws Exception {
213 InputStream jsonStream = PwaasConfig.class
214 .getResourceAsStream("/pwaas.json");
215 InputStream jsonStreamInvalid1 = PwaasConfig.class
216 .getResourceAsStream("/pwaas-invalid-mode.json");
217 InputStream jsonStreamInvalid2 = PwaasConfig.class
218 .getResourceAsStream("/pwaas-invalid-pwlabel.json");
219 InputStream jsonStreamInvalid3 = PwaasConfig.class
220 .getResourceAsStream("/pwaas-invalid-vlan.json");
221 InputStream jsonStreamInvalid4 = PwaasConfig.class
222 .getResourceAsStream("/pwaas-conflicting-vlan.json");
223
224 String key = SegmentRoutingManager.APP_NAME;
225 ApplicationId subject = new TestApplicationId(key);
226 ObjectMapper mapper = new ObjectMapper();
227
228 JsonNode jsonNode = mapper.readTree(jsonStream);
229 JsonNode jsonNodeInvalid1 = mapper.readTree(jsonStreamInvalid1);
230 JsonNode jsonNodeInvalid2 = mapper.readTree(jsonStreamInvalid2);
231 JsonNode jsonNodeInvalid3 = mapper.readTree(jsonStreamInvalid3);
232 JsonNode jsonNodeInvalid4 = mapper.readTree(jsonStreamInvalid4);
233
234 ConfigApplyDelegate delegate = new MockDelegate();
235
236 DeviceService devService = new MockDeviceService();
237 InterfaceService infService = new MockInterfaceService(INTERFACES);
238
239 // create two devices and add them
240 DefaultAnnotations.Builder builderDev1 = DefaultAnnotations.builder();
241 DefaultAnnotations.Builder builderDev2 = DefaultAnnotations.builder();
242
243 Device dev1 = new MockDevice(DEV1, builderDev1.build());
244 Device dev2 = new MockDevice(DEV2, builderDev2.build());
245 ((MockDeviceService) devService).addDevice(dev1);
246 ((MockDeviceService) devService).addDevice(dev2);
247
248 config = new PwaasConfig(devService, infService);
249 invalidConfigVlan = new PwaasConfig(devService, infService);
250 invalidConfigMode = new PwaasConfig(devService, infService);
251 invalidConfigLabel = new PwaasConfig(devService, infService);
252 invalidConfigConflictingVlan = new PwaasConfig(devService, infService);
253
254 config.init(subject, key, jsonNode, mapper, delegate);
255 invalidConfigVlan.init(subject, key, jsonNodeInvalid1, mapper, delegate);
256 invalidConfigMode.init(subject, key, jsonNodeInvalid2, mapper, delegate);
257 invalidConfigLabel.init(subject, key, jsonNodeInvalid3, mapper, delegate);
258 invalidConfigConflictingVlan.init(subject, key, jsonNodeInvalid4, mapper, delegate);
259
260 config.deviceService = devService;
261 config.intfService = infService;
262
263 invalidConfigVlan.deviceService = devService;
264 invalidConfigVlan.intfService = infService;
265
266 invalidConfigLabel.deviceService = devService;
267 invalidConfigLabel.intfService = infService;
268
269 invalidConfigMode.deviceService = devService;
270 invalidConfigMode.intfService = infService;
271
272 invalidConfigConflictingVlan.deviceService = devService;
273 invalidConfigConflictingVlan.intfService = infService;
274 }
275
276 /**
277 * Tests config validity.
278 */
279 @Test
280 public void testIsValid() {
281 try {
282 assertTrue(config.isValid());
283 } catch (IllegalArgumentException e) {
284 assertTrue(false);
285 }
286 }
287
288 /**
289 * Tests config in-validity.
290 */
291 @Test
292 public void testValid1() {
293 assertFalse(invalidConfigVlan.isValid());
294 }
295
296 @Test
297 public void testValid2() {
298 assertFalse(invalidConfigMode.isValid());
299 }
300
301 @Test
302 public void testValid3() {
303 assertFalse(invalidConfigLabel.isValid());
304 }
305
Andreas Pantelopoulosb3e45a32018-01-16 11:51:32 -0800306 @Test
Andreas Pantelopoulos5e7be3d2017-10-23 12:18:25 -0700307 public void testValid4() {
Andreas Pantelopoulosb3e45a32018-01-16 11:51:32 -0800308 assertFalse(invalidConfigConflictingVlan.isValid());
Andreas Pantelopoulos5e7be3d2017-10-23 12:18:25 -0700309 }
310
311 /**
312 * Tests getPwIds.
313 */
314 @Test
315 public void testGetPwIds() {
316 Set<Long> pwIds = config.getPwIds();
317
318 assertThat(pwIds.size(), is(2));
319 assertTrue(pwIds.contains(Long.parseLong(TUNNEL_ID_1)));
320 assertTrue(pwIds.contains(Long.parseLong(TUNNEL_ID_2)));
321 assertFalse(pwIds.contains(Long.parseLong(NOT_PRESENT_TUNNEL_ID)));
322 }
323
324 /**
325 * Tests getPwDescription.
326 */
327 @Test
328 public void testGetPwDescription() {
329 DefaultL2TunnelDescription l2TunnelDescription = null;
330
331 DefaultL2Tunnel l2Tunnel = new DefaultL2Tunnel(
332 L2Mode.valueOf(MODE_1),
333 SD_TAG_1,
334 Long.parseLong(TUNNEL_ID_1),
335 PW_LABEL_1
336 );
337 DefaultL2TunnelPolicy l2TunnelPolicy = new DefaultL2TunnelPolicy(
338 Long.parseLong(TUNNEL_ID_1),
339 INGRESS_1,
340 INGRESS_INNER_TAG_1,
341 INGRESS_OUTER_TAG_1,
342 EGRESS_1,
343 EGRESS_INNER_TAG_1,
344 EGRESS_OUTER_TAG_1
345 );
346
347 l2TunnelDescription = config.getPwDescription(Long.parseLong(TUNNEL_ID_1));
348 assertThat(l2TunnelDescription.l2Tunnel().pwMode(), is(l2Tunnel.pwMode()));
349 assertThat(l2TunnelDescription.l2Tunnel().sdTag(), is(l2Tunnel.sdTag()));
350 assertThat(l2TunnelDescription.l2Tunnel().tunnelId(), is(l2Tunnel.tunnelId()));
351 assertThat(l2TunnelDescription.l2Tunnel().pwLabel(), is(l2Tunnel.pwLabel()));
352 assertThat(l2TunnelDescription.l2TunnelPolicy().tunnelId(), is(l2TunnelPolicy.tunnelId()));
353 assertThat(l2TunnelDescription.l2TunnelPolicy().cP1InnerTag(), is(l2TunnelPolicy.cP1InnerTag()));
354 assertThat(l2TunnelDescription.l2TunnelPolicy().cP1OuterTag(), is(l2TunnelPolicy.cP1OuterTag()));
355 assertThat(l2TunnelDescription.l2TunnelPolicy().cP2InnerTag(), is(l2TunnelPolicy.cP2InnerTag()));
356 assertThat(l2TunnelDescription.l2TunnelPolicy().cP2OuterTag(), is(l2TunnelPolicy.cP2OuterTag()));
357 assertThat(l2TunnelDescription.l2TunnelPolicy().cP1(), is(l2TunnelPolicy.cP1()));
358 assertThat(l2TunnelDescription.l2TunnelPolicy().cP2(), is(l2TunnelPolicy.cP2()));
359
360 l2Tunnel = new DefaultL2Tunnel(
361 L2Mode.valueOf(MODE_2),
362 SD_TAG_2,
363 Long.parseLong(TUNNEL_ID_2),
364 PW_LABEL_2
365 );
366 l2TunnelPolicy = new DefaultL2TunnelPolicy(
367 Long.parseLong(TUNNEL_ID_2),
368 INGRESS_2,
369 INGRESS_INNER_TAG_2,
370 INGRESS_OUTER_TAG_2,
371 EGRESS_2,
372 EGRESS_INNER_TAG_2,
373 EGRESS_OUTER_TAG_2
374 );
375
376 l2TunnelDescription = config.getPwDescription(Long.parseLong(TUNNEL_ID_2));
377 assertThat(l2TunnelDescription.l2Tunnel().pwMode(), is(l2Tunnel.pwMode()));
378 assertThat(l2TunnelDescription.l2Tunnel().sdTag(), is(l2Tunnel.sdTag()));
379 assertThat(l2TunnelDescription.l2Tunnel().tunnelId(), is(l2Tunnel.tunnelId()));
380 assertThat(l2TunnelDescription.l2Tunnel().pwLabel(), is(l2Tunnel.pwLabel()));
381 assertThat(l2TunnelDescription.l2TunnelPolicy().tunnelId(), is(l2TunnelPolicy.tunnelId()));
382 assertThat(l2TunnelDescription.l2TunnelPolicy().cP1InnerTag(), is(l2TunnelPolicy.cP1InnerTag()));
383 assertThat(l2TunnelDescription.l2TunnelPolicy().cP1OuterTag(), is(l2TunnelPolicy.cP1OuterTag()));
384 assertThat(l2TunnelDescription.l2TunnelPolicy().cP2OuterTag(), is(l2TunnelPolicy.cP2OuterTag()));
385 assertThat(l2TunnelDescription.l2TunnelPolicy().cP2OuterTag(), is(l2TunnelPolicy.cP2OuterTag()));
386 assertThat(l2TunnelDescription.l2TunnelPolicy().cP1(), is(l2TunnelPolicy.cP1()));
387 assertThat(l2TunnelDescription.l2TunnelPolicy().cP2(), is(l2TunnelPolicy.cP2()));
388 }
389
390 private class MockDelegate implements ConfigApplyDelegate {
391 @Override
392 public void onApply(Config config) {
393 }
394 }
395
396}