blob: d21bb2e639be28c0e5b21f8e3fde3acce1332fb8 [file] [log] [blame]
Thomas Vachuska781d18b2014-10-27 10:31:25 -07001/*
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07002 * Copyright 2014 Open Networking Laboratory
Thomas Vachuska781d18b2014-10-27 10:31:25 -07003 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07004 * 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
Thomas Vachuska781d18b2014-10-27 10:31:25 -07007 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07008 * 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.
Thomas Vachuska781d18b2014-10-27 10:31:25 -070015 */
Jonathan Hartce430a42014-10-16 20:44:29 -070016package org.onlab.onos.sdnip;
17
Thomas Vachuskab97cf282014-10-20 23:31:12 -070018import com.google.common.collect.Sets;
Jonathan Hartce430a42014-10-16 20:44:29 -070019import org.easymock.IArgumentMatcher;
20import org.junit.Before;
21import org.junit.Ignore;
22import org.junit.Test;
Thomas Vachuskae0f804a2014-10-27 23:40:48 -070023import org.onlab.onos.core.ApplicationId;
Jonathan Hartce430a42014-10-16 20:44:29 -070024import org.onlab.onos.net.ConnectPoint;
25import org.onlab.onos.net.DeviceId;
26import org.onlab.onos.net.PortNumber;
27import org.onlab.onos.net.flow.DefaultTrafficSelector;
28import org.onlab.onos.net.flow.DefaultTrafficTreatment;
29import org.onlab.onos.net.flow.TrafficSelector;
30import org.onlab.onos.net.flow.TrafficTreatment;
Pavlin Radoslavov76b0ae22014-10-27 15:33:19 -070031import org.onlab.onos.net.host.InterfaceIpAddress;
Jonathan Hartce430a42014-10-16 20:44:29 -070032import org.onlab.onos.net.intent.IntentService;
33import org.onlab.onos.net.intent.PointToPointIntent;
34import org.onlab.onos.sdnip.bgp.BgpConstants;
35import org.onlab.onos.sdnip.config.BgpPeer;
36import org.onlab.onos.sdnip.config.BgpSpeaker;
37import org.onlab.onos.sdnip.config.Interface;
38import org.onlab.onos.sdnip.config.InterfaceAddress;
39import org.onlab.onos.sdnip.config.SdnIpConfigService;
40import org.onlab.packet.Ethernet;
41import org.onlab.packet.IPv4;
42import org.onlab.packet.IpAddress;
43import org.onlab.packet.IpPrefix;
44import org.onlab.packet.MacAddress;
45
Thomas Vachuskab97cf282014-10-20 23:31:12 -070046import java.util.ArrayList;
47import java.util.Collections;
48import java.util.HashMap;
49import java.util.LinkedList;
50import java.util.List;
51import java.util.Map;
52
53import static org.easymock.EasyMock.*;
Jonathan Hartce430a42014-10-16 20:44:29 -070054
55/**
56 * Unit tests for PeerConnectivityManager interface.
57 */
58public class PeerConnectivityManagerTest {
59
Thomas Vachuskab97cf282014-10-20 23:31:12 -070060 private static final ApplicationId APPID = new ApplicationId() {
61 @Override
62 public short id() {
63 return 0;
64 }
65
66 @Override
67 public String name() {
68 return "foo";
69 }
70 };
71
Jonathan Hartce430a42014-10-16 20:44:29 -070072 private PeerConnectivityManager peerConnectivityManager;
73 private IntentService intentService;
74 private SdnIpConfigService configInfoService;
75 private InterfaceService interfaceService;
76
77 private Map<String, BgpSpeaker> bgpSpeakers;
78 private Map<String, Interface> interfaces;
79 private Map<IpAddress, BgpPeer> peers;
80
81 private Map<String, BgpSpeaker> configuredBgpSpeakers;
82 private Map<String, Interface> configuredInterfaces;
83 private Map<IpAddress, BgpPeer> configuredPeers;
84 private List<PointToPointIntent> intentList;
85
86 private final String dpid1 = "00:00:00:00:00:00:00:01";
87 private final String dpid2 = "00:00:00:00:00:00:00:02";
88
89 private final DeviceId deviceId1 =
90 DeviceId.deviceId(SdnIp.dpidToUri(dpid1));
91 private final DeviceId deviceId2 =
92 DeviceId.deviceId(SdnIp.dpidToUri(dpid2));
93
94 // Interfaces connected to BGP speakers
95 private final ConnectPoint s1Eth100 =
96 new ConnectPoint(deviceId1, PortNumber.portNumber(100));
97 private final ConnectPoint s2Eth100 =
98 new ConnectPoint(deviceId2, PortNumber.portNumber(100));
99
100 // Interfaces connected to BGP peers
101 private final ConnectPoint s1Eth1 =
102 new ConnectPoint(deviceId1, PortNumber.portNumber(1));
103 private final ConnectPoint s2Eth1 =
104 new ConnectPoint(deviceId2, PortNumber.portNumber(1));
105
Jonathan Hartce430a42014-10-16 20:44:29 -0700106 private final TrafficTreatment noTreatment =
107 DefaultTrafficTreatment.builder().build();
108
109 @Before
110 public void setUp() throws Exception {
111 bgpSpeakers = Collections.unmodifiableMap(setUpBgpSpeakers());
112 interfaces = Collections.unmodifiableMap(setUpInterfaces());
113 peers = Collections.unmodifiableMap(setUpPeers());
114
115 initPeerConnectivity();
116 intentList = setUpIntentList();
117 }
118
119 /**
120 * Sets up BGP speakers.
121 *
122 * @return configured BGP speakers as a map from speaker name to speaker
123 */
124 private Map<String, BgpSpeaker> setUpBgpSpeakers() {
125
126 configuredBgpSpeakers = new HashMap<>();
127
128 BgpSpeaker bgpSpeaker1 = new BgpSpeaker(
129 "bgpSpeaker1",
130 "00:00:00:00:00:00:00:01", 100,
131 "00:00:00:00:00:01");
132 List<InterfaceAddress> interfaceAddresses1 =
133 new LinkedList<InterfaceAddress>();
134 interfaceAddresses1.add(new InterfaceAddress(dpid1, 1, "192.168.10.101"));
135 interfaceAddresses1.add(new InterfaceAddress(dpid2, 1, "192.168.20.101"));
136 bgpSpeaker1.setInterfaceAddresses(interfaceAddresses1);
137 configuredBgpSpeakers.put(bgpSpeaker1.name(), bgpSpeaker1);
138
139 // BGP speaker2 is attached to the same switch port with speaker1
140 BgpSpeaker bgpSpeaker2 = new BgpSpeaker(
141 "bgpSpeaker2",
142 "00:00:00:00:00:00:00:01", 100,
143 "00:00:00:00:00:02");
144 List<InterfaceAddress> interfaceAddresses2 =
145 new LinkedList<InterfaceAddress>();
146 interfaceAddresses2.add(new InterfaceAddress(dpid1, 1, "192.168.10.102"));
147 interfaceAddresses2.add(new InterfaceAddress(dpid2, 1, "192.168.20.102"));
148 bgpSpeaker2.setInterfaceAddresses(interfaceAddresses2);
149 configuredBgpSpeakers.put(bgpSpeaker2.name(), bgpSpeaker2);
150
151 BgpSpeaker bgpSpeaker3 = new BgpSpeaker(
152 "bgpSpeaker3",
153 "00:00:00:00:00:00:00:02", 100,
154 "00:00:00:00:00:03");
155 List<InterfaceAddress> interfaceAddresses3 =
156 new LinkedList<InterfaceAddress>();
157 interfaceAddresses3.add(new InterfaceAddress(dpid1, 1, "192.168.10.103"));
158 interfaceAddresses3.add(new InterfaceAddress(dpid2, 1, "192.168.20.103"));
159 bgpSpeaker3.setInterfaceAddresses(interfaceAddresses3);
160 configuredBgpSpeakers.put(bgpSpeaker3.name(), bgpSpeaker3);
161
162 return configuredBgpSpeakers;
163 }
164
165 /**
166 * Sets up logical interfaces, which emulate the configured interfaces
167 * in SDN-IP application.
168 *
169 * @return configured interfaces as a MAP from Interface name to Interface
170 */
171 private Map<String, Interface> setUpInterfaces() {
172
173 configuredInterfaces = new HashMap<>();
174
175 String interfaceSw1Eth1 = "s1-eth1";
Pavlin Radoslavov76b0ae22014-10-27 15:33:19 -0700176 InterfaceIpAddress ia1 =
177 new InterfaceIpAddress(IpAddress.valueOf("192.168.10.1"),
178 IpPrefix.valueOf("192.168.10.0/24"));
Jonathan Hartce430a42014-10-16 20:44:29 -0700179 Interface intfsw1eth1 = new Interface(s1Eth1,
Pavlin Radoslavov76b0ae22014-10-27 15:33:19 -0700180 Collections.singleton(ia1),
Jonathan Hartce430a42014-10-16 20:44:29 -0700181 MacAddress.valueOf("00:00:00:00:00:01"));
182
183 configuredInterfaces.put(interfaceSw1Eth1, intfsw1eth1);
184 String interfaceSw2Eth1 = "s2-eth1";
Pavlin Radoslavov76b0ae22014-10-27 15:33:19 -0700185 InterfaceIpAddress ia2 =
186 new InterfaceIpAddress(IpAddress.valueOf("192.168.20.2"),
187 IpPrefix.valueOf("192.168.20.0/24"));
Jonathan Hartce430a42014-10-16 20:44:29 -0700188 Interface intfsw2eth1 = new Interface(s2Eth1,
Pavlin Radoslavov76b0ae22014-10-27 15:33:19 -0700189 Collections.singleton(ia2),
Jonathan Hartce430a42014-10-16 20:44:29 -0700190 MacAddress.valueOf("00:00:00:00:00:02"));
191 configuredInterfaces.put(interfaceSw2Eth1, intfsw2eth1);
192
193 interfaceService = createMock(InterfaceService.class);
194
195 expect(interfaceService.getInterface(s1Eth1))
196 .andReturn(intfsw1eth1).anyTimes();
197 expect(interfaceService.getInterface(s2Eth1))
198 .andReturn(intfsw2eth1).anyTimes();
199
200 // Non-existent interface used during one of the tests
201 expect(interfaceService.getInterface(new ConnectPoint(
202 DeviceId.deviceId(SdnIp.dpidToUri("00:00:00:00:00:00:01:00")),
203 PortNumber.portNumber(1))))
204 .andReturn(null).anyTimes();
205
206 expect(interfaceService.getInterfaces()).andReturn(
207 Sets.newHashSet(configuredInterfaces.values())).anyTimes();
208 replay(interfaceService);
209
210 return configuredInterfaces;
211 }
212
213 /**
214 * Sets up BGP daemon peers.
215 *
216 * @return configured BGP peers as a MAP from peer IP address to BgpPeer
217 */
218 private Map<IpAddress, BgpPeer> setUpPeers() {
219
220 configuredPeers = new HashMap<>();
221
222 String peerSw1Eth1 = "192.168.10.1";
223 configuredPeers.put(IpAddress.valueOf(peerSw1Eth1),
224 new BgpPeer(dpid1, 1, peerSw1Eth1));
225
226 // Two BGP peers are connected to switch 2 port 1.
227 String peer1Sw2Eth1 = "192.168.20.1";
228 configuredPeers.put(IpAddress.valueOf(peer1Sw2Eth1),
229 new BgpPeer(dpid2, 1, peer1Sw2Eth1));
230
231 String peer2Sw2Eth1 = "192.168.20.2";
232 configuredPeers.put(IpAddress.valueOf(peer2Sw2Eth1),
233 new BgpPeer(dpid2, 1, peer2Sw2Eth1));
234
235 return configuredPeers;
236 }
237
238 /**
239 * Sets up expected point to point intent list.
240 *
241 * @return point to point intent list
242 */
243 private List<PointToPointIntent> setUpIntentList() {
244
245 intentList = new ArrayList<PointToPointIntent>();
246
247 setUpBgpIntents();
248 setUpIcmpIntents();
249
250 return intentList;
251
252 }
253
254 /**
255 * Constructs a BGP intent and put it into the intentList.
256 * <p/>
257 * The purpose of this method is too simplify the setUpBgpIntents() method,
258 * and to make the setUpBgpIntents() easy to read.
259 *
260 * @param srcPrefix source IP prefix to match
261 * @param dstPrefix destination IP prefix to match
262 * @param srcTcpPort source TCP port to match
263 * @param dstTcpPort destination TCP port to match
264 * @param srcConnectPoint source connect point for PointToPointIntent
265 * @param dstConnectPoint destination connect point for PointToPointIntent
266 */
267 private void bgpPathintentConstructor(String srcPrefix, String dstPrefix,
268 Short srcTcpPort, Short dstTcpPort,
269 ConnectPoint srcConnectPoint, ConnectPoint dstConnectPoint) {
270
271 TrafficSelector.Builder builder = DefaultTrafficSelector.builder()
272 .matchEthType(Ethernet.TYPE_IPV4)
273 .matchIPProtocol(IPv4.PROTOCOL_TCP)
274 .matchIPSrc(IpPrefix.valueOf(srcPrefix))
275 .matchIPDst(IpPrefix.valueOf(dstPrefix));
276
277 if (srcTcpPort != null) {
278 builder.matchTcpSrc(srcTcpPort);
279 }
280 if (dstTcpPort != null) {
281 builder.matchTcpDst(dstTcpPort);
282 }
283
284 PointToPointIntent intent = new PointToPointIntent(
Thomas Vachuskab97cf282014-10-20 23:31:12 -0700285 APPID, builder.build(), noTreatment,
Jonathan Hartce430a42014-10-16 20:44:29 -0700286 srcConnectPoint, dstConnectPoint);
287
288 intentList.add(intent);
289 }
290
291 /**
292 * Sets up intents for BGP paths.
293 */
294 private void setUpBgpIntents() {
295
296 Short bgpPort = Short.valueOf((short) BgpConstants.BGP_PORT);
297
298 // Start to build intents between BGP speaker1 and BGP peer1
299 bgpPathintentConstructor(
300 "192.168.10.101/32", "192.168.10.1/32", null, bgpPort,
301 s1Eth100, s1Eth1);
302 bgpPathintentConstructor(
303 "192.168.10.101/32", "192.168.10.1/32", bgpPort, null,
304 s1Eth100, s1Eth1);
305
306 bgpPathintentConstructor(
307 "192.168.10.1/32", "192.168.10.101/32", null, bgpPort,
308 s1Eth1, s1Eth100);
309 bgpPathintentConstructor(
310 "192.168.10.1/32", "192.168.10.101/32", bgpPort, null,
311 s1Eth1, s1Eth100);
312
313 // Start to build intents between BGP speaker1 and BGP peer2
314 bgpPathintentConstructor(
315 "192.168.20.101/32", "192.168.20.1/32", null, bgpPort,
316 s1Eth100, s2Eth1);
317 bgpPathintentConstructor(
318 "192.168.20.101/32", "192.168.20.1/32", bgpPort, null,
319 s1Eth100, s2Eth1);
320
321 bgpPathintentConstructor(
322 "192.168.20.1/32", "192.168.20.101/32", null, bgpPort,
323 s2Eth1, s1Eth100);
324 bgpPathintentConstructor(
325 "192.168.20.1/32", "192.168.20.101/32", bgpPort, null,
326 s2Eth1, s1Eth100);
327
328 // Start to build intents between BGP speaker1 and BGP peer3
329 bgpPathintentConstructor(
330 "192.168.20.101/32", "192.168.20.2/32", null, bgpPort,
331 s1Eth100, s2Eth1);
332 bgpPathintentConstructor(
333 "192.168.20.101/32", "192.168.20.2/32", bgpPort, null,
334 s1Eth100, s2Eth1);
335
336 bgpPathintentConstructor(
337 "192.168.20.2/32", "192.168.20.101/32", null, bgpPort,
338 s2Eth1, s1Eth100);
339 bgpPathintentConstructor(
340 "192.168.20.2/32", "192.168.20.101/32", bgpPort, null,
341 s2Eth1, s1Eth100);
342
343 //
344 // Start to build intents between BGP speaker2 and BGP peer1
345 bgpPathintentConstructor(
346 "192.168.10.102/32", "192.168.10.1/32", null, bgpPort,
347 s1Eth100, s1Eth1);
348 bgpPathintentConstructor(
349 "192.168.10.102/32", "192.168.10.1/32", bgpPort, null,
350 s1Eth100, s1Eth1);
351
352 bgpPathintentConstructor(
353 "192.168.10.1/32", "192.168.10.102/32", null, bgpPort,
354 s1Eth1, s1Eth100);
355 bgpPathintentConstructor(
356 "192.168.10.1/32", "192.168.10.102/32", bgpPort, null,
357 s1Eth1, s1Eth100);
358 // Start to build intents between BGP speaker2 and BGP peer2
359 bgpPathintentConstructor(
360 "192.168.20.102/32", "192.168.20.1/32", null, bgpPort,
361 s1Eth100, s2Eth1);
362 bgpPathintentConstructor(
363 "192.168.20.102/32", "192.168.20.1/32", bgpPort, null,
364 s1Eth100, s2Eth1);
365
366 bgpPathintentConstructor(
367 "192.168.20.1/32", "192.168.20.102/32", null, bgpPort,
368 s2Eth1, s1Eth100);
369 bgpPathintentConstructor(
370 "192.168.20.1/32", "192.168.20.102/32", bgpPort, null,
371 s2Eth1, s1Eth100);
372
373 // Start to build intents between BGP speaker2 and BGP peer3
374 bgpPathintentConstructor(
375 "192.168.20.102/32", "192.168.20.2/32", null, bgpPort,
376 s1Eth100, s2Eth1);
377 bgpPathintentConstructor(
378 "192.168.20.102/32", "192.168.20.2/32", bgpPort, null,
379 s1Eth100, s2Eth1);
380
381 bgpPathintentConstructor(
382 "192.168.20.2/32", "192.168.20.102/32", null, bgpPort,
383 s2Eth1, s1Eth100);
384 bgpPathintentConstructor(
385 "192.168.20.2/32", "192.168.20.102/32", bgpPort, null,
386 s2Eth1, s1Eth100);
387
388 //
389 // Start to build intents between BGP speaker3 and BGP peer1
390 bgpPathintentConstructor(
391 "192.168.10.103/32", "192.168.10.1/32", null, bgpPort,
392 s2Eth100, s1Eth1);
393 bgpPathintentConstructor(
394 "192.168.10.103/32", "192.168.10.1/32", bgpPort, null,
395 s2Eth100, s1Eth1);
396
397 bgpPathintentConstructor(
398 "192.168.10.1/32", "192.168.10.103/32", null, bgpPort,
399 s1Eth1, s2Eth100);
400 bgpPathintentConstructor(
401 "192.168.10.1/32", "192.168.10.103/32", bgpPort, null,
402 s1Eth1, s2Eth100);
403
404 // Start to build intents between BGP speaker3 and BGP peer2
405 bgpPathintentConstructor(
406 "192.168.20.103/32", "192.168.20.1/32", null, bgpPort,
407 s2Eth100, s2Eth1);
408 bgpPathintentConstructor(
409 "192.168.20.103/32", "192.168.20.1/32", bgpPort, null,
410 s2Eth100, s2Eth1);
411
412 bgpPathintentConstructor(
413 "192.168.20.1/32", "192.168.20.103/32", null, bgpPort,
414 s2Eth1, s2Eth100);
415 bgpPathintentConstructor(
416 "192.168.20.1/32", "192.168.20.103/32", bgpPort, null,
417 s2Eth1, s2Eth100);
418
419 // Start to build intents between BGP speaker3 and BGP peer3
420 bgpPathintentConstructor(
421 "192.168.20.103/32", "192.168.20.2/32", null, bgpPort,
422 s2Eth100, s2Eth1);
423 bgpPathintentConstructor(
424 "192.168.20.103/32", "192.168.20.2/32", bgpPort, null,
425 s2Eth100, s2Eth1);
426
427 bgpPathintentConstructor(
428 "192.168.20.2/32", "192.168.20.103/32", null, bgpPort,
429 s2Eth1, s2Eth100);
430 bgpPathintentConstructor(
431 "192.168.20.2/32", "192.168.20.103/32", bgpPort, null,
432 s2Eth1, s2Eth100);
433 }
434
435 /**
436 * Constructs a BGP intent and put it into the intentList.
437 * <p/>
438 * The purpose of this method is too simplify the setUpBgpIntents() method,
439 * and to make the setUpBgpIntents() easy to read.
440 *
441 * @param srcPrefix source IP prefix to match
442 * @param dstPrefix destination IP prefix to match
443 * @param srcConnectPoint source connect point for PointToPointIntent
444 * @param dstConnectPoint destination connect point for PointToPointIntent
445 */
446 private void icmpPathintentConstructor(String srcPrefix, String dstPrefix,
447 ConnectPoint srcConnectPoint, ConnectPoint dstConnectPoint) {
448
449 TrafficSelector selector = DefaultTrafficSelector.builder()
450 .matchEthType(Ethernet.TYPE_IPV4)
451 .matchIPProtocol(IPv4.PROTOCOL_ICMP)
452 .matchIPSrc(IpPrefix.valueOf(srcPrefix))
453 .matchIPDst(IpPrefix.valueOf(dstPrefix))
454 .build();
455
456 PointToPointIntent intent = new PointToPointIntent(
Thomas Vachuskab97cf282014-10-20 23:31:12 -0700457 APPID, selector, noTreatment,
Jonathan Hartce430a42014-10-16 20:44:29 -0700458 srcConnectPoint, dstConnectPoint);
459
460 intentList.add(intent);
461 }
462
463 /**
464 * Sets up intents for ICMP paths.
465 */
466 private void setUpIcmpIntents() {
467
468 // Start to build intents between BGP speaker1 and BGP peer1
469 icmpPathintentConstructor(
470 "192.168.10.101/32", "192.168.10.1/32", s1Eth100, s1Eth1);
471 icmpPathintentConstructor(
472 "192.168.10.1/32", "192.168.10.101/32", s1Eth1, s1Eth100);
473
474 // Start to build intents between BGP speaker1 and BGP peer2
475 icmpPathintentConstructor(
476 "192.168.20.101/32", "192.168.20.1/32", s1Eth100, s2Eth1);
477 icmpPathintentConstructor(
478 "192.168.20.1/32", "192.168.20.101/32", s2Eth1, s1Eth100);
479
480 // Start to build intents between BGP speaker1 and BGP peer3
481 icmpPathintentConstructor(
482 "192.168.20.101/32", "192.168.20.2/32", s1Eth100, s2Eth1);
483 icmpPathintentConstructor(
484 "192.168.20.2/32", "192.168.20.101/32", s2Eth1, s1Eth100);
485
486 //
487 // Start to build intents between BGP speaker2 and BGP peer1
488 icmpPathintentConstructor(
489 "192.168.10.102/32", "192.168.10.1/32", s1Eth100, s1Eth1);
490 icmpPathintentConstructor(
491 "192.168.10.1/32", "192.168.10.102/32", s1Eth1, s1Eth100);
492
493 // Start to build intents between BGP speaker2 and BGP peer2
494 icmpPathintentConstructor(
495 "192.168.20.102/32", "192.168.20.1/32", s1Eth100, s2Eth1);
496 icmpPathintentConstructor(
497 "192.168.20.1/32", "192.168.20.102/32", s2Eth1, s1Eth100);
498
499 // Start to build intents between BGP speaker2 and BGP peer3
500 icmpPathintentConstructor(
501 "192.168.20.102/32", "192.168.20.2/32", s1Eth100, s2Eth1);
502 icmpPathintentConstructor(
503 "192.168.20.2/32", "192.168.20.102/32", s2Eth1, s1Eth100);
504
505 //
506 // Start to build intents between BGP speaker3 and BGP peer1
507 icmpPathintentConstructor(
508 "192.168.10.103/32", "192.168.10.1/32", s2Eth100, s1Eth1);
509 icmpPathintentConstructor(
510 "192.168.10.1/32", "192.168.10.103/32", s1Eth1, s2Eth100);
511
512 // Start to build intents between BGP speaker3 and BGP peer2
513 icmpPathintentConstructor(
514 "192.168.20.103/32", "192.168.20.1/32", s2Eth100, s2Eth1);
515 icmpPathintentConstructor(
516 "192.168.20.1/32", "192.168.20.103/32", s2Eth1, s2Eth100);
517
518 // Start to build intents between BGP speaker3 and BGP peer3
519 icmpPathintentConstructor(
520 "192.168.20.103/32", "192.168.20.2/32", s2Eth100, s2Eth1);
521 icmpPathintentConstructor(
522 "192.168.20.2/32", "192.168.20.103/32", s2Eth1, s2Eth100);
523
524 }
525
526 /**
527 * Initializes peer connectivity testing environment.
528 */
529 private void initPeerConnectivity() {
530
531 configInfoService = createMock(SdnIpConfigService.class);
532 expect(configInfoService.getBgpPeers()).andReturn(peers).anyTimes();
533 expect(configInfoService.getBgpSpeakers()).andReturn(bgpSpeakers).anyTimes();
534 replay(configInfoService);
535
536 intentService = createMock(IntentService.class);
537 replay(intentService);
538
Thomas Vachuskab97cf282014-10-20 23:31:12 -0700539 peerConnectivityManager = new PeerConnectivityManager(APPID, configInfoService,
Jonathan Hartce430a42014-10-16 20:44:29 -0700540 interfaceService, intentService);
541 }
542
543 /*
544 * EasyMock matcher that matches {@link PointToPointIntent}s but
545 * ignores the {@link IntentId} when matching.
546 * <p/>
547 * The normal intent equals method tests that the intent IDs are equal,
548 * however in these tests we can't know what the intent IDs will be in
549 * advance, so we can't set up expected intents with the correct IDs. Thus,
550 * the solution is to use an EasyMock matcher that verifies that all the
551 * value properties of the provided intent match the expected values, but
552 * ignores the intent ID when testing equality.
553 */
554 private static final class IdAgnosticPointToPointIntentMatcher implements
555 IArgumentMatcher {
556
557 private final PointToPointIntent intent;
558 private String providedIntentString;
559
560 /**
561 * Constructor taking the expected intent to match against.
562 *
563 * @param intent the expected intent
564 */
565 public IdAgnosticPointToPointIntentMatcher(PointToPointIntent intent) {
566 this.intent = intent;
567 }
568
569 @Override
570 public void appendTo(StringBuffer strBuffer) {
571 strBuffer.append("PointToPointIntentMatcher unable to match: "
572 + providedIntentString);
573 }
574
575 @Override
576 public boolean matches(Object object) {
577 if (!(object instanceof PointToPointIntent)) {
578 return false;
579 }
580
581 PointToPointIntent providedIntent = (PointToPointIntent) object;
582 providedIntentString = providedIntent.toString();
583
584 PointToPointIntent matchIntent =
Thomas Vachuskab97cf282014-10-20 23:31:12 -0700585 new PointToPointIntent(providedIntent.appId(),
Jonathan Hartce430a42014-10-16 20:44:29 -0700586 intent.selector(), intent.treatment(),
587 intent.ingressPoint(), intent.egressPoint());
588
589 return matchIntent.equals(providedIntent);
590 }
591 }
592
593 /**
594 * Matcher method to set an expected intent to match against (ignoring the
595 * the intent ID).
596 *
597 * @param intent the expected intent
598 * @return something of type PointToPointIntent
599 */
600 private static PointToPointIntent eqExceptId(
601 PointToPointIntent intent) {
602 reportMatcher(new IdAgnosticPointToPointIntentMatcher(intent));
603 return null;
604 }
605
606 /**
607 * Tests whether peer connectivity manager can set up correct BGP and
608 * ICMP intents according to specific configuration.
609 * <p/>
610 * Two tricky cases included in the configuration are: 2 peers on a same
611 * switch port, peer on the same switch with BGPd.
612 */
613 @Test
614 public void testConnectionSetup() {
615
616 reset(intentService);
617
618 // Sets up the expected PointToPoint intents.
619 for (int i = 0; i < intentList.size(); i++) {
620 intentService.submit(eqExceptId(intentList.get(i)));
621 }
622
623 replay(intentService);
624
625 // Running the interface to be tested.
626 peerConnectivityManager.start();
627
628 verify(intentService);
629
630 }
631
632 /**
633 * Tests a corner case, when there are no interfaces in the configuration.
634 */
635 @Test
636 public void testNullInterfaces() {
637 reset(interfaceService);
638 expect(interfaceService.getInterfaces()).andReturn(
639 Sets.<Interface>newHashSet()).anyTimes();
640 expect(interfaceService.getInterface(s2Eth1))
641 .andReturn(null).anyTimes();
642 expect(interfaceService.getInterface(s1Eth1))
643 .andReturn(null).anyTimes();
644 replay(interfaceService);
645
646 reset(configInfoService);
647 expect(configInfoService.getBgpPeers()).andReturn(peers).anyTimes();
648 expect(configInfoService.getBgpSpeakers()).andReturn(bgpSpeakers).anyTimes();
649 replay(configInfoService);
650
651 reset(intentService);
652 replay(intentService);
653 peerConnectivityManager.start();
654 verify(intentService);
655 }
656
657 /**
658 * Tests a corner case, when there are no BGP peers in the configuration.
659 */
660 @Test
661 public void testNullBgpPeers() {
662 reset(interfaceService);
663 expect(interfaceService.getInterfaces()).andReturn(
664 Sets.newHashSet(interfaces.values())).anyTimes();
665 replay(interfaceService);
666
667 reset(configInfoService);
668 expect(configInfoService.getBgpPeers()).andReturn(
669 new HashMap<IpAddress, BgpPeer>()).anyTimes();
670 expect(configInfoService.getBgpSpeakers()).andReturn(
671 bgpSpeakers).anyTimes();
672 replay(configInfoService);
673
674 reset(intentService);
675 replay(intentService);
676 peerConnectivityManager.start();
677 verify(intentService);
678 }
679
680 /**
681 * Tests a corner case, when there is no BGP speakers in the configuration.
682 */
683 @Test
684 public void testNullBgpSpeakers() {
685 reset(interfaceService);
686 expect(interfaceService.getInterfaces()).andReturn(
687 Sets.newHashSet(interfaces.values())).anyTimes();
688 replay(interfaceService);
689
690 reset(configInfoService);
691 expect(configInfoService.getBgpPeers()).andReturn(
692 peers).anyTimes();
693 expect(configInfoService.getBgpSpeakers()).andReturn(
694 null).anyTimes();
695 replay(configInfoService);
696
697 reset(intentService);
698 replay(intentService);
699 peerConnectivityManager.start();
700 verify(intentService);
701 }
702
703 /**
704 * Tests a corner case, when there is no Interface configured for one BGP
705 * peer.
706 */
707 @Test
708 public void testNoPeerInterface() {
709 String peerSw100Eth1 = "192.168.200.1";
710 configuredPeers.put(IpAddress.valueOf(peerSw100Eth1),
711 new BgpPeer("00:00:00:00:00:00:01:00", 1, peerSw100Eth1));
712 testConnectionSetup();
713 }
714
715 /**
716 * Tests a corner case, when there is no Interface configured for one BGP
717 * speaker.
718 * TODO: we should add a configuration correctness checking module/method
719 * before testing this corner case.
720 */
721 @Ignore
722 @Test
723 public void testNoSpeakerInterface() {
724 BgpSpeaker bgpSpeaker100 = new BgpSpeaker(
725 "bgpSpeaker100",
726 "00:00:00:00:00:00:01:00", 100,
727 "00:00:00:00:01:00");
728 List<InterfaceAddress> interfaceAddresses100 =
729 new LinkedList<InterfaceAddress>();
730 interfaceAddresses100.add(new InterfaceAddress(dpid1, 1, "192.168.10.201"));
731 interfaceAddresses100.add(new InterfaceAddress(dpid2, 1, "192.168.20.201"));
732 bgpSpeaker100.setInterfaceAddresses(interfaceAddresses100);
733 configuredBgpSpeakers.put(bgpSpeaker100.name(), bgpSpeaker100);
734 testConnectionSetup();
735 }
736}