<s> from oslo_utils import timeutils <EOL> from oslo_utils import versionutils <EOL> from nova import db <EOL> from nova import exception <EOL> from nova import objects <EOL> from nova . objects import base as obj_base <EOL> from nova . objects import fields <EOL> from nova import utils <EOL> FIXED_IP_OPTIONAL_ATTRS = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' ] <EOL> @ obj_base . NovaObjectRegistry . register <EOL> class FixedIP ( obj_base . NovaPersistentObject , obj_base . NovaObject , <EOL> obj_base . NovaObjectDictCompat ) : <EOL> VERSION = '<STR_LIT>' <EOL> fields = { <EOL> '<STR_LIT:id>' : fields . IntegerField ( ) , <EOL> '<STR_LIT:address>' : fields . IPV4AndV6AddressField ( ) , <EOL> '<STR_LIT>' : fields . IntegerField ( nullable = True ) , <EOL> '<STR_LIT>' : fields . IntegerField ( nullable = True ) , <EOL> '<STR_LIT>' : fields . UUIDField ( nullable = True ) , <EOL> '<STR_LIT>' : fields . BooleanField ( ) , <EOL> '<STR_LIT>' : fields . BooleanField ( ) , <EOL> '<STR_LIT>' : fields . BooleanField ( ) , <EOL> '<STR_LIT:host>' : fields . StringField ( nullable = True ) , <EOL> '<STR_LIT>' : fields . BooleanField ( ) , <EOL> '<STR_LIT>' : fields . ObjectField ( '<STR_LIT>' , nullable = True ) , <EOL> '<STR_LIT>' : fields . ObjectField ( '<STR_LIT>' , nullable = True ) , <EOL> '<STR_LIT>' : fields . ObjectField ( '<STR_LIT>' , <EOL> nullable = True ) , <EOL> '<STR_LIT>' : fields . ObjectField ( '<STR_LIT>' ) , <EOL> } <EOL> def obj_make_compatible ( self , primitive , target_version ) : <EOL> super ( FixedIP , self ) . obj_make_compatible ( primitive , target_version ) <EOL> target_version = versionutils . convert_version_to_tuple ( target_version ) <EOL> if target_version < ( <NUM_LIT:1> , <NUM_LIT:4> ) and '<STR_LIT>' in primitive : <EOL> del primitive [ '<STR_LIT>' ] <EOL> @ staticmethod <EOL> def _from_db_object ( context , fixedip , db_fixedip , expected_attrs = None ) : <EOL> if expected_attrs is None : <EOL> expected_attrs = [ ] <EOL> for field in fixedip . fields : <EOL> if field == '<STR_LIT>' : <EOL> continue <EOL> if field not in FIXED_IP_OPTIONAL_ATTRS : <EOL> fixedip [ field ] = db_fixedip [ field ] <EOL> if '<STR_LIT>' in expected_attrs : <EOL> fixedip . instance = objects . Instance . _from_db_object ( <EOL> context , <EOL> objects . Instance ( context ) , <EOL> db_fixedip [ '<STR_LIT>' ] ) if db_fixedip [ '<STR_LIT>' ] else None <EOL> if '<STR_LIT>' in expected_attrs : <EOL> fixedip . network = objects . Network . _from_db_object ( <EOL> context , <EOL> objects . Network ( context ) , <EOL> db_fixedip [ '<STR_LIT>' ] ) if db_fixedip [ '<STR_LIT>' ] else None <EOL> if '<STR_LIT>' in expected_attrs : <EOL> db_vif = db_fixedip [ '<STR_LIT>' ] <EOL> vif = objects . VirtualInterface . _from_db_object ( <EOL> context , <EOL> objects . VirtualInterface ( context ) , <EOL> db_fixedip [ '<STR_LIT>' ] ) if db_vif else None <EOL> fixedip . virtual_interface = vif <EOL> if '<STR_LIT>' in expected_attrs : <EOL> fixedip . floating_ips = obj_base . obj_make_list ( <EOL> context , objects . FloatingIPList ( context ) , <EOL> objects . FloatingIP , db_fixedip [ '<STR_LIT>' ] ) <EOL> fixedip . _context = context <EOL> fixedip . obj_reset_changes ( ) <EOL> return fixedip <EOL> @ obj_base . remotable_classmethod <EOL> def get_by_id ( cls , context , id , expected_attrs = None ) : <EOL> if expected_attrs is None : <EOL> expected_attrs = [ ] <EOL> get_network = '<STR_LIT>' in expected_attrs <EOL> db_fixedip = db . fixed_ip_get ( context , id , get_network = get_network ) <EOL> return cls . _from_db_object ( context , cls ( context ) , db_fixedip , <EOL> expected_attrs ) <EOL> @ obj_base . remotable_classmethod <EOL> def get_by_address ( cls , context , address , expected_attrs = None ) : <EOL> if expected_attrs is None : <EOL> expected_attrs = [ ] <EOL> db_fixedip = db . fixed_ip_get_by_address ( context , str ( address ) , <EOL> columns_to_join = expected_attrs ) <EOL> return cls . _from_db_object ( context , cls ( context ) , db_fixedip , <EOL> expected_attrs ) <EOL> @ obj_base . remotable_classmethod <EOL> def get_by_floating_address ( cls , context , address ) : <EOL> db_fixedip = db . fixed_ip_get_by_floating_address ( context , str ( address ) ) <EOL> if db_fixedip is not None : <EOL> return cls . _from_db_object ( context , cls ( context ) , db_fixedip ) <EOL> @ obj_base . remotable_classmethod <EOL> def get_by_network_and_host ( cls , context , network_id , host ) : <EOL> db_fixedip = db . fixed_ip_get_by_network_host ( context , network_id , host ) <EOL> return cls . _from_db_object ( context , cls ( context ) , db_fixedip ) <EOL> @ obj_base . remotable_classmethod <EOL> def associate ( cls , context , address , instance_uuid , network_id = None , <EOL> reserved = False , vif_id = None ) : <EOL> db_fixedip = db . fixed_ip_associate ( context , address , instance_uuid , <EOL> network_id = network_id , <EOL> reserved = reserved , <EOL> virtual_interface_id = vif_id ) <EOL> return cls . _from_db_object ( context , cls ( context ) , db_fixedip ) <EOL> @ obj_base . remotable_classmethod <EOL> def associate_pool ( cls , context , network_id , instance_uuid = None , <EOL> host = None , vif_id = None ) : <EOL> db_fixedip = db . fixed_ip_associate_pool ( context , network_id , <EOL> instance_uuid = instance_uuid , <EOL> host = host , <EOL> virtual_interface_id = vif_id ) <EOL> return cls . _from_db_object ( context , cls ( context ) , db_fixedip ) <EOL> @ obj_base . remotable_classmethod <EOL> def disassociate_by_address ( cls , context , address ) : <EOL> db . fixed_ip_disassociate ( context , address ) <EOL> @ obj_base . remotable_classmethod <EOL> def _disassociate_all_by_timeout ( cls , context , host , time_str ) : <EOL> time = timeutils . parse_isotime ( time_str ) <EOL> return db . fixed_ip_disassociate_all_by_timeout ( context , host , time ) <EOL> @ classmethod <EOL> def disassociate_all_by_timeout ( cls , context , host , time ) : <EOL> return cls . _disassociate_all_by_timeout ( context , host , <EOL> utils . isotime ( time ) ) <EOL> @ obj_base . remotable <EOL> def create ( self ) : <EOL> updates = self . obj_get_changes ( ) <EOL> if '<STR_LIT:id>' in updates : <EOL> raise exception . ObjectActionError ( action = '<STR_LIT>' , <EOL> reason = '<STR_LIT>' ) <EOL> if '<STR_LIT:address>' in updates : <EOL> updates [ '<STR_LIT:address>' ] = str ( updates [ '<STR_LIT:address>' ] ) <EOL> db_fixedip = db . fixed_ip_create ( self . _context , updates ) <EOL> self . _from_db_object ( self . _context , self , db_fixedip ) <EOL> @ obj_base . remotable <EOL> def save ( self ) : <EOL> updates = self . obj_get_changes ( ) <EOL> if '<STR_LIT:address>' in updates : <EOL> raise exception . ObjectActionError ( action = '<STR_LIT>' , <EOL> reason = '<STR_LIT>' ) <EOL> db . fixed_ip_update ( self . _context , str ( self . address ) , updates ) <EOL> self . obj_reset_changes ( ) <EOL> @ obj_base . remotable <EOL> def disassociate ( self ) : <EOL> db . fixed_ip_disassociate ( self . _context , str ( self . address ) ) <EOL> self . instance_uuid = None <EOL> self . instance = None <EOL> self . obj_reset_changes ( [ '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> @ obj_base . NovaObjectRegistry . register <EOL> class FixedIPList ( obj_base . ObjectListBase , obj_base . NovaObject ) : <EOL> VERSION = '<STR_LIT>' <EOL> fields = { <EOL> '<STR_LIT>' : fields . ListOfObjectsField ( '<STR_LIT>' ) , <EOL> } <EOL> @ obj_base . remotable_classmethod <EOL> def get_all ( cls , context ) : <EOL> db_fixedips = db . fixed_ip_get_all ( context ) <EOL> return obj_base . obj_make_list ( context , cls ( context ) , <EOL> objects . FixedIP , db_fixedips ) <EOL> @ obj_base . remotable_classmethod <EOL> def get_by_instance_uuid ( cls , context , instance_uuid ) : <EOL> expected_attrs = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> db_fixedips = db . fixed_ip_get_by_instance ( context , instance_uuid ) <EOL> return obj_base . obj_make_list ( context , cls ( context ) , <EOL> objects . FixedIP , db_fixedips , <EOL> expected_attrs = expected_attrs ) <EOL> @ obj_base . remotable_classmethod <EOL> def get_by_host ( cls , context , host ) : <EOL> db_fixedips = db . fixed_ip_get_by_host ( context , host ) <EOL> return obj_base . obj_make_list ( context , cls ( context ) , <EOL> objects . FixedIP , db_fixedips ) <EOL> @ obj_base . remotable_classmethod <EOL> def get_by_virtual_interface_id ( cls , context , vif_id ) : <EOL> expected_attrs = [ '<STR_LIT>' , '<STR_LIT>' ] <EOL> db_fixedips = db . fixed_ips_by_virtual_interface ( context , vif_id ) <EOL> return obj_base . obj_make_list ( context , cls ( context ) , <EOL> objects . FixedIP , db_fixedips , <EOL> expected_attrs = expected_attrs ) <EOL> @ obj_base . remotable_classmethod <EOL> def get_by_network ( cls , context , network , host = None ) : <EOL> ipinfo = db . network_get_associated_fixed_ips ( context , <EOL> network [ '<STR_LIT:id>' ] , <EOL> host = host ) <EOL> if not ipinfo : <EOL> return [ ] <EOL> fips = cls ( context = context , objects = [ ] ) <EOL> for info in ipinfo : <EOL> inst = objects . Instance ( context = context , <EOL> uuid = info [ '<STR_LIT>' ] , <EOL> hostname = info [ '<STR_LIT>' ] , <EOL> created_at = info [ '<STR_LIT>' ] , <EOL> updated_at = info [ '<STR_LIT>' ] ) <EOL> vif = objects . VirtualInterface ( context = context , <EOL> id = info [ '<STR_LIT>' ] , <EOL> address = info [ '<STR_LIT>' ] ) <EOL> fip = objects . FixedIP ( context = context , <EOL> address = info [ '<STR_LIT:address>' ] , <EOL> instance_uuid = info [ '<STR_LIT>' ] , <EOL> network_id = info [ '<STR_LIT>' ] , <EOL> virtual_interface_id = info [ '<STR_LIT>' ] , <EOL> allocated = info [ '<STR_LIT>' ] , <EOL> leased = info [ '<STR_LIT>' ] , <EOL> default_route = info [ '<STR_LIT>' ] , <EOL> instance = inst , <EOL> virtual_interface = vif ) <EOL> fips . objects . append ( fip ) <EOL> fips . obj_reset_changes ( ) <EOL> return fips <EOL> @ obj_base . remotable_classmethod <EOL> def bulk_create ( self , context , fixed_ips ) : <EOL> ips = [ ] <EOL> for fixedip in fixed_ips : <EOL> ip = obj_base . obj_to_primitive ( fixedip ) <EOL> if '<STR_LIT:id>' in ip : <EOL> raise exception . ObjectActionError ( action = '<STR_LIT>' , <EOL> reason = '<STR_LIT>' ) <EOL> ips . append ( ip ) <EOL> db . fixed_ip_bulk_create ( context , ips ) </s>
