blob: de8d1c1dfd90540638940bc7c04834343904be5e [file] [log] [blame]
Shashikanth VH1ca26ce2015-11-20 23:19:49 +05301/*
2 * Copyright 2014-2015 Open Networking Laboratory
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
5 * the License. You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
10 * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
11 * specific language governing permissions and limitations under the License.
12 */
13package org.onosproject.provider.bgp.topology.impl;
14
15import static org.junit.Assert.assertNotNull;
16import static org.junit.Assert.assertNull;
17import static org.junit.Assert.assertTrue;
18
19import java.util.Collection;
20import java.util.HashSet;
21import java.util.LinkedList;
22import java.util.List;
23import java.util.Map;
24import java.util.Set;
25import java.util.concurrent.CopyOnWriteArraySet;
26
27import org.junit.After;
28import org.junit.Before;
29import org.junit.Test;
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053030import org.onosproject.bgp.controller.BgpCfg;
31import org.onosproject.bgp.controller.BgpController;
32import org.onosproject.bgp.controller.BgpId;
33import org.onosproject.bgp.controller.BgpPeer;
Shashikanth VH1ca26ce2015-11-20 23:19:49 +053034import org.onosproject.bgp.controller.BgpLinkListener;
35import org.onosproject.bgp.controller.BgpNodeListener;
36import org.onosproject.bgp.controller.BgpPeerManager;
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053037import org.onosproject.bgpio.exceptions.BgpParseException;
38import org.onosproject.bgpio.protocol.BgpMessage;
39import org.onosproject.bgpio.protocol.linkstate.BgpLinkLSIdentifier;
40import org.onosproject.bgpio.protocol.linkstate.BgpNodeLSIdentifier;
41import org.onosproject.bgpio.protocol.linkstate.BgpNodeLSNlriVer4;
Shashikanth VH1ca26ce2015-11-20 23:19:49 +053042import org.onosproject.bgpio.protocol.linkstate.BgpLinkLsNlriVer4;
43import org.onosproject.bgpio.protocol.linkstate.NodeDescriptors;
44import org.onosproject.bgpio.types.AutonomousSystemTlv;
45import org.onosproject.bgpio.types.LinkLocalRemoteIdentifiersTlv;
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053046import org.onosproject.bgpio.types.BgpValueType;
Shashikanth VH1ca26ce2015-11-20 23:19:49 +053047import org.onosproject.bgpio.types.RouteDistinguisher;
48import org.onosproject.bgpio.util.Constants;
49import org.onosproject.net.ConnectPoint;
50import org.onosproject.net.DeviceId;
51import org.onosproject.net.MastershipRole;
52import org.onosproject.net.device.DeviceDescription;
53import org.onosproject.net.device.DeviceProvider;
54import org.onosproject.net.device.DeviceProviderRegistry;
55import org.onosproject.net.device.DeviceProviderService;
56import org.onosproject.net.device.PortDescription;
57import org.onosproject.net.device.PortStatistics;
58import org.onosproject.net.link.LinkDescription;
59import org.onosproject.net.link.LinkProvider;
60import org.onosproject.net.link.LinkProviderRegistry;
61import org.onosproject.net.link.LinkProviderService;
62import org.onosproject.net.provider.ProviderId;
63
64public class BgpTopologyProviderTest {
65
66 private static final DeviceId DID1 = DeviceId
67 .deviceId("bgp:bgpls://0:direct:0/&=bgpnodelsidentifier%7bnodedescriptors=nodedescriptors%7bdestype=512,"
68 + "%20deslength=4,%20subtlvs=[autonomoussystemtlv%7btype=512,%20length=4,%20asnum=100%7d]%7d%7d");
69 private static final DeviceId DID2 = DeviceId
70 .deviceId("bgp:bgpls://0:direct:0/&=bgpnodelsidentifier%7bnodedescriptors=nodedescriptors%7bdestype=512,"
71 + "%20deslength=4,%20subtlvs=[autonomoussystemtlv%7btype=512,%20length=4,%20asnum=10%7d]%7d%7d");
72 private static final DeviceId DID3 = DeviceId
73 .deviceId("bgp:bgpls://direct:0/&=nodedescriptors%7bdestype=512,%20deslength=4,"
74 + "%20subtlvs=[autonomoussystemtlv%7btype=512,%20length=4,%20asnum=100%7d]%7d");
75 private final BgpTopologyProvider provider = new BgpTopologyProvider();
76 private final TestDeviceRegistry nodeRegistry = new TestDeviceRegistry();
77 private final TestLinkRegistry linkRegistry = new TestLinkRegistry();
78 private final TestController controller = new TestController();
79
80 @Before
81 public void startUp() {
82 provider.deviceProviderRegistry = nodeRegistry;
83 provider.linkProviderRegistry = linkRegistry;
84 provider.controller = controller;
85 provider.activate();
86 assertNotNull("provider should be registered", nodeRegistry.provider);
87 assertNotNull("provider should be registered", linkRegistry.provider);
88 assertNotNull("listener should be registered", controller.nodeListener);
89 }
90
91 @After
92 public void tearDown() {
93 provider.deactivate();
94 assertNull("listener should be removed", controller.nodeListener);
95 provider.controller = null;
96 provider.deviceProviderRegistry = null;
97 provider.linkProviderRegistry = null;
98 }
99
100 /* Class implement device test registry */
101 private class TestDeviceRegistry implements DeviceProviderRegistry {
102 DeviceProvider provider;
103
104 Set<DeviceId> connected = new HashSet<>();
105
106 @Override
107 public DeviceProviderService register(DeviceProvider provider) {
108 this.provider = provider;
109 return new TestProviderService();
110 }
111
112 @Override
113 public void unregister(DeviceProvider provider) {
114 }
115
116 @Override
117 public Set<ProviderId> getProviders() {
118 return null;
119 }
120
121 private class TestProviderService implements DeviceProviderService {
122
123 @Override
124 public DeviceProvider provider() {
125 return null;
126 }
127
128 @Override
129 public void deviceConnected(DeviceId deviceId, DeviceDescription deviceDescription) {
130 if (deviceId.equals(DID1)) {
131 connected.add(deviceId);
132 }
133 }
134
135 @Override
136 public void deviceDisconnected(DeviceId deviceId) {
137 if (deviceId.equals(DID1)) {
138 connected.remove(deviceId);
139 }
140 }
141
142 @Override
143 public void updatePorts(DeviceId deviceId, List<PortDescription> portDescriptions) {
144 // TODO Auto-generated method stub
145
146 }
147
148 @Override
149 public void portStatusChanged(DeviceId deviceId, PortDescription portDescription) {
150 // TODO Auto-generated method stub
151
152 }
153
154 @Override
155 public void receivedRoleReply(DeviceId deviceId, MastershipRole requested, MastershipRole response) {
156 // TODO Auto-generated method stub
157
158 }
159
160 @Override
161 public void updatePortStatistics(DeviceId deviceId, Collection<PortStatistics> portStatistics) {
162 // TODO Auto-generated method stub
163
164 }
165 }
166 }
167
168 /* class implement link test registery */
169 private class TestLinkRegistry implements LinkProviderRegistry {
170 LinkProvider provider;
171
172 Set<DeviceId> connected = new HashSet<>();
173
174 @Override
175 public LinkProviderService register(LinkProvider provider) {
176 this.provider = provider;
177 return new TestProviderService();
178 }
179
180 @Override
181 public void unregister(LinkProvider provider) {
182 }
183
184 @Override
185 public Set<ProviderId> getProviders() {
186 return null;
187 }
188
189 private class TestProviderService implements LinkProviderService {
190
191 @Override
192 public LinkProvider provider() {
193 // TODO Auto-generated method stub
194 return null;
195 }
196
197 @Override
198 public void linkDetected(LinkDescription linkDescription) {
199 if ((linkDescription.src().deviceId().equals(DID3))
200 && (linkDescription.dst().deviceId().equals(DID3))) {
201 connected.add(linkDescription.src().deviceId());
202 }
203 }
204
205 @Override
206 public void linkVanished(LinkDescription linkDescription) {
207 if ((linkDescription.src().deviceId().equals(DID3))
208 && (linkDescription.dst().deviceId().equals(DID3))) {
209 connected.remove(linkDescription.src().deviceId());
210 }
211 }
212
213 @Override
214 public void linksVanished(ConnectPoint connectPoint) {
215 // TODO Auto-generated method stub
216
217 }
218
219 @Override
220 public void linksVanished(DeviceId deviceId) {
221 connected.remove(deviceId);
222 }
223
224 }
225 }
226
227 /* class implement test controller */
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530228 private class TestController implements BgpController {
Shashikanth VH1ca26ce2015-11-20 23:19:49 +0530229 protected Set<BgpNodeListener> nodeListener = new CopyOnWriteArraySet<>();
230 protected Set<BgpLinkListener> linkListener = new CopyOnWriteArraySet<>();
231
232 @Override
233 public void addListener(BgpNodeListener nodeListener) {
234 this.nodeListener.add(nodeListener);
235 }
236
237 @Override
238 public void removeListener(BgpNodeListener nodeListener) {
239 this.nodeListener = null;
240 }
241
242 @Override
243 public void addLinkListener(BgpLinkListener linkListener) {
244 this.linkListener.add(linkListener);
245 }
246
247 @Override
248 public void removeLinkListener(BgpLinkListener linkListener) {
249 this.linkListener = null;
250 }
251
252 @Override
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530253 public Iterable<BgpPeer> getPeers() {
Shashikanth VH1ca26ce2015-11-20 23:19:49 +0530254 // TODO Auto-generated method stub
255 return null;
256 }
257
258 @Override
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530259 public BgpPeer getPeer(BgpId bgpId) {
Shashikanth VH1ca26ce2015-11-20 23:19:49 +0530260 // TODO Auto-generated method stub
261 return null;
262 }
263
264 @Override
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530265 public void writeMsg(BgpId bgpId, BgpMessage msg) {
Shashikanth VH1ca26ce2015-11-20 23:19:49 +0530266 // TODO Auto-generated method stub
267
268 }
269
270 @Override
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530271 public void processBGPPacket(BgpId bgpId, BgpMessage msg) throws BgpParseException {
Shashikanth VH1ca26ce2015-11-20 23:19:49 +0530272 // TODO Auto-generated method stub
273
274 }
275
276 @Override
277 public void closeConnectedPeers() {
278 // TODO Auto-generated method stub
279
280 }
281
282 @Override
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530283 public BgpCfg getConfig() {
Shashikanth VH1ca26ce2015-11-20 23:19:49 +0530284 // TODO Auto-generated method stub
285 return null;
286 }
287
288 @Override
289 public int connectedPeerCount() {
290 // TODO Auto-generated method stub
291 return 0;
292 }
293
294
295 @Override
296 public BgpPeerManager peerManager() {
297 // TODO Auto-generated method stub
298 return null;
299 }
300
301 @Override
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530302 public Map<BgpId, BgpPeer> connectedPeers() {
Shashikanth VH1ca26ce2015-11-20 23:19:49 +0530303 // TODO Auto-generated method stub
304 return null;
305 }
306
307 @Override
308 public Set<BgpNodeListener> listener() {
309 // TODO Auto-generated method stub
310 return null;
311 }
312
313 @Override
314 public Set<BgpLinkListener> linkListener() {
315 // TODO Auto-generated method stub
316 return null;
317 }
318 }
319
320 /* Validate node is added to the device validating URI, RIB should get updated properly */
321 @Test
322 public void bgpTopologyProviderTestAddDevice1() {
323 int deviceAddCount = 0;
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530324 LinkedList<BgpValueType> subTlvs;
Shashikanth VH1ca26ce2015-11-20 23:19:49 +0530325 subTlvs = new LinkedList<>();
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530326 BgpValueType tlv = new AutonomousSystemTlv(100);
Shashikanth VH1ca26ce2015-11-20 23:19:49 +0530327 short deslength = AutonomousSystemTlv.LENGTH;
328 short desType = AutonomousSystemTlv.TYPE;
329
330 subTlvs.add(tlv);
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530331 BgpNodeLSIdentifier localNodeDescriptors = new BgpNodeLSIdentifier(new NodeDescriptors(subTlvs, deslength,
Shashikanth VH1ca26ce2015-11-20 23:19:49 +0530332 desType));
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530333 BgpNodeLSNlriVer4 nodeNlri = new BgpNodeLSNlriVer4(0, (byte) Constants.DIRECT, localNodeDescriptors, false,
Shashikanth VH1ca26ce2015-11-20 23:19:49 +0530334 new RouteDistinguisher());
335
336 nodeNlri.setNodeLSIdentifier(localNodeDescriptors);
337 for (BgpNodeListener l : controller.nodeListener) {
338 l.addNode(nodeNlri);
339 deviceAddCount = nodeRegistry.connected.size();
340 assertTrue(deviceAddCount == 1);
341 l.deleteNode(nodeNlri);
342 deviceAddCount = nodeRegistry.connected.size();
343 assertTrue(deviceAddCount == 0);
344 }
345 }
346
347 /* Validate node is not added to the device for invalid URI, RIB count should be zero */
348 @Test
349 public void bgpTopologyProviderTestAddDevice2() {
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530350 LinkedList<BgpValueType> subTlvs;
351 BgpValueType tlv = new AutonomousSystemTlv(10);
Shashikanth VH1ca26ce2015-11-20 23:19:49 +0530352 short deslength = AutonomousSystemTlv.LENGTH;
353 short desType = AutonomousSystemTlv.TYPE;
354
355 subTlvs = new LinkedList<>();
356 subTlvs.add(tlv);
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530357 BgpNodeLSIdentifier localNodeDescriptors = new BgpNodeLSIdentifier(new NodeDescriptors(subTlvs, deslength,
Shashikanth VH1ca26ce2015-11-20 23:19:49 +0530358 desType));
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530359 BgpNodeLSNlriVer4 nodeNlri = new BgpNodeLSNlriVer4(0, (byte) Constants.DIRECT, localNodeDescriptors, false,
Shashikanth VH1ca26ce2015-11-20 23:19:49 +0530360 new RouteDistinguisher());
361
362 nodeNlri.setNodeLSIdentifier(localNodeDescriptors);
363 for (BgpNodeListener l : controller.nodeListener) {
364 l.addNode(nodeNlri);
365 assertTrue("Failed to add device", (nodeRegistry.connected.size() == 0));
366 }
367 }
368
369 /* Delete node when node does not exist, RIB count should be zero */
370 @Test
371 public void bgpTopologyProviderTestAddDevice3() {
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530372 LinkedList<BgpValueType> subTlvs;
373 BgpValueType tlv = new AutonomousSystemTlv(10);
Shashikanth VH1ca26ce2015-11-20 23:19:49 +0530374 short deslength = AutonomousSystemTlv.LENGTH;
375 short desType = AutonomousSystemTlv.TYPE;
376
377 subTlvs = new LinkedList<>();
378 subTlvs.add(tlv);
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530379 BgpNodeLSIdentifier localNodeDescriptors = new BgpNodeLSIdentifier(new NodeDescriptors(subTlvs, deslength,
Shashikanth VH1ca26ce2015-11-20 23:19:49 +0530380 desType));
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530381 BgpNodeLSNlriVer4 nodeNlri = new BgpNodeLSNlriVer4(0, (byte) Constants.DIRECT, localNodeDescriptors, false,
Shashikanth VH1ca26ce2015-11-20 23:19:49 +0530382 new RouteDistinguisher());
383
384 nodeNlri.setNodeLSIdentifier(localNodeDescriptors);
385 for (BgpNodeListener l : controller.nodeListener) {
386 l.deleteNode(nodeNlri);
387 assertTrue("Failed to add device", (nodeRegistry.connected.size() == 0));
388 }
389 }
390
391 /* Validate link is added to the device validating URI, RIB should get updated properly */
392 @Test
393 public void bgpTopologyProviderTestAddLink1() {
394
395 NodeDescriptors localNodeDescriptors;
396 NodeDescriptors remoteNodeDescriptors;
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530397 LinkedList<BgpValueType> subTlvs;
398 LinkedList<BgpValueType> linkDescriptor = new LinkedList<>();
399 BgpValueType tlvLocalRemoteId;
Shashikanth VH1ca26ce2015-11-20 23:19:49 +0530400 short deslength = AutonomousSystemTlv.LENGTH;
401 short desType = AutonomousSystemTlv.TYPE;
402
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530403 BgpValueType tlv = new AutonomousSystemTlv(100);
Shashikanth VH1ca26ce2015-11-20 23:19:49 +0530404 subTlvs = new LinkedList<>();
405 subTlvs.add(tlv);
406
407 localNodeDescriptors = new NodeDescriptors(subTlvs, deslength, desType);
408 remoteNodeDescriptors = new NodeDescriptors(subTlvs, deslength, desType);
409 tlvLocalRemoteId = new LinkLocalRemoteIdentifiersTlv(1, 2);
410 linkDescriptor.add(tlvLocalRemoteId);
411
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530412 BgpLinkLSIdentifier linkLSIdentifier = new BgpLinkLSIdentifier(localNodeDescriptors, remoteNodeDescriptors,
Shashikanth VH1ca26ce2015-11-20 23:19:49 +0530413 linkDescriptor);
414 BgpLinkLsNlriVer4 linkNlri = new BgpLinkLsNlriVer4((byte) Constants.DIRECT, 0, linkLSIdentifier, null, false);
415
416 for (BgpLinkListener l : controller.linkListener) {
417 l.addLink(linkNlri);
418 assertTrue(linkRegistry.connected.size() == 1);
419 l.deleteLink(linkNlri);
420 assertTrue(linkRegistry.connected.size() == 0);
421 }
422 }
423}