blob: fe5fc3d4a94a4ca231a1f51f99679f5e66f71da9 [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 */
Brian O'Connorabafb502014-12-02 22:26:20 -080016package org.onosproject.sdnip;
Jonathan Hartce430a42014-10-16 20:44:29 -070017
Jonathan Hart51372182014-12-03 21:32:34 -080018import static org.easymock.EasyMock.createMock;
19import static org.easymock.EasyMock.expect;
20import static org.easymock.EasyMock.replay;
21import static org.easymock.EasyMock.reset;
22import static org.easymock.EasyMock.verify;
23
24import java.util.ArrayList;
25import java.util.Collections;
26import java.util.HashMap;
27import java.util.LinkedList;
28import java.util.List;
29import java.util.Map;
30
Jonathan Hartce430a42014-10-16 20:44:29 -070031import org.junit.Before;
32import org.junit.Ignore;
33import org.junit.Test;
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -080034import org.onlab.junit.TestUtils;
35import org.onlab.junit.TestUtils.TestUtilsException;
Jonathan Hart51372182014-12-03 21:32:34 -080036import org.onlab.packet.Ethernet;
37import org.onlab.packet.IPv4;
38import org.onlab.packet.IpAddress;
39import org.onlab.packet.IpPrefix;
40import org.onlab.packet.MacAddress;
Brian O'Connorabafb502014-12-02 22:26:20 -080041import org.onosproject.core.ApplicationId;
42import org.onosproject.net.ConnectPoint;
43import org.onosproject.net.DeviceId;
44import org.onosproject.net.PortNumber;
45import org.onosproject.net.flow.DefaultTrafficSelector;
46import org.onosproject.net.flow.DefaultTrafficTreatment;
47import org.onosproject.net.flow.TrafficSelector;
48import org.onosproject.net.flow.TrafficTreatment;
49import org.onosproject.net.host.InterfaceIpAddress;
50import org.onosproject.net.intent.AbstractIntentTest;
51import org.onosproject.net.intent.Intent;
52import org.onosproject.net.intent.IntentOperations;
53import org.onosproject.net.intent.IntentService;
54import org.onosproject.net.intent.PointToPointIntent;
55import org.onosproject.sdnip.bgp.BgpConstants;
56import org.onosproject.sdnip.config.BgpPeer;
57import org.onosproject.sdnip.config.BgpSpeaker;
58import org.onosproject.sdnip.config.Interface;
59import org.onosproject.sdnip.config.InterfaceAddress;
60import org.onosproject.sdnip.config.SdnIpConfigurationService;
Jonathan Hartce430a42014-10-16 20:44:29 -070061
Jonathan Hart51372182014-12-03 21:32:34 -080062import com.google.common.collect.Sets;
Jonathan Hartce430a42014-10-16 20:44:29 -070063
64/**
Jonathan Hart51372182014-12-03 21:32:34 -080065 * Unit tests for PeerConnectivityManager.
Jonathan Hartce430a42014-10-16 20:44:29 -070066 */
Brian O'Connor520c0522014-11-23 23:50:47 -080067public class PeerConnectivityManagerTest extends AbstractIntentTest {
Jonathan Hartce430a42014-10-16 20:44:29 -070068
Thomas Vachuskab97cf282014-10-20 23:31:12 -070069 private static final ApplicationId APPID = new ApplicationId() {
70 @Override
71 public short id() {
72 return 0;
73 }
74
75 @Override
76 public String name() {
77 return "foo";
78 }
79 };
80
Jonathan Hartce430a42014-10-16 20:44:29 -070081 private PeerConnectivityManager peerConnectivityManager;
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -080082 private IntentSynchronizer intentSynchronizer;
Jonathan Hart9965d772014-12-02 10:28:34 -080083 private SdnIpConfigurationService configInfoService;
Jonathan Hartce430a42014-10-16 20:44:29 -070084 private InterfaceService interfaceService;
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -080085 private IntentService intentService;
Jonathan Hartce430a42014-10-16 20:44:29 -070086
87 private Map<String, BgpSpeaker> bgpSpeakers;
88 private Map<String, Interface> interfaces;
89 private Map<IpAddress, BgpPeer> peers;
90
91 private Map<String, BgpSpeaker> configuredBgpSpeakers;
92 private Map<String, Interface> configuredInterfaces;
93 private Map<IpAddress, BgpPeer> configuredPeers;
94 private List<PointToPointIntent> intentList;
95
96 private final String dpid1 = "00:00:00:00:00:00:00:01";
97 private final String dpid2 = "00:00:00:00:00:00:00:02";
98
99 private final DeviceId deviceId1 =
100 DeviceId.deviceId(SdnIp.dpidToUri(dpid1));
101 private final DeviceId deviceId2 =
102 DeviceId.deviceId(SdnIp.dpidToUri(dpid2));
103
104 // Interfaces connected to BGP speakers
105 private final ConnectPoint s1Eth100 =
106 new ConnectPoint(deviceId1, PortNumber.portNumber(100));
107 private final ConnectPoint s2Eth100 =
108 new ConnectPoint(deviceId2, PortNumber.portNumber(100));
109
110 // Interfaces connected to BGP peers
111 private final ConnectPoint s1Eth1 =
112 new ConnectPoint(deviceId1, PortNumber.portNumber(1));
113 private final ConnectPoint s2Eth1 =
114 new ConnectPoint(deviceId2, PortNumber.portNumber(1));
115
Jonathan Hartce430a42014-10-16 20:44:29 -0700116 private final TrafficTreatment noTreatment =
117 DefaultTrafficTreatment.builder().build();
118
119 @Before
120 public void setUp() throws Exception {
Brian O'Connor520c0522014-11-23 23:50:47 -0800121 super.setUp();
Jonathan Hartce430a42014-10-16 20:44:29 -0700122 bgpSpeakers = Collections.unmodifiableMap(setUpBgpSpeakers());
123 interfaces = Collections.unmodifiableMap(setUpInterfaces());
124 peers = Collections.unmodifiableMap(setUpPeers());
125
126 initPeerConnectivity();
127 intentList = setUpIntentList();
128 }
129
130 /**
131 * Sets up BGP speakers.
132 *
133 * @return configured BGP speakers as a map from speaker name to speaker
134 */
135 private Map<String, BgpSpeaker> setUpBgpSpeakers() {
136
137 configuredBgpSpeakers = new HashMap<>();
138
139 BgpSpeaker bgpSpeaker1 = new BgpSpeaker(
140 "bgpSpeaker1",
141 "00:00:00:00:00:00:00:01", 100,
142 "00:00:00:00:00:01");
143 List<InterfaceAddress> interfaceAddresses1 =
144 new LinkedList<InterfaceAddress>();
145 interfaceAddresses1.add(new InterfaceAddress(dpid1, 1, "192.168.10.101"));
146 interfaceAddresses1.add(new InterfaceAddress(dpid2, 1, "192.168.20.101"));
147 bgpSpeaker1.setInterfaceAddresses(interfaceAddresses1);
148 configuredBgpSpeakers.put(bgpSpeaker1.name(), bgpSpeaker1);
149
150 // BGP speaker2 is attached to the same switch port with speaker1
151 BgpSpeaker bgpSpeaker2 = new BgpSpeaker(
152 "bgpSpeaker2",
153 "00:00:00:00:00:00:00:01", 100,
154 "00:00:00:00:00:02");
155 List<InterfaceAddress> interfaceAddresses2 =
156 new LinkedList<InterfaceAddress>();
157 interfaceAddresses2.add(new InterfaceAddress(dpid1, 1, "192.168.10.102"));
158 interfaceAddresses2.add(new InterfaceAddress(dpid2, 1, "192.168.20.102"));
159 bgpSpeaker2.setInterfaceAddresses(interfaceAddresses2);
160 configuredBgpSpeakers.put(bgpSpeaker2.name(), bgpSpeaker2);
161
162 BgpSpeaker bgpSpeaker3 = new BgpSpeaker(
163 "bgpSpeaker3",
164 "00:00:00:00:00:00:00:02", 100,
165 "00:00:00:00:00:03");
166 List<InterfaceAddress> interfaceAddresses3 =
167 new LinkedList<InterfaceAddress>();
168 interfaceAddresses3.add(new InterfaceAddress(dpid1, 1, "192.168.10.103"));
169 interfaceAddresses3.add(new InterfaceAddress(dpid2, 1, "192.168.20.103"));
170 bgpSpeaker3.setInterfaceAddresses(interfaceAddresses3);
171 configuredBgpSpeakers.put(bgpSpeaker3.name(), bgpSpeaker3);
172
173 return configuredBgpSpeakers;
174 }
175
176 /**
177 * Sets up logical interfaces, which emulate the configured interfaces
178 * in SDN-IP application.
179 *
180 * @return configured interfaces as a MAP from Interface name to Interface
181 */
182 private Map<String, Interface> setUpInterfaces() {
183
184 configuredInterfaces = new HashMap<>();
185
186 String interfaceSw1Eth1 = "s1-eth1";
Pavlin Radoslavov76b0ae22014-10-27 15:33:19 -0700187 InterfaceIpAddress ia1 =
188 new InterfaceIpAddress(IpAddress.valueOf("192.168.10.1"),
189 IpPrefix.valueOf("192.168.10.0/24"));
Jonathan Hartce430a42014-10-16 20:44:29 -0700190 Interface intfsw1eth1 = new Interface(s1Eth1,
Pavlin Radoslavov76b0ae22014-10-27 15:33:19 -0700191 Collections.singleton(ia1),
Jonathan Hartce430a42014-10-16 20:44:29 -0700192 MacAddress.valueOf("00:00:00:00:00:01"));
193
194 configuredInterfaces.put(interfaceSw1Eth1, intfsw1eth1);
195 String interfaceSw2Eth1 = "s2-eth1";
Pavlin Radoslavov76b0ae22014-10-27 15:33:19 -0700196 InterfaceIpAddress ia2 =
197 new InterfaceIpAddress(IpAddress.valueOf("192.168.20.2"),
198 IpPrefix.valueOf("192.168.20.0/24"));
Jonathan Hartce430a42014-10-16 20:44:29 -0700199 Interface intfsw2eth1 = new Interface(s2Eth1,
Pavlin Radoslavov76b0ae22014-10-27 15:33:19 -0700200 Collections.singleton(ia2),
Jonathan Hartce430a42014-10-16 20:44:29 -0700201 MacAddress.valueOf("00:00:00:00:00:02"));
202 configuredInterfaces.put(interfaceSw2Eth1, intfsw2eth1);
203
204 interfaceService = createMock(InterfaceService.class);
205
206 expect(interfaceService.getInterface(s1Eth1))
207 .andReturn(intfsw1eth1).anyTimes();
208 expect(interfaceService.getInterface(s2Eth1))
209 .andReturn(intfsw2eth1).anyTimes();
210
211 // Non-existent interface used during one of the tests
212 expect(interfaceService.getInterface(new ConnectPoint(
213 DeviceId.deviceId(SdnIp.dpidToUri("00:00:00:00:00:00:01:00")),
214 PortNumber.portNumber(1))))
215 .andReturn(null).anyTimes();
216
217 expect(interfaceService.getInterfaces()).andReturn(
218 Sets.newHashSet(configuredInterfaces.values())).anyTimes();
219 replay(interfaceService);
220
221 return configuredInterfaces;
222 }
223
224 /**
225 * Sets up BGP daemon peers.
226 *
227 * @return configured BGP peers as a MAP from peer IP address to BgpPeer
228 */
229 private Map<IpAddress, BgpPeer> setUpPeers() {
230
231 configuredPeers = new HashMap<>();
232
233 String peerSw1Eth1 = "192.168.10.1";
234 configuredPeers.put(IpAddress.valueOf(peerSw1Eth1),
235 new BgpPeer(dpid1, 1, peerSw1Eth1));
236
237 // Two BGP peers are connected to switch 2 port 1.
238 String peer1Sw2Eth1 = "192.168.20.1";
239 configuredPeers.put(IpAddress.valueOf(peer1Sw2Eth1),
240 new BgpPeer(dpid2, 1, peer1Sw2Eth1));
241
242 String peer2Sw2Eth1 = "192.168.20.2";
243 configuredPeers.put(IpAddress.valueOf(peer2Sw2Eth1),
244 new BgpPeer(dpid2, 1, peer2Sw2Eth1));
245
246 return configuredPeers;
247 }
248
249 /**
250 * Sets up expected point to point intent list.
251 *
252 * @return point to point intent list
253 */
254 private List<PointToPointIntent> setUpIntentList() {
255
256 intentList = new ArrayList<PointToPointIntent>();
257
258 setUpBgpIntents();
259 setUpIcmpIntents();
260
261 return intentList;
262
263 }
264
265 /**
266 * Constructs a BGP intent and put it into the intentList.
267 * <p/>
268 * The purpose of this method is too simplify the setUpBgpIntents() method,
269 * and to make the setUpBgpIntents() easy to read.
270 *
271 * @param srcPrefix source IP prefix to match
272 * @param dstPrefix destination IP prefix to match
273 * @param srcTcpPort source TCP port to match
274 * @param dstTcpPort destination TCP port to match
275 * @param srcConnectPoint source connect point for PointToPointIntent
276 * @param dstConnectPoint destination connect point for PointToPointIntent
277 */
278 private void bgpPathintentConstructor(String srcPrefix, String dstPrefix,
279 Short srcTcpPort, Short dstTcpPort,
280 ConnectPoint srcConnectPoint, ConnectPoint dstConnectPoint) {
281
282 TrafficSelector.Builder builder = DefaultTrafficSelector.builder()
283 .matchEthType(Ethernet.TYPE_IPV4)
284 .matchIPProtocol(IPv4.PROTOCOL_TCP)
285 .matchIPSrc(IpPrefix.valueOf(srcPrefix))
286 .matchIPDst(IpPrefix.valueOf(dstPrefix));
287
288 if (srcTcpPort != null) {
289 builder.matchTcpSrc(srcTcpPort);
290 }
291 if (dstTcpPort != null) {
292 builder.matchTcpDst(dstTcpPort);
293 }
294
295 PointToPointIntent intent = new PointToPointIntent(
Thomas Vachuskab97cf282014-10-20 23:31:12 -0700296 APPID, builder.build(), noTreatment,
Jonathan Hartce430a42014-10-16 20:44:29 -0700297 srcConnectPoint, dstConnectPoint);
298
299 intentList.add(intent);
300 }
301
302 /**
303 * Sets up intents for BGP paths.
304 */
305 private void setUpBgpIntents() {
306
307 Short bgpPort = Short.valueOf((short) BgpConstants.BGP_PORT);
308
309 // Start to build intents between BGP speaker1 and BGP peer1
310 bgpPathintentConstructor(
311 "192.168.10.101/32", "192.168.10.1/32", null, bgpPort,
312 s1Eth100, s1Eth1);
313 bgpPathintentConstructor(
314 "192.168.10.101/32", "192.168.10.1/32", bgpPort, null,
315 s1Eth100, s1Eth1);
316
317 bgpPathintentConstructor(
318 "192.168.10.1/32", "192.168.10.101/32", null, bgpPort,
319 s1Eth1, s1Eth100);
320 bgpPathintentConstructor(
321 "192.168.10.1/32", "192.168.10.101/32", bgpPort, null,
322 s1Eth1, s1Eth100);
323
324 // Start to build intents between BGP speaker1 and BGP peer2
325 bgpPathintentConstructor(
326 "192.168.20.101/32", "192.168.20.1/32", null, bgpPort,
327 s1Eth100, s2Eth1);
328 bgpPathintentConstructor(
329 "192.168.20.101/32", "192.168.20.1/32", bgpPort, null,
330 s1Eth100, s2Eth1);
331
332 bgpPathintentConstructor(
333 "192.168.20.1/32", "192.168.20.101/32", null, bgpPort,
334 s2Eth1, s1Eth100);
335 bgpPathintentConstructor(
336 "192.168.20.1/32", "192.168.20.101/32", bgpPort, null,
337 s2Eth1, s1Eth100);
338
339 // Start to build intents between BGP speaker1 and BGP peer3
340 bgpPathintentConstructor(
341 "192.168.20.101/32", "192.168.20.2/32", null, bgpPort,
342 s1Eth100, s2Eth1);
343 bgpPathintentConstructor(
344 "192.168.20.101/32", "192.168.20.2/32", bgpPort, null,
345 s1Eth100, s2Eth1);
346
347 bgpPathintentConstructor(
348 "192.168.20.2/32", "192.168.20.101/32", null, bgpPort,
349 s2Eth1, s1Eth100);
350 bgpPathintentConstructor(
351 "192.168.20.2/32", "192.168.20.101/32", bgpPort, null,
352 s2Eth1, s1Eth100);
353
354 //
355 // Start to build intents between BGP speaker2 and BGP peer1
356 bgpPathintentConstructor(
357 "192.168.10.102/32", "192.168.10.1/32", null, bgpPort,
358 s1Eth100, s1Eth1);
359 bgpPathintentConstructor(
360 "192.168.10.102/32", "192.168.10.1/32", bgpPort, null,
361 s1Eth100, s1Eth1);
362
363 bgpPathintentConstructor(
364 "192.168.10.1/32", "192.168.10.102/32", null, bgpPort,
365 s1Eth1, s1Eth100);
366 bgpPathintentConstructor(
367 "192.168.10.1/32", "192.168.10.102/32", bgpPort, null,
368 s1Eth1, s1Eth100);
369 // Start to build intents between BGP speaker2 and BGP peer2
370 bgpPathintentConstructor(
371 "192.168.20.102/32", "192.168.20.1/32", null, bgpPort,
372 s1Eth100, s2Eth1);
373 bgpPathintentConstructor(
374 "192.168.20.102/32", "192.168.20.1/32", bgpPort, null,
375 s1Eth100, s2Eth1);
376
377 bgpPathintentConstructor(
378 "192.168.20.1/32", "192.168.20.102/32", null, bgpPort,
379 s2Eth1, s1Eth100);
380 bgpPathintentConstructor(
381 "192.168.20.1/32", "192.168.20.102/32", bgpPort, null,
382 s2Eth1, s1Eth100);
383
384 // Start to build intents between BGP speaker2 and BGP peer3
385 bgpPathintentConstructor(
386 "192.168.20.102/32", "192.168.20.2/32", null, bgpPort,
387 s1Eth100, s2Eth1);
388 bgpPathintentConstructor(
389 "192.168.20.102/32", "192.168.20.2/32", bgpPort, null,
390 s1Eth100, s2Eth1);
391
392 bgpPathintentConstructor(
393 "192.168.20.2/32", "192.168.20.102/32", null, bgpPort,
394 s2Eth1, s1Eth100);
395 bgpPathintentConstructor(
396 "192.168.20.2/32", "192.168.20.102/32", bgpPort, null,
397 s2Eth1, s1Eth100);
398
399 //
400 // Start to build intents between BGP speaker3 and BGP peer1
401 bgpPathintentConstructor(
402 "192.168.10.103/32", "192.168.10.1/32", null, bgpPort,
403 s2Eth100, s1Eth1);
404 bgpPathintentConstructor(
405 "192.168.10.103/32", "192.168.10.1/32", bgpPort, null,
406 s2Eth100, s1Eth1);
407
408 bgpPathintentConstructor(
409 "192.168.10.1/32", "192.168.10.103/32", null, bgpPort,
410 s1Eth1, s2Eth100);
411 bgpPathintentConstructor(
412 "192.168.10.1/32", "192.168.10.103/32", bgpPort, null,
413 s1Eth1, s2Eth100);
414
415 // Start to build intents between BGP speaker3 and BGP peer2
416 bgpPathintentConstructor(
417 "192.168.20.103/32", "192.168.20.1/32", null, bgpPort,
418 s2Eth100, s2Eth1);
419 bgpPathintentConstructor(
420 "192.168.20.103/32", "192.168.20.1/32", bgpPort, null,
421 s2Eth100, s2Eth1);
422
423 bgpPathintentConstructor(
424 "192.168.20.1/32", "192.168.20.103/32", null, bgpPort,
425 s2Eth1, s2Eth100);
426 bgpPathintentConstructor(
427 "192.168.20.1/32", "192.168.20.103/32", bgpPort, null,
428 s2Eth1, s2Eth100);
429
430 // Start to build intents between BGP speaker3 and BGP peer3
431 bgpPathintentConstructor(
432 "192.168.20.103/32", "192.168.20.2/32", null, bgpPort,
433 s2Eth100, s2Eth1);
434 bgpPathintentConstructor(
435 "192.168.20.103/32", "192.168.20.2/32", bgpPort, null,
436 s2Eth100, s2Eth1);
437
438 bgpPathintentConstructor(
439 "192.168.20.2/32", "192.168.20.103/32", null, bgpPort,
440 s2Eth1, s2Eth100);
441 bgpPathintentConstructor(
442 "192.168.20.2/32", "192.168.20.103/32", bgpPort, null,
443 s2Eth1, s2Eth100);
444 }
445
446 /**
447 * Constructs a BGP intent and put it into the intentList.
448 * <p/>
449 * The purpose of this method is too simplify the setUpBgpIntents() method,
450 * and to make the setUpBgpIntents() easy to read.
451 *
452 * @param srcPrefix source IP prefix to match
453 * @param dstPrefix destination IP prefix to match
454 * @param srcConnectPoint source connect point for PointToPointIntent
455 * @param dstConnectPoint destination connect point for PointToPointIntent
456 */
457 private void icmpPathintentConstructor(String srcPrefix, String dstPrefix,
458 ConnectPoint srcConnectPoint, ConnectPoint dstConnectPoint) {
459
460 TrafficSelector selector = DefaultTrafficSelector.builder()
461 .matchEthType(Ethernet.TYPE_IPV4)
462 .matchIPProtocol(IPv4.PROTOCOL_ICMP)
463 .matchIPSrc(IpPrefix.valueOf(srcPrefix))
464 .matchIPDst(IpPrefix.valueOf(dstPrefix))
465 .build();
466
467 PointToPointIntent intent = new PointToPointIntent(
Thomas Vachuskab97cf282014-10-20 23:31:12 -0700468 APPID, selector, noTreatment,
Jonathan Hartce430a42014-10-16 20:44:29 -0700469 srcConnectPoint, dstConnectPoint);
470
471 intentList.add(intent);
472 }
473
474 /**
475 * Sets up intents for ICMP paths.
476 */
477 private void setUpIcmpIntents() {
478
479 // Start to build intents between BGP speaker1 and BGP peer1
480 icmpPathintentConstructor(
481 "192.168.10.101/32", "192.168.10.1/32", s1Eth100, s1Eth1);
482 icmpPathintentConstructor(
483 "192.168.10.1/32", "192.168.10.101/32", s1Eth1, s1Eth100);
484
485 // Start to build intents between BGP speaker1 and BGP peer2
486 icmpPathintentConstructor(
487 "192.168.20.101/32", "192.168.20.1/32", s1Eth100, s2Eth1);
488 icmpPathintentConstructor(
489 "192.168.20.1/32", "192.168.20.101/32", s2Eth1, s1Eth100);
490
491 // Start to build intents between BGP speaker1 and BGP peer3
492 icmpPathintentConstructor(
493 "192.168.20.101/32", "192.168.20.2/32", s1Eth100, s2Eth1);
494 icmpPathintentConstructor(
495 "192.168.20.2/32", "192.168.20.101/32", s2Eth1, s1Eth100);
496
497 //
498 // Start to build intents between BGP speaker2 and BGP peer1
499 icmpPathintentConstructor(
500 "192.168.10.102/32", "192.168.10.1/32", s1Eth100, s1Eth1);
501 icmpPathintentConstructor(
502 "192.168.10.1/32", "192.168.10.102/32", s1Eth1, s1Eth100);
503
504 // Start to build intents between BGP speaker2 and BGP peer2
505 icmpPathintentConstructor(
506 "192.168.20.102/32", "192.168.20.1/32", s1Eth100, s2Eth1);
507 icmpPathintentConstructor(
508 "192.168.20.1/32", "192.168.20.102/32", s2Eth1, s1Eth100);
509
510 // Start to build intents between BGP speaker2 and BGP peer3
511 icmpPathintentConstructor(
512 "192.168.20.102/32", "192.168.20.2/32", s1Eth100, s2Eth1);
513 icmpPathintentConstructor(
514 "192.168.20.2/32", "192.168.20.102/32", s2Eth1, s1Eth100);
515
516 //
517 // Start to build intents between BGP speaker3 and BGP peer1
518 icmpPathintentConstructor(
519 "192.168.10.103/32", "192.168.10.1/32", s2Eth100, s1Eth1);
520 icmpPathintentConstructor(
521 "192.168.10.1/32", "192.168.10.103/32", s1Eth1, s2Eth100);
522
523 // Start to build intents between BGP speaker3 and BGP peer2
524 icmpPathintentConstructor(
525 "192.168.20.103/32", "192.168.20.1/32", s2Eth100, s2Eth1);
526 icmpPathintentConstructor(
527 "192.168.20.1/32", "192.168.20.103/32", s2Eth1, s2Eth100);
528
529 // Start to build intents between BGP speaker3 and BGP peer3
530 icmpPathintentConstructor(
531 "192.168.20.103/32", "192.168.20.2/32", s2Eth100, s2Eth1);
532 icmpPathintentConstructor(
533 "192.168.20.2/32", "192.168.20.103/32", s2Eth1, s2Eth100);
534
535 }
536
537 /**
538 * Initializes peer connectivity testing environment.
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800539 *
540 * @throws TestUtilsException if exceptions when using TestUtils
Jonathan Hartce430a42014-10-16 20:44:29 -0700541 */
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800542 private void initPeerConnectivity() throws TestUtilsException {
Jonathan Hartce430a42014-10-16 20:44:29 -0700543
Jonathan Hart9965d772014-12-02 10:28:34 -0800544 configInfoService = createMock(SdnIpConfigurationService.class);
Jonathan Hartce430a42014-10-16 20:44:29 -0700545 expect(configInfoService.getBgpPeers()).andReturn(peers).anyTimes();
546 expect(configInfoService.getBgpSpeakers()).andReturn(bgpSpeakers).anyTimes();
547 replay(configInfoService);
548
549 intentService = createMock(IntentService.class);
550 replay(intentService);
551
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800552 intentSynchronizer = new IntentSynchronizer(APPID, intentService);
553 intentSynchronizer.leaderChanged(true);
554 TestUtils.setField(intentSynchronizer, "isActivatedLeader", true);
555
556 peerConnectivityManager =
557 new PeerConnectivityManager(APPID, intentSynchronizer,
558 configInfoService, interfaceService);
Jonathan Hartce430a42014-10-16 20:44:29 -0700559 }
560
Jonathan Hartce430a42014-10-16 20:44:29 -0700561 /**
562 * Tests whether peer connectivity manager can set up correct BGP and
563 * ICMP intents according to specific configuration.
564 * <p/>
565 * Two tricky cases included in the configuration are: 2 peers on a same
566 * switch port, peer on the same switch with BGPd.
567 */
568 @Test
569 public void testConnectionSetup() {
570
571 reset(intentService);
572
Pavlin Radoslavovdde22ae2014-11-24 11:47:17 -0800573 // Setup the expected intents
Pavlin Radoslavov248c2ae2014-12-02 09:51:25 -0800574 IntentOperations.Builder builder = IntentOperations.builder(APPID);
Pavlin Radoslavovdde22ae2014-11-24 11:47:17 -0800575 for (Intent intent : intentList) {
576 builder.addSubmitOperation(intent);
Jonathan Hartce430a42014-10-16 20:44:29 -0700577 }
Pavlin Radoslavovdde22ae2014-11-24 11:47:17 -0800578 intentService.execute(TestIntentServiceHelper.eqExceptId(
579 builder.build()));
Jonathan Hartce430a42014-10-16 20:44:29 -0700580 replay(intentService);
581
582 // Running the interface to be tested.
583 peerConnectivityManager.start();
584
585 verify(intentService);
586
587 }
588
589 /**
590 * Tests a corner case, when there are no interfaces in the configuration.
591 */
592 @Test
593 public void testNullInterfaces() {
594 reset(interfaceService);
595 expect(interfaceService.getInterfaces()).andReturn(
596 Sets.<Interface>newHashSet()).anyTimes();
597 expect(interfaceService.getInterface(s2Eth1))
598 .andReturn(null).anyTimes();
599 expect(interfaceService.getInterface(s1Eth1))
600 .andReturn(null).anyTimes();
601 replay(interfaceService);
602
603 reset(configInfoService);
604 expect(configInfoService.getBgpPeers()).andReturn(peers).anyTimes();
605 expect(configInfoService.getBgpSpeakers()).andReturn(bgpSpeakers).anyTimes();
606 replay(configInfoService);
607
608 reset(intentService);
Pavlin Radoslavov248c2ae2014-12-02 09:51:25 -0800609 IntentOperations.Builder builder = IntentOperations.builder(APPID);
610 intentService.execute(builder.build());
Jonathan Hartce430a42014-10-16 20:44:29 -0700611 replay(intentService);
612 peerConnectivityManager.start();
613 verify(intentService);
614 }
615
616 /**
617 * Tests a corner case, when there are no BGP peers in the configuration.
618 */
619 @Test
620 public void testNullBgpPeers() {
621 reset(interfaceService);
622 expect(interfaceService.getInterfaces()).andReturn(
623 Sets.newHashSet(interfaces.values())).anyTimes();
624 replay(interfaceService);
625
626 reset(configInfoService);
627 expect(configInfoService.getBgpPeers()).andReturn(
628 new HashMap<IpAddress, BgpPeer>()).anyTimes();
629 expect(configInfoService.getBgpSpeakers()).andReturn(
630 bgpSpeakers).anyTimes();
631 replay(configInfoService);
632
633 reset(intentService);
Pavlin Radoslavov248c2ae2014-12-02 09:51:25 -0800634 IntentOperations.Builder builder = IntentOperations.builder(APPID);
635 intentService.execute(builder.build());
Jonathan Hartce430a42014-10-16 20:44:29 -0700636 replay(intentService);
637 peerConnectivityManager.start();
638 verify(intentService);
639 }
640
641 /**
642 * Tests a corner case, when there is no BGP speakers in the configuration.
643 */
644 @Test
645 public void testNullBgpSpeakers() {
646 reset(interfaceService);
647 expect(interfaceService.getInterfaces()).andReturn(
648 Sets.newHashSet(interfaces.values())).anyTimes();
649 replay(interfaceService);
650
651 reset(configInfoService);
652 expect(configInfoService.getBgpPeers()).andReturn(
653 peers).anyTimes();
654 expect(configInfoService.getBgpSpeakers()).andReturn(
Jonathan Hart51372182014-12-03 21:32:34 -0800655 Collections.emptyMap()).anyTimes();
Jonathan Hartce430a42014-10-16 20:44:29 -0700656 replay(configInfoService);
657
658 reset(intentService);
Jonathan Hart51372182014-12-03 21:32:34 -0800659 IntentOperations.Builder builder = IntentOperations.builder(APPID);
660 intentService.execute(builder.build());
Jonathan Hartce430a42014-10-16 20:44:29 -0700661 replay(intentService);
662 peerConnectivityManager.start();
663 verify(intentService);
664 }
665
666 /**
667 * Tests a corner case, when there is no Interface configured for one BGP
668 * peer.
669 */
670 @Test
671 public void testNoPeerInterface() {
672 String peerSw100Eth1 = "192.168.200.1";
673 configuredPeers.put(IpAddress.valueOf(peerSw100Eth1),
674 new BgpPeer("00:00:00:00:00:00:01:00", 1, peerSw100Eth1));
675 testConnectionSetup();
676 }
677
678 /**
679 * Tests a corner case, when there is no Interface configured for one BGP
680 * speaker.
681 * TODO: we should add a configuration correctness checking module/method
682 * before testing this corner case.
683 */
684 @Ignore
685 @Test
686 public void testNoSpeakerInterface() {
687 BgpSpeaker bgpSpeaker100 = new BgpSpeaker(
688 "bgpSpeaker100",
689 "00:00:00:00:00:00:01:00", 100,
690 "00:00:00:00:01:00");
691 List<InterfaceAddress> interfaceAddresses100 =
692 new LinkedList<InterfaceAddress>();
693 interfaceAddresses100.add(new InterfaceAddress(dpid1, 1, "192.168.10.201"));
694 interfaceAddresses100.add(new InterfaceAddress(dpid2, 1, "192.168.20.201"));
695 bgpSpeaker100.setInterfaceAddresses(interfaceAddresses100);
696 configuredBgpSpeakers.put(bgpSpeaker100.name(), bgpSpeaker100);
697 testConnectionSetup();
698 }
699}