Browse Source

Add GoBGP data source

Sebastian Lohff 1 year ago
parent
commit
87642cc4d9

+ 1
- 0
.gitignore View File

@@ -4,3 +4,4 @@
4 4
 .*.swo
5 5
 db.sqlite3
6 6
 dnmapper/settings.py
7
+__pycache__

+ 0
- 0
backend/__init__.py View File


+ 65
- 0
backend/gobgp.py View File

@@ -0,0 +1,65 @@
1
+import grpc
2
+
3
+from backend.gobgp_api import gobgp_pb2_grpc, attribute_pb2
4
+from backend.gobgp_api.gobgp_pb2 import TableType, Family, ListPeerRequest, ListPathRequest
5
+
6
+
7
+def get_bgp_data(gobgp_host):
8
+    _timeout = 10
9
+
10
+    channel = grpc.insecure_channel('{}:50051'.format(gobgp_host))
11
+    stub = gobgp_pb2_grpc.GobgpApiStub(channel)
12
+    data = []
13
+
14
+    peers = stub.ListPeer(ListPeerRequest(), _timeout)
15
+    for peer in peers:
16
+        entry = {
17
+            "local_id": peer.peer.state.router_id,
18
+            "local_as": peer.peer.conf.peer_as,
19
+            "peers": [],  # we don't export any peers
20
+            "routes": [],
21
+        }
22
+        neigh = peer.peer.conf.neighbor_address
23
+
24
+        for af in [Family.Afi.AFI_IP, Family.Afi.AFI_IP6]:
25
+            req = ListPathRequest(
26
+                name=neigh,
27
+                table_type=TableType.ADJ_IN,
28
+                family=Family(afi=af, safi=Family.Safi.SAFI_UNICAST),
29
+                sort_type=ListPathRequest.SortType.PREFIX)
30
+            rib = stub.ListPath(req)
31
+            for dest in rib:
32
+                prefix = dest.destination.prefix
33
+                for path in dest.destination.paths:
34
+                    as_path = []
35
+                    next_hop = "<unknown>"  # currently here to prevent error from NOT NULL constraint
36
+
37
+                    # parse attrs
38
+                    for pattr in path.pattrs:
39
+                        if pattr.type_url == "type.googleapis.com/gobgpapi.NextHopAttribute":
40
+                            nh = attribute_pb2.NextHopAttribute()
41
+                            nh.ParseFromString(pattr.value)
42
+                            next_hop = nh.next_hop
43
+                        elif pattr.type_url == "type.googleapis.com/gobgpapi.AsPathAttribute":
44
+                            asp = attribute_pb2.AsPathAttribute()
45
+                            asp.ParseFromString(pattr.value)
46
+                            for seg in asp.segments:
47
+                                if seg.type == 2:
48
+                                    as_path = seg.numbers
49
+                        elif pattr.type_url == "type.googleapis.com/gobgpapi.MpReachNLRIAttribute":
50
+                            mpreach = attribute_pb2.MpReachNLRIAttribute()
51
+                            mpreach.ParseFromString(pattr.value)
52
+                            next_hop = mpreach.next_hops[0]
53
+
54
+                    if not as_path:
55
+                        continue
56
+
57
+                    route = {
58
+                        "prefix": prefix,
59
+                        "path": as_path,
60
+                        "nexthop": next_hop,
61
+                    }
62
+                entry["routes"].append(route)
63
+
64
+        data.append(entry)
65
+    return data

+ 0
- 0
backend/gobgp_api/__init__.py View File


+ 4912
- 0
backend/gobgp_api/attribute_pb2.py
File diff suppressed because it is too large
View File


+ 3
- 0
backend/gobgp_api/attribute_pb2_grpc.py View File

@@ -0,0 +1,3 @@
1
+# Generated by the gRPC Python protocol compiler plugin. DO NOT EDIT!
2
+import grpc
3
+

+ 692
- 0
backend/gobgp_api/capability_pb2.py View File

@@ -0,0 +1,692 @@
1
+# -*- coding: utf-8 -*-
2
+# Generated by the protocol buffer compiler.  DO NOT EDIT!
3
+# source: capability.proto
4
+
5
+from google.protobuf.internal import enum_type_wrapper
6
+from google.protobuf import descriptor as _descriptor
7
+from google.protobuf import message as _message
8
+from google.protobuf import reflection as _reflection
9
+from google.protobuf import symbol_database as _symbol_database
10
+# @@protoc_insertion_point(imports)
11
+
12
+_sym_db = _symbol_database.Default()
13
+
14
+
15
+import gobgp_pb2 as gobgp__pb2
16
+
17
+
18
+DESCRIPTOR = _descriptor.FileDescriptor(
19
+  name='capability.proto',
20
+  package='gobgpapi',
21
+  syntax='proto3',
22
+  serialized_options=None,
23
+  serialized_pb=b'\n\x10\x63\x61pability.proto\x12\x08gobgpapi\x1a\x0bgobgp.proto\";\n\x17MultiProtocolCapability\x12 \n\x06\x66\x61mily\x18\x01 \x01(\x0b\x32\x10.gobgpapi.Family\"\x18\n\x16RouteRefreshCapability\"\x1d\n\x1b\x43\x61rryingLabelInfoCapability\"q\n\x1e\x45xtendedNexthopCapabilityTuple\x12%\n\x0bnlri_family\x18\x01 \x01(\x0b\x32\x10.gobgpapi.Family\x12(\n\x0enexthop_family\x18\x02 \x01(\x0b\x32\x10.gobgpapi.Family\"U\n\x19\x45xtendedNexthopCapability\x12\x38\n\x06tuples\x18\x01 \x03(\x0b\x32(.gobgpapi.ExtendedNexthopCapabilityTuple\"Q\n\x1eGracefulRestartCapabilityTuple\x12 \n\x06\x66\x61mily\x18\x01 \x01(\x0b\x32\x10.gobgpapi.Family\x12\r\n\x05\x66lags\x18\x02 \x01(\r\"r\n\x19GracefulRestartCapability\x12\r\n\x05\x66lags\x18\x01 \x01(\r\x12\x0c\n\x04time\x18\x02 \x01(\r\x12\x38\n\x06tuples\x18\x03 \x03(\x0b\x32(.gobgpapi.GracefulRestartCapabilityTuple\")\n\x1b\x46ourOctetASNumberCapability\x12\n\n\x02\x61s\x18\x01 \x01(\r\"_\n\x16\x41\x64\x64PathCapabilityTuple\x12 \n\x06\x66\x61mily\x18\x01 \x01(\x0b\x32\x10.gobgpapi.Family\x12#\n\x04mode\x18\x02 \x01(\x0e\x32\x15.gobgpapi.AddPathMode\"E\n\x11\x41\x64\x64PathCapability\x12\x30\n\x06tuples\x18\x01 \x03(\x0b\x32 .gobgpapi.AddPathCapabilityTuple\" \n\x1e\x45nhancedRouteRefreshCapability\"h\n\'LongLivedGracefulRestartCapabilityTuple\x12 \n\x06\x66\x61mily\x18\x01 \x01(\x0b\x32\x10.gobgpapi.Family\x12\r\n\x05\x66lags\x18\x02 \x01(\r\x12\x0c\n\x04time\x18\x03 \x01(\r\"g\n\"LongLivedGracefulRestartCapability\x12\x41\n\x06tuples\x18\x01 \x03(\x0b\x32\x31.gobgpapi.LongLivedGracefulRestartCapabilityTuple\"\x1d\n\x1bRouteRefreshCiscoCapability\"0\n\x11UnknownCapability\x12\x0c\n\x04\x63ode\x18\x01 \x01(\r\x12\r\n\x05value\x18\x02 \x01(\x0c*L\n\x0b\x41\x64\x64PathMode\x12\r\n\tMODE_NONE\x10\x00\x12\x10\n\x0cMODE_RECEIVE\x10\x01\x12\r\n\tMODE_SEND\x10\x02\x12\r\n\tMODE_BOTH\x10\x03\x62\x06proto3'
24
+  ,
25
+  dependencies=[gobgp__pb2.DESCRIPTOR,])
26
+
27
+_ADDPATHMODE = _descriptor.EnumDescriptor(
28
+  name='AddPathMode',
29
+  full_name='gobgpapi.AddPathMode',
30
+  filename=None,
31
+  file=DESCRIPTOR,
32
+  values=[
33
+    _descriptor.EnumValueDescriptor(
34
+      name='MODE_NONE', index=0, number=0,
35
+      serialized_options=None,
36
+      type=None),
37
+    _descriptor.EnumValueDescriptor(
38
+      name='MODE_RECEIVE', index=1, number=1,
39
+      serialized_options=None,
40
+      type=None),
41
+    _descriptor.EnumValueDescriptor(
42
+      name='MODE_SEND', index=2, number=2,
43
+      serialized_options=None,
44
+      type=None),
45
+    _descriptor.EnumValueDescriptor(
46
+      name='MODE_BOTH', index=3, number=3,
47
+      serialized_options=None,
48
+      type=None),
49
+  ],
50
+  containing_type=None,
51
+  serialized_options=None,
52
+  serialized_start=1099,
53
+  serialized_end=1175,
54
+)
55
+_sym_db.RegisterEnumDescriptor(_ADDPATHMODE)
56
+
57
+AddPathMode = enum_type_wrapper.EnumTypeWrapper(_ADDPATHMODE)
58
+MODE_NONE = 0
59
+MODE_RECEIVE = 1
60
+MODE_SEND = 2
61
+MODE_BOTH = 3
62
+
63
+
64
+
65
+_MULTIPROTOCOLCAPABILITY = _descriptor.Descriptor(
66
+  name='MultiProtocolCapability',
67
+  full_name='gobgpapi.MultiProtocolCapability',
68
+  filename=None,
69
+  file=DESCRIPTOR,
70
+  containing_type=None,
71
+  fields=[
72
+    _descriptor.FieldDescriptor(
73
+      name='family', full_name='gobgpapi.MultiProtocolCapability.family', index=0,
74
+      number=1, type=11, cpp_type=10, label=1,
75
+      has_default_value=False, default_value=None,
76
+      message_type=None, enum_type=None, containing_type=None,
77
+      is_extension=False, extension_scope=None,
78
+      serialized_options=None, file=DESCRIPTOR),
79
+  ],
80
+  extensions=[
81
+  ],
82
+  nested_types=[],
83
+  enum_types=[
84
+  ],
85
+  serialized_options=None,
86
+  is_extendable=False,
87
+  syntax='proto3',
88
+  extension_ranges=[],
89
+  oneofs=[
90
+  ],
91
+  serialized_start=43,
92
+  serialized_end=102,
93
+)
94
+
95
+
96
+_ROUTEREFRESHCAPABILITY = _descriptor.Descriptor(
97
+  name='RouteRefreshCapability',
98
+  full_name='gobgpapi.RouteRefreshCapability',
99
+  filename=None,
100
+  file=DESCRIPTOR,
101
+  containing_type=None,
102
+  fields=[
103
+  ],
104
+  extensions=[
105
+  ],
106
+  nested_types=[],
107
+  enum_types=[
108
+  ],
109
+  serialized_options=None,
110
+  is_extendable=False,
111
+  syntax='proto3',
112
+  extension_ranges=[],
113
+  oneofs=[
114
+  ],
115
+  serialized_start=104,
116
+  serialized_end=128,
117
+)
118
+
119
+
120
+_CARRYINGLABELINFOCAPABILITY = _descriptor.Descriptor(
121
+  name='CarryingLabelInfoCapability',
122
+  full_name='gobgpapi.CarryingLabelInfoCapability',
123
+  filename=None,
124
+  file=DESCRIPTOR,
125
+  containing_type=None,
126
+  fields=[
127
+  ],
128
+  extensions=[
129
+  ],
130
+  nested_types=[],
131
+  enum_types=[
132
+  ],
133
+  serialized_options=None,
134
+  is_extendable=False,
135
+  syntax='proto3',
136
+  extension_ranges=[],
137
+  oneofs=[
138
+  ],
139
+  serialized_start=130,
140
+  serialized_end=159,
141
+)
142
+
143
+
144
+_EXTENDEDNEXTHOPCAPABILITYTUPLE = _descriptor.Descriptor(
145
+  name='ExtendedNexthopCapabilityTuple',
146
+  full_name='gobgpapi.ExtendedNexthopCapabilityTuple',
147
+  filename=None,
148
+  file=DESCRIPTOR,
149
+  containing_type=None,
150
+  fields=[
151
+    _descriptor.FieldDescriptor(
152
+      name='nlri_family', full_name='gobgpapi.ExtendedNexthopCapabilityTuple.nlri_family', index=0,
153
+      number=1, type=11, cpp_type=10, label=1,
154
+      has_default_value=False, default_value=None,
155
+      message_type=None, enum_type=None, containing_type=None,
156
+      is_extension=False, extension_scope=None,
157
+      serialized_options=None, file=DESCRIPTOR),
158
+    _descriptor.FieldDescriptor(
159
+      name='nexthop_family', full_name='gobgpapi.ExtendedNexthopCapabilityTuple.nexthop_family', index=1,
160
+      number=2, type=11, cpp_type=10, label=1,
161
+      has_default_value=False, default_value=None,
162
+      message_type=None, enum_type=None, containing_type=None,
163
+      is_extension=False, extension_scope=None,
164
+      serialized_options=None, file=DESCRIPTOR),
165
+  ],
166
+  extensions=[
167
+  ],
168
+  nested_types=[],
169
+  enum_types=[
170
+  ],
171
+  serialized_options=None,
172
+  is_extendable=False,
173
+  syntax='proto3',
174
+  extension_ranges=[],
175
+  oneofs=[
176
+  ],
177
+  serialized_start=161,
178
+  serialized_end=274,
179
+)
180
+
181
+
182
+_EXTENDEDNEXTHOPCAPABILITY = _descriptor.Descriptor(
183
+  name='ExtendedNexthopCapability',
184
+  full_name='gobgpapi.ExtendedNexthopCapability',
185
+  filename=None,
186
+  file=DESCRIPTOR,
187
+  containing_type=None,
188
+  fields=[
189
+    _descriptor.FieldDescriptor(
190
+      name='tuples', full_name='gobgpapi.ExtendedNexthopCapability.tuples', index=0,
191
+      number=1, type=11, cpp_type=10, label=3,
192
+      has_default_value=False, default_value=[],
193
+      message_type=None, enum_type=None, containing_type=None,
194
+      is_extension=False, extension_scope=None,
195
+      serialized_options=None, file=DESCRIPTOR),
196
+  ],
197
+  extensions=[
198
+  ],
199
+  nested_types=[],
200
+  enum_types=[
201
+  ],
202
+  serialized_options=None,
203
+  is_extendable=False,
204
+  syntax='proto3',
205
+  extension_ranges=[],
206
+  oneofs=[
207
+  ],
208
+  serialized_start=276,
209
+  serialized_end=361,
210
+)
211
+
212
+
213
+_GRACEFULRESTARTCAPABILITYTUPLE = _descriptor.Descriptor(
214
+  name='GracefulRestartCapabilityTuple',
215
+  full_name='gobgpapi.GracefulRestartCapabilityTuple',
216
+  filename=None,
217
+  file=DESCRIPTOR,
218
+  containing_type=None,
219
+  fields=[
220
+    _descriptor.FieldDescriptor(
221
+      name='family', full_name='gobgpapi.GracefulRestartCapabilityTuple.family', index=0,
222
+      number=1, type=11, cpp_type=10, label=1,
223
+      has_default_value=False, default_value=None,
224
+      message_type=None, enum_type=None, containing_type=None,
225
+      is_extension=False, extension_scope=None,
226
+      serialized_options=None, file=DESCRIPTOR),
227
+    _descriptor.FieldDescriptor(
228
+      name='flags', full_name='gobgpapi.GracefulRestartCapabilityTuple.flags', index=1,
229
+      number=2, type=13, cpp_type=3, label=1,
230
+      has_default_value=False, default_value=0,
231
+      message_type=None, enum_type=None, containing_type=None,
232
+      is_extension=False, extension_scope=None,
233
+      serialized_options=None, file=DESCRIPTOR),
234
+  ],
235
+  extensions=[
236
+  ],
237
+  nested_types=[],
238
+  enum_types=[
239
+  ],
240
+  serialized_options=None,
241
+  is_extendable=False,
242
+  syntax='proto3',
243
+  extension_ranges=[],
244
+  oneofs=[
245
+  ],
246
+  serialized_start=363,
247
+  serialized_end=444,
248
+)
249
+
250
+
251
+_GRACEFULRESTARTCAPABILITY = _descriptor.Descriptor(
252
+  name='GracefulRestartCapability',
253
+  full_name='gobgpapi.GracefulRestartCapability',
254
+  filename=None,
255
+  file=DESCRIPTOR,
256
+  containing_type=None,
257
+  fields=[
258
+    _descriptor.FieldDescriptor(
259
+      name='flags', full_name='gobgpapi.GracefulRestartCapability.flags', index=0,
260
+      number=1, type=13, cpp_type=3, label=1,
261
+      has_default_value=False, default_value=0,
262
+      message_type=None, enum_type=None, containing_type=None,
263
+      is_extension=False, extension_scope=None,
264
+      serialized_options=None, file=DESCRIPTOR),
265
+    _descriptor.FieldDescriptor(
266
+      name='time', full_name='gobgpapi.GracefulRestartCapability.time', index=1,
267
+      number=2, type=13, cpp_type=3, label=1,
268
+      has_default_value=False, default_value=0,
269
+      message_type=None, enum_type=None, containing_type=None,
270
+      is_extension=False, extension_scope=None,
271
+      serialized_options=None, file=DESCRIPTOR),
272
+    _descriptor.FieldDescriptor(
273
+      name='tuples', full_name='gobgpapi.GracefulRestartCapability.tuples', index=2,
274
+      number=3, type=11, cpp_type=10, label=3,
275
+      has_default_value=False, default_value=[],
276
+      message_type=None, enum_type=None, containing_type=None,
277
+      is_extension=False, extension_scope=None,
278
+      serialized_options=None, file=DESCRIPTOR),
279
+  ],
280
+  extensions=[
281
+  ],
282
+  nested_types=[],
283
+  enum_types=[
284
+  ],
285
+  serialized_options=None,
286
+  is_extendable=False,
287
+  syntax='proto3',
288
+  extension_ranges=[],
289
+  oneofs=[
290
+  ],
291
+  serialized_start=446,
292
+  serialized_end=560,
293
+)
294
+
295
+
296
+_FOUROCTETASNUMBERCAPABILITY = _descriptor.Descriptor(
297
+  name='FourOctetASNumberCapability',
298
+  full_name='gobgpapi.FourOctetASNumberCapability',
299
+  filename=None,
300
+  file=DESCRIPTOR,
301
+  containing_type=None,
302
+  fields=[
303
+    _descriptor.FieldDescriptor(
304
+      name='as', full_name='gobgpapi.FourOctetASNumberCapability.as', index=0,
305
+      number=1, type=13, cpp_type=3, label=1,
306
+      has_default_value=False, default_value=0,
307
+      message_type=None, enum_type=None, containing_type=None,
308
+      is_extension=False, extension_scope=None,
309
+      serialized_options=None, file=DESCRIPTOR),
310
+  ],
311
+  extensions=[
312
+  ],
313
+  nested_types=[],
314
+  enum_types=[
315
+  ],
316
+  serialized_options=None,
317
+  is_extendable=False,
318
+  syntax='proto3',
319
+  extension_ranges=[],
320
+  oneofs=[
321
+  ],
322
+  serialized_start=562,
323
+  serialized_end=603,
324
+)
325
+
326
+
327
+_ADDPATHCAPABILITYTUPLE = _descriptor.Descriptor(
328
+  name='AddPathCapabilityTuple',
329
+  full_name='gobgpapi.AddPathCapabilityTuple',
330
+  filename=None,
331
+  file=DESCRIPTOR,
332
+  containing_type=None,
333
+  fields=[
334
+    _descriptor.FieldDescriptor(
335
+      name='family', full_name='gobgpapi.AddPathCapabilityTuple.family', index=0,
336
+      number=1, type=11, cpp_type=10, label=1,
337
+      has_default_value=False, default_value=None,
338
+      message_type=None, enum_type=None, containing_type=None,
339
+      is_extension=False, extension_scope=None,
340
+      serialized_options=None, file=DESCRIPTOR),
341
+    _descriptor.FieldDescriptor(
342
+      name='mode', full_name='gobgpapi.AddPathCapabilityTuple.mode', index=1,
343
+      number=2, type=14, cpp_type=8, label=1,
344
+      has_default_value=False, default_value=0,
345
+      message_type=None, enum_type=None, containing_type=None,
346
+      is_extension=False, extension_scope=None,
347
+      serialized_options=None, file=DESCRIPTOR),
348
+  ],
349
+  extensions=[
350
+  ],
351
+  nested_types=[],
352
+  enum_types=[
353
+  ],
354
+  serialized_options=None,
355
+  is_extendable=False,
356
+  syntax='proto3',
357
+  extension_ranges=[],
358
+  oneofs=[
359
+  ],
360
+  serialized_start=605,
361
+  serialized_end=700,
362
+)
363
+
364
+
365
+_ADDPATHCAPABILITY = _descriptor.Descriptor(
366
+  name='AddPathCapability',
367
+  full_name='gobgpapi.AddPathCapability',
368
+  filename=None,
369
+  file=DESCRIPTOR,
370
+  containing_type=None,
371
+  fields=[
372
+    _descriptor.FieldDescriptor(
373
+      name='tuples', full_name='gobgpapi.AddPathCapability.tuples', index=0,
374
+      number=1, type=11, cpp_type=10, label=3,
375
+      has_default_value=False, default_value=[],
376
+      message_type=None, enum_type=None, containing_type=None,
377
+      is_extension=False, extension_scope=None,
378
+      serialized_options=None, file=DESCRIPTOR),
379
+  ],
380
+  extensions=[
381
+  ],
382
+  nested_types=[],
383
+  enum_types=[
384
+  ],
385
+  serialized_options=None,
386
+  is_extendable=False,
387
+  syntax='proto3',
388
+  extension_ranges=[],
389
+  oneofs=[
390
+  ],
391
+  serialized_start=702,
392
+  serialized_end=771,
393
+)
394
+
395
+
396
+_ENHANCEDROUTEREFRESHCAPABILITY = _descriptor.Descriptor(
397
+  name='EnhancedRouteRefreshCapability',
398
+  full_name='gobgpapi.EnhancedRouteRefreshCapability',
399
+  filename=None,
400
+  file=DESCRIPTOR,
401
+  containing_type=None,
402
+  fields=[
403
+  ],
404
+  extensions=[
405
+  ],
406
+  nested_types=[],
407
+  enum_types=[
408
+  ],
409
+  serialized_options=None,
410
+  is_extendable=False,
411
+  syntax='proto3',
412
+  extension_ranges=[],
413
+  oneofs=[
414
+  ],
415
+  serialized_start=773,
416
+  serialized_end=805,
417
+)
418
+
419
+
420
+_LONGLIVEDGRACEFULRESTARTCAPABILITYTUPLE = _descriptor.Descriptor(
421
+  name='LongLivedGracefulRestartCapabilityTuple',
422
+  full_name='gobgpapi.LongLivedGracefulRestartCapabilityTuple',
423
+  filename=None,
424
+  file=DESCRIPTOR,
425
+  containing_type=None,
426
+  fields=[
427
+    _descriptor.FieldDescriptor(
428
+      name='family', full_name='gobgpapi.LongLivedGracefulRestartCapabilityTuple.family', index=0,
429
+      number=1, type=11, cpp_type=10, label=1,
430
+      has_default_value=False, default_value=None,
431
+      message_type=None, enum_type=None, containing_type=None,
432
+      is_extension=False, extension_scope=None,
433
+      serialized_options=None, file=DESCRIPTOR),
434
+    _descriptor.FieldDescriptor(
435
+      name='flags', full_name='gobgpapi.LongLivedGracefulRestartCapabilityTuple.flags', index=1,
436
+      number=2, type=13, cpp_type=3, label=1,
437
+      has_default_value=False, default_value=0,
438
+      message_type=None, enum_type=None, containing_type=None,
439
+      is_extension=False, extension_scope=None,
440
+      serialized_options=None, file=DESCRIPTOR),
441
+    _descriptor.FieldDescriptor(
442
+      name='time', full_name='gobgpapi.LongLivedGracefulRestartCapabilityTuple.time', index=2,
443
+      number=3, type=13, cpp_type=3, label=1,
444
+      has_default_value=False, default_value=0,
445
+      message_type=None, enum_type=None, containing_type=None,
446
+      is_extension=False, extension_scope=None,
447
+      serialized_options=None, file=DESCRIPTOR),
448
+  ],
449
+  extensions=[
450
+  ],
451
+  nested_types=[],
452
+  enum_types=[
453
+  ],
454
+  serialized_options=None,
455
+  is_extendable=False,
456
+  syntax='proto3',
457
+  extension_ranges=[],
458
+  oneofs=[
459
+  ],
460
+  serialized_start=807,
461
+  serialized_end=911,
462
+)
463
+
464
+
465
+_LONGLIVEDGRACEFULRESTARTCAPABILITY = _descriptor.Descriptor(
466
+  name='LongLivedGracefulRestartCapability',
467
+  full_name='gobgpapi.LongLivedGracefulRestartCapability',
468
+  filename=None,
469
+  file=DESCRIPTOR,
470
+  containing_type=None,
471
+  fields=[
472
+    _descriptor.FieldDescriptor(
473
+      name='tuples', full_name='gobgpapi.LongLivedGracefulRestartCapability.tuples', index=0,
474
+      number=1, type=11, cpp_type=10, label=3,
475
+      has_default_value=False, default_value=[],
476
+      message_type=None, enum_type=None, containing_type=None,
477
+      is_extension=False, extension_scope=None,
478
+      serialized_options=None, file=DESCRIPTOR),
479
+  ],
480
+  extensions=[
481
+  ],
482
+  nested_types=[],
483
+  enum_types=[
484
+  ],
485
+  serialized_options=None,
486
+  is_extendable=False,
487
+  syntax='proto3',
488
+  extension_ranges=[],
489
+  oneofs=[
490
+  ],
491
+  serialized_start=913,
492
+  serialized_end=1016,
493
+)
494
+
495
+
496
+_ROUTEREFRESHCISCOCAPABILITY = _descriptor.Descriptor(
497
+  name='RouteRefreshCiscoCapability',
498
+  full_name='gobgpapi.RouteRefreshCiscoCapability',
499
+  filename=None,
500
+  file=DESCRIPTOR,
501
+  containing_type=None,
502
+  fields=[
503
+  ],
504
+  extensions=[
505
+  ],
506
+  nested_types=[],
507
+  enum_types=[
508
+  ],
509
+  serialized_options=None,
510
+  is_extendable=False,
511
+  syntax='proto3',
512
+  extension_ranges=[],
513
+  oneofs=[
514
+  ],
515
+  serialized_start=1018,
516
+  serialized_end=1047,
517
+)
518
+
519
+
520
+_UNKNOWNCAPABILITY = _descriptor.Descriptor(
521
+  name='UnknownCapability',
522
+  full_name='gobgpapi.UnknownCapability',
523
+  filename=None,
524
+  file=DESCRIPTOR,
525
+  containing_type=None,
526
+  fields=[
527
+    _descriptor.FieldDescriptor(
528
+      name='code', full_name='gobgpapi.UnknownCapability.code', index=0,
529
+      number=1, type=13, cpp_type=3, label=1,
530
+      has_default_value=False, default_value=0,
531
+      message_type=None, enum_type=None, containing_type=None,
532
+      is_extension=False, extension_scope=None,
533
+      serialized_options=None, file=DESCRIPTOR),
534
+    _descriptor.FieldDescriptor(
535
+      name='value', full_name='gobgpapi.UnknownCapability.value', index=1,
536
+      number=2, type=12, cpp_type=9, label=1,
537
+      has_default_value=False, default_value=b"",
538
+      message_type=None, enum_type=None, containing_type=None,
539
+      is_extension=False, extension_scope=None,
540
+      serialized_options=None, file=DESCRIPTOR),
541
+  ],
542
+  extensions=[
543
+  ],
544
+  nested_types=[],
545
+  enum_types=[
546
+  ],
547
+  serialized_options=None,
548
+  is_extendable=False,
549
+  syntax='proto3',
550
+  extension_ranges=[],
551
+  oneofs=[
552
+  ],
553
+  serialized_start=1049,
554
+  serialized_end=1097,
555
+)
556
+
557
+_MULTIPROTOCOLCAPABILITY.fields_by_name['family'].message_type = gobgp__pb2._FAMILY
558
+_EXTENDEDNEXTHOPCAPABILITYTUPLE.fields_by_name['nlri_family'].message_type = gobgp__pb2._FAMILY
559
+_EXTENDEDNEXTHOPCAPABILITYTUPLE.fields_by_name['nexthop_family'].message_type = gobgp__pb2._FAMILY
560
+_EXTENDEDNEXTHOPCAPABILITY.fields_by_name['tuples'].message_type = _EXTENDEDNEXTHOPCAPABILITYTUPLE
561
+_GRACEFULRESTARTCAPABILITYTUPLE.fields_by_name['family'].message_type = gobgp__pb2._FAMILY
562
+_GRACEFULRESTARTCAPABILITY.fields_by_name['tuples'].message_type = _GRACEFULRESTARTCAPABILITYTUPLE
563
+_ADDPATHCAPABILITYTUPLE.fields_by_name['family'].message_type = gobgp__pb2._FAMILY
564
+_ADDPATHCAPABILITYTUPLE.fields_by_name['mode'].enum_type = _ADDPATHMODE
565
+_ADDPATHCAPABILITY.fields_by_name['tuples'].message_type = _ADDPATHCAPABILITYTUPLE
566
+_LONGLIVEDGRACEFULRESTARTCAPABILITYTUPLE.fields_by_name['family'].message_type = gobgp__pb2._FAMILY
567
+_LONGLIVEDGRACEFULRESTARTCAPABILITY.fields_by_name['tuples'].message_type = _LONGLIVEDGRACEFULRESTARTCAPABILITYTUPLE
568
+DESCRIPTOR.message_types_by_name['MultiProtocolCapability'] = _MULTIPROTOCOLCAPABILITY
569
+DESCRIPTOR.message_types_by_name['RouteRefreshCapability'] = _ROUTEREFRESHCAPABILITY
570
+DESCRIPTOR.message_types_by_name['CarryingLabelInfoCapability'] = _CARRYINGLABELINFOCAPABILITY
571
+DESCRIPTOR.message_types_by_name['ExtendedNexthopCapabilityTuple'] = _EXTENDEDNEXTHOPCAPABILITYTUPLE
572
+DESCRIPTOR.message_types_by_name['ExtendedNexthopCapability'] = _EXTENDEDNEXTHOPCAPABILITY
573
+DESCRIPTOR.message_types_by_name['GracefulRestartCapabilityTuple'] = _GRACEFULRESTARTCAPABILITYTUPLE
574
+DESCRIPTOR.message_types_by_name['GracefulRestartCapability'] = _GRACEFULRESTARTCAPABILITY
575
+DESCRIPTOR.message_types_by_name['FourOctetASNumberCapability'] = _FOUROCTETASNUMBERCAPABILITY
576
+DESCRIPTOR.message_types_by_name['AddPathCapabilityTuple'] = _ADDPATHCAPABILITYTUPLE
577
+DESCRIPTOR.message_types_by_name['AddPathCapability'] = _ADDPATHCAPABILITY
578
+DESCRIPTOR.message_types_by_name['EnhancedRouteRefreshCapability'] = _ENHANCEDROUTEREFRESHCAPABILITY
579
+DESCRIPTOR.message_types_by_name['LongLivedGracefulRestartCapabilityTuple'] = _LONGLIVEDGRACEFULRESTARTCAPABILITYTUPLE
580
+DESCRIPTOR.message_types_by_name['LongLivedGracefulRestartCapability'] = _LONGLIVEDGRACEFULRESTARTCAPABILITY
581
+DESCRIPTOR.message_types_by_name['RouteRefreshCiscoCapability'] = _ROUTEREFRESHCISCOCAPABILITY
582
+DESCRIPTOR.message_types_by_name['UnknownCapability'] = _UNKNOWNCAPABILITY
583
+DESCRIPTOR.enum_types_by_name['AddPathMode'] = _ADDPATHMODE
584
+_sym_db.RegisterFileDescriptor(DESCRIPTOR)
585
+
586
+MultiProtocolCapability = _reflection.GeneratedProtocolMessageType('MultiProtocolCapability', (_message.Message,), {
587
+  'DESCRIPTOR' : _MULTIPROTOCOLCAPABILITY,
588
+  '__module__' : 'capability_pb2'
589
+  # @@protoc_insertion_point(class_scope:gobgpapi.MultiProtocolCapability)
590
+  })
591
+_sym_db.RegisterMessage(MultiProtocolCapability)
592
+
593
+RouteRefreshCapability = _reflection.GeneratedProtocolMessageType('RouteRefreshCapability', (_message.Message,), {
594
+  'DESCRIPTOR' : _ROUTEREFRESHCAPABILITY,
595
+  '__module__' : 'capability_pb2'
596
+  # @@protoc_insertion_point(class_scope:gobgpapi.RouteRefreshCapability)
597
+  })
598
+_sym_db.RegisterMessage(RouteRefreshCapability)
599
+
600
+CarryingLabelInfoCapability = _reflection.GeneratedProtocolMessageType('CarryingLabelInfoCapability', (_message.Message,), {
601
+  'DESCRIPTOR' : _CARRYINGLABELINFOCAPABILITY,
602
+  '__module__' : 'capability_pb2'
603
+  # @@protoc_insertion_point(class_scope:gobgpapi.CarryingLabelInfoCapability)
604
+  })
605
+_sym_db.RegisterMessage(CarryingLabelInfoCapability)
606
+
607
+ExtendedNexthopCapabilityTuple = _reflection.GeneratedProtocolMessageType('ExtendedNexthopCapabilityTuple', (_message.Message,), {
608
+  'DESCRIPTOR' : _EXTENDEDNEXTHOPCAPABILITYTUPLE,
609
+  '__module__' : 'capability_pb2'
610
+  # @@protoc_insertion_point(class_scope:gobgpapi.ExtendedNexthopCapabilityTuple)
611
+  })
612
+_sym_db.RegisterMessage(ExtendedNexthopCapabilityTuple)
613
+
614
+ExtendedNexthopCapability = _reflection.GeneratedProtocolMessageType('ExtendedNexthopCapability', (_message.Message,), {
615
+  'DESCRIPTOR' : _EXTENDEDNEXTHOPCAPABILITY,
616
+  '__module__' : 'capability_pb2'
617
+  # @@protoc_insertion_point(class_scope:gobgpapi.ExtendedNexthopCapability)
618
+  })
619
+_sym_db.RegisterMessage(ExtendedNexthopCapability)
620
+
621
+GracefulRestartCapabilityTuple = _reflection.GeneratedProtocolMessageType('GracefulRestartCapabilityTuple', (_message.Message,), {
622
+  'DESCRIPTOR' : _GRACEFULRESTARTCAPABILITYTUPLE,
623
+  '__module__' : 'capability_pb2'
624
+  # @@protoc_insertion_point(class_scope:gobgpapi.GracefulRestartCapabilityTuple)
625
+  })
626
+_sym_db.RegisterMessage(GracefulRestartCapabilityTuple)
627
+
628
+GracefulRestartCapability = _reflection.GeneratedProtocolMessageType('GracefulRestartCapability', (_message.Message,), {
629
+  'DESCRIPTOR' : _GRACEFULRESTARTCAPABILITY,
630
+  '__module__' : 'capability_pb2'
631
+  # @@protoc_insertion_point(class_scope:gobgpapi.GracefulRestartCapability)
632
+  })
633
+_sym_db.RegisterMessage(GracefulRestartCapability)
634
+
635
+FourOctetASNumberCapability = _reflection.GeneratedProtocolMessageType('FourOctetASNumberCapability', (_message.Message,), {
636
+  'DESCRIPTOR' : _FOUROCTETASNUMBERCAPABILITY,
637
+  '__module__' : 'capability_pb2'
638
+  # @@protoc_insertion_point(class_scope:gobgpapi.FourOctetASNumberCapability)
639
+  })
640
+_sym_db.RegisterMessage(FourOctetASNumberCapability)
641
+
642
+AddPathCapabilityTuple = _reflection.GeneratedProtocolMessageType('AddPathCapabilityTuple', (_message.Message,), {
643
+  'DESCRIPTOR' : _ADDPATHCAPABILITYTUPLE,
644
+  '__module__' : 'capability_pb2'
645
+  # @@protoc_insertion_point(class_scope:gobgpapi.AddPathCapabilityTuple)
646
+  })
647
+_sym_db.RegisterMessage(AddPathCapabilityTuple)
648
+
649
+AddPathCapability = _reflection.GeneratedProtocolMessageType('AddPathCapability', (_message.Message,), {
650
+  'DESCRIPTOR' : _ADDPATHCAPABILITY,
651
+  '__module__' : 'capability_pb2'
652
+  # @@protoc_insertion_point(class_scope:gobgpapi.AddPathCapability)
653
+  })
654
+_sym_db.RegisterMessage(AddPathCapability)
655
+
656
+EnhancedRouteRefreshCapability = _reflection.GeneratedProtocolMessageType('EnhancedRouteRefreshCapability', (_message.Message,), {
657
+  'DESCRIPTOR' : _ENHANCEDROUTEREFRESHCAPABILITY,
658
+  '__module__' : 'capability_pb2'
659
+  # @@protoc_insertion_point(class_scope:gobgpapi.EnhancedRouteRefreshCapability)
660
+  })
661
+_sym_db.RegisterMessage(EnhancedRouteRefreshCapability)
662
+
663
+LongLivedGracefulRestartCapabilityTuple = _reflection.GeneratedProtocolMessageType('LongLivedGracefulRestartCapabilityTuple', (_message.Message,), {
664
+  'DESCRIPTOR' : _LONGLIVEDGRACEFULRESTARTCAPABILITYTUPLE,
665
+  '__module__' : 'capability_pb2'
666
+  # @@protoc_insertion_point(class_scope:gobgpapi.LongLivedGracefulRestartCapabilityTuple)
667
+  })
668
+_sym_db.RegisterMessage(LongLivedGracefulRestartCapabilityTuple)
669
+
670
+LongLivedGracefulRestartCapability = _reflection.GeneratedProtocolMessageType('LongLivedGracefulRestartCapability', (_message.Message,), {
671
+  'DESCRIPTOR' : _LONGLIVEDGRACEFULRESTARTCAPABILITY,
672
+  '__module__' : 'capability_pb2'
673
+  # @@protoc_insertion_point(class_scope:gobgpapi.LongLivedGracefulRestartCapability)
674
+  })
675
+_sym_db.RegisterMessage(LongLivedGracefulRestartCapability)
676
+
677
+RouteRefreshCiscoCapability = _reflection.GeneratedProtocolMessageType('RouteRefreshCiscoCapability', (_message.Message,), {
678
+  'DESCRIPTOR' : _ROUTEREFRESHCISCOCAPABILITY,
679
+  '__module__' : 'capability_pb2'
680
+  # @@protoc_insertion_point(class_scope:gobgpapi.RouteRefreshCiscoCapability)
681
+  })
682
+_sym_db.RegisterMessage(RouteRefreshCiscoCapability)
683
+
684
+UnknownCapability = _reflection.GeneratedProtocolMessageType('UnknownCapability', (_message.Message,), {
685
+  'DESCRIPTOR' : _UNKNOWNCAPABILITY,
686
+  '__module__' : 'capability_pb2'
687
+  # @@protoc_insertion_point(class_scope:gobgpapi.UnknownCapability)
688
+  })
689
+_sym_db.RegisterMessage(UnknownCapability)
690
+
691
+
692
+# @@protoc_insertion_point(module_scope)

+ 3
- 0
backend/gobgp_api/capability_pb2_grpc.py View File

@@ -0,0 +1,3 @@
1
+# Generated by the gRPC Python protocol compiler plugin. DO NOT EDIT!
2
+import grpc
3
+

+ 9250
- 0
backend/gobgp_api/gobgp_pb2.py
File diff suppressed because it is too large
View File


+ 1671
- 0
backend/gobgp_api/gobgp_pb2_grpc.py
File diff suppressed because it is too large
View File


+ 30
- 0
bgpdata/migrations/0017_auto_20200531_0200.py View File

@@ -0,0 +1,30 @@
1
+# -*- coding: utf-8 -*-
2
+# Generated by Django 1.11.25 on 2020-05-31 00:00
3
+from __future__ import unicode_literals
4
+
5
+from django.db import migrations, models
6
+
7
+
8
+class Migration(migrations.Migration):
9
+
10
+    dependencies = [
11
+        ('bgpdata', '0016_auto_20170117_0103'),
12
+    ]
13
+
14
+    operations = [
15
+        migrations.AlterField(
16
+            model_name='confighost',
17
+            name='checkMethod',
18
+            field=models.CharField(choices=[('CMK', 'Check MK'), ('PLAIN', 'Plain'), ('GOBGP', 'GoBGP')], max_length=10),
19
+        ),
20
+        migrations.AlterField(
21
+            model_name='crawllog',
22
+            name='severity',
23
+            field=models.CharField(choices=[('INFO', 'info'), ('ERROR', 'error'), ('DEBUG', 'debug'), ('WARN', 'warning')], max_length=10),
24
+        ),
25
+        migrations.AlterField(
26
+            model_name='peering',
27
+            name='origin',
28
+            field=models.CharField(choices=[('path', 'BGP Path'), ('direct', 'Direct Connection')], max_length=10),
29
+        ),
30
+    ]

+ 2
- 1
bgpdata/models.py View File

@@ -11,13 +11,14 @@ class ConfigHost(models.Model):
11 11
     CHECK_CHOICES = (
12 12
         ('CMK', "Check MK"),
13 13
         ('PLAIN', "Plain"),
14
+        ('GOBGP', "GoBGP"),
14 15
     )
15 16
 
16 17
     # asno, ip, check method,
17 18
     name = models.CharField(max_length=50)
18 19
     number = models.IntegerField()
19 20
     ip = models.GenericIPAddressField()
20
-    checkMethod = models.CharField(max_length=4, choices=CHECK_CHOICES)
21
+    checkMethod = models.CharField(max_length=10, choices=CHECK_CHOICES)
21 22
 
22 23
     def __str__(self):
23 24
         return "%s (%s / %s)" % (self.name, self.number, self.ip)

+ 100
- 86
bin/crawl.py View File

@@ -1,13 +1,10 @@
1
-#!/usr/bin/env python2
1
+#!/usr/bin/env python
2 2
 # This file is part of dnmapper, an AS--level mapping tool
3 3
 # Licensed under GNU General Public License v3 or later
4 4
 # Written by Sebastian Lohff (seba@someserver.de)
5 5
 
6 6
 from __future__ import print_function
7 7
 
8
-# config
9
-LAST_SEEN_DAYS = 7
10
-
11 8
 # prepare environment
12 9
 import sys
13 10
 sys.path.append("..")
@@ -23,6 +20,10 @@ from django.db.models import Q, Max
23 20
 
24 21
 from bgpdata.models import ConfigHost, CrawlRun, CrawlLog, AS, BorderRouter, Announcement, Peering, BorderRouterPair
25 22
 from routerparsers import getBGPData, RouterParserException
23
+from backend import gobgp
24
+
25
+# config
26
+LAST_SEEN_DAYS = 7
26 27
 
27 28
 
28 29
 def getOrCreateAS(crawl, number, online=True):
@@ -52,7 +53,9 @@ def main():
52 53
         print(" -- Getting data for host %s" % host)
53 54
         try:
54 55
             if host.checkMethod == 'CMK':
55
-                data = getBGPData(host.ip, host.number)
56
+                data = [getBGPData(host.ip, host.number)]
57
+            elif host.checkMethod == 'GOBGP':
58
+                data = gobgp.get_bgp_data(host.ip)
56 59
             else:
57 60
                 CrawlLog.log(crawl, "Method %s is not currently supported, skipping host" % host.checkMethod, host=host, severity=CrawlLog.ERROR)
58 61
                 continue
@@ -67,82 +70,8 @@ def main():
67 70
             CrawlLog.log(crawl, msg, host=host, severity=CrawlLog.ERROR)
68 71
             continue
69 72
 
70
-        print(" -- parsing...")
71
-
72
-        currASno = int(data["local_as"])
73
-        currAS = getOrCreateAS(crawl, currASno)
74
-
75
-        currAS.directlyCrawled = True
76
-        currAS.save()
77
-
78
-        currRouter = None
79
-        try:
80
-            currRouter = BorderRouter.objects.get(AS=currAS, routerID=data["local_id"])
81
-            currRouter.pingable = True
82
-            currRouter.reachable = True
83
-            currRouter.save()
84
-        except BorderRouter.DoesNotExist:
85
-            currRouter = BorderRouter(AS=currAS, routerID=data["local_id"], pingable=True, reachable=True)
86
-            currRouter.save()
87
-
88
-        print(" --> peers")
89
-        for peer in data["peers"]:
90
-            # peerings
91
-            # data: BGP{state, neighbor_id, neighbor_as}, description
92
-
93
-            # a) find/create neighbor
94
-            print(" ----> Peer:", int(peer["BGP"]["neighbor_as"]))
95
-            neighAS = getOrCreateAS(crawl, int(peer["BGP"]["neighbor_as"]), online=peer["BGP"]["online"])
96
-
97
-            # b) find out if a peering already exists (maybe where we only need to add our router id?)
98
-            peering = None
99
-            try:
100
-                peering = Peering.getPeering(currAS, neighAS)
101
-            except Peering.DoesNotExist:
102
-                peering = Peering(as1=currAS, as2=neighAS, origin=Peering.DIRECT)
103
-                peering.save()
104
-
105
-            # c) look for router/peering pairs
106
-            if peer["BGP"]["neighbor_id"]:
107
-                try:
108
-                    neighRouter = BorderRouter.objects.get(AS=neighAS, routerID=peer["BGP"]["neighbor_id"])
109
-                except BorderRouter.DoesNotExist:
110
-                    neighRouter = BorderRouter(AS=neighAS, routerID=peer["BGP"]["neighbor_id"], pingable=False, reachable=False)
111
-                    neighRouter.save()
112
-                try:
113
-                    BorderRouterPair.getPairing(peering, currRouter, neighRouter)
114
-                except BorderRouterPair.DoesNotExist:
115
-                    pairs = BorderRouterPair.objects.filter(Q(peering=peering) & (Q(router1=neighRouter, router2=None)|Q(router1=None, router2=neighRouter)))
116
-                    if pairs.count() > 0:
117
-                        pair = pairs[0]
118
-                        if pair.router1 == None:
119
-                            pair.router1 = currRouter
120
-                        else:
121
-                            pair.router2 = currRouter
122
-                        pair.save()
123
-                    else:
124
-                        pair = BorderRouterPair(peering=peering, router1=currRouter, router2=neighRouter)
125
-                        pair.save()
126
-
127
-        print(" --> Announcements")
128
-        if "routes" in data and data["routes"]:
129
-            for route in data["routes"]:
130
-                print(" ---->", route["prefix"])
131
-                if "/" not in route["prefix"]:
132
-                    continue
133
-
134
-                crawlAS = currAS
135
-                if len(route["path"]) > 0:
136
-                    crawlAS = getOrCreateAS(crawl, route["path"][0])
137
-                    originAS = getOrCreateAS(crawl, route["path"][-1])
138
-                ip, prefix = route["prefix"].split("/")
139
-                a = Announcement(router=currRouter, ip=ip, prefix=prefix,
140
-                                    ASPath=" ".join(route["path"]), nextHop=route["nexthop"],
141
-                                    crawlAS=crawlAS, originAS=originAS)
142
-                a.save()
143
-        else:
144
-            print(" !! No routes found in host output")
145
-            CrawlLog.log(crawl, "No routes found in host output (no bgp feed included?)", host=host, severity=CrawlLog.WARN)
73
+        for elem in data:
74
+            parseData(crawl, host, elem)
146 75
 
147 76
     # 3. calculate missing data
148 77
     print(" -- Adding extra data from announcements...")
@@ -165,10 +94,11 @@ def main():
165 94
                 firstAS = secondAS
166 95
 
167 96
     # 3.2 add ASses, routers and peerings from old crawlruns (last should suffice)
168
-    # find 
97
+    # find
169 98
     print(" --> copy old ASses")
170 99
     timerangeStart = crawl.startTime - datetime.timedelta(LAST_SEEN_DAYS)
171
-    oldASses = AS.objects.filter(online=True, crawl__startTime__gte=timerangeStart).values("number").annotate(lastSeen=Max('crawl_id')).filter(~Q(lastSeen=crawl.pk))
100
+    oldASses = AS.objects.filter(online=True, crawl__startTime__gte=timerangeStart) \
101
+                 .values("number").annotate(lastSeen=Max('crawl_id')).filter(~Q(lastSeen=crawl.pk))
172 102
 
173 103
     # 3.2.1. copy old asses
174 104
     print(" ----> create ASses")
@@ -181,7 +111,7 @@ def main():
181 111
             if not newAS.online and not newAS.lastSeen:
182 112
                 newAS.lastSeen = oldAS.crawl
183 113
                 newAS.save()
184
-        except:
114
+        except Exception:
185 115
             newAS = AS(number=oldAS.number, crawl=crawl, lastSeen=oldAS.crawl, directlyCrawled=False, online=False)
186 116
             newAS.save()
187 117
 
@@ -193,7 +123,7 @@ def main():
193 123
         for peering in oldAS.getPeerings():
194 124
             print(" --------> Peering %s <--> %s" % (peering.as1.number, peering.as2.number))
195 125
             peering = Peering(
196
-                        as1=AS.objects.get(number=peering.as1.number, crawl=crawl), 
126
+                        as1=AS.objects.get(number=peering.as1.number, crawl=crawl),
197 127
                         as2=AS.objects.get(number=peering.as2.number, crawl=crawl),
198 128
                         origin=peering.origin)
199 129
             peering.save()
@@ -208,11 +138,95 @@ def main():
208 138
     crawl.asCount = crawl.as_set.count()
209 139
     crawl.asOnlineCount = crawl.as_set.filter(online=True).count()
210 140
     crawl.asOfflineCount = crawl.as_set.filter(online=False).count()
211
-    crawl.peeringCount = Peering.objects.filter(Q(as1__crawl=crawl)|Q(as2__crawl=crawl)).count()
141
+    crawl.peeringCount = Peering.objects.filter(Q(as1__crawl=crawl) | Q(as2__crawl=crawl)).count()
212 142
     crawl.save()
213 143
 
214 144
     print(" !! Done")
215 145
     CrawlLog.log(crawl, "Crawl completed", severity=CrawlLog.INFO)
216 146
 
147
+
148
+def parseData(crawl, host, data):
149
+    print(" -- parsing...")
150
+
151
+    currASno = int(data["local_as"])
152
+    currAS = getOrCreateAS(crawl, currASno)
153
+
154
+    currAS.directlyCrawled = True
155
+    currAS.save()
156
+
157
+    currRouter = None
158
+    try:
159
+        currRouter = BorderRouter.objects.get(AS=currAS, routerID=data["local_id"])
160
+        currRouter.pingable = True
161
+        currRouter.reachable = True
162
+        currRouter.save()
163
+    except BorderRouter.DoesNotExist:
164
+        currRouter = BorderRouter(AS=currAS, routerID=data["local_id"], pingable=True, reachable=True)
165
+        currRouter.save()
166
+
167
+    print(" --> peers")
168
+    for peer in data["peers"]:
169
+        # peerings
170
+        # data: BGP{state, neighbor_id, neighbor_as}, description
171
+
172
+        # a) find/create neighbor
173
+        print(" ----> Peer:", int(peer["BGP"]["neighbor_as"]))
174
+        neighAS = getOrCreateAS(crawl, int(peer["BGP"]["neighbor_as"]), online=peer["BGP"]["online"])
175
+
176
+        # b) find out if a peering already exists (maybe where we only need to add our router id?)
177
+        peering = None
178
+        try:
179
+            peering = Peering.getPeering(currAS, neighAS)
180
+        except Peering.DoesNotExist:
181
+            peering = Peering(as1=currAS, as2=neighAS, origin=Peering.DIRECT)
182
+            peering.save()
183
+
184
+        # c) look for router/peering pairs
185
+        if peer["BGP"]["neighbor_id"]:
186
+            try:
187
+                neighRouter = BorderRouter.objects.get(AS=neighAS, routerID=peer["BGP"]["neighbor_id"])
188
+            except BorderRouter.DoesNotExist:
189
+                neighRouter = BorderRouter(AS=neighAS, routerID=peer["BGP"]["neighbor_id"],
190
+                                           pingable=False, reachable=False)
191
+                neighRouter.save()
192
+            try:
193
+                BorderRouterPair.getPairing(peering, currRouter, neighRouter)
194
+            except BorderRouterPair.DoesNotExist:
195
+                pairs = BorderRouterPair.objects.filter(Q(peering=peering) & (Q(router1=neighRouter, router2=None) |
196
+                                                                              Q(router1=None, router2=neighRouter)))
197
+                if pairs.count() > 0:
198
+                    pair = pairs[0]
199
+                    if pair.router1 is None:
200
+                        pair.router1 = currRouter
201
+                    else:
202
+                        pair.router2 = currRouter
203
+                    pair.save()
204
+                else:
205
+                    pair = BorderRouterPair(peering=peering, router1=currRouter, router2=neighRouter)
206
+                    pair.save()
207
+
208
+    print(" --> Announcements")
209
+    if "routes" in data and data["routes"]:
210
+        for route in data["routes"]:
211
+            print(" ---->", route["prefix"])
212
+            if "/" not in route["prefix"]:
213
+                continue
214
+
215
+            crawlAS = currAS
216
+            if len(route["path"]) > 0:
217
+                route["path"] = list(map(str, route["path"]))
218
+                crawlAS = getOrCreateAS(crawl, route["path"][0])
219
+                originAS = getOrCreateAS(crawl, route["path"][-1])
220
+            ip, prefix = route["prefix"].split("/")
221
+            a = Announcement(router=currRouter, ip=ip, prefix=prefix,
222
+                             ASPath=" ".join(route["path"]), nextHop=route["nexthop"],
223
+                             crawlAS=crawlAS, originAS=originAS)
224
+            a.save()
225
+    else:
226
+        print(" !! No routes found in host output")
227
+        CrawlLog.log(crawl, "No routes found in host output (no bgp feed included?)",
228
+                     host=host, severity=CrawlLog.WARN)
229
+
230
+
217 231
 if __name__ == '__main__':
218 232
     main()

Loading…
Cancel
Save