# Copyright (c) 2014 Alex Meade.  All rights reserved.
# Copyright (c) 2014 Clinton Knight.  All rights reserved.
#
#    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.


import copy
import math
import time

from oslo_log import log as logging
import six

from cinder import exception
from cinder.i18n import _, _LW
from cinder import utils
from cinder.volume.drivers.netapp.dataontap.client import api as netapp_api
from cinder.volume.drivers.netapp.dataontap.client import client_base


LOG = logging.getLogger(__name__)


@six.add_metaclass(utils.TraceWrapperMetaclass)
class Client(client_base.Client):

    def __init__(self, volume_list=None, **kwargs):
        super(Client, self).__init__(**kwargs)
        vfiler = kwargs.get('vfiler', None)
        self.connection.set_vfiler(vfiler)

        (major, minor) = self.get_ontapi_version(cached=False)
        self.connection.set_api_version(major, minor)

        self.volume_list = volume_list
        self._init_features()

    def _init_features(self):
        super(Client, self)._init_features()

        ontapi_version = self.get_ontapi_version()   # major, minor

        ontapi_1_20 = ontapi_version >= (1, 20)
        self.features.add_feature('SYSTEM_METRICS', supported=ontapi_1_20)

    def _invoke_vfiler_api(self, na_element, vfiler):
        server = copy.copy(self.connection)
        server.set_vfiler(vfiler)
        result = server.invoke_successfully(na_element, True)
        return result

    def _invoke_7mode_iterator_getter(self, start_api_name, next_api_name,
                                      end_api_name, record_container_tag_name,
                                      maximum=100):
        """Invoke a 7-mode iterator-style getter API."""
        data = []

        start_api = netapp_api.NaElement(start_api_name)
        start_result = self.connection.invoke_successfully(start_api)
        tag = start_result.get_child_content('tag')
        if not tag:
            return data

        try:
            while True:
                next_api = netapp_api.NaElement(next_api_name)
                next_api.add_new_child('tag', tag)
                next_api.add_new_child('maximum', six.text_type(maximum))
                next_result = self.connection.invoke_successfully(next_api)
                records = next_result.get_child_content('records') or 0
                if int(records) == 0:
                    break

                record_container = next_result.get_child_by_name(
                    record_container_tag_name) or netapp_api.NaElement('none')

                data.extend(record_container.get_children())
        finally: