def test_set_gw_info_vlan2(self): mac1 = "22:22:c6:d0:02:3c" ipaddr1 = ipaddress.ip_address("10.1.1.11") gwinfo_msg1 = GWInfo(ip=IPAddress(version=IPAddress.IPV4, address=ipaddr1.packed), mac=mac1, vlan="1") self._stub.SetGatewayInfo(gwinfo_msg1) mac2 = "33:22:c6:d0:02:3c" ipaddr2 = ipaddress.ip_address("20.1.1.11") gwinfo_msg2 = GWInfo(ip=IPAddress(version=IPAddress.IPV4, address=ipaddr2.packed), mac=mac2, vlan="2") self._stub.SetGatewayInfo(gwinfo_msg2) gw_info_list = self._stub.ListGatewayInfo(Void()) count1 = 0 count2 = 0 for gw_info in gw_info_list.gw_list: if gw_info == gwinfo_msg1: count1 = count1 + 1 if gw_info == gwinfo_msg2: count2 = count2 + 1 self.assertEqual(count1, 1) self.assertEqual(count2, 1)
def test_set_gw_info(self): mac1 = "22:22:c6:d0:02:3c" ipaddr1 = ipaddress.ip_address("10.1.1.11") gwinfo_msg = GWInfo() gwinfo_msg.ip.version = IPBlock.IPV4 gwinfo_msg.ip.address = ipaddr1.packed gwinfo_msg.mac = mac1 self._stub.SetGatewayInfo(gwinfo_msg) gw_info = self._stub.GetGatewayInfo(Void()) gw_ip_get = ipaddress.ip_address(gw_info.ip.address) self.assertEqual(ipaddr1, gw_ip_get) self.assertEqual(mac1, gw_info.mac)
def update_ip(self, ip: Optional[str], vlan_id=None): """ Update IP address of the GW in mobilityD GW table. Args: ip: gw ip address vlan_id: vlan of the GW, None in case of no vlan used. """ try: ip_addr = ipaddress.ip_address(ip) except ValueError: logging.debug("could not parse GW IP: %s", ip) return gw_ip = IPAddress(version=IPAddress.IPV4, address=ip_addr.packed) # keep mac address same if its same GW IP vlan_key = _get_vlan_key(vlan_id) if vlan_key in self._backing_map: gw_info = self._backing_map[vlan_key] if gw_info and gw_info.ip == gw_ip: logging.debug("GW update: no change %s", ip) return updated_info = GWInfo(ip=gw_ip, mac="", vlan=vlan_key) self._backing_map[vlan_key] = updated_info logging.info("GW update: GW IP[%s]: %s" % (vlan_key, ip))
def _get_gw_info(ip, mac="", vlan=NO_VLAN): ip_addr = ipaddress.ip_address(ip) gw_ip = IPAddress( version=IPAddress.IPV4, address=ip_addr.packed, ) return GWInfo(ip=gw_ip, mac=mac, vlan=vlan)
def mocked_set_mobilityd_gw_info(ip: IPAddress, mac: str, vlan: str): global gw_info_map global gw_info_lock with gw_info_lock: gw_info = GWInfo(ip=ip, mac=mac, vlan=vlan) gw_info_map[vlan] = gw_info
def update_mac(self, ip: Optional[str], mac: Optional[str], vlan_id=None): """ Update mac address of GW in mobilityD GW table Args: ip: gw ip address. vlan_id: Vlan of the gw. mac: mac address of the GW. """ try: ip_addr = ipaddress.ip_address(ip) except ValueError: logging.debug("could not parse GW IP: %s", ip) return vlan_key = _get_vlan_key(vlan_id) # TODO: enhance check for MAC address sanity. if mac is None or ':' not in mac: logging.error( "Incorrect mac format: %s for IP %s (vlan_key %s)", mac, ip, vlan_id, ) return gw_ip = IPAddress( version=IPAddress.IPV4, address=ip_addr.packed, ) updated_info = GWInfo(ip=gw_ip, mac=mac, vlan=vlan_key) self._backing_map[vlan_key] = updated_info logging.info("GW update: GW IP[%s]: %s : mac %s" % (vlan_key, ip, mac))
def set_gw_ip_addressk_handler(client, args): try: ipaddr = ipaddress.ip_address(args.gwip) except ValueError: print("Error: invalid IP address format: %s" % args.gwip) return gwinfo_msg = GWInfo() if ipaddr.version == 4: gwinfo_msg.ip.version = IPBlock.IPV4 else: print("Error: IP version %d is not supported yet" % ipaddr.version) return gwinfo_msg.ip.address = ipaddr.packed gwinfo_msg.mac = "" client.SetGatewayInfo(gwinfo_msg)
def update_mac(self, ip: str, mac: Optional[str], vlan_id: Optional[str] = ""): vlan_key = _get_vlan_key(vlan_id) # TODO: enhance check for MAC address sanity. if mac is None or ':' not in mac: logging.error("Incorrect mac format: %s for IP %s (vlan_key %s)", mac, ip, vlan_id) return ip_addr = ipaddress.ip_address(ip) gw_ip = IPAddress(version=IPAddress.IPV4, address=ip_addr.packed) updated_info = GWInfo(ip=gw_ip, mac=mac, vlan=vlan_id) self._backing_map[vlan_key] = updated_info
def update_ip(self, ip: str, vlan_id: Optional[str] = ""): vlan_key = _get_vlan_key(vlan_id) logging.info("GW IP[%s]: %s" % (vlan_key, ip)) ip_addr = ipaddress.ip_address(ip) gw_ip = IPAddress(version=IPAddress.IPV4, address=ip_addr.packed) # keep mac address same if its same GW IP if vlan_key in self._backing_map: gw_info = self._backing_map[vlan_key] if gw_info and gw_info.ip == gw_ip: logging.debug("IP update: no change %s", ip) return updated_info = GWInfo(ip=gw_ip, mac="", vlan=vlan_id) self._backing_map[vlan_key] = updated_info
def set_mobilityd_gw_info(ip: IPAddress, mac: str, vlan: str): """ Make RPC call to 'SetGatewayInfo' method of local mobilityD service """ try: chan = ServiceRegistry.get_rpc_channel(SERVICE_NAME, ServiceRegistry.LOCAL) except ValueError: logging.error('Cant get RPC channel to %s', SERVICE_NAME) return client = MobilityServiceStub(chan) try: gwinfo = GWInfo(ip=ip, mac=mac, vlan=vlan) client.SetGatewayInfo(gwinfo) except grpc.RpcError as err: logging.error("SetGatewayInfo error[%s] %s", err.code(), err.details())
def get_mobilityd_gw_info() -> GWInfo: """ Make RPC call to 'GetGatewayInfo' method of local mobilityD service """ try: chan = ServiceRegistry.get_rpc_channel(SERVICE_NAME, ServiceRegistry.LOCAL) except ValueError: logging.error('Cant get RPC channel to %s', SERVICE_NAME) return GWInfo() client = MobilityServiceStub(chan) try: return client.GetGatewayInfo(Void()) except grpc.RpcError as err: logging.error("GetGatewayInfoRequest error[%s] %s", err.code(), err.details())
def test_set_gw_info_vlan(self): mac1 = "22:22:c6:d0:02:3c" ipaddr1 = ipaddress.ip_address("10.1.1.11") gwinfo_msg = GWInfo(ip=IPAddress(version=IPAddress.IPV4, address=ipaddr1.packed), mac=mac1, vlan="1") self._stub.SetGatewayInfo(gwinfo_msg) gw_info_list = self._stub.ListGatewayInfo(Void()) for gw_info in gw_info_list.gw_list: if gw_info.vlan == "1": gw_ip_get = ipaddress.ip_address(gw_info.ip.address) self.assertEqual(ipaddr1, gw_ip_get) self.assertEqual(mac1, gw_info.mac) return assert 0
def get_mobilityd_gw_info() -> List[GWInfo]: """ Make RPC call to 'GetGatewayInfo' method of local mobilityD service """ try: chan = ServiceRegistry.get_rpc_channel( SERVICE_NAME, ServiceRegistry.LOCAL, ) except ValueError: logging.error('Cant get RPC channel to %s', SERVICE_NAME) return GWInfo() client = MobilityServiceStub(chan) try: return client.ListGatewayInfo(Void()).gw_list except grpc.RpcError as err: logging.error( "ListGatewayInfo error[%s] %s", err.code(), err.details(), extra=EXCLUDE_FROM_ERROR_MONITORING if indicates_connection_error(err) else None, ) return []
def GetGatewayInfo(self, void, context): ip = ipaddress.ip_address(self._ipv4_allocator.get_gateway_ip_adress()) gw_ip = IPAddress(version=IPAddress.IPV4, address=ip.packed) gw_mac = self._ipv4_allocator.get_gateway_mac_adress() return GWInfo(ip=gw_ip, mac=gw_mac)
from lte.protos.mobilityd_pb2 import IPAddress, GWInfo, IPBlock from magma.pipelined.tests.app.start_pipelined import ( TestSetup, PipelinedController, ) from magma.pipelined.bridge_util import BridgeTools from magma.pipelined.tests.pipelined_test_util import (start_ryu_app_thread, stop_ryu_app_thread, create_service_manager, SnapshotVerifier) from magma.pipelined.app import inout gw_info = GWInfo() def mocked_get_mobilityd_gw_info() -> GWInfo: global gw_info return gw_info def mocked_set_mobilityd_gw_info(updated_gw_info: GWInfo) -> GWInfo: global gw_info gw_info = updated_gw_info class InOutNonNatTest(unittest.TestCase): BRIDGE = 'testing_br' IFACE = 'testing_br'