blob: 30bb4470725983f604ddb070626765987a84e086 [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.BgpNodeListener;
35import org.onosproject.bgp.controller.BgpPeerManager;
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053036import org.onosproject.bgpio.exceptions.BgpParseException;
37import org.onosproject.bgpio.protocol.BgpMessage;
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053038import org.onosproject.bgpio.protocol.linkstate.BgpNodeLSIdentifier;
39import org.onosproject.bgpio.protocol.linkstate.BgpNodeLSNlriVer4;
Shashikanth VH1ca26ce2015-11-20 23:19:49 +053040import org.onosproject.bgpio.protocol.linkstate.NodeDescriptors;
41import org.onosproject.bgpio.types.AutonomousSystemTlv;
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053042import org.onosproject.bgpio.types.BgpValueType;
Shashikanth VH1ca26ce2015-11-20 23:19:49 +053043import org.onosproject.bgpio.types.RouteDistinguisher;
44import org.onosproject.bgpio.util.Constants;
Shashikanth VH1ca26ce2015-11-20 23:19:49 +053045import org.onosproject.net.DeviceId;
46import org.onosproject.net.MastershipRole;
47import org.onosproject.net.device.DeviceDescription;
48import org.onosproject.net.device.DeviceProvider;
49import org.onosproject.net.device.DeviceProviderRegistry;
50import org.onosproject.net.device.DeviceProviderService;
51import org.onosproject.net.device.PortDescription;
52import org.onosproject.net.device.PortStatistics;
Shashikanth VH1ca26ce2015-11-20 23:19:49 +053053import org.onosproject.net.provider.ProviderId;
54
55public 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 VH1ca26ce2015-11-20 23:19:49 +053068 private final TestController controller = new TestController();
69
70 @Before
71 public void startUp() {
72 provider.deviceProviderRegistry = nodeRegistry;
Shashikanth VH1ca26ce2015-11-20 23:19:49 +053073 provider.controller = controller;
74 provider.activate();
75 assertNotNull("provider should be registered", nodeRegistry.provider);
Shashikanth VH1ca26ce2015-11-20 23:19:49 +053076 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 VH1ca26ce2015-11-20 23:19:49 +053085 }
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 VH1ca26ce2015-11-20 23:19:49 +0530155 /* class implement test controller */
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530156 private class TestController implements BgpController {
Shashikanth VH1ca26ce2015-11-20 23:19:49 +0530157 protected Set<BgpNodeListener> nodeListener = new CopyOnWriteArraySet<>();
Shashikanth VH1ca26ce2015-11-20 23:19:49 +0530158
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 VH5dd8dbe2015-11-26 13:22:18 +0530170 public Iterable<BgpPeer> getPeers() {
Shashikanth VH1ca26ce2015-11-20 23:19:49 +0530171 // TODO Auto-generated method stub
172 return null;
173 }
174
175 @Override
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530176 public BgpPeer getPeer(BgpId bgpId) {
Shashikanth VH1ca26ce2015-11-20 23:19:49 +0530177 // TODO Auto-generated method stub
178 return null;
179 }
180
181 @Override
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530182 public void writeMsg(BgpId bgpId, BgpMessage msg) {
Shashikanth VH1ca26ce2015-11-20 23:19:49 +0530183 // TODO Auto-generated method stub
184
185 }
186
187 @Override
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530188 public void processBGPPacket(BgpId bgpId, BgpMessage msg) throws BgpParseException {
Shashikanth VH1ca26ce2015-11-20 23:19:49 +0530189 // 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 VH5dd8dbe2015-11-26 13:22:18 +0530200 public BgpCfg getConfig() {
Shashikanth VH1ca26ce2015-11-20 23:19:49 +0530201 // 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 VH5dd8dbe2015-11-26 13:22:18 +0530219 public Map<BgpId, BgpPeer> connectedPeers() {
Shashikanth VH1ca26ce2015-11-20 23:19:49 +0530220 // 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 VH1ca26ce2015-11-20 23:19:49 +0530229 }
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 VH5dd8dbe2015-11-26 13:22:18 +0530235 LinkedList<BgpValueType> subTlvs;
Shashikanth VH1ca26ce2015-11-20 23:19:49 +0530236 subTlvs = new LinkedList<>();
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530237 BgpValueType tlv = new AutonomousSystemTlv(100);
Shashikanth VH1ca26ce2015-11-20 23:19:49 +0530238 short deslength = AutonomousSystemTlv.LENGTH;
239 short desType = AutonomousSystemTlv.TYPE;
240
241 subTlvs.add(tlv);
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530242 BgpNodeLSIdentifier localNodeDescriptors = new BgpNodeLSIdentifier(new NodeDescriptors(subTlvs, deslength,
Shashikanth VH1ca26ce2015-11-20 23:19:49 +0530243 desType));
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530244 BgpNodeLSNlriVer4 nodeNlri = new BgpNodeLSNlriVer4(0, (byte) Constants.DIRECT, localNodeDescriptors, false,
Shashikanth VH1ca26ce2015-11-20 23:19:49 +0530245 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 VH5dd8dbe2015-11-26 13:22:18 +0530261 LinkedList<BgpValueType> subTlvs;
262 BgpValueType tlv = new AutonomousSystemTlv(10);
Shashikanth VH1ca26ce2015-11-20 23:19:49 +0530263 short deslength = AutonomousSystemTlv.LENGTH;
264 short desType = AutonomousSystemTlv.TYPE;
265
266 subTlvs = new LinkedList<>();
267 subTlvs.add(tlv);
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530268 BgpNodeLSIdentifier localNodeDescriptors = new BgpNodeLSIdentifier(new NodeDescriptors(subTlvs, deslength,
Shashikanth VH1ca26ce2015-11-20 23:19:49 +0530269 desType));
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530270 BgpNodeLSNlriVer4 nodeNlri = new BgpNodeLSNlriVer4(0, (byte) Constants.DIRECT, localNodeDescriptors, false,
Shashikanth VH1ca26ce2015-11-20 23:19:49 +0530271 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 VH5dd8dbe2015-11-26 13:22:18 +0530283 LinkedList<BgpValueType> subTlvs;
284 BgpValueType tlv = new AutonomousSystemTlv(10);
Shashikanth VH1ca26ce2015-11-20 23:19:49 +0530285 short deslength = AutonomousSystemTlv.LENGTH;
286 short desType = AutonomousSystemTlv.TYPE;
287
288 subTlvs = new LinkedList<>();
289 subTlvs.add(tlv);
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530290 BgpNodeLSIdentifier localNodeDescriptors = new BgpNodeLSIdentifier(new NodeDescriptors(subTlvs, deslength,
Shashikanth VH1ca26ce2015-11-20 23:19:49 +0530291 desType));
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530292 BgpNodeLSNlriVer4 nodeNlri = new BgpNodeLSNlriVer4(0, (byte) Constants.DIRECT, localNodeDescriptors, false,
Shashikanth VH1ca26ce2015-11-20 23:19:49 +0530293 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 VH1ca26ce2015-11-20 23:19:49 +0530301}