Shashikanth VH | 1ca26ce | 2015-11-20 23:19:49 +0530 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 13 | package org.onosproject.provider.bgp.topology.impl; |
| 14 | |
| 15 | import static org.junit.Assert.assertNotNull; |
| 16 | import static org.junit.Assert.assertNull; |
| 17 | import static org.junit.Assert.assertTrue; |
| 18 | |
| 19 | import java.util.Collection; |
| 20 | import java.util.HashSet; |
| 21 | import java.util.LinkedList; |
| 22 | import java.util.List; |
| 23 | import java.util.Map; |
| 24 | import java.util.Set; |
| 25 | import java.util.concurrent.CopyOnWriteArraySet; |
| 26 | |
| 27 | import org.junit.After; |
| 28 | import org.junit.Before; |
| 29 | import org.junit.Test; |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 30 | import org.onosproject.bgp.controller.BgpCfg; |
| 31 | import org.onosproject.bgp.controller.BgpController; |
| 32 | import org.onosproject.bgp.controller.BgpId; |
| 33 | import org.onosproject.bgp.controller.BgpPeer; |
Shashikanth VH | 3fe3798 | 2015-11-30 11:50:07 +0530 | [diff] [blame] | 34 | import org.onosproject.bgp.controller.BgpLocalRib; |
Shashikanth VH | 1ca26ce | 2015-11-20 23:19:49 +0530 | [diff] [blame] | 35 | import org.onosproject.bgp.controller.BgpNodeListener; |
| 36 | import org.onosproject.bgp.controller.BgpPeerManager; |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 37 | import org.onosproject.bgpio.exceptions.BgpParseException; |
| 38 | import org.onosproject.bgpio.protocol.BgpMessage; |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 39 | import org.onosproject.bgpio.protocol.linkstate.BgpNodeLSIdentifier; |
| 40 | import org.onosproject.bgpio.protocol.linkstate.BgpNodeLSNlriVer4; |
Shashikanth VH | 1ca26ce | 2015-11-20 23:19:49 +0530 | [diff] [blame] | 41 | import org.onosproject.bgpio.protocol.linkstate.NodeDescriptors; |
| 42 | import org.onosproject.bgpio.types.AutonomousSystemTlv; |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 43 | import org.onosproject.bgpio.types.BgpValueType; |
Shashikanth VH | 1ca26ce | 2015-11-20 23:19:49 +0530 | [diff] [blame] | 44 | import org.onosproject.bgpio.types.RouteDistinguisher; |
| 45 | import org.onosproject.bgpio.util.Constants; |
Shashikanth VH | 1ca26ce | 2015-11-20 23:19:49 +0530 | [diff] [blame] | 46 | import org.onosproject.net.DeviceId; |
| 47 | import org.onosproject.net.MastershipRole; |
| 48 | import org.onosproject.net.device.DeviceDescription; |
| 49 | import org.onosproject.net.device.DeviceProvider; |
| 50 | import org.onosproject.net.device.DeviceProviderRegistry; |
| 51 | import org.onosproject.net.device.DeviceProviderService; |
| 52 | import org.onosproject.net.device.PortDescription; |
| 53 | import org.onosproject.net.device.PortStatistics; |
Shashikanth VH | 1ca26ce | 2015-11-20 23:19:49 +0530 | [diff] [blame] | 54 | import org.onosproject.net.provider.ProviderId; |
| 55 | |
| 56 | public class BgpTopologyProviderTest { |
| 57 | |
| 58 | private static final DeviceId DID1 = DeviceId |
Shashikanth VH | e68bb5e | 2016-02-18 23:44:50 +0530 | [diff] [blame^] | 59 | .deviceId("bgp:rd=0:proto=direct:id=0:as=100"); |
Shashikanth VH | 1ca26ce | 2015-11-20 23:19:49 +0530 | [diff] [blame] | 60 | private static final DeviceId DID2 = DeviceId |
Shashikanth VH | e68bb5e | 2016-02-18 23:44:50 +0530 | [diff] [blame^] | 61 | .deviceId("bgp:rd=0:proto=direct:id=0:as=10"); |
Shashikanth VH | 1ca26ce | 2015-11-20 23:19:49 +0530 | [diff] [blame] | 62 | private static final DeviceId DID3 = DeviceId |
Shashikanth VH | e68bb5e | 2016-02-18 23:44:50 +0530 | [diff] [blame^] | 63 | .deviceId("bgp:rd=0:proto=direct:id=0:as=100"); |
Shashikanth VH | 1ca26ce | 2015-11-20 23:19:49 +0530 | [diff] [blame] | 64 | private final BgpTopologyProvider provider = new BgpTopologyProvider(); |
| 65 | private final TestDeviceRegistry nodeRegistry = new TestDeviceRegistry(); |
Shashikanth VH | 1ca26ce | 2015-11-20 23:19:49 +0530 | [diff] [blame] | 66 | private final TestController controller = new TestController(); |
| 67 | |
| 68 | @Before |
| 69 | public void startUp() { |
| 70 | provider.deviceProviderRegistry = nodeRegistry; |
Shashikanth VH | 1ca26ce | 2015-11-20 23:19:49 +0530 | [diff] [blame] | 71 | provider.controller = controller; |
| 72 | provider.activate(); |
| 73 | assertNotNull("provider should be registered", nodeRegistry.provider); |
Shashikanth VH | 1ca26ce | 2015-11-20 23:19:49 +0530 | [diff] [blame] | 74 | assertNotNull("listener should be registered", controller.nodeListener); |
| 75 | } |
| 76 | |
| 77 | @After |
| 78 | public void tearDown() { |
| 79 | provider.deactivate(); |
| 80 | assertNull("listener should be removed", controller.nodeListener); |
| 81 | provider.controller = null; |
| 82 | provider.deviceProviderRegistry = null; |
Shashikanth VH | 1ca26ce | 2015-11-20 23:19:49 +0530 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | /* Class implement device test registry */ |
| 86 | private class TestDeviceRegistry implements DeviceProviderRegistry { |
| 87 | DeviceProvider provider; |
| 88 | |
| 89 | Set<DeviceId> connected = new HashSet<>(); |
| 90 | |
| 91 | @Override |
| 92 | public DeviceProviderService register(DeviceProvider provider) { |
| 93 | this.provider = provider; |
| 94 | return new TestProviderService(); |
| 95 | } |
| 96 | |
| 97 | @Override |
| 98 | public void unregister(DeviceProvider provider) { |
| 99 | } |
| 100 | |
| 101 | @Override |
| 102 | public Set<ProviderId> getProviders() { |
| 103 | return null; |
| 104 | } |
| 105 | |
| 106 | private class TestProviderService implements DeviceProviderService { |
| 107 | |
| 108 | @Override |
| 109 | public DeviceProvider provider() { |
| 110 | return null; |
| 111 | } |
| 112 | |
| 113 | @Override |
| 114 | public void deviceConnected(DeviceId deviceId, DeviceDescription deviceDescription) { |
| 115 | if (deviceId.equals(DID1)) { |
| 116 | connected.add(deviceId); |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | @Override |
| 121 | public void deviceDisconnected(DeviceId deviceId) { |
| 122 | if (deviceId.equals(DID1)) { |
| 123 | connected.remove(deviceId); |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | @Override |
| 128 | public void updatePorts(DeviceId deviceId, List<PortDescription> portDescriptions) { |
| 129 | // TODO Auto-generated method stub |
| 130 | |
| 131 | } |
| 132 | |
| 133 | @Override |
| 134 | public void portStatusChanged(DeviceId deviceId, PortDescription portDescription) { |
| 135 | // TODO Auto-generated method stub |
| 136 | |
| 137 | } |
| 138 | |
| 139 | @Override |
| 140 | public void receivedRoleReply(DeviceId deviceId, MastershipRole requested, MastershipRole response) { |
| 141 | // TODO Auto-generated method stub |
| 142 | |
| 143 | } |
| 144 | |
| 145 | @Override |
| 146 | public void updatePortStatistics(DeviceId deviceId, Collection<PortStatistics> portStatistics) { |
| 147 | // TODO Auto-generated method stub |
| 148 | |
| 149 | } |
| 150 | } |
| 151 | } |
| 152 | |
Shashikanth VH | 1ca26ce | 2015-11-20 23:19:49 +0530 | [diff] [blame] | 153 | /* class implement test controller */ |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 154 | private class TestController implements BgpController { |
Shashikanth VH | 1ca26ce | 2015-11-20 23:19:49 +0530 | [diff] [blame] | 155 | protected Set<BgpNodeListener> nodeListener = new CopyOnWriteArraySet<>(); |
Shashikanth VH | 1ca26ce | 2015-11-20 23:19:49 +0530 | [diff] [blame] | 156 | |
| 157 | @Override |
| 158 | public void addListener(BgpNodeListener nodeListener) { |
| 159 | this.nodeListener.add(nodeListener); |
| 160 | } |
| 161 | |
| 162 | @Override |
| 163 | public void removeListener(BgpNodeListener nodeListener) { |
| 164 | this.nodeListener = null; |
| 165 | } |
| 166 | |
| 167 | @Override |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 168 | public Iterable<BgpPeer> getPeers() { |
Shashikanth VH | 1ca26ce | 2015-11-20 23:19:49 +0530 | [diff] [blame] | 169 | // TODO Auto-generated method stub |
| 170 | return null; |
| 171 | } |
| 172 | |
| 173 | @Override |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 174 | public BgpPeer getPeer(BgpId bgpId) { |
Shashikanth VH | 1ca26ce | 2015-11-20 23:19:49 +0530 | [diff] [blame] | 175 | // TODO Auto-generated method stub |
| 176 | return null; |
| 177 | } |
| 178 | |
| 179 | @Override |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 180 | public void writeMsg(BgpId bgpId, BgpMessage msg) { |
Shashikanth VH | 1ca26ce | 2015-11-20 23:19:49 +0530 | [diff] [blame] | 181 | // TODO Auto-generated method stub |
| 182 | |
| 183 | } |
| 184 | |
| 185 | @Override |
Jonathan Hart | 51539b8 | 2015-10-29 09:53:04 -0700 | [diff] [blame] | 186 | public void processBgpPacket(BgpId bgpId, BgpMessage msg) throws BgpParseException { |
Shashikanth VH | 1ca26ce | 2015-11-20 23:19:49 +0530 | [diff] [blame] | 187 | // TODO Auto-generated method stub |
| 188 | |
| 189 | } |
| 190 | |
| 191 | @Override |
| 192 | public void closeConnectedPeers() { |
| 193 | // TODO Auto-generated method stub |
| 194 | |
| 195 | } |
| 196 | |
| 197 | @Override |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 198 | public BgpCfg getConfig() { |
Shashikanth VH | 1ca26ce | 2015-11-20 23:19:49 +0530 | [diff] [blame] | 199 | // TODO Auto-generated method stub |
| 200 | return null; |
| 201 | } |
| 202 | |
| 203 | @Override |
| 204 | public int connectedPeerCount() { |
| 205 | // TODO Auto-generated method stub |
| 206 | return 0; |
| 207 | } |
| 208 | |
Shashikanth VH | 3fe3798 | 2015-11-30 11:50:07 +0530 | [diff] [blame] | 209 | @Override |
| 210 | public BgpLocalRib bgpLocalRibVpn() { |
| 211 | // TODO Auto-generated method stub |
| 212 | return null; |
| 213 | } |
| 214 | |
| 215 | @Override |
| 216 | public BgpLocalRib bgpLocalRib() { |
| 217 | // TODO Auto-generated method stub |
| 218 | return null; |
| 219 | } |
Shashikanth VH | 1ca26ce | 2015-11-20 23:19:49 +0530 | [diff] [blame] | 220 | |
| 221 | @Override |
| 222 | public BgpPeerManager peerManager() { |
| 223 | // TODO Auto-generated method stub |
| 224 | return null; |
| 225 | } |
| 226 | |
| 227 | @Override |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 228 | public Map<BgpId, BgpPeer> connectedPeers() { |
Shashikanth VH | 1ca26ce | 2015-11-20 23:19:49 +0530 | [diff] [blame] | 229 | // TODO Auto-generated method stub |
| 230 | return null; |
| 231 | } |
| 232 | |
| 233 | @Override |
| 234 | public Set<BgpNodeListener> listener() { |
| 235 | // TODO Auto-generated method stub |
| 236 | return null; |
| 237 | } |
Shashikanth VH | 1ca26ce | 2015-11-20 23:19:49 +0530 | [diff] [blame] | 238 | } |
| 239 | |
| 240 | /* Validate node is added to the device validating URI, RIB should get updated properly */ |
| 241 | @Test |
| 242 | public void bgpTopologyProviderTestAddDevice1() { |
| 243 | int deviceAddCount = 0; |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 244 | LinkedList<BgpValueType> subTlvs; |
Shashikanth VH | 1ca26ce | 2015-11-20 23:19:49 +0530 | [diff] [blame] | 245 | subTlvs = new LinkedList<>(); |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 246 | BgpValueType tlv = new AutonomousSystemTlv(100); |
Shashikanth VH | 1ca26ce | 2015-11-20 23:19:49 +0530 | [diff] [blame] | 247 | short deslength = AutonomousSystemTlv.LENGTH; |
| 248 | short desType = AutonomousSystemTlv.TYPE; |
| 249 | |
| 250 | subTlvs.add(tlv); |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 251 | BgpNodeLSIdentifier localNodeDescriptors = new BgpNodeLSIdentifier(new NodeDescriptors(subTlvs, deslength, |
Shashikanth VH | 1ca26ce | 2015-11-20 23:19:49 +0530 | [diff] [blame] | 252 | desType)); |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 253 | BgpNodeLSNlriVer4 nodeNlri = new BgpNodeLSNlriVer4(0, (byte) Constants.DIRECT, localNodeDescriptors, false, |
Shashikanth VH | 1ca26ce | 2015-11-20 23:19:49 +0530 | [diff] [blame] | 254 | new RouteDistinguisher()); |
| 255 | |
| 256 | nodeNlri.setNodeLSIdentifier(localNodeDescriptors); |
| 257 | for (BgpNodeListener l : controller.nodeListener) { |
| 258 | l.addNode(nodeNlri); |
| 259 | deviceAddCount = nodeRegistry.connected.size(); |
| 260 | assertTrue(deviceAddCount == 1); |
| 261 | l.deleteNode(nodeNlri); |
| 262 | deviceAddCount = nodeRegistry.connected.size(); |
| 263 | assertTrue(deviceAddCount == 0); |
| 264 | } |
| 265 | } |
| 266 | |
| 267 | /* Validate node is not added to the device for invalid URI, RIB count should be zero */ |
| 268 | @Test |
| 269 | public void bgpTopologyProviderTestAddDevice2() { |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 270 | LinkedList<BgpValueType> subTlvs; |
| 271 | BgpValueType tlv = new AutonomousSystemTlv(10); |
Shashikanth VH | 1ca26ce | 2015-11-20 23:19:49 +0530 | [diff] [blame] | 272 | short deslength = AutonomousSystemTlv.LENGTH; |
| 273 | short desType = AutonomousSystemTlv.TYPE; |
| 274 | |
| 275 | subTlvs = new LinkedList<>(); |
| 276 | subTlvs.add(tlv); |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 277 | BgpNodeLSIdentifier localNodeDescriptors = new BgpNodeLSIdentifier(new NodeDescriptors(subTlvs, deslength, |
Shashikanth VH | 1ca26ce | 2015-11-20 23:19:49 +0530 | [diff] [blame] | 278 | desType)); |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 279 | BgpNodeLSNlriVer4 nodeNlri = new BgpNodeLSNlriVer4(0, (byte) Constants.DIRECT, localNodeDescriptors, false, |
Shashikanth VH | 1ca26ce | 2015-11-20 23:19:49 +0530 | [diff] [blame] | 280 | new RouteDistinguisher()); |
| 281 | |
| 282 | nodeNlri.setNodeLSIdentifier(localNodeDescriptors); |
| 283 | for (BgpNodeListener l : controller.nodeListener) { |
| 284 | l.addNode(nodeNlri); |
| 285 | assertTrue("Failed to add device", (nodeRegistry.connected.size() == 0)); |
| 286 | } |
| 287 | } |
| 288 | |
| 289 | /* Delete node when node does not exist, RIB count should be zero */ |
| 290 | @Test |
| 291 | public void bgpTopologyProviderTestAddDevice3() { |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 292 | LinkedList<BgpValueType> subTlvs; |
| 293 | BgpValueType tlv = new AutonomousSystemTlv(10); |
Shashikanth VH | 1ca26ce | 2015-11-20 23:19:49 +0530 | [diff] [blame] | 294 | short deslength = AutonomousSystemTlv.LENGTH; |
| 295 | short desType = AutonomousSystemTlv.TYPE; |
| 296 | |
| 297 | subTlvs = new LinkedList<>(); |
| 298 | subTlvs.add(tlv); |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 299 | BgpNodeLSIdentifier localNodeDescriptors = new BgpNodeLSIdentifier(new NodeDescriptors(subTlvs, deslength, |
Shashikanth VH | 1ca26ce | 2015-11-20 23:19:49 +0530 | [diff] [blame] | 300 | desType)); |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 301 | BgpNodeLSNlriVer4 nodeNlri = new BgpNodeLSNlriVer4(0, (byte) Constants.DIRECT, localNodeDescriptors, false, |
Shashikanth VH | 1ca26ce | 2015-11-20 23:19:49 +0530 | [diff] [blame] | 302 | new RouteDistinguisher()); |
| 303 | |
| 304 | nodeNlri.setNodeLSIdentifier(localNodeDescriptors); |
| 305 | for (BgpNodeListener l : controller.nodeListener) { |
| 306 | l.deleteNode(nodeNlri); |
| 307 | assertTrue("Failed to add device", (nodeRegistry.connected.size() == 0)); |
| 308 | } |
| 309 | } |
Shashikanth VH | 1ca26ce | 2015-11-20 23:19:49 +0530 | [diff] [blame] | 310 | } |