Open Ethernet Networking (OpEN) API Guide and Reference Manual  3.11.1.2
bgp_example.py
1 /*! \file bgp_example.py
2  */
3 
4 #!/mnt/fastpath/usr/bin/python
5 
6 """bgp_example.py: OpEN API Border Gateway Protocol (BGP) configuration example"""
7 
8 import OpEN_py as OpEN
9 from OpENUtil import *
10 import socket
11 import struct
12 from sys import version_info
13 
14 open_ = OpENUtil()
15 
16 #
17 # Copyright 2016 Broadcom.
18 #
19 # Licensed under the Apache License, Version 2.0 (the "License")
20 # you may not use this file except in compliance with the License.
21 # You may obtain a copy of the License at
22 #
23 # http://www.apache.org/licenses/LICENSE-2.0
24 #
25 # Unless required by applicable law or agreed to in writing, software
26 # distributed under the License is distributed on an "AS IS" BASIS,
27 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
28 # See the License for the specific language governing permissions and
29 # limitations under the License.
30 #
31 
32 #
33 # Python 2.6.6
34 #
35 
36 # Maximum number of protocol names
37 XX_PROTO_MAX = 10
38 
39 def ip_to_int(addr):
40  """Convert ipv4 string to integer"""
41 
42  return struct.unpack("!I", socket.inet_aton(addr))[0]
43 
44 def int_to_ip(addr):
45  """Convert ipv4 integer to string"""
46 
47  return socket.inet_ntoa(struct.pack("!I", addr))
48 
49 def ipv6_to_int(addr):
50  """Convert ipv6 string to integer"""
51 
52  str_ = socket.inet_pton(socket.AF_INET6, addr)
53  a, b = struct.unpack('!2Q', str_)
54  return (a << 64) | b
55 
56 def int_to_ipv6(addr):
57  """Convert ipv6 integer to string"""
58 
59  a = addr >> 64
60  b = addr & ((1 << 64) - 1)
61  return socket.inet_ntop(socket.AF_INET6, struct.pack('!2Q', a, b))
62 
63 def ip_to_open_inet(address):
64  """ Convert ip address to integer and family and return as open_inet_addr_t """
65 
66  type = 'unsupported'
67  addr = OpEN.open_inet_addr_t()
68  addr.family = OpEN.OPEN_AF_NONE
69  try:
70  addr.addr.ipv4 = ip_to_int(address)
71  addr.family = OpEN.OPEN_AF_INET
72  result = OpEN.OPEN_E_NONE
73  type = 'ipv4'
74  except:
75  if socket.has_ipv6:
76  try:
77  addr.addr.ipv6 = ipv6_to_int(address)
78  addr.family = OpEN.OPEN_AF_INET6
79  result = OpEN.OPEN_E_NONE
80  type = 'ipv6'
81  except:
82  result = OpEN.OPEN_E_PARAM
83  else:
84  type += ' ipv6'
85  result = OpEN.OPEN_E_UNAVAIL
86 
87  return (result, addr, type)
88 
89 def print_sanity_results(result, test, msg, feat):
90  """Print overall comparison results"""
91 
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))
96  else:
97  print ("Sanity Failure - %s - %s." % (msg, feat))
98 
99 def print_bad_result(result, msg):
100  """Print some general error messages if the result is bad"""
101 
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))
106 
107 def get_first_routing_protocol_name(client_handle):
108  """ Get the first available routing protocol name. """
109 
110  max_proto_len_p = OpEN.new_uint32_tp()
111 
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
116  try:
117  proto_buff_string = open_.getStringBuffer(max_proto_len)
118  except OpENBufferSizeError:
119  print("get_first_routing_protocol_name: getStringBuffer raised OpENBufferSizeError")
120  return
121  except TypeError:
122  print("get_first_routing_protocol_name: getStringBuffer raised TypeError")
123  return
124 
125  proto_buff = OpEN.open_buffdesc()
126  proto_buff.pstart = proto_buff_string
127  proto_buff.size = max_proto_len
128 
129  proto_id = 0
130  next_proto_id_p = OpEN.new_uint32_tp()
131 
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)
135  # Get the first available protocol name
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)
139  else:
140  for idx in xrange(0, XX_PROTO_MAX) :
141  result = OpEN.openapiIpRouterProtoNameNextGet(client_handle, proto_id, proto_buff, next_proto_id_p)
142  # Get the first available protocol name
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)
147 
148  OpEN.delete_uint32_tp(max_proto_len_p)
149 
150  return ''
151 
152 def get_first_routing_interface(client_handle):
153  """ Get the first available routing interface. """
154 
155  intf = 0
156  intf_p = OpEN.new_uint32_tp()
157 
158  result = OpEN.openapiRtrIntfNextGet(client_handle, intf, intf_p)
159  intf = OpEN.uint32_tp_value(intf_p)
160 
161  OpEN.delete_uint32_tp(intf_p)
162  return result, intf
163 
164 class BgpExample :
165  """Simple BGP class implementing basic CRUD examples """
166 
167  def __init__(self, client) :
168  self.m_client = client
169 
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. """
173 
174  result = OpEN.openapiBgpLocalASSet (self.m_client, asn)
175  print_bad_result(result, 'openapiBgpLocalASSet')
176 
177  tmp_p = OpEN.new_uint32_tp()
178 
179  if result == OpEN.OPEN_E_NONE:
180  # Fetch for demonstration
181  result = OpEN.openapiBgpLocalASGet (self.m_client, tmp_p)
182  print_bad_result(result, 'openapiBgpLocalASGet')
183 
184  print_sanity_results(result, asn==OpEN.uint32_tp_value(tmp_p), 'local asn', str(asn))
185  OpEN.delete_uint32_tp(tmp_p)
186 
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. """
190 
191  addr_p = OpEN.new_uint32_tp()
192  addr = ip_to_int(address)
193 
194  result = OpEN.openapiBgpLocalIdSet (self.m_client, addr)
195  print_bad_result(result, 'openapiBgpLocalIdSet')
196 
197  if result == OpEN.OPEN_E_NONE:
198  # Fetch for demonstration
199  result = OpEN.openapiBgpLocalIdGet (self.m_client, addr_p)
200  print_bad_result(result, 'openapiBgpLocalASGet')
201 
202  print_sanity_results(result, addr == OpEN.uint32_tp_value(addr_p), 'local id', address)
203  OpEN.delete_uint32_tp(addr_p)
204 
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. """
208 
209  result = OpEN.openapiBgpLocalPrefSet (self.m_client, preference)
210  print_bad_result(result, 'openapiBgpLocalPrefSet')
211  tmp_p = OpEN.new_uint32_tp()
212 
213  if result == OpEN.OPEN_E_NONE:
214  # Fetch for demonstration
215  result = OpEN.openapiBgpLocalPrefGet (self.m_client, tmp_p)
216  print_bad_result(result, 'openapiBgpLocalPrefGet')
217 
218  print_sanity_results(result, preference == OpEN.uint32_tp_value(tmp_p), 'local pref', str(preference))
219  OpEN.delete_uint32_tp(tmp_p)
220 
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. """
224 
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()
228 
229  if result == OpEN.OPEN_E_NONE:
230  # Fetch for demonstration
231  result = OpEN.openapiIpRouterPreferenceGet (self.m_client, OpEN.OPEN_PREF_EBGP, tmp_p)
232  print_bad_result(result, 'openapiIpRouterPreferenceGet EBGP')
233 
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)
236 
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()
240 
241  if result == OpEN.OPEN_E_NONE:
242  # Fetch for demonstration
243  result = OpEN.openapiIpRouterPreferenceGet (self.m_client, OpEN.OPEN_PREF_IBGP, tmp_p)
244  print_bad_result(result, 'openapiIpRouterPreferenceGet IBGP')
245 
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)
248 
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()
252 
253  if result == OpEN.OPEN_E_NONE:
254  # Fetch for demonstration
255  result = OpEN.openapiIpRouterPreferenceGet (self.m_client, OpEN.OPEN_PREF_LOCAL_BGP, tmp_p)
256  print_bad_result(result, 'openapiIpRouterPreferenceGet LOCAL')
257 
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)
260 
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. """
264 
265  result = OpEN.openapiBgpGlobalHoldTimeConfiguredSet (self.m_client, seconds)
266  print_bad_result(result, 'openapiBgpGlobalHoldTimeConfiguredSet')
267  tmp_p = OpEN.new_uint32_tp()
268 
269  if result == OpEN.OPEN_E_NONE:
270  # Fetch for demonstration
271  result = OpEN.openapiBgpGlobalHoldTimeConfiguredGet (self.m_client, tmp_p)
272  print_bad_result(result, 'openapiBgpGlobalHoldTimeConfiguredGet')
273 
274  print_sanity_results(result, seconds==OpEN.uint32_tp_value(tmp_p), 'global hold time', str(seconds))
275  OpEN.delete_uint32_tp(tmp_p)
276 
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. """
280 
281  result = OpEN.openapiBgpGlobalKeepAliveConfiguredSet (self.m_client, seconds)
282  print_bad_result(result, 'openapiBgpGlobalKeepAliveConfiguredSet')
283  tmp_p = OpEN.new_uint32_tp()
284 
285  if result == OpEN.OPEN_E_NONE:
286  # Fetch for demonstration
287  result = OpEN.openapiBgpGlobalKeepAliveConfiguredGet (self.m_client, tmp_p)
288  print_bad_result(result, 'openapiBgpGlobalKeepAliveConfiguredGet')
289 
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)
292 
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. """
296 
297  result = OpEN.openapiBgpLogNeighborChangesSet (self.m_client, log_changes)
298  print_bad_result(result, 'openapiBgpLogNeighborChangesSet')
299  tmp_p = OpEN.new_bool_tp()
300 
301  if result == OpEN.OPEN_E_NONE:
302  # Fetch for demonstration
303  result = OpEN.openapiBgpLogNeighborChangesGet (self.m_client, tmp_p)
304  print_bad_result(result, 'openapiBgpLogNeighborChangesGet')
305 
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)
308 
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. """
312 
313  tmp_p = OpEN.new_uint32_tp()
314  max_p = OpEN.new_uint32_tp()
315 
316  # Get max number of paths for the active template
317  result = OpEN.openapiBgpMaxPathsGet(self.m_client, af, is_ibgp, max_p)
318  print_bad_result(result, 'openapiBgpMaxPathsGet')
319 
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')
323 
324  if result == OpEN.OPEN_E_NONE:
325  # Fetch for demonstration
326  result = OpEN.openapiBgpMaxPathsGet(self.m_client, af, is_ibgp, tmp_p)
327  print_bad_result(result, 'openapiBgpMaxPathsGet')
328 
329  max_v = OpEN.uint32_tp_value(max_p)
330  if is_ibgp:
331  bgp = 'Internal (%d)' % max_v
332  else:
333  bgp = 'External (%d)' % max_v
334 
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)
338 
339  def test_network(self, normal_mode, use_route_map, address, prefix, route_map_name) :
340  """ Configures an arbitrary network to advertise via BGP. """
341 
342  result, addr, type = ip_to_open_inet(address)
343  msg = type + ' peer network'
344 
345  if addr.family != OpEN.OPEN_AF_NONE:
346  try:
347  name_string = open_.getStringBuffer(len(route_map_name) + 1, route_map_name)
348  except OpENBufferSizeError:
349  print("test_network: getStringBuffer raised OpENBufferSizeError")
350  return
351  except TypeError:
352  print("test_network: getStringBuffer raised TypeError")
353  return
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,
358  normal_mode,
359  use_route_map,
360  addr,
361  prefix,
362  name_buff)
363  print_bad_result(result, 'openapiBgpNetworkAddDelete')
364 
365  print_sanity_results(result, True, msg, address)
366 
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. """
370 
371  result = OpEN.OPEN_E_FAIL
372 
373  proto_name = get_first_routing_protocol_name(self.m_client)
374  if len(proto_name)>0:
375  try:
376  proto_string = open_.getStringBuffer(len(proto_name) + 1, proto_name)
377  except OpENBufferSizeError:
378  print("test_redistribution: getStringBuffer raised OpENBufferSizeError")
379  return
380  except TypeError:
381  print("test_redistribution: getStringBuffer raised TypeError")
382  return
383  proto_buff = OpEN.open_buffdesc()
384  proto_buff.pstart = proto_string
385  proto_buff.size = len(proto_name) + 1
386 
387  try:
388  route_string = open_.getStringBuffer(len(route_map_name) + 1, route_map_name)
389  except OpENBufferSizeError:
390  print("test_redistribution: getStringBuffer raised OpENBufferSizeError")
391  return
392  except TypeError:
393  print("test_redistribution: getStringBuffer raised TypeError")
394  return
395 
396  route_buff = OpEN.open_buffdesc()
397  route_buff.pstart = route_string
398  route_buff.size = len(route_map_name) + 1
399 
400  result = OpEN.openapiBgpRedistributionSet(self.m_client,
401  add,
402  proto_buff,
403  match_set,
404  match_bits,
405  metric_set,
406  metric_value_set,
407  metric_value,
408  route_map_set,
409  af,
410  route_buff)
411  print_bad_result(result, 'openapiBgpRedistributionSet')
412 
413  print_sanity_results(result, True, 'redistribution', route_map_name)
414 
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."""
418 
419  result, addr, type = ip_to_open_inet(address)
420  msg = type + ' peer remote asn %d scope %d' % (asn, scope_id)
421 
422  if addr.family != OpEN.OPEN_AF_NONE:
423  result = OpEN.openapiBgpPeerRemoteASSet(self.m_client,
424  addr,
425  scope_id,
426  asn)
427  print_bad_result(result, 'openapiBgpPeerRemoteASSet')
428 
429  tmp_p = OpEN.new_uint32_tp()
430  if result == OpEN.OPEN_E_NONE:
431  result = OpEN.openapiBgpPeerRemoteASGet(self.m_client,
432  addr,
433  scope_id,
434  tmp_p)
435  print_bad_result(result, 'openapiBgpPeerRemoteASGet')
436 
437  print_sanity_results(result, asn == OpEN.uint32_tp_value(tmp_p), msg, address)
438  OpEN.delete_uint32_tp(tmp_p)
439 
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."""
443 
444  result, addr, type = ip_to_open_inet(address)
445  msg = type + ' peer admin status'
446 
447  if addr.family != OpEN.OPEN_AF_NONE:
448  result = OpEN.openapiBgpPeerAdminStatusSet(self.m_client,
449  addr,
450  scope_id,
451  status,
452  def_value)
453  print_bad_result(result, 'openapiBgpPeerAdminStatusSet')
454 
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,
459  addr,
460  scope_id,
461  tmp_p)
462  print_bad_result(result, 'openapiBgpPeerAdminStatusGet')
463 
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)
466 
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."""
470 
471  result, addr, type = ip_to_open_inet(address)
472  msg = type + ' peer keep alive'
473 
474  if addr.family != OpEN.OPEN_AF_NONE:
475  result = OpEN.openapiBgpPeerKeepAliveConfiguredSet(self.m_client,
476  addr,
477  scope_id,
478  time,
479  def_value)
480  print_bad_result(result, 'openapiBgpPeerKeepAliveConfiguredSet')
481 
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,
486  addr,
487  scope_id,
488  tmp_p)
489  print_bad_result(result, 'openapiBgpPeerKeepAliveConfiguredGet')
490 
491  print_sanity_results(result, time == OpEN.uint32_tp_value(tmp_p), msg, time)
492  OpEN.delete_uint32_tp(tmp_p)
493 
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."""
497 
498  result, addr, type = ip_to_open_inet(address)
499  msg = type + ' peer hold time'
500 
501  if addr.family != OpEN.OPEN_AF_NONE:
502  result = OpEN.openapiBgpPeerHoldTimeConfiguredSet(self.m_client,
503  addr,
504  scope_id,
505  time,
506  def_value)
507  print_bad_result(result, 'openapiBgpPeerHoldTimeConfiguredSet')
508 
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,
513  addr,
514  scope_id,
515  tmp_p)
516  print_bad_result(result, 'openapiBgpPeerHoldTimeConfiguredGet')
517 
518  print_sanity_results(result, time == OpEN.uint32_tp_value(tmp_p), msg, time)
519  OpEN.delete_uint32_tp(tmp_p)
520 
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."""
523 
524  result, addr, type = ip_to_open_inet(address)
525  msg = type + ' peer activate'
526 
527  if addr.family != OpEN.OPEN_AF_NONE:
528  result = OpEN.openapiBgpPeerActivateSet(self.m_client,
529  addr,
530  scope_id,
531  af,
532  activate,
533  def_value)
534  print_bad_result(result, 'openapiBgpPeerActivateSet')
535 
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,
540  addr,
541  scope_id,
542  af,
543  tmp_p)
544  print_bad_result(result, 'openapiBgpPeerActivateGet')
545 
546  print_sanity_results(result, activate == OpEN.bool_tp_value(tmp_p), msg, activate)
547  OpEN.delete_bool_tp(tmp_p)
548 
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."""
552 
553  result, addr, type = ip_to_open_inet(address)
554  msg = type + ' peer advertisement interval'
555 
556  if addr.family != OpEN.OPEN_AF_NONE:
557  result = OpEN.openapiBgpPeerAdvertisementIntervalSet(self.m_client,
558  addr,
559  scope_id,
560  af,
561  interval,
562  def_value)
563  print_bad_result(result, 'openapiBgpPeerAdvertisementIntervalSet')
564 
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,
569  addr,
570  scope_id,
571  af,
572  tmp_p)
573  print_bad_result(result, 'openapiBgpPeerActivateGet')
574 
575  print_sanity_results(result, interval == OpEN.uint32_tp_value(tmp_p), msg, interval)
576  OpEN.delete_uint32_tp(tmp_p)
577 
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."""
581 
582  result, addr, type = ip_to_open_inet(address)
583  msg = type + ' peer next hop'
584 
585  if addr.family != OpEN.OPEN_AF_NONE:
586  result = OpEN.openapiBgpPeerNextHopSelfModeSet(self.m_client,
587  addr,
588  scope_id,
589  af,
590  enable,
591  def_value)
592  print_bad_result(result, 'openapiBgpPeerNextHopSelfModeSet')
593 
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,
598  addr,
599  scope_id,
600  af,
601  tmp_p)
602  print_bad_result(result, 'openapiBgpPeerNextHopSelfModeGet')
603 
604  print_sanity_results(result, enable == OpEN.OPEN_CONTROL_tp_value(tmp_p), msg, enable)
605  OpEN.delete_OPEN_CONTROL_tp(tmp_p)
606 
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."""
612 
613  result, addr, type = ip_to_open_inet(address)
614  msg = type + ' peer prefix limit'
615  limit = 0
616 
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)
621  # Demonstrate get/set the maximum number of IPv4 routes
622  limit = OpEN.uint32_tp_value(limit_p)
623  else:
624  # Demonstrate alternate method to use the maximum number routes
625  limit = OpEN.OPEN_BGP_NBR_MAX_PFX_NOLIMIT
626 
627  result = OpEN.openapiBgpPeerPfxLimitSet(self.m_client,
628  addr,
629  scope_id,
630  af,
631  limit,
632  threshold,
633  warning_only,
634  def_value)
635  print_bad_result(result, 'openapiBgpPeerPfxLimitSet')
636 
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,
643  addr,
644  scope_id,
645  af,
646  limit_p,
647  threshold_p,
648  warning_p)
649  print_bad_result(result, 'openapiBgpPeerPfxLimitGet')
650 
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))
654 
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)
659 
660 
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."""
665 
666  result, addr, type = ip_to_open_inet(address)
667  msg = type + ' peer remote source scope %d source %d' % (scope_id, source)
668 
669  if addr.family != OpEN.OPEN_AF_NONE:
670  result = OpEN.openapiBgpPeerUpdateSourceSet(self.m_client,
671  addr,
672  scope_id,
673  source,
674  def_value)
675  print_bad_result(result, 'openapiBgpPeerUpdateSourceSet')
676 
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,
681  addr,
682  scope_id,
683  tmp_p)
684  print_bad_result(result, 'openapiBgpPeerUpdateSourceGet')
685 
686  print_sanity_results(result, source == OpEN.uint32_tp_value(tmp_p), msg, address)
687  OpEN.delete_uint32_tp(tmp_p)
688 
689 def main():
690  """Demonstrate OpEN usage for BGP APIs"""
691 
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')
710  # Demonstrate internal, external, and NSSA external bit sets
711  match_bits = 0
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, '')
718  # Neighbor specific tests
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)
737  # Attempt to retrieve a routing interface
738  result, intf = get_first_routing_interface(client)
739  if result == OpEN.OPEN_E_NONE:
740  # Configure remote as link local address
741  example.test_peer_remote_as(1, "fe80::1", intf)
742  # Configure update source using same routing interface
743  example.test_peer_update_source("20.2.2.2", 0, intf, True)
744  else:
745  print ("\nSanity tests are incomplete because no routing interfaces are available!")
746  print ("Please configure a routing interface and re-run test.")
747  print ("\nComplete")
748 
749  open_.terminate()
750  else :
751  print ("Unable to connect")
752 
753 if __name__ == '__main__': main()
754