1 /*! \file bgp_example.py
6 """bgp_example.py: OpEN API Border Gateway Protocol (BGP) configuration example"""
12 from sys
import version_info
40 """Convert ipv4 string to integer"""
42 return struct.unpack(
"!I", socket.inet_aton(addr))[0]
45 """Convert ipv4 integer to string"""
47 return socket.inet_ntoa(struct.pack(
"!I", addr))
49 def ipv6_to_int(addr):
50 """Convert ipv6 string to integer"""
52 str_ = socket.inet_pton(socket.AF_INET6, addr)
53 a, b = struct.unpack(
'!2Q', str_)
56 def int_to_ipv6(addr):
57 """Convert ipv6 integer to string"""
60 b = addr & ((1 << 64) - 1)
61 return socket.inet_ntop(socket.AF_INET6, struct.pack(
'!2Q', a, b))
63 def ip_to_open_inet(address):
64 """ Convert ip address to integer and family and return as open_inet_addr_t """
67 addr = OpEN.open_inet_addr_t()
68 addr.family = OpEN.OPEN_AF_NONE
70 addr.addr.ipv4 = ip_to_int(address)
71 addr.family = OpEN.OPEN_AF_INET
72 result = OpEN.OPEN_E_NONE
77 addr.addr.ipv6 = ipv6_to_int(address)
78 addr.family = OpEN.OPEN_AF_INET6
79 result = OpEN.OPEN_E_NONE
82 result = OpEN.OPEN_E_PARAM
85 result = OpEN.OPEN_E_UNAVAIL
87 return (result, addr, type)
89 def print_sanity_results(result, test, msg, feat):
90 """Print overall comparison results"""
92 if result == OpEN.OPEN_E_UNAVAIL:
93 print (
"Sanity test skipped - %s - %s." % (msg, feat))
94 elif result == OpEN.OPEN_E_NONE
and test ==
True:
95 print (
"Sanity Success - %s - %s." % (msg, feat))
97 print (
"Sanity Failure - %s - %s." % (msg, feat))
99 def print_bad_result(result, msg):
100 """Print some general error messages if the result is bad"""
102 if result == OpEN.OPEN_E_UNAVAIL:
103 print (
"Feature not supported - %s (err %d)." % (msg, result))
104 elif result != OpEN.OPEN_E_NONE:
105 print (
"Test Failure - %s (err %d)." % (msg, result))
107 def get_first_routing_protocol_name(client_handle):
108 """ Get the first available routing protocol name. """
110 max_proto_len_p = OpEN.new_uint32_tp()
112 result = OpEN.openapiRouteProtoNameLenMax(client_handle, max_proto_len_p)
113 print_bad_result(result,
'openapiRouteProtoNameLenMax')
114 if result == OpEN.OPEN_E_NONE:
115 max_proto_len = OpEN.uint32_tp_value(max_proto_len_p) + 1
117 proto_buff_string = open_.getStringBuffer(max_proto_len)
118 except OpENBufferSizeError:
119 print(
"get_first_routing_protocol_name: getStringBuffer raised OpENBufferSizeError")
122 print(
"get_first_routing_protocol_name: getStringBuffer raised TypeError")
125 proto_buff = OpEN.open_buffdesc()
126 proto_buff.pstart = proto_buff_string
127 proto_buff.size = max_proto_len
130 next_proto_id_p = OpEN.new_uint32_tp()
132 if version_info >= (3,0,0):
133 for idx
in range(0, XX_PROTO_MAX) :
134 result = OpEN.openapiIpRouterProtoNameNextGet(client_handle, proto_id, proto_buff, next_proto_id_p)
136 if result == OpEN.OPEN_E_NONE
and (len(proto_buff_string.cast())>0):
137 return proto_buff_string.cast()
138 proto_id = OpEN.uint32_tp_value(next_proto_id_p)
140 for idx
in xrange(0, XX_PROTO_MAX) :
141 result = OpEN.openapiIpRouterProtoNameNextGet(client_handle, proto_id, proto_buff, next_proto_id_p)
143 if result == OpEN.OPEN_E_NONE
and (len(proto_buff_string.cast())>0):
144 return proto_buff_string.cast()
145 proto_id = OpEN.uint32_tp_value(next_proto_id_p)
146 OpEN.delete_uint32_tp(next_proto_id_p)
148 OpEN.delete_uint32_tp(max_proto_len_p)
152 def get_first_routing_interface(client_handle):
153 """ Get the first available routing interface. """
156 intf_p = OpEN.new_uint32_tp()
158 result = OpEN.openapiRtrIntfNextGet(client_handle, intf, intf_p)
159 intf = OpEN.uint32_tp_value(intf_p)
161 OpEN.delete_uint32_tp(intf_p)
165 """Simple BGP class implementing basic CRUD examples """
167 def __init__(self, client) :
168 self.m_client = client
170 def test_local_as(self, asn) :
171 """ Sets the local BGP autonomous system number to the given value.
172 The value is then read and compared to verify the operation. """
174 result = OpEN.openapiBgpLocalASSet (self.m_client, asn)
175 print_bad_result(result,
'openapiBgpLocalASSet')
177 tmp_p = OpEN.new_uint32_tp()
179 if result == OpEN.OPEN_E_NONE:
181 result = OpEN.openapiBgpLocalASGet (self.m_client, tmp_p)
182 print_bad_result(result,
'openapiBgpLocalASGet')
184 print_sanity_results(result, asn==OpEN.uint32_tp_value(tmp_p),
'local asn', str(asn))
185 OpEN.delete_uint32_tp(tmp_p)
187 def test_local_id(self, address) :
188 """ Sets the BGP router id to some arbitrary address.
189 The value is then read and compared to verify the operation. """
191 addr_p = OpEN.new_uint32_tp()
192 addr = ip_to_int(address)
194 result = OpEN.openapiBgpLocalIdSet (self.m_client, addr)
195 print_bad_result(result,
'openapiBgpLocalIdSet')
197 if result == OpEN.OPEN_E_NONE:
199 result = OpEN.openapiBgpLocalIdGet (self.m_client, addr_p)
200 print_bad_result(result,
'openapiBgpLocalASGet')
202 print_sanity_results(result, addr == OpEN.uint32_tp_value(addr_p),
'local id', address)
203 OpEN.delete_uint32_tp(addr_p)
205 def test_local_pref(self, preference) :
206 """ Sets the BGP default local preference to the given value.
207 The value is then read and compared to verify the operation. """
209 result = OpEN.openapiBgpLocalPrefSet (self.m_client, preference)
210 print_bad_result(result,
'openapiBgpLocalPrefSet')
211 tmp_p = OpEN.new_uint32_tp()
213 if result == OpEN.OPEN_E_NONE:
215 result = OpEN.openapiBgpLocalPrefGet (self.m_client, tmp_p)
216 print_bad_result(result,
'openapiBgpLocalPrefGet')
218 print_sanity_results(result, preference == OpEN.uint32_tp_value(tmp_p),
'local pref', str(preference))
219 OpEN.delete_uint32_tp(tmp_p)
221 def test_distance(self, external_distance, internal_distance, local_distance) :
222 """ Sets the BGP distance for external, internal, and local to some arbitrary value.
223 The value is then read and compared to verify the operation. """
225 result = OpEN.openapiIpRouterPreferenceSet (self.m_client, OpEN.OPEN_PREF_EBGP, external_distance)
226 print_bad_result(result,
'openapiIpRouterPreferenceSet EBGP')
227 tmp_p = OpEN.new_uint32_tp()
229 if result == OpEN.OPEN_E_NONE:
231 result = OpEN.openapiIpRouterPreferenceGet (self.m_client, OpEN.OPEN_PREF_EBGP, tmp_p)
232 print_bad_result(result,
'openapiIpRouterPreferenceGet EBGP')
234 print_sanity_results(result, external_distance == OpEN.uint32_tp_value(tmp_p),
'external distance', str(external_distance))
235 OpEN.delete_uint32_tp(tmp_p)
237 result = OpEN.openapiIpRouterPreferenceSet (self.m_client, OpEN.OPEN_PREF_IBGP, internal_distance)
238 print_bad_result(result,
'openapiIpRouterPreferenceSet IBGP')
239 tmp_p = OpEN.new_uint32_tp()
241 if result == OpEN.OPEN_E_NONE:
243 result = OpEN.openapiIpRouterPreferenceGet (self.m_client, OpEN.OPEN_PREF_IBGP, tmp_p)
244 print_bad_result(result,
'openapiIpRouterPreferenceGet IBGP')
246 print_sanity_results(result, internal_distance == OpEN.uint32_tp_value(tmp_p),
'internal distance', str(internal_distance))
247 OpEN.delete_uint32_tp(tmp_p)
249 result = OpEN.openapiIpRouterPreferenceSet (self.m_client, OpEN.OPEN_PREF_LOCAL_BGP, local_distance)
250 print_bad_result(result,
'openapiIpRouterPreferenceSet LOCAL')
251 tmp_p = OpEN.new_uint32_tp()
253 if result == OpEN.OPEN_E_NONE:
255 result = OpEN.openapiIpRouterPreferenceGet (self.m_client, OpEN.OPEN_PREF_LOCAL_BGP, tmp_p)
256 print_bad_result(result,
'openapiIpRouterPreferenceGet LOCAL')
258 print_sanity_results(result, local_distance == OpEN.uint32_tp_value(tmp_p),
'local distance', str(local_distance))
259 OpEN.delete_uint32_tp(tmp_p)
261 def test_global_hold_time(self, seconds) :
262 """ Set the configured hold time for a given BGP peer.
263 The time is then read and compared to verify the operation. """
265 result = OpEN.openapiBgpGlobalHoldTimeConfiguredSet (self.m_client, seconds)
266 print_bad_result(result,
'openapiBgpGlobalHoldTimeConfiguredSet')
267 tmp_p = OpEN.new_uint32_tp()
269 if result == OpEN.OPEN_E_NONE:
271 result = OpEN.openapiBgpGlobalHoldTimeConfiguredGet (self.m_client, tmp_p)
272 print_bad_result(result,
'openapiBgpGlobalHoldTimeConfiguredGet')
274 print_sanity_results(result, seconds==OpEN.uint32_tp_value(tmp_p),
'global hold time', str(seconds))
275 OpEN.delete_uint32_tp(tmp_p)
277 def test_global_keep_alive(self, seconds) :
278 """ Set the configured keep alive time for a given BGP peer.
279 The time is then read and compared to verify the operation. """
281 result = OpEN.openapiBgpGlobalKeepAliveConfiguredSet (self.m_client, seconds)
282 print_bad_result(result,
'openapiBgpGlobalKeepAliveConfiguredSet')
283 tmp_p = OpEN.new_uint32_tp()
285 if result == OpEN.OPEN_E_NONE:
287 result = OpEN.openapiBgpGlobalKeepAliveConfiguredGet (self.m_client, tmp_p)
288 print_bad_result(result,
'openapiBgpGlobalKeepAliveConfiguredGet')
290 print_sanity_results(result, seconds==OpEN.uint32_tp_value(tmp_p),
'global keep alive time', str(seconds))
291 OpEN.delete_uint32_tp(tmp_p)
293 def test_log_neighbor(self, log_changes) :
294 """ Sets the configuration indicating BGP to log neighbor changes.
295 The value is then read and compared to verify the operation. """
297 result = OpEN.openapiBgpLogNeighborChangesSet (self.m_client, log_changes)
298 print_bad_result(result,
'openapiBgpLogNeighborChangesSet')
299 tmp_p = OpEN.new_bool_tp()
301 if result == OpEN.OPEN_E_NONE:
303 result = OpEN.openapiBgpLogNeighborChangesGet (self.m_client, tmp_p)
304 print_bad_result(result,
'openapiBgpLogNeighborChangesGet')
306 print_sanity_results(result, log_changes==OpEN.bool_tp_value(tmp_p),
'log neighbor', str(log_changes))
307 OpEN.delete_bool_tp(tmp_p)
309 def test_max_paths(self, af, is_ibgp) :
310 """ Set the maximum number of paths that BGP can report.
311 The value is then read and compared to verify the operation. """
313 tmp_p = OpEN.new_uint32_tp()
314 max_p = OpEN.new_uint32_tp()
317 result = OpEN.openapiBgpMaxPathsGet(self.m_client, af, is_ibgp, max_p)
318 print_bad_result(result,
'openapiBgpMaxPathsGet')
320 if result == OpEN.OPEN_E_NONE:
321 result = OpEN.openapiBgpMaxPathsSet(self.m_client, af, is_ibgp, OpEN.uint32_tp_value(max_p))
322 print_bad_result(result,
'openapiBgpMaxPathsSet')
324 if result == OpEN.OPEN_E_NONE:
326 result = OpEN.openapiBgpMaxPathsGet(self.m_client, af, is_ibgp, tmp_p)
327 print_bad_result(result,
'openapiBgpMaxPathsGet')
329 max_v = OpEN.uint32_tp_value(max_p)
331 bgp =
'Internal (%d)' % max_v
333 bgp =
'External (%d)' % max_v
335 print_sanity_results(result, max_v == OpEN.uint32_tp_value(tmp_p),
'max paths', bgp)
336 OpEN.delete_uint32_tp(tmp_p)
337 OpEN.delete_uint32_tp(max_p)
339 def test_network(self, normal_mode, use_route_map, address, prefix, route_map_name) :
340 """ Configures an arbitrary network to advertise via BGP. """
342 result, addr, type = ip_to_open_inet(address)
343 msg = type +
' peer network'
345 if addr.family != OpEN.OPEN_AF_NONE:
347 name_string = open_.getStringBuffer(len(route_map_name) + 1, route_map_name)
348 except OpENBufferSizeError:
349 print(
"test_network: getStringBuffer raised OpENBufferSizeError")
352 print(
"test_network: getStringBuffer raised TypeError")
354 name_buff = OpEN.open_buffdesc()
355 name_buff.pstart = name_string
356 name_buff.size = len(route_map_name) + 1
357 result = OpEN.openapiBgpNetworkAddDelete(self.m_client,
363 print_bad_result(result,
'openapiBgpNetworkAddDelete')
365 print_sanity_results(result,
True, msg, address)
367 def test_redistribution(self, add, match_set, match_bits, metric_set,
368 metric_value_set, metric_value, route_map_set, af, route_map_name) :
369 """ Configures an arbitrary BGP route redistribution. """
371 result = OpEN.OPEN_E_FAIL
373 proto_name = get_first_routing_protocol_name(self.m_client)
374 if len(proto_name)>0:
376 proto_string = open_.getStringBuffer(len(proto_name) + 1, proto_name)
377 except OpENBufferSizeError:
378 print(
"test_redistribution: getStringBuffer raised OpENBufferSizeError")
381 print(
"test_redistribution: getStringBuffer raised TypeError")
383 proto_buff = OpEN.open_buffdesc()
384 proto_buff.pstart = proto_string
385 proto_buff.size = len(proto_name) + 1
388 route_string = open_.getStringBuffer(len(route_map_name) + 1, route_map_name)
389 except OpENBufferSizeError:
390 print(
"test_redistribution: getStringBuffer raised OpENBufferSizeError")
393 print(
"test_redistribution: getStringBuffer raised TypeError")
396 route_buff = OpEN.open_buffdesc()
397 route_buff.pstart = route_string
398 route_buff.size = len(route_map_name) + 1
400 result = OpEN.openapiBgpRedistributionSet(self.m_client,
411 print_bad_result(result,
'openapiBgpRedistributionSet')
413 print_sanity_results(result,
True,
'redistribution', route_map_name)
415 def test_peer_remote_as(self, asn, address, scope_id) :
416 """ Create a peer with a given IP address and remote AS number.
417 The remote AS number is then read and compared to verify the operation."""
419 result, addr, type = ip_to_open_inet(address)
420 msg = type +
' peer remote asn %d scope %d' % (asn, scope_id)
422 if addr.family != OpEN.OPEN_AF_NONE:
423 result = OpEN.openapiBgpPeerRemoteASSet(self.m_client,
427 print_bad_result(result,
'openapiBgpPeerRemoteASSet')
429 tmp_p = OpEN.new_uint32_tp()
430 if result == OpEN.OPEN_E_NONE:
431 result = OpEN.openapiBgpPeerRemoteASGet(self.m_client,
435 print_bad_result(result,
'openapiBgpPeerRemoteASGet')
437 print_sanity_results(result, asn == OpEN.uint32_tp_value(tmp_p), msg, address)
438 OpEN.delete_uint32_tp(tmp_p)
440 def test_peer_admin_status(self, address, scope_id, status, def_value) :
441 """ Create a peer with a given IP address and remote AS number.
442 The remote AS number is then read and compared to verify the operation."""
444 result, addr, type = ip_to_open_inet(address)
445 msg = type +
' peer admin status'
447 if addr.family != OpEN.OPEN_AF_NONE:
448 result = OpEN.openapiBgpPeerAdminStatusSet(self.m_client,
453 print_bad_result(result,
'openapiBgpPeerAdminStatusSet')
455 tmp_p = OpEN.new_OPEN_BGP_PEER_STATE_tp()
456 if result == OpEN.OPEN_E_NONE:
457 result = OpEN.openapiBgpPeerAdminStatusGet(self.m_client,
458 OpEN.OPEN_BGP_GET_FINAL,
462 print_bad_result(result,
'openapiBgpPeerAdminStatusGet')
464 print_sanity_results(result, status == OpEN.OPEN_BGP_PEER_STATE_tp_value(tmp_p), msg, status)
465 OpEN.delete_OPEN_BGP_PEER_STATE_tp(tmp_p)
467 def test_peer_keep_alive(self, address, scope_id, time, def_value) :
468 """ Set the configured keep alive time for a given BGP peer.
469 The time is then read and compared to verify the operation."""
471 result, addr, type = ip_to_open_inet(address)
472 msg = type +
' peer keep alive'
474 if addr.family != OpEN.OPEN_AF_NONE:
475 result = OpEN.openapiBgpPeerKeepAliveConfiguredSet(self.m_client,
480 print_bad_result(result,
'openapiBgpPeerKeepAliveConfiguredSet')
482 tmp_p = OpEN.new_uint32_tp()
483 if result == OpEN.OPEN_E_NONE:
484 result = OpEN.openapiBgpPeerKeepAliveConfiguredGet(self.m_client,
485 OpEN.OPEN_BGP_GET_FINAL,
489 print_bad_result(result,
'openapiBgpPeerKeepAliveConfiguredGet')
491 print_sanity_results(result, time == OpEN.uint32_tp_value(tmp_p), msg, time)
492 OpEN.delete_uint32_tp(tmp_p)
494 def test_peer_hold_time(self, address, scope_id, time, def_value) :
495 """ Set the configured hold time for a given BGP peer.
496 The time is then read and compared to verify the operation."""
498 result, addr, type = ip_to_open_inet(address)
499 msg = type +
' peer hold time'
501 if addr.family != OpEN.OPEN_AF_NONE:
502 result = OpEN.openapiBgpPeerHoldTimeConfiguredSet(self.m_client,
507 print_bad_result(result,
'openapiBgpPeerHoldTimeConfiguredSet')
509 tmp_p = OpEN.new_uint32_tp()
510 if result == OpEN.OPEN_E_NONE:
511 result = OpEN.openapiBgpPeerHoldTimeConfiguredGet(self.m_client,
512 OpEN.OPEN_BGP_GET_FINAL,
516 print_bad_result(result,
'openapiBgpPeerHoldTimeConfiguredGet')
518 print_sanity_results(result, time == OpEN.uint32_tp_value(tmp_p), msg, time)
519 OpEN.delete_uint32_tp(tmp_p)
521 def test_peer_activate(self, af, address, scope_id, activate, def_value) :
522 """ Configure peer to advertise and accept routes for the given address family."""
524 result, addr, type = ip_to_open_inet(address)
525 msg = type +
' peer activate'
527 if addr.family != OpEN.OPEN_AF_NONE:
528 result = OpEN.openapiBgpPeerActivateSet(self.m_client,
534 print_bad_result(result,
'openapiBgpPeerActivateSet')
536 tmp_p = OpEN.new_bool_tp()
537 if result == OpEN.OPEN_E_NONE:
538 result = OpEN.openapiBgpPeerActivateGet(self.m_client,
539 OpEN.OPEN_BGP_GET_FINAL,
544 print_bad_result(result,
'openapiBgpPeerActivateGet')
546 print_sanity_results(result, activate == OpEN.bool_tp_value(tmp_p), msg, activate)
547 OpEN.delete_bool_tp(tmp_p)
549 def test_peer_advertisement(self, af, address, scope_id, interval, def_value) :
550 """ Set the advertisment interval for a given BGP peer.
551 The interval is then read and compared to verify the operation."""
553 result, addr, type = ip_to_open_inet(address)
554 msg = type +
' peer advertisement interval'
556 if addr.family != OpEN.OPEN_AF_NONE:
557 result = OpEN.openapiBgpPeerAdvertisementIntervalSet(self.m_client,
563 print_bad_result(result,
'openapiBgpPeerAdvertisementIntervalSet')
565 tmp_p = OpEN.new_uint32_tp()
566 if result == OpEN.OPEN_E_NONE:
567 result = OpEN.openapiBgpPeerAdvertisementIntervalGet(self.m_client,
568 OpEN.OPEN_BGP_GET_FINAL,
573 print_bad_result(result,
'openapiBgpPeerActivateGet')
575 print_sanity_results(result, interval == OpEN.uint32_tp_value(tmp_p), msg, interval)
576 OpEN.delete_uint32_tp(tmp_p)
578 def test_peer_next_hop_self(self, af, address, scope_id, enable, def_value) :
579 """ Configure BGP to use a local address when advertising routes to a given internal peer.
580 The flag is then read and compared to verify the operation."""
582 result, addr, type = ip_to_open_inet(address)
583 msg = type +
' peer next hop'
585 if addr.family != OpEN.OPEN_AF_NONE:
586 result = OpEN.openapiBgpPeerNextHopSelfModeSet(self.m_client,
592 print_bad_result(result,
'openapiBgpPeerNextHopSelfModeSet')
594 tmp_p = OpEN.new_OPEN_CONTROL_tp()
595 if result == OpEN.OPEN_E_NONE:
596 result = OpEN.openapiBgpPeerNextHopSelfModeGet(self.m_client,
597 OpEN.OPEN_BGP_GET_FINAL,
602 print_bad_result(result,
'openapiBgpPeerNextHopSelfModeGet')
604 print_sanity_results(result, enable == OpEN.OPEN_CONTROL_tp_value(tmp_p), msg, enable)
605 OpEN.delete_OPEN_CONTROL_tp(tmp_p)
607 def test_peer_pfx_limit(self, af, address, scope_id, threshold, warning_only, def_value) :
608 """ Set the prefix limit configured for a given peer.
609 In this example, the maximum prefix limit is retrieved from the switch based
610 on the IP address family. This max value and the given threshold, and
611 warningOnly flag is set and read back for comparison."""
613 result, addr, type = ip_to_open_inet(address)
614 msg = type +
' peer prefix limit'
617 if addr.family != OpEN.OPEN_AF_NONE:
618 if addr.family == OpEN.OPEN_AF_INET:
619 limit_p = OpEN.new_uint32_tp()
620 OpEN.openapiBgpIpv4RouteMax(self.m_client, limit_p)
622 limit = OpEN.uint32_tp_value(limit_p)
625 limit = OpEN.OPEN_BGP_NBR_MAX_PFX_NOLIMIT
627 result = OpEN.openapiBgpPeerPfxLimitSet(self.m_client,
635 print_bad_result(result,
'openapiBgpPeerPfxLimitSet')
637 limit_p = OpEN.new_uint32_tp()
638 threshold_p = OpEN.new_uint32_tp()
639 warning_p = OpEN.new_bool_tp()
640 if result == OpEN.OPEN_E_NONE:
641 result = OpEN.openapiBgpPeerPfxLimitGet(self.m_client,
642 OpEN.OPEN_BGP_GET_FINAL,
649 print_bad_result(result,
'openapiBgpPeerPfxLimitGet')
651 test = (limit == OpEN.uint32_tp_value(limit_p)
and
652 threshold == OpEN.uint32_tp_value(threshold_p)
and
653 warning_only == OpEN.bool_tp_value(warning_p))
655 print_sanity_results(result, test, msg,
'%d, %d, ' % (limit, threshold) + str(warning_only))
656 OpEN.delete_uint32_tp(limit_p)
657 OpEN.delete_uint32_tp(threshold_p)
658 OpEN.delete_bool_tp(warning_p)
661 def test_peer_update_source(self, address, scope_id, source, def_value) :
662 """ Set the interface whose IP address that BGP uses as the source
663 IP address in packets sent to a given peer. The interface is
664 then read and compared to verify the operation."""
666 result, addr, type = ip_to_open_inet(address)
667 msg = type +
' peer remote source scope %d source %d' % (scope_id, source)
669 if addr.family != OpEN.OPEN_AF_NONE:
670 result = OpEN.openapiBgpPeerUpdateSourceSet(self.m_client,
675 print_bad_result(result,
'openapiBgpPeerUpdateSourceSet')
677 tmp_p = OpEN.new_uint32_tp()
678 if result == OpEN.OPEN_E_NONE:
679 result = OpEN.openapiBgpPeerUpdateSourceGet(self.m_client,
680 OpEN.OPEN_BGP_GET_FINAL,
684 print_bad_result(result,
'openapiBgpPeerUpdateSourceGet')
686 print_sanity_results(result, source == OpEN.uint32_tp_value(tmp_p), msg, address)
687 OpEN.delete_uint32_tp(tmp_p)
690 """Demonstrate OpEN usage for BGP APIs"""
692 print (
"Begin Sanity tests...")
693 ret = open_.connect(
"bgp_example")
694 if ret == OpEN.OPEN_E_NONE:
695 open_.getNetworkOSVersion()
696 client = open_.get_client()
697 example = BgpExample(client)
698 print (
"Establish BGP and global parameters...")
699 example.test_local_as(1)
700 example.test_local_id(
'10.1.1.1')
701 example.test_local_pref(1234567890)
702 example.test_distance(10, 20, 30)
703 example.test_global_hold_time(65535)
704 example.test_global_keep_alive(65535)
705 example.test_log_neighbor(
True)
706 example.test_max_paths(OpEN.OPEN_AF_INET,
False)
707 example.test_max_paths(OpEN.OPEN_AF_INET,
True)
708 example.test_network(
True,
True,
'20.10.0.0', 24,
'my-map1')
709 example.test_network(
False,
True,
'20.10.0.0', 24,
'my-map1')
712 match_bits |= OpEN.OPEN_OSPF_METRIC_TYPE_INTERNAL
713 match_bits |= OpEN.OPEN_OSPF_METRIC_TYPE_EXT1
714 match_bits |= OpEN.OPEN_OSPF_METRIC_TYPE_EXT2
715 match_bits |= OpEN.OPEN_OSPF_METRIC_TYPE_NSSA_EXT1
716 match_bits |= OpEN.OPEN_OSPF_METRIC_TYPE_NSSA_EXT2
717 example.test_redistribution(
True,
True, match_bits,
True,
True, 4294967295,
False, OpEN.OPEN_AF_INET,
'')
719 print (
"Create a ipv4 test peer...")
720 example.test_peer_remote_as(1,
"20.2.2.2", 0)
721 example.test_peer_admin_status(
"20.2.2.2", 0, OpEN.OPEN_BGP_START,
True)
722 example.test_peer_keep_alive(
"20.2.2.2", 0, 600,
True)
723 example.test_peer_hold_time(
"20.2.2.2", 0, 700,
True)
724 example.test_peer_activate(OpEN.OPEN_AF_INET6,
"20.2.2.2", 0,
True,
True)
725 example.test_peer_advertisement(OpEN.OPEN_AF_INET,
"20.2.2.2", 0, 200,
True)
726 example.test_peer_next_hop_self(OpEN.OPEN_AF_INET,
"20.2.2.2", 0, OpEN.OPEN_ENABLE,
True)
727 example.test_peer_pfx_limit(OpEN.OPEN_AF_INET,
"20.2.2.2", 0, 20,
True,
True)
728 print (
"Create a ipv6 test peer...")
729 example.test_peer_remote_as(1,
"2222::", 0)
730 example.test_peer_admin_status(
"2222::", 0, OpEN.OPEN_BGP_STOP,
True)
731 example.test_peer_keep_alive(
"2222::", 0, 600,
True)
732 example.test_peer_hold_time(
"2222::", 0, 700,
True)
733 example.test_peer_activate(OpEN.OPEN_AF_INET6,
"2222::", 0,
True,
True)
734 example.test_peer_advertisement(OpEN.OPEN_AF_INET6,
"2222::", 0, 222,
True)
735 example.test_peer_next_hop_self(OpEN.OPEN_AF_INET6,
"2222::", 0, OpEN.OPEN_ENABLE,
True)
736 example.test_peer_pfx_limit(OpEN.OPEN_AF_INET6,
"2222::", 0, 60,
True,
True)
738 result, intf = get_first_routing_interface(client)
739 if result == OpEN.OPEN_E_NONE:
741 example.test_peer_remote_as(1,
"fe80::1", intf)
743 example.test_peer_update_source(
"20.2.2.2", 0, intf,
True)
745 print (
"\nSanity tests are incomplete because no routing interfaces are available!")
746 print (
"Please configure a routing interface and re-run test.")
751 print (
"Unable to connect")
753 if __name__ ==
'__main__': main()