blob: 9f23d32c33f8973db14c23303490ef5da5487372 [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
59 .deviceId("bgp:bgpls://0:direct:0/&=bgpnodelsidentifier%7bnodedescriptors=nodedescriptors%7bdestype=512,"
60 + "%20deslength=4,%20subtlvs=[autonomoussystemtlv%7btype=512,%20length=4,%20asnum=100%7d]%7d%7d");
61 private static final DeviceId DID2 = DeviceId
62 .deviceId("bgp:bgpls://0:direct:0/&=bgpnodelsidentifier%7bnodedescriptors=nodedescriptors%7bdestype=512,"
63 + "%20deslength=4,%20subtlvs=[autonomoussystemtlv%7btype=512,%20length=4,%20asnum=10%7d]%7d%7d");
64 private static final DeviceId DID3 = DeviceId
65 .deviceId("bgp:bgpls://direct:0/&=nodedescriptors%7bdestype=512,%20deslength=4,"
66 + "%20subtlvs=[autonomoussystemtlv%7btype=512,%20length=4,%20asnum=100%7d]%7d");
67 private final BgpTopologyProvider provider = new BgpTopologyProvider();
68 private final TestDeviceRegistry nodeRegistry = new TestDeviceRegistry();
Shashikanth VH1ca26ce2015-11-20 23:19:49 +053069 private final TestController controller = new TestController();
70
71 @Before
72 public void startUp() {
73 provider.deviceProviderRegistry = nodeRegistry;
Shashikanth VH1ca26ce2015-11-20 23:19:49 +053074 provider.controller = controller;
75 provider.activate();
76 assertNotNull("provider should be registered", nodeRegistry.provider);
Shashikanth VH1ca26ce2015-11-20 23:19:49 +053077 assertNotNull("listener should be registered", controller.nodeListener);
78 }
79
80 @After
81 public void tearDown() {
82 provider.deactivate();
83 assertNull("listener should be removed", controller.nodeListener);
84 provider.controller = null;
85 provider.deviceProviderRegistry = null;
Shashikanth VH1ca26ce2015-11-20 23:19:49 +053086 }
87
88 /* Class implement device test registry */
89 private class TestDeviceRegistry implements DeviceProviderRegistry {
90 DeviceProvider provider;
91
92 Set<DeviceId> connected = new HashSet<>();
93
94 @Override
95 public DeviceProviderService register(DeviceProvider provider) {
96 this.provider = provider;
97 return new TestProviderService();
98 }
99
100 @Override
101 public void unregister(DeviceProvider provider) {
102 }
103
104 @Override
105 public Set<ProviderId> getProviders() {
106 return null;
107 }
108
109 private class TestProviderService implements DeviceProviderService {
110
111 @Override
112 public DeviceProvider provider() {
113 return null;
114 }
115
116 @Override
117 public void deviceConnected(DeviceId deviceId, DeviceDescription deviceDescription) {
118 if (deviceId.equals(DID1)) {
119 connected.add(deviceId);
120 }
121 }
122
123 @Override
124 public void deviceDisconnected(DeviceId deviceId) {
125 if (deviceId.equals(DID1)) {
126 connected.remove(deviceId);
127 }
128 }
129
130 @Override
131 public void updatePorts(DeviceId deviceId, List<PortDescription> portDescriptions) {
132 // TODO Auto-generated method stub
133
134 }
135
136 @Override
137 public void portStatusChanged(DeviceId deviceId, PortDescription portDescription) {
138 // TODO Auto-generated method stub
139
140 }
141
142 @Override
143 public void receivedRoleReply(DeviceId deviceId, MastershipRole requested, MastershipRole response) {
144 // TODO Auto-generated method stub
145
146 }
147
148 @Override
149 public void updatePortStatistics(DeviceId deviceId, Collection<PortStatistics> portStatistics) {
150 // TODO Auto-generated method stub
151
152 }
153 }
154 }
155
Shashikanth VH1ca26ce2015-11-20 23:19:49 +0530156 /* class implement test controller */
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530157 private class TestController implements BgpController {
Shashikanth VH1ca26ce2015-11-20 23:19:49 +0530158 protected Set<BgpNodeListener> nodeListener = new CopyOnWriteArraySet<>();
Shashikanth VH1ca26ce2015-11-20 23:19:49 +0530159
160 @Override
161 public void addListener(BgpNodeListener nodeListener) {
162 this.nodeListener.add(nodeListener);
163 }
164
165 @Override
166 public void removeListener(BgpNodeListener nodeListener) {
167 this.nodeListener = null;
168 }
169
170 @Override
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530171 public Iterable<BgpPeer> getPeers() {
Shashikanth VH1ca26ce2015-11-20 23:19:49 +0530172 // TODO Auto-generated method stub
173 return null;
174 }
175
176 @Override
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530177 public BgpPeer getPeer(BgpId bgpId) {
Shashikanth VH1ca26ce2015-11-20 23:19:49 +0530178 // TODO Auto-generated method stub
179 return null;
180 }
181
182 @Override
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530183 public void writeMsg(BgpId bgpId, BgpMessage msg) {
Shashikanth VH1ca26ce2015-11-20 23:19:49 +0530184 // TODO Auto-generated method stub
185
186 }
187
188 @Override
Jonathan Hart51539b82015-10-29 09:53:04 -0700189 public void processBgpPacket(BgpId bgpId, BgpMessage msg) throws BgpParseException {
Shashikanth VH1ca26ce2015-11-20 23:19:49 +0530190 // TODO Auto-generated method stub
191
192 }
193
194 @Override
195 public void closeConnectedPeers() {
196 // TODO Auto-generated method stub
197
198 }
199
200 @Override
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530201 public BgpCfg getConfig() {
Shashikanth VH1ca26ce2015-11-20 23:19:49 +0530202 // TODO Auto-generated method stub
203 return null;
204 }
205
206 @Override
207 public int connectedPeerCount() {
208 // TODO Auto-generated method stub
209 return 0;
210 }
211
Shashikanth VH3fe37982015-11-30 11:50:07 +0530212 @Override
213 public BgpLocalRib bgpLocalRibVpn() {
214 // TODO Auto-generated method stub
215 return null;
216 }
217
218 @Override
219 public BgpLocalRib bgpLocalRib() {
220 // TODO Auto-generated method stub
221 return null;
222 }
Shashikanth VH1ca26ce2015-11-20 23:19:49 +0530223
224 @Override
225 public BgpPeerManager peerManager() {
226 // TODO Auto-generated method stub
227 return null;
228 }
229
230 @Override
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530231 public Map<BgpId, BgpPeer> connectedPeers() {
Shashikanth VH1ca26ce2015-11-20 23:19:49 +0530232 // TODO Auto-generated method stub
233 return null;
234 }
235
236 @Override
237 public Set<BgpNodeListener> listener() {
238 // TODO Auto-generated method stub
239 return null;
240 }
Shashikanth VH1ca26ce2015-11-20 23:19:49 +0530241 }
242
243 /* Validate node is added to the device validating URI, RIB should get updated properly */
244 @Test
245 public void bgpTopologyProviderTestAddDevice1() {
246 int deviceAddCount = 0;
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530247 LinkedList<BgpValueType> subTlvs;
Shashikanth VH1ca26ce2015-11-20 23:19:49 +0530248 subTlvs = new LinkedList<>();
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530249 BgpValueType tlv = new AutonomousSystemTlv(100);
Shashikanth VH1ca26ce2015-11-20 23:19:49 +0530250 short deslength = AutonomousSystemTlv.LENGTH;
251 short desType = AutonomousSystemTlv.TYPE;
252
253 subTlvs.add(tlv);
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530254 BgpNodeLSIdentifier localNodeDescriptors = new BgpNodeLSIdentifier(new NodeDescriptors(subTlvs, deslength,
Shashikanth VH1ca26ce2015-11-20 23:19:49 +0530255 desType));
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530256 BgpNodeLSNlriVer4 nodeNlri = new BgpNodeLSNlriVer4(0, (byte) Constants.DIRECT, localNodeDescriptors, false,
Shashikanth VH1ca26ce2015-11-20 23:19:49 +0530257 new RouteDistinguisher());
258
259 nodeNlri.setNodeLSIdentifier(localNodeDescriptors);
260 for (BgpNodeListener l : controller.nodeListener) {
261 l.addNode(nodeNlri);
262 deviceAddCount = nodeRegistry.connected.size();
263 assertTrue(deviceAddCount == 1);
264 l.deleteNode(nodeNlri);
265 deviceAddCount = nodeRegistry.connected.size();
266 assertTrue(deviceAddCount == 0);
267 }
268 }
269
270 /* Validate node is not added to the device for invalid URI, RIB count should be zero */
271 @Test
272 public void bgpTopologyProviderTestAddDevice2() {
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530273 LinkedList<BgpValueType> subTlvs;
274 BgpValueType tlv = new AutonomousSystemTlv(10);
Shashikanth VH1ca26ce2015-11-20 23:19:49 +0530275 short deslength = AutonomousSystemTlv.LENGTH;
276 short desType = AutonomousSystemTlv.TYPE;
277
278 subTlvs = new LinkedList<>();
279 subTlvs.add(tlv);
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530280 BgpNodeLSIdentifier localNodeDescriptors = new BgpNodeLSIdentifier(new NodeDescriptors(subTlvs, deslength,
Shashikanth VH1ca26ce2015-11-20 23:19:49 +0530281 desType));
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530282 BgpNodeLSNlriVer4 nodeNlri = new BgpNodeLSNlriVer4(0, (byte) Constants.DIRECT, localNodeDescriptors, false,
Shashikanth VH1ca26ce2015-11-20 23:19:49 +0530283 new RouteDistinguisher());
284
285 nodeNlri.setNodeLSIdentifier(localNodeDescriptors);
286 for (BgpNodeListener l : controller.nodeListener) {
287 l.addNode(nodeNlri);
288 assertTrue("Failed to add device", (nodeRegistry.connected.size() == 0));
289 }
290 }
291
292 /* Delete node when node does not exist, RIB count should be zero */
293 @Test
294 public void bgpTopologyProviderTestAddDevice3() {
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530295 LinkedList<BgpValueType> subTlvs;
296 BgpValueType tlv = new AutonomousSystemTlv(10);
Shashikanth VH1ca26ce2015-11-20 23:19:49 +0530297 short deslength = AutonomousSystemTlv.LENGTH;
298 short desType = AutonomousSystemTlv.TYPE;
299
300 subTlvs = new LinkedList<>();
301 subTlvs.add(tlv);
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530302 BgpNodeLSIdentifier localNodeDescriptors = new BgpNodeLSIdentifier(new NodeDescriptors(subTlvs, deslength,
Shashikanth VH1ca26ce2015-11-20 23:19:49 +0530303 desType));
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530304 BgpNodeLSNlriVer4 nodeNlri = new BgpNodeLSNlriVer4(0, (byte) Constants.DIRECT, localNodeDescriptors, false,
Shashikanth VH1ca26ce2015-11-20 23:19:49 +0530305 new RouteDistinguisher());
306
307 nodeNlri.setNodeLSIdentifier(localNodeDescriptors);
308 for (BgpNodeListener l : controller.nodeListener) {
309 l.deleteNode(nodeNlri);
310 assertTrue("Failed to add device", (nodeRegistry.connected.size() == 0));
311 }
312 }
Shashikanth VH1ca26ce2015-11-20 23:19:49 +0530313}