<s> from django . db import models <EOL> from core . models import Service , PlCoreBase , Slice , Instance , Tenant , TenantWithContainer , Node , Image , User , Flavor , Subscriber , NetworkParameter , NetworkParameterType , Port , AddressPool <EOL> from core . models . plcorebase import StrippedCharField <EOL> import os <EOL> from django . db import models , transaction <EOL> from django . forms . models import model_to_dict <EOL> from django . db . models import Q <EOL> from operator import itemgetter , attrgetter , methodcaller <EOL> from core . models import Tag <EOL> from core . models . service import LeastLoadedNodeScheduler <EOL> import traceback <EOL> from xos . exceptions import * <EOL> from xos . config import Config <EOL> class ConfigurationError ( Exception ) : <EOL> pass <EOL> VROUTER_KIND = "<STR_LIT>" <EOL> CORD_USE_VTN = getattr ( Config ( ) , "<STR_LIT>" , False ) <EOL> class VRouterService ( Service ) : <EOL> KIND = VROUTER_KIND <EOL> class Meta : <EOL> app_label = "<STR_LIT>" <EOL> verbose_name = "<STR_LIT>" <EOL> proxy = True <EOL> def ip_to_mac ( self , ip ) : <EOL> ( a , b , c , d ) = ip . split ( '<STR_LIT:.>' ) <EOL> return "<STR_LIT>" % ( int ( a ) , int ( b ) , int ( c ) , int ( d ) ) <EOL> def get_gateways ( self ) : <EOL> gateways = [ ] <EOL> aps = self . addresspools . all ( ) <EOL> for ap in aps : <EOL> gateways . append ( { "<STR_LIT>" : ap . gateway_ip , "<STR_LIT>" : ap . gateway_mac } ) <EOL> return gateways <EOL> def get_address_pool ( self , name ) : <EOL> ap = AddressPool . objects . filter ( name = name , service = self ) <EOL> if not ap : <EOL> raise Exception ( "<STR_LIT>" % name ) <EOL> return ap [ <NUM_LIT:0> ] <EOL> def get_tenant ( self , ** kwargs ) : <EOL> address_pool_name = kwargs . pop ( "<STR_LIT>" ) <EOL> ap = self . get_address_pool ( address_pool_name ) <EOL> ip = ap . get_address ( ) <EOL> if not ip : <EOL> raise Exception ( "<STR_LIT>" % ap . name ) <EOL> t = VRouterTenant ( provider_service = self , ** kwargs ) <EOL> t . public_ip = ip <EOL> t . public_mac = self . ip_to_mac ( ip ) <EOL> t . address_pool_id = ap . id <EOL> t . save ( ) <EOL> return t <EOL> class VRouterTenant ( Tenant ) : <EOL> class Meta : <EOL> proxy = True <EOL> KIND = VROUTER_KIND <EOL> simple_attributes = ( ( "<STR_LIT>" , None ) , <EOL> ( "<STR_LIT>" , None ) , <EOL> ( "<STR_LIT>" , None ) , <EOL> ) <EOL> @ property <EOL> def gateway_ip ( self ) : <EOL> if not self . address_pool : <EOL> return None <EOL> return self . address_pool . gateway_ip <EOL> @ property <EOL> def gateway_mac ( self ) : <EOL> if not self . address_pool : <EOL> return None <EOL> return self . address_pool . gateway_mac <EOL> @ property <EOL> def cidr ( self ) : <EOL> if not self . address_pool : <EOL> return None <EOL> return self . address_pool . cidr <EOL> @ property <EOL> def netbits ( self ) : <EOL> if self . cidr : <EOL> parts = self . cidr . split ( "<STR_LIT:/>" ) <EOL> if len ( parts ) == <NUM_LIT:2> : <EOL> return int ( parts [ <NUM_LIT:1> ] . strip ( ) ) <EOL> return None <EOL> @ property <EOL> def address_pool ( self ) : <EOL> if getattr ( self , "<STR_LIT>" , None ) : <EOL> return self . cached_address_pool <EOL> if not self . address_pool_id : <EOL> return None <EOL> aps = AddressPool . objects . filter ( id = self . address_pool_id ) <EOL> if not aps : <EOL> return None <EOL> ap = aps [ <NUM_LIT:0> ] <EOL> self . cached_address_pool = ap <EOL> return ap <EOL> @ address_pool . setter <EOL> def address_pool ( self , value ) : <EOL> if value : <EOL> value = value . id <EOL> if ( value != self . get_attribute ( "<STR_LIT>" , None ) ) : <EOL> self . cached_address_pool = None <EOL> self . set_attribute ( "<STR_LIT>" , value ) <EOL> def cleanup_addresspool ( self ) : <EOL> if self . address_pool_id : <EOL> ap = AddressPool . objects . filter ( id = self . address_pool_id ) <EOL> if ap : <EOL> ap [ <NUM_LIT:0> ] . put_address ( self . public_ip ) <EOL> self . public_ip = None <EOL> def delete ( self , * args , ** kwargs ) : <EOL> self . cleanup_addresspool ( ) <EOL> super ( VRouterTenant , self ) . delete ( * args , ** kwargs ) <EOL> VRouterTenant . setup_simple_attributes ( ) </s>
