def _prefix_mgr_client(): prefix_mgr_client_inst = prefix_mgr_client.PrefixMgrClient( zmq.Context(), "tcp://localhost:5000") resp = prefix_mgr_client_inst.add_prefix( '2620:0:1cff:dead:bef1:ffff:ffff:4/128') self.assertTrue(resp.success) resp = prefix_mgr_client_inst.view_prefix() prefix_entry4 = lsdb_types.PrefixEntry( prefix=ip_str_to_prefix( '2620:0:1cff:dead:bef1:ffff:ffff:4/128'), type=lsdb_types.PrefixType.LOOPBACK) self.assertTrue(resp.success) self.assertTrue(prefix_entry4 in resp.prefixes) resp = prefix_mgr_client_inst.withdraw_prefix( '2620:0:1cff:dead:bef1:ffff:ffff:4/128') self.assertTrue(resp.success) resp = prefix_mgr_client_inst.view_prefix() self.assertTrue(resp.success) self.assertFalse(prefix_entry4 in resp.prefixes) resp = prefix_mgr_client_inst.withdraw_prefix( '2620:0:1cff:dead:bef1:ffff:ffff:5/128') self.assertFalse(resp.success)
def run(self, prefixes): prefixes = [utils.ip_str_to_prefix(p) for p in prefixes.split(',')] try: self.client.deleteUnicastRoutes(self.client.client_id, prefixes) except Exception as e: print('Failed to delete routes.') print('Exception: {}'.format(e)) sys.exit(1) print('Deleted {} routes.'.format(len(prefixes)))
def send_cmd_to_prefix_mgr(self, cmd, prefixes=None, prefix_type='BREEZE'): ''' Send the given cmd to prefix manager and return resp ''' TYPE_TO_VALUES = lsdb_types.PrefixType._NAMES_TO_VALUES if prefix_type not in TYPE_TO_VALUES: raise Exception('Unknown type {}. Use any of {}'.format( prefix_type, ', '.join(TYPE_TO_VALUES.keys()))) req_msg = prefix_mgr_types.PrefixManagerRequest() req_msg.cmd = cmd req_msg.type = TYPE_TO_VALUES[prefix_type] req_msg.prefixes = [] if prefixes is not None: for prefix in prefixes: req_msg.prefixes.append( lsdb_types.PrefixEntry(prefix=ip_str_to_prefix(prefix), type=TYPE_TO_VALUES[prefix_type])) self._prefix_mgr_cmd_socket.send_thrift_obj(req_msg) return self._prefix_mgr_cmd_socket.recv_thrift_obj( prefix_mgr_types.PrefixManagerResponse)
def run_set(self, node_name, prefix_str): key = Consts.STATIC_PREFIX_ALLOC_PARAM_KEY prefix = utils.ip_str_to_prefix(prefix_str) # Retrieve previous allocation resp = self.client.get_keys([key]) allocs = None if key in resp.keyVals: allocs = serializer.deserialize_thrift_object( resp.keyVals.get(key).value, alloc_types.StaticAllocation) else: allocs = alloc_types.StaticAllocation(nodePrefixes={}) # Return if there is no change if allocs.nodePrefixes.get(node_name) == prefix: print('No changes needed. {}\'s prefix is already set to {}' .format(node_name, prefix_str)) return # Update value in KvStore allocs.nodePrefixes[node_name] = prefix value = serializer.serialize_thrift_object(allocs) self.run(key, value, 'breeze', None, Consts.CONST_TTL_INF)
def build_routes(prefixes, nexthops): ''' :param prefixes: List of prefixes in string representation :param nexthops: List of nexthops ip addresses in string presentation :returns: list ip_types.UnicastRoute (structured routes) :rtype: list ''' prefixes = [utils.ip_str_to_prefix(p) for p in prefixes] nhs = [] for nh_iface in nexthops: iface, addr = None, None # Nexthop may or may not be link-local. Handle it here well if '@' in nh_iface: addr, iface = nh_iface.split('@') elif '%' in nh_iface: addr, iface = nh_iface.split('%') else: addr = nh_iface nexthop = utils.ip_str_to_addr(addr) nexthop.ifName = iface nhs.append(nexthop) return [ip_types.UnicastRoute(dest=p, nexthops=nhs) for p in prefixes]
from __future__ import print_function from __future__ import unicode_literals from __future__ import division from openr.utils import socket from openr.clients import prefix_mgr_client from openr.PrefixManager import ttypes as prefix_mgr_types from openr.Lsdb import ttypes as lsdb_types from openr.cli.utils.utils import ip_str_to_prefix, sprint_prefix import zmq import unittest from multiprocessing import Process prefix_entry1 = lsdb_types.PrefixEntry( prefix=ip_str_to_prefix('2620:0:1cff:dead:bef1:ffff:ffff:1/128'), type=lsdb_types.PrefixType.LOOPBACK) prefix_entry2 = lsdb_types.PrefixEntry( prefix=ip_str_to_prefix('2620:0:1cff:dead:bef1:ffff:ffff:2/128'), type=lsdb_types.PrefixType.LOOPBACK) prefix_entry3 = lsdb_types.PrefixEntry( prefix=ip_str_to_prefix('2620:0:1cff:dead:bef1:ffff:ffff:3/128'), type=lsdb_types.PrefixType.LOOPBACK) class PrefixMgr(): def __init__(self, zmq_ctx, url): self._prefix_mgr_server_socket = socket.Socket(zmq_ctx, zmq.REP) self._prefix_mgr_server_socket.bind(url)