def _delete_zone_and_remove_fc_initiators(self, wwns, host_id): # Get tgt_port_wwns and init_targ_map to remove zone. portg_id = None if not self.fcsan: self.fcsan = fczm_utils.create_lookup_service() if self.fcsan: zone_helper = fc_zone_helper.FCZoneHelper(self.fcsan, self.client) (tgt_port_wwns, portg_id, init_targ_map) = ( zone_helper.get_init_targ_map(wwns, host_id)) else: (tgt_port_wwns, init_targ_map) = ( self.client.get_init_targ_map(wwns)) # Remove the initiators from host if need. if host_id: fc_initiators = self.client.get_host_fc_initiators(host_id) for wwn in wwns: if wwn in fc_initiators: self.client.remove_fc_from_host(wwn) info = {'driver_volume_type': 'fibre_channel', 'data': {'target_wwn': tgt_port_wwns, 'initiator_target_map': init_targ_map}} return info, portg_id
def terminate_connection(self, volume, connector, **kwargs): """Delete map between a volume and a host.""" wwns = connector['wwpns'] volume_name = huawei_utils.encode_name(volume['id']) lun_id = volume.get('provider_location') host_name = connector['host'] left_lunnum = -1 lungroup_id = None view_id = None LOG.info( _LI('terminate_connection: volume name: %(volume)s, ' 'wwpns: %(wwns)s, ' 'lun_id: %(lunid)s.'), { 'volume': volume_name, 'wwns': wwns, 'lunid': lun_id }, ) if host_name and len(host_name) > constants.MAX_HOSTNAME_LENGTH: host_name = six.text_type(hash(host_name)) host_id = self.restclient.find_host(host_name) if host_id: mapping_view_name = constants.MAPPING_VIEW_PREFIX + host_id view_id = self.restclient.find_mapping_view(mapping_view_name) if view_id: lungroup_id = self.restclient.find_lungroup_from_map(view_id) if lun_id and self.restclient.check_lun_exist(lun_id): if lungroup_id: lungroup_ids = self.restclient.get_lungroupids_by_lunid(lun_id) if lungroup_id in lungroup_ids: self.restclient.remove_lun_from_lungroup( lungroup_id, lun_id) else: LOG.warning( _LW("Lun is not in lungroup. " "Lun id: %(lun_id)s. " "Lungroup id: %(lungroup_id)s."), { "lun_id": lun_id, "lungroup_id": lungroup_id }) else: LOG.warning(_LW("Can't find lun on the array.")) if lungroup_id: left_lunnum = self.restclient.get_lunnum_from_lungroup(lungroup_id) if int(left_lunnum) > 0: info = {'driver_volume_type': 'fibre_channel', 'data': {}} else: if not self.fcsan_lookup_service: self.fcsan_lookup_service = fczm_utils.create_lookup_service() if self.fcsan_lookup_service: zone_helper = fc_zone_helper.FCZoneHelper( self.fcsan_lookup_service, self.restclient) (tgt_port_wwns, init_targ_map) = (zone_helper.build_ini_targ_map(wwns)) else: (tgt_port_wwns, init_targ_map) = (self.restclient.get_init_targ_map(wwns)) for wwn in wwns: if self.restclient.is_fc_initiator_associated_to_host(wwn): self.restclient.remove_fc_from_host(wwn) if lungroup_id: if view_id and self.restclient.lungroup_associated( view_id, lungroup_id): self.restclient.delete_lungroup_mapping_view( view_id, lungroup_id) self.restclient.delete_lungroup(lungroup_id) if host_id: hostgroup_name = constants.HOSTGROUP_PREFIX + host_id hostgroup_id = self.restclient.find_hostgroup(hostgroup_name) if hostgroup_id: if view_id and self.restclient.hostgroup_associated( view_id, hostgroup_id): self.restclient.delete_hostgoup_mapping_view( view_id, hostgroup_id) self.restclient.remove_host_from_hostgroup( hostgroup_id, host_id) self.restclient.delete_hostgroup(hostgroup_id) if not self.restclient.check_fc_initiators_exist_in_host( host_id): self.restclient.remove_host(host_id) if view_id: self.restclient.delete_mapping_view(view_id) info = { 'driver_volume_type': 'fibre_channel', 'data': { 'target_wwn': tgt_port_wwns, 'initiator_target_map': init_targ_map } } LOG.info(_LI("terminate_connection, return data is: %s."), info) return info
def initialize_connection(self, volume, connector): wwns = connector['wwpns'] volume_name = huawei_utils.encode_name(volume['id']) LOG.info( _LI('initialize_connection, initiator: %(wwpns)s,' ' volume name: %(volume)s.'), { 'wwpns': wwns, 'volume': volume_name }, ) lun_id = self.restclient.get_lunid(volume, volume_name) host_name_before_hash = None host_name = connector['host'] if host_name and (len(host_name) > constants.MAX_HOSTNAME_LENGTH): host_name_before_hash = host_name host_name = six.text_type(hash(host_name)) if not self.fcsan_lookup_service: self.fcsan_lookup_service = fczm_utils.create_lookup_service() if self.fcsan_lookup_service: # Use FC switch. host_id = self.restclient.add_host_with_check( host_name, host_name_before_hash) zone_helper = fc_zone_helper.FCZoneHelper( self.fcsan_lookup_service, self.restclient) (tgt_port_wwns, init_targ_map) = (zone_helper.build_ini_targ_map(wwns)) for ini in init_targ_map: self.restclient.ensure_fc_initiator_added(ini, host_id) else: # Not use FC switch. host_id = self.restclient.add_host_with_check( host_name, host_name_before_hash) online_wwns_in_host = ( self.restclient.get_host_online_fc_initiators(host_id)) online_free_wwns = self.restclient.get_online_free_wwns() for wwn in wwns: if (wwn not in online_wwns_in_host and wwn not in online_free_wwns): wwns_in_host = ( self.restclient.get_host_fc_initiators(host_id)) iqns_in_host = ( self.restclient.get_host_iscsi_initiators(host_id)) if not wwns_in_host and not iqns_in_host: self.restclient.remove_host(host_id) msg = (_('Can not add FC initiator to host.')) LOG.error(msg) raise exception.VolumeBackendAPIException(data=msg) for wwn in wwns: if wwn in online_free_wwns: self.restclient.add_fc_port_to_host(host_id, wwn) (tgt_port_wwns, init_targ_map) = (self.restclient.get_init_targ_map(wwns)) # Add host into hostgroup. hostgroup_id = self.restclient.add_host_into_hostgroup(host_id) self.restclient.do_mapping(lun_id, hostgroup_id, host_id) host_lun_id = self.restclient.find_host_lun_id(host_id, lun_id) # Return FC properties. info = { 'driver_volume_type': 'fibre_channel', 'data': { 'target_lun': int(host_lun_id), 'target_discovered': True, 'target_wwn': tgt_port_wwns, 'volume_id': volume['id'], 'initiator_target_map': init_targ_map }, } LOG.info(_LI("initialize_connection, return data is: %s."), info) return info
def initialize_connection(self, volume, connector): lun_id, lun_type = self.get_lun_id_and_type(volume) wwns = connector['wwpns'] LOG.info( 'initialize_connection, initiator: %(wwpns)s,' ' LUN ID: %(lun_id)s.', {'wwpns': wwns, 'lun_id': lun_id},) portg_id = None host_id = self.client.add_host_with_check(connector['host']) if not self.fcsan: self.fcsan = fczm_utils.create_lookup_service() if self.fcsan: # Use FC switch. zone_helper = fc_zone_helper.FCZoneHelper(self.fcsan, self.client) try: (tgt_port_wwns, portg_id, init_targ_map) = ( zone_helper.build_ini_targ_map(wwns, host_id, lun_id, lun_type)) except Exception as err: self.remove_host_with_check(host_id) msg = _('build_ini_targ_map fails. %s') % err raise exception.VolumeBackendAPIException(data=msg) for ini in init_targ_map: self.client.ensure_fc_initiator_added(ini, host_id) else: # Not use FC switch. online_wwns_in_host = ( self.client.get_host_online_fc_initiators(host_id)) online_free_wwns = self.client.get_online_free_wwns() fc_initiators_on_array = self.client.get_fc_initiator_on_array() wwns = [i for i in wwns if i in fc_initiators_on_array] for wwn in wwns: if (wwn not in online_wwns_in_host and wwn not in online_free_wwns): wwns_in_host = ( self.client.get_host_fc_initiators(host_id)) iqns_in_host = ( self.client.get_host_iscsi_initiators(host_id)) if not (wwns_in_host or iqns_in_host or self.client.is_host_associated_to_hostgroup(host_id)): self.client.remove_host(host_id) msg = _('No FC initiator can be added to host.') LOG.error(msg) raise exception.VolumeBackendAPIException(data=msg) for wwn in wwns: if wwn in online_free_wwns: self.client.add_fc_port_to_host(host_id, wwn) (tgt_port_wwns, init_targ_map) = ( self.client.get_init_targ_map(wwns)) # Add host into hostgroup. hostgroup_id = self.client.add_host_to_hostgroup(host_id) metadata = huawei_utils.get_volume_private_data(volume) LOG.info("initialize_connection, metadata is: %s.", metadata) hypermetro_lun = metadata.get('hypermetro_id') is not None map_info = self.client.do_mapping(lun_id, hostgroup_id, host_id, portg_id, lun_type, hypermetro_lun) host_lun_id = self.client.get_host_lun_id(host_id, lun_id, lun_type) # Return FC properties. fc_info = {'driver_volume_type': 'fibre_channel', 'data': {'target_lun': int(host_lun_id), 'target_discovered': True, 'target_wwn': tgt_port_wwns, 'volume_id': volume.id, 'initiator_target_map': init_targ_map, 'map_info': map_info}, } # Deal with hypermetro connection. if hypermetro_lun: loc_tgt_wwn = fc_info['data']['target_wwn'] local_ini_tgt_map = fc_info['data']['initiator_target_map'] hyperm = hypermetro.HuaweiHyperMetro(self.client, self.rmt_client, self.configuration) rmt_fc_info = hyperm.connect_volume_fc(volume, connector) rmt_tgt_wwn = rmt_fc_info['data']['target_wwn'] rmt_ini_tgt_map = rmt_fc_info['data']['initiator_target_map'] fc_info['data']['target_wwn'] = (loc_tgt_wwn + rmt_tgt_wwn) wwns = connector['wwpns'] for wwn in wwns: if (wwn in local_ini_tgt_map and wwn in rmt_ini_tgt_map): fc_info['data']['initiator_target_map'][wwn].extend( rmt_ini_tgt_map[wwn]) elif (wwn not in local_ini_tgt_map and wwn in rmt_ini_tgt_map): fc_info['data']['initiator_target_map'][wwn] = ( rmt_ini_tgt_map[wwn]) # else, do nothing loc_map_info = fc_info['data']['map_info'] rmt_map_info = rmt_fc_info['data']['map_info'] same_host_id = self._get_same_hostid(loc_map_info, rmt_map_info) self.client.change_hostlun_id(loc_map_info, same_host_id) hyperm.rmt_client.change_hostlun_id(rmt_map_info, same_host_id) fc_info['data']['target_lun'] = same_host_id hyperm.rmt_client.logout() fczm_utils.add_fc_zone(fc_info) LOG.info("Return FC info is: %s.", fc_info) return fc_info