def release_ip_handler(client, args): try: sid_msg = SIDUtils.to_pb(args.sid) except ValueError: print("Error: invalid SubscriberID format: %s" % args.sid) return try: ip = ipaddress.ip_address(args.ip) except ValueError: print("Error: invalid IP format: %s" % args.ip) return ip_msg = IPAddress() if ip.version == 4: ip_msg.version = IPAddress.IPV4 elif ip.version == 6: ip_msg.version = IPAddress.IPV6 else: print("Error: unknown IP version") return ip_msg.address = ip.packed request = ReleaseIPRequest() request.sid.CopyFrom(sid_msg) request.ip.CopyFrom(ip_msg) client.ReleaseIPAddress(request)
def AllocateIPAddress(self, request, context): """ Allocate an IP address from the free IP pool """ if request.version == AllocateIPRequest.IPV4: try: ip_addr = IPAddress() composite_sid = SIDUtils.to_str(request.sid) if request.apn: composite_sid = composite_sid + "." + request.apn ip, vlan = self._ipv4_allocator.alloc_ip_address(composite_sid) logging.info("Allocated IPv4 %s for sid %s for apn %s" % (ip, SIDUtils.to_str(request.sid), request.apn)) ip_addr.version = IPAddress.IPV4 ip_addr.address = ip.packed return AllocateIPAddressResponse(ip_addr=ip_addr, vlan=str(vlan)) except NoAvailableIPError: context.set_details('No free IPv4 IP available') context.set_code(grpc.StatusCode.RESOURCE_EXHAUSTED) except DuplicatedIPAllocationError: context.set_details('IP has been allocated for this subscriber') context.set_code(grpc.StatusCode.ALREADY_EXISTS) else: self._unimplemented_ip_version_error(context) return AllocateIPAddressResponse()
def AllocateIPAddress(self, request, context): """ Allocate an IP address from the free IP pool """ resp = IPAddress() if request.version == AllocateIPRequest.IPV4: try: subscriber_id = SIDUtils.to_str(request.sid) ip = self._ipv4_allocator.alloc_ip_address(subscriber_id) logging.info("Allocated IPv4 %s for sid %s" % (ip, subscriber_id)) resp.version = IPAddress.IPV4 resp.address = ip.packed except NoAvailableIPError: context.set_details('No free IPv4 IP available') context.set_code(grpc.StatusCode.RESOURCE_EXHAUSTED) except DuplicatedIPAllocationError: context.set_details( 'IP has been allocated for this subscriber') context.set_code(grpc.StatusCode.ALREADY_EXISTS) else: self._unimplemented_ip_version_error(context) return resp