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