#    (c) Copyright 2014 Brocade Communications Systems Inc.
#    All Rights Reserved.
#
#    Copyright 2014 OpenStack Foundation
#
#    Licensed under the Apache License, Version 2.0 (the "License"); you may
#    not use this file except in compliance with the License. You may obtain
#    a copy of the License at
#
#         http://www.apache.org/licenses/LICENSE-2.0
#
#    Unless required by applicable law or agreed to in writing, software
#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
#    License for the specific language governing permissions and limitations
#    under the License.
#


"""Unit tests for Brocade fc zone driver."""

import mock
from oslo.config import cfg
import paramiko

from cinder import exception
from cinder.i18n import _
from cinder.openstack.common import importutils
from cinder.openstack.common import log as logging
from cinder import test
from cinder.volume import configuration as conf
from cinder.zonemanager.drivers.brocade.brcd_fc_zone_driver \
    import BrcdFCZoneDriver

LOG = logging.getLogger(__name__)

_active_cfg_before_add = {}
_active_cfg_before_delete = {
    'zones': {
        'openstack10008c7cff523b0120240002ac000a50': (
            ['10:00:8c:7c:ff:52:3b:01',
             '20:24:00:02:ac:00:0a:50']), 't_zone': ['1,0']},
        'active_zone_config': 'cfg1'}
_activate = True
_zone_name = 'openstack10008c7cff523b0120240002ac000a50'
_target_ns_map = {'100000051e55a100': ['20240002ac000a50']}
_initiator_ns_map = {'100000051e55a100': ['10008c7cff523b01']}
_zone_map_to_add = {'openstack10008c7cff523b0120240002ac000a50': (
    ['10:00:8c:7c:ff:52:3b:01', '20:24:00:02:ac:00:0a:50'])}

_initiator_target_map = {'10008c7cff523b01': ['20240002ac000a50']}
_device_map_to_verify = {
    '100000051e55a100': {
        'initiator_port_wwn_list': [
            '10008c7cff523b01'], 'target_port_wwn_list': ['20240002ac000a50']}}
_fabric_wwn = '100000051e55a100'


class BrcdFcZoneDriverBaseTest(object):

    def setup_config(self, is_normal, mode):
        fc_test_opts = [
            cfg.StrOpt('fc_fabric_address_BRCD_FAB_1', default='10.24.48.213',
                       help='FC Fabric names'),
        ]
        configuration = conf.Configuration(fc_test_opts)
        # fill up config
        configuration.zoning_mode = 'fabric'
        configuration.zone_driver = ('cinder.tests.zonemanager.'
                                     'test_brcd_fc_zone_driver.'
                                     'FakeBrcdFCZoneDriver')
        configuration.brcd_sb_connector = ('cinder.tests.zonemanager.'
                                           'test_brcd_fc_zone_driver'
                                           '.FakeBrcdFCZoneClientCLI')
        configuration.zoning_policy = 'initiator-target'
        configuration.zone_activate = True
        configuration.zone_name_prefix = 'openstack'
        configuration.fc_san_lookup_service = ('cinder.tests.zonemanager.'
                                               'test_brcd_fc_zone_driver.'
                                               'FakeBrcdFCSanLookupService')

        configuration.fc_fabric_names = 'BRCD_FAB_1'
        configuration.fc_fabric_address_BRCD_FAB_1 = '10.24.48.213'
        if (is_normal):
            configuration.fc_fabric_user_BRCD_FAB_1 = 'admin'
        else:
            configuration.fc_fabric_user_BRCD_FAB_1 = 'invaliduser'
        configuration.fc_fabric_password_BRCD_FAB_1 = 'password'

        if (mode == 1):
            configuration.zoning_policy_BRCD_FAB_1 = 'initiator-target'
        elif (mode == 2):
            configuration.zoning_policy_BRCD_FAB_1 = 'initiator'
        else:
            configuration.zoning_policy_BRCD_FAB_1 = 'initiator-target'
        configuration.zone_activate_BRCD_FAB_1 = True
        configuration.zone_name_prefix_BRCD_FAB_1 = 'openstack_fab1'