# Copyright (c) 2015, 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.
#
import hashlib
import json
import logging
import os

import MySQLdb  # pylint: disable=E0401

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


def fetch_tor(node, node_id):
    """
    Query mysql database using the node's neighbors and return the tor record.
    The key for the tor loopkup is the md5 hash of the json serialized
    neighbors.

    Returns: tor dict:
         { 'neighbors' : dict( <neighbors),
           'type' : <tor-type>,
           'hostName' : <hostname>,
           ...
         }
    """

    # Connect to mysql server
    user = os.environ.get("MYSQL_USER", "root")
    db = os.environ.get("MYSQL_DB", "db")  # pylint: disable=C0103
    host = os.environ.get("MYSQL_HOST", "localhost")
    log.debug("%s: Connecting to mysql %s:%s:%s", node_id, host, user, db)

    assert db and host and user, "Params to connect to mysql server missing"
    con = MySQLdb.connect(db=db, host=host, user=user)
    cur = con.cursor()

    # Build a dict of neighbors based on node's LLDP neighbors.
    #
    # Format:
    #    { <interface> : { 'device': <neighbor-device>, 'port': <neighbor-port> }
    #      ...
    #    }

    neighbors = {}
    for intf, nlist in node.neighbors.items():
        ndevice = nlist[0]
        device = ndevice.device.split(".")[0]
        neighbors[intf] = {"device": device, "port": ndevice.interface}

    # Serialize the neighbors and get the md5 hash
    neighbors_str = json.dumps(neighbors, sort_keys=True)
    digest = hashlib.md5(neighbors_str).hexdigest()
    log.debug("%s: hash: %s", node_id, hash)

    # Lookup tor by hash
    stmt = "select hostName, torData, type from tor where hash=%s"
    where = (digest,)
    cur.execute(stmt, where)
    host_name, tor_data, tor_type = cur.fetchone()
    tor = json.loads(tor_data)
    tor["type"] = tor_type

    # Check for hash collision
    if tor["neighbors"] != neighbors:
        raise RuntimeError(f"{node_id}: hash collision for TOR {host_name}")
    return tor


def assign_url(tor):
    if tor["type"] == "systest":
        return "files/templates/dm1-tor-systest.template"
    if tor["type"] == "software":
        return "files/templates/dm1-tor-software.template"
    if tor["type"] == "server":
        return "files/templates/dm1-tor-server.template"

    raise RuntimeError(f"Unknown TOR type {tor['type']}")


def assign_var(tor):
    # The tor 'type' and 'neighbors' are just for internal usage. They are not
    # variables required by the template.
    tor.pop("type")
    tor.pop("neighbors")
    return tor


def main(node_id, pool, node):
    """
    Return the resource requested for the pool type.

    node_id - Node identifier, usually the serial number
    pool - Type of resource requested, its either url or variable.
    node - Copy of the Node object
    """
    try:
        # Get a copy of the tor record stored in mysql, based on the node's
        # neighbors.
        tor = fetch_tor(node, node_id)

        if pool == "url":
            return assign_url(tor)
        if pool == "variables":
            return assign_var(tor)

        assert False, f"Unknown pool {pool}"
    except Exception as exc:
        msg = f"{node_id}: failed to allocate resource from '{pool}'"
        log.error(msg)
        log.error(str(exc))
        raise RuntimeError(f"{msg} : {exc}") from exc
