def _get_allocate_ip_response(self, composite_sid, version, context, ip_addr, request): try: ip, vlan = self._ip_address_man.alloc_ip_address( composite_sid, version) logging.info("Allocated IP %s for sid %s for apn %s" % (ip, SIDUtils.to_str(request.sid), request.apn)) ip_addr.version = version ip_addr.address = ip.packed return AllocateIPAddressResponse(ip_list=[ip_addr], vlan=str(vlan)) except NoAvailableIPError: context.set_details('No free 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) except DuplicateIPAssignmentError: context.set_details('IP has been allocated for other subscriber') context.set_code(grpc.StatusCode.ALREADY_EXISTS) except MaxCalculationError: context.set_details('Reached maximum IPv6 calculation tries') context.set_code(grpc.StatusCode.RESOURCE_EXHAUSTED) except SubscriberDBConnectionError: context.set_details('Could not connect to SubscriberDB') context.set_code(grpc.StatusCode.FAILED_PRECONDITION) except SubscriberDBStaticIPValueError: context.set_details( 'Could not parse static IP response from SubscriberDB') context.set_code(grpc.StatusCode.FAILED_PRECONDITION) except SubscriberDBMultiAPNValueError: context.set_details( 'Could not parse MultiAPN IP response from SubscriberDB') context.set_code(grpc.StatusCode.FAILED_PRECONDITION) return AllocateIPAddressResponse()
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 """ logging.debug("Received AllocateIPAddress") self._print_grpc(request) ip_addr = IPAddress() composite_sid = SIDUtils.to_str(request.sid) if request.apn: composite_sid = composite_sid + "." + request.apn if request.version == AllocateIPRequest.IPV4: resp = self._get_allocate_ip_response(composite_sid + ",ipv4", IPAddress.IPV4, context, ip_addr, request) elif request.version == AllocateIPRequest.IPV6: resp = self._get_allocate_ip_response(composite_sid + ",ipv6", IPAddress.IPV6, context, ip_addr, request) elif request.version == AllocateIPRequest.IPV4V6: ipv4_response = self._get_allocate_ip_response( composite_sid + ",ipv4", IPAddress.IPV4, context, ip_addr, request) ipv6_response = self._get_allocate_ip_response( composite_sid + ",ipv6", IPAddress.IPV6, context, ip_addr, request) ipv4_addr = ipv4_response.ip_list[0] ipv6_addr = ipv6_response.ip_list[0] # Get vlan from IPv4 Allocate response resp = AllocateIPAddressResponse(ip_list=[ipv4_addr, ipv6_addr], vlan=ipv4_response.vlan) else: resp = AllocateIPAddressResponse() self._print_grpc(resp) return resp
def AllocateIPAddress(self, request, context): """ Allocate an IP address from the free IP pool """ logging.debug("Received AllocateIPAddress") self._print_grpc(request) composite_sid = SIDUtils.to_str(request.sid) if request.apn: composite_sid = composite_sid + "." + request.apn if request.version == AllocateIPRequest.IPV4: resp = self._get_allocate_ip_response( composite_sid + ",ipv4", IPAddress.IPV4, context, request, ) elif request.version == AllocateIPRequest.IPV6: resp = self._get_allocate_ip_response( composite_sid + ",ipv6", IPAddress.IPV6, context, request, ) elif request.version == AllocateIPRequest.IPV4V6: ipv4_response = self._get_allocate_ip_response( composite_sid + ",ipv4", IPAddress.IPV4, context, request, ) ipv6_response = self._get_allocate_ip_response( composite_sid + ",ipv6", IPAddress.IPV6, context, request, ) try: ipv4_address = ipv4_response.ip_list[0] ipv6_address = ipv6_response.ip_list[0] except IndexError: logging.warning( "IPv4/IPv6 IP address allocation not successful") resp = AllocateIPAddressResponse() else: resp = AllocateIPAddressResponse( ip_list=[ipv4_address, ipv6_address], vlan=ipv4_response.vlan, ) else: resp = AllocateIPAddressResponse() self._print_grpc(resp) return resp