# Copyright (c) 2014, Arista Networks, Inc.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are
# met:
#
#   Redistributions of source code must retain the above copyright notice,
#   this list of conditions and the following disclaimer.
#
#   Redistributions in binary form must reproduce the above copyright
#   notice, this list of conditions and the following disclaimer in the
#   documentation and/or other materials provided with the distribution.
#
#   Neither the name of Arista Networks nor the names of its
#   contributors may be used to endorse or promote products derived from
#   this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL ARISTA NETWORKS
# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
# BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
# OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
# IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#

"""
Allocates a resource from a key/value YAML file (under
DATA_ROOT/resources) that contains a set of resources to be
allocated. Here is an example (DATA_ROOT/resoruces/ip_address):

    192.168.1.1/24: null
    192.168.1.2/24: null

When a resource is allocated within a node definition, the first available
null value will be replaced by the node unique_id.

    192.168.1.1/24: 001c731a2b3c
    192.168.1.2/24: null

On subsequent attempts to allocate the resource to the same node,
ztpserver will first check to see whether the node has already been
allocated a resource from the pool. If it has, it will reuse the
resource instead of allocating a new one.

In order to free a resource from a pool, simply turn the
valueassociated to it back to ``null``, by editing the resource
file. Alternatively, ``$ztps --clear-resources`` can be used in order
to freeall resources in all file-based resource files.

Definition example:

    actions:
      -
        action: add_config
        attributes:
          url: files/templates/ma1.templates
          variables:
            ipaddress: allocate('mgmt_subnet')
        name: "configure ma1"
"""

import logging
import os
from collections import OrderedDict

from ztpserver.config import runtime
from ztpserver.constants import CONTENT_TYPE_YAML
from ztpserver.serializers import dump, load

log = logging.getLogger(__name__)  # pylint: disable=C0103


def load_resource(node_id, filename):
    data = OrderedDict()

    contents = load(filename, CONTENT_TYPE_YAML, node_id, lock=True)

    if contents and isinstance(contents, dict):
        for key, value in contents.iteritems():
            data[key] = str(value) if value else None
    else:
        if not contents:
            contents = "empty pool"
        raise RuntimeError(contents)

    return data


def lookup(node_id, data):
    """Return an existing allocated resource if one exists"""

    try:
        key = next(m[0] for m in data.iteritems() if m[1] == node_id)
    except StopIteration:
        key = None

    return key


def main(node_id, pool, _):
    try:
        file_path = os.path.join(runtime.default.data_root, "resources")
        filename = os.path.join(file_path, pool)

        data = load_resource(node_id, filename)
        log.debug("%s: loaded resource pool '%s': %s", node_id, pool, data)

        match = lookup(node_id, data)

        if match:
            log.debug("%s: already allocated resource '%s':'%s'", node_id, pool, match)
            return match

        entry = next(x[0] for x in data.items() if x[1] is None)
        log.debug("%s: allocated '%s':'%s'", node_id, pool, entry)

        data[entry] = node_id

        log.debug("%s: writing resource pool '%s': %s", node_id, pool, data)
        file_path = os.path.join(file_path, pool)

        # serialize data
        for key, value in data.items():
            data[key] = str(value) if value else None

        dump(data, file_path, CONTENT_TYPE_YAML, node_id, lock=True)

    except StopIteration as exc:
        log.error("%s: no resource free in '%s'", node_id, pool)
        raise RuntimeError(f"{node_id}: no resource free in '{pool}'") from exc
    except Exception as exc:
        msg = f"{node_id}: failed to allocate resource from '{pool}'"
        log.error(msg)
        raise RuntimeError(f"{msg} : {exc}") from exc

    return str(entry)
