<s> import netaddr <EOL> from oslo_utils import versionutils <EOL> import nova . conf <EOL> from nova import db <EOL> from nova import exception <EOL> from nova . i18n import _ <EOL> from nova import objects <EOL> from nova . objects import base as obj_base <EOL> from nova . objects import fields <EOL> CONF = nova . conf . CONF <EOL> @ obj_base . NovaObjectRegistry . register <EOL> class Network ( 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:label>' : fields . StringField ( ) , <EOL> '<STR_LIT>' : fields . BooleanField ( ) , <EOL> '<STR_LIT>' : fields . IPV4NetworkField ( nullable = True ) , <EOL> '<STR_LIT>' : fields . IPV6NetworkField ( nullable = True ) , <EOL> '<STR_LIT>' : fields . BooleanField ( ) , <EOL> '<STR_LIT>' : fields . IPV4AddressField ( nullable = True ) , <EOL> '<STR_LIT>' : fields . IPV4AddressField ( nullable = True ) , <EOL> '<STR_LIT>' : fields . IPV4AddressField ( nullable = True ) , <EOL> '<STR_LIT>' : fields . IPV6AddressField ( nullable = True ) , <EOL> '<STR_LIT>' : fields . IPV6AddressField ( nullable = True ) , <EOL> '<STR_LIT>' : fields . StringField ( nullable = True ) , <EOL> '<STR_LIT>' : fields . StringField ( nullable = True ) , <EOL> '<STR_LIT>' : fields . IPAddressField ( nullable = True ) , <EOL> '<STR_LIT>' : fields . IPAddressField ( nullable = True ) , <EOL> '<STR_LIT>' : fields . IntegerField ( nullable = True ) , <EOL> '<STR_LIT>' : fields . IPAddressField ( nullable = True ) , <EOL> '<STR_LIT>' : fields . IntegerField ( nullable = True ) , <EOL> '<STR_LIT>' : fields . IPAddressField ( nullable = True ) , <EOL> '<STR_LIT>' : fields . IPV4AddressField ( nullable = True ) , <EOL> '<STR_LIT>' : fields . IntegerField ( nullable = True ) , <EOL> '<STR_LIT>' : fields . UUIDField ( nullable = True ) , <EOL> '<STR_LIT>' : fields . IntegerField ( nullable = True ) , <EOL> '<STR_LIT:host>' : fields . StringField ( nullable = True ) , <EOL> '<STR_LIT>' : fields . UUIDField ( ) , <EOL> '<STR_LIT>' : fields . IntegerField ( nullable = True ) , <EOL> '<STR_LIT>' : fields . IPAddressField ( nullable = True ) , <EOL> '<STR_LIT>' : fields . BooleanField ( ) , <EOL> '<STR_LIT>' : fields . BooleanField ( ) , <EOL> } <EOL> @ staticmethod <EOL> def _convert_legacy_ipv6_netmask ( netmask ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> prefix = int ( netmask ) <EOL> return netaddr . IPNetwork ( '<STR_LIT>' % prefix ) . netmask <EOL> except ValueError : <EOL> pass <EOL> try : <EOL> return netaddr . IPNetwork ( netmask ) . netmask <EOL> except netaddr . AddrFormatError : <EOL> raise ValueError ( _ ( '<STR_LIT>' <EOL> '<STR_LIT>' ) % netmask ) <EOL> def obj_make_compatible ( self , primitive , target_version ) : <EOL> target_version = versionutils . convert_version_to_tuple ( target_version ) <EOL> if target_version < ( <NUM_LIT:1> , <NUM_LIT:2> ) : <EOL> if '<STR_LIT>' in primitive : <EOL> del primitive [ '<STR_LIT>' ] <EOL> if '<STR_LIT>' in primitive : <EOL> del primitive [ '<STR_LIT>' ] <EOL> if '<STR_LIT>' in primitive : <EOL> del primitive [ '<STR_LIT>' ] <EOL> if '<STR_LIT>' in primitive : <EOL> del primitive [ '<STR_LIT>' ] <EOL> @ staticmethod <EOL> def _from_db_object ( context , network , db_network ) : <EOL> for field in network . fields : <EOL> db_value = db_network [ field ] <EOL> if field is '<STR_LIT>' and db_value is not None : <EOL> db_value = network . _convert_legacy_ipv6_netmask ( db_value ) <EOL> if field is '<STR_LIT>' and db_value is None : <EOL> db_value = CONF . network_device_mtu <EOL> if field is '<STR_LIT>' and db_value is None : <EOL> db_value = db_network [ '<STR_LIT>' ] <EOL> if field is '<STR_LIT>' and CONF . share_dhcp_address : <EOL> db_value = CONF . share_dhcp_address <EOL> network [ field ] = db_value <EOL> network . _context = context <EOL> network . obj_reset_changes ( ) <EOL> return network <EOL> @ obj_base . remotable_classmethod <EOL> def get_by_id ( cls , context , network_id , project_only = '<STR_LIT>' ) : <EOL> db_network = db . network_get ( context , network_id , <EOL> project_only = project_only ) <EOL> return cls . _from_db_object ( context , cls ( ) , db_network ) <EOL> @ obj_base . remotable_classmethod <EOL> def get_by_uuid ( cls , context , network_uuid ) : <EOL> db_network = db . network_get_by_uuid ( context , network_uuid ) <EOL> return cls . _from_db_object ( context , cls ( ) , db_network ) <EOL> @ obj_base . remotable_classmethod <EOL> def get_by_cidr ( cls , context , cidr ) : <EOL> db_network = db . network_get_by_cidr ( context , cidr ) <EOL> return cls . _from_db_object ( context , cls ( ) , db_network ) <EOL> @ obj_base . remotable_classmethod <EOL> def associate ( cls , context , project_id , network_id = None , force = False ) : <EOL> db . network_associate ( context , project_id , network_id = network_id , <EOL> force = force ) <EOL> @ obj_base . remotable_classmethod <EOL> def disassociate ( cls , context , network_id , host = False , project = False ) : <EOL> db . network_disassociate ( context , network_id , host , project ) <EOL> @ obj_base . remotable_classmethod <EOL> def in_use_on_host ( cls , context , network_id , host ) : <EOL> return db . network_in_use_on_host ( context , network_id , host ) <EOL> def _get_primitive_changes ( self ) : <EOL> changes = { } <EOL> for key , value in self . obj_get_changes ( ) . items ( ) : <EOL> if isinstance ( value , netaddr . IPAddress ) : <EOL> changes [ key ] = str ( value ) <EOL> else : <EOL> changes [ key ] = value <EOL> return changes <EOL> @ obj_base . remotable <EOL> def create ( self ) : <EOL> updates = self . _get_primitive_changes ( ) <EOL> if '<STR_LIT:id>' in updates : <EOL> raise exception . ObjectActionError ( action = '<STR_LIT>' , <EOL> reason = '<STR_LIT>' ) <EOL> db_network = db . network_create_safe ( self . _context , updates ) <EOL> self . _from_db_object ( self . _context , self , db_network ) <EOL> @ obj_base . remotable <EOL> def destroy ( self ) : <EOL> db . network_delete_safe ( self . _context , self . id ) <EOL> self . deleted = True <EOL> self . obj_reset_changes ( [ '<STR_LIT>' ] ) <EOL> @ obj_base . remotable <EOL> def save ( self ) : <EOL> context = self . _context <EOL> updates = self . _get_primitive_changes ( ) <EOL> if '<STR_LIT>' in updates : <EOL> updates [ '<STR_LIT>' ] = netaddr . IPNetwork ( <EOL> updates [ '<STR_LIT>' ] ) . netmask <EOL> set_host = '<STR_LIT:host>' in updates <EOL> if set_host : <EOL> db . network_set_host ( context , self . id , updates . pop ( '<STR_LIT:host>' ) ) <EOL> if updates : <EOL> db_network = db . network_update ( context , self . id , updates ) <EOL> elif set_host : <EOL> db_network = db . network_get ( context , self . id ) <EOL> else : <EOL> db_network = None <EOL> if db_network is not None : <EOL> self . _from_db_object ( context , self , db_network ) <EOL> @ obj_base . NovaObjectRegistry . register <EOL> class NetworkList ( 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 , project_only = '<STR_LIT>' ) : <EOL> db_networks = db . network_get_all ( context , project_only ) <EOL> return obj_base . obj_make_list ( context , cls ( context ) , objects . Network , <EOL> db_networks ) <EOL> @ obj_base . remotable_classmethod <EOL> def get_by_uuids ( cls , context , network_uuids , project_only = '<STR_LIT>' ) : <EOL> db_networks = db . network_get_all_by_uuids ( context , network_uuids , <EOL> project_only ) <EOL> return obj_base . obj_make_list ( context , cls ( context ) , objects . Network , <EOL> db_networks ) <EOL> @ obj_base . remotable_classmethod <EOL> def get_by_host ( cls , context , host ) : <EOL> db_networks = db . network_get_all_by_host ( context , host ) <EOL> return obj_base . obj_make_list ( context , cls ( context ) , objects . Network , <EOL> db_networks ) <EOL> @ obj_base . remotable_classmethod <EOL> def get_by_project ( cls , context , project_id , associate = True ) : <EOL> db_networks = db . project_get_networks ( context , project_id , <EOL> associate = associate ) <EOL> return obj_base . obj_make_list ( context , cls ( context ) , objects . Network , <EOL> db_networks ) </s>
