blob: 7a810eee004d1644bffa7090ceeee7c2e3361e9c [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 VH3fe37982015-11-30 11:50:07 +053034import org.onosproject.bgp.controller.BgpLocalRib;
Shashikanth VH1ca26ce2015-11-20 23:19:49 +053035import 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;
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053039import org.onosproject.bgpio.protocol.linkstate.BgpNodeLSIdentifier;
40import org.onosproject.bgpio.protocol.linkstate.BgpNodeLSNlriVer4;
Shashikanth VH1ca26ce2015-11-20 23:19:49 +053041import org.onosproject.bgpio.protocol.linkstate.NodeDescriptors;
42import org.onosproject.bgpio.types.AutonomousSystemTlv;
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053043import org.onosproject.bgpio.types.BgpValueType;
Shashikanth VH1ca26ce2015-11-20 23:19:49 +053044import org.onosproject.bgpio.types.RouteDistinguisher;
45import org.onosproject.bgpio.util.Constants;
Shashikanth VH1ca26ce2015-11-20 23:19:49 +053046import org.onosproject.net.DeviceId;
47import org.onosproject.net.MastershipRole;
48import org.onosproject.net.device.DeviceDescription;
49import org.onosproject.net.device.DeviceProvider;
50import org.onosproject.net.device.DeviceProviderRegistry;
51import org.onosproject.net.device.DeviceProviderService;
52import org.onosproject.net.device.PortDescription;
53import org.onosproject.net.device.PortStatistics;
Shashikanth VH1ca26ce2015-11-20 23:19:49 +053054import org.onosproject.net.provider.ProviderId;
55
56public class BgpTopologyProviderTest {
57
58 private static final DeviceId DID1 = DeviceId
Shashikanth VHe68bb5e2016-02-18 23:44:50 +053059 .deviceId("bgp:rd=0:proto=direct:id=0:as=100");
Shashikanth VH1ca26ce2015-11-20 23:19:49 +053060 private static final DeviceId DID2 = DeviceId
Shashikanth VHe68bb5e2016-02-18 23:44:50 +053061 .deviceId("bgp:rd=0:proto=direct:id=0:as=10");
Shashikanth VH1ca26ce2015-11-20 23:19:49 +053062 private static final DeviceId DID3 = DeviceId
Shashikanth VHe68bb5e2016-02-18 23:44:50 +053063 .deviceId("bgp:rd=0:proto=direct:id=0:as=100");
Shashikanth VH1ca26ce2015-11-20 23:19:49 +053064 private final BgpTopologyProvider provider = new BgpTopologyProvider();
65 private final TestDeviceRegistry nodeRegistry = new TestDeviceRegistry();
Shashikanth VH1ca26ce2015-11-20 23:19:49 +053066 private final TestController controller = new TestController();
67
68 @Before
69 public void startUp() {
70 provider.deviceProviderRegistry = nodeRegistry;
Shashikanth VH1ca26ce2015-11-20 23:19:49 +053071 provider.controller = controller;
72 provider.activate();
73 assertNotNull("provider should be registered", nodeRegistry.provider);
Shashikanth VH1ca26ce2015-11-20 23:19:49 +053074 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 VH1ca26ce2015-11-20 23:19:49 +053083 }
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 VH1ca26ce2015-11-20 23:19:49 +0530153 /* class implement test controller */
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530154 private class TestController implements BgpController {
Shashikanth VH1ca26ce2015-11-20 23:19:49 +0530155 protected Set<BgpNodeListener> nodeListener = new CopyOnWriteArraySet<>();
Shashikanth VH1ca26ce2015-11-20 23:19:49 +0530156
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 VH5dd8dbe2015-11-26 13:22:18 +0530168 public Iterable<BgpPeer> getPeers() {
Shashikanth VH1ca26ce2015-11-20 23:19:49 +0530169 // TODO Auto-generated method stub
170 return null;
171 }
172
173 @Override
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530174 public BgpPeer getPeer(BgpId bgpId) {
Shashikanth VH1ca26ce2015-11-20 23:19:49 +0530175 // TODO Auto-generated method stub
176 return null;
177 }
178
179 @Override
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530180 public void writeMsg(BgpId bgpId, BgpMessage msg) {
Shashikanth VH1ca26ce2015-11-20 23:19:49 +0530181 // TODO Auto-generated method stub
182
183 }
184
185 @Override
Jonathan Hart51539b82015-10-29 09:53:04 -0700186 public void processBgpPacket(BgpId bgpId, BgpMessage msg) throws BgpParseException {
Shashikanth VH1ca26ce2015-11-20 23:19:49 +0530187 // 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 VH5dd8dbe2015-11-26 13:22:18 +0530198 public BgpCfg getConfig() {
Shashikanth VH1ca26ce2015-11-20 23:19:49 +0530199 // 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 VH3fe37982015-11-30 11:50:07 +0530209 @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 VH1ca26ce2015-11-20 23:19:49 +0530220
221 @Override
222 public BgpPeerManager peerManager() {
223 // TODO Auto-generated method stub
224 return null;
225 }
226
227 @Override
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530228 public Map<BgpId, BgpPeer> connectedPeers() {
Shashikanth VH1ca26ce2015-11-20 23:19:49 +0530229 // 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 VH1ca26ce2015-11-20 23:19:49 +0530238 }
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 VH5dd8dbe2015-11-26 13:22:18 +0530244 LinkedList<BgpValueType> subTlvs;
Shashikanth VH1ca26ce2015-11-20 23:19:49 +0530245 subTlvs = new LinkedList<>();
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530246 BgpValueType tlv = new AutonomousSystemTlv(100);
Shashikanth VH1ca26ce2015-11-20 23:19:49 +0530247 short deslength = AutonomousSystemTlv.LENGTH;
248 short desType = AutonomousSystemTlv.TYPE;
249
250 subTlvs.add(tlv);
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530251 BgpNodeLSIdentifier localNodeDescriptors = new BgpNodeLSIdentifier(new NodeDescriptors(subTlvs, deslength,
Shashikanth VH1ca26ce2015-11-20 23:19:49 +0530252 desType));
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530253 BgpNodeLSNlriVer4 nodeNlri = new BgpNodeLSNlriVer4(0, (byte) Constants.DIRECT, localNodeDescriptors, false,
Shashikanth VH1ca26ce2015-11-20 23:19:49 +0530254 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 VH5dd8dbe2015-11-26 13:22:18 +0530270 LinkedList<BgpValueType> subTlvs;
271 BgpValueType tlv = new AutonomousSystemTlv(10);
Shashikanth VH1ca26ce2015-11-20 23:19:49 +0530272 short deslength = AutonomousSystemTlv.LENGTH;
273 short desType = AutonomousSystemTlv.TYPE;
274
275 subTlvs = new LinkedList<>();
276 subTlvs.add(tlv);
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530277 BgpNodeLSIdentifier localNodeDescriptors = new BgpNodeLSIdentifier(new NodeDescriptors(subTlvs, deslength,
Shashikanth VH1ca26ce2015-11-20 23:19:49 +0530278 desType));
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530279 BgpNodeLSNlriVer4 nodeNlri = new BgpNodeLSNlriVer4(0, (byte) Constants.DIRECT, localNodeDescriptors, false,
Shashikanth VH1ca26ce2015-11-20 23:19:49 +0530280 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 VH5dd8dbe2015-11-26 13:22:18 +0530292 LinkedList<BgpValueType> subTlvs;
293 BgpValueType tlv = new AutonomousSystemTlv(10);
Shashikanth VH1ca26ce2015-11-20 23:19:49 +0530294 short deslength = AutonomousSystemTlv.LENGTH;
295 short desType = AutonomousSystemTlv.TYPE;
296
297 subTlvs = new LinkedList<>();
298 subTlvs.add(tlv);
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530299 BgpNodeLSIdentifier localNodeDescriptors = new BgpNodeLSIdentifier(new NodeDescriptors(subTlvs, deslength,
Shashikanth VH1ca26ce2015-11-20 23:19:49 +0530300 desType));
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530301 BgpNodeLSNlriVer4 nodeNlri = new BgpNodeLSNlriVer4(0, (byte) Constants.DIRECT, localNodeDescriptors, false,
Shashikanth VH1ca26ce2015-11-20 23:19:49 +0530302 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 VH1ca26ce2015-11-20 23:19:49 +0530310}