#!/usr/bin/env python
# Copyright C 2002-2013 by Isilon Systems, Inc. All rights not expressly
# granted herein are reserved.
# Created in the United States of America.
#
# This digital media is protected by U.S. and international copyright and
# intellectual property laws. Unless otherwise specified, all information
# and screens appearing as part of this digital medium, including
# software, services, documents, text, images, icons, and logos design;
# the selection, assembly, arrangement, and design thereof; and the code
# that enables its presentation, are the sole property of Isilon Systems,
# Inc.
#
# Except as otherwise required by applicable law, no part of any
# copyrighted material may be reproduced, modified, translated,
# distributed, published, or transmitted by any means, electronic or
# mechanical, for any purpose, without the prior written consent of Isilon
# Systems.
#
# Isilon and Isilon Systems are registered trademarks of Isilon Systems,
# Inc.  "Isilon IQ" is a trademark of Isilon Systems, Inc.  All other
# trademarks and registered trademarks are the property of their
# respective holders.

# Purpose:  This script is specifically used by CELOG to check if current
# protection policy for a node pool leaves the node pool underprotected.
# This script kicks off the relevant checks, and waits for them to finish.
# The script then updates the equivalence entry's valid_policy map to
# show that it's been updated.  There can only be one instance of the script
# running at a time on a cluster.

import errno
import os
import signal
import sys
import traceback
import time
import optparse

import isi.fs.drive_purposing as drive_purposing
from isi.fs._drive_purpose_db import PRODUCT_NAME_KEY, MODEL_NAME_KEY
from isi.app.lib import safelockfile
import isi.misc as imisc
from isi.fs.drive_purposing import logger
import isi.sys.version as sysver
import isi.reliability as reliability

LOCK_FILE = "/ifs/.ifsvar/modules/smartpools/failsim.lock"
MAX_RETRIES = 10

ARGS_LIST = [
    optparse.make_option("--equivalence_id", "-e",
            help = "The equivalence entry id that the evaluation should be "
            "run on.",
            dest = "equivalence_entry_id",
            type = "int"),
    optparse.make_option("--protection-policy", "-p",
            help = "Protection policy to evaluate for the given equivalence "
            "entry.",
            dest = "protection_policy",
            type = "string"),
    optparse.make_option("--test", "-t",
            help = "Use this option when you want a pause after calculating "
            "validity of a policy",
            action = "store_true",
            dest = "is_test"
            )
]

def write_database(purposedb):
    try:
        purposedb.write()
    except (IOError, OSError), e:
        logger().raise_exception(type(e), str(e) + ".  "
                "Operation will be re-attempted.")

def equivalence_entry_has_changed(equiv_id, original_equivalence_entry):
    purposedb = drive_purposing.DrivePurposeDB()

    if equiv_id not in purposedb.eq_entries():
        return True

    # Gather all the information relevant to the MTTDL calculation from
    # the original equivalence entry, and the new one, and check to see
    # if they differ on any value.  If so, we can't use the result
    # of the valid policy calculation since it's out of date.
    old_entry_info = purposedb.gather_mttdl_calc_info(
            original_equivalence_entry)
    new_entry_info = purposedb.gather_mttdl_calc_info(
            purposedb.eq_entries()[equiv_id])

    # The two dictionaries must have the exact same key-value pair set
    # to be considered the same.
    # Side-note:  Python's == comparison between dictionaries makes sure
    # they have the exact same keys, and does a value == comparison for
    # each key.
    if old_entry_info != new_entry_info:
        return True

    return False

def calculate_policy_mttdl(options, purposedb, equivalence_entry):
    # Need to get relevant to MTTDL calc data here, then call
    # reliability.mttdl_from_node_config.
    # It returns None if policy is invalid, otherwise it returns how many
    # years the MTTDL is.
    info = purposedb.gather_mttdl_calc_info(equivalence_entry)

    try:
        return reliability.mttdl_from_node_config(
                info["num_nodes"], info["hdd_drives"], info["hdd_driveGB"],
                info["ssd_drives"], info["ssd_driveGB"],
                options.protection_policy, info[PRODUCT_NAME_KEY],
                model_name=info[MODEL_NAME_KEY])
    except:
        logger().log("Timeout occurred when calculating policy MTTDL.  "
                "Policy: " + str(options.protection_policy) + ".  " +
                "Equivalence ID: " + str(options.equivalence_entry_id) + ".  "
                "Entry information: " + str(info))

def set_lower_overhead_policies_as_invalid(purposedb, equivalence_entry):
    # Save policies as invalid if they weren't suggested but could have been,
    # and had lower overhead than the suggested protection

    info = purposedb.gather_mttdl_calc_info(equivalence_entry)

    # XXX This code MUST follow the exact same process of determining which
    # policies could be suggested and how those policies are prioritized as
    # reliability.py:suggested_policy_from_node_config
    sorted_policy_list = reliability.sorted_suggestable_policies(
        info['num_nodes'], info[PRODUCT_NAME_KEY], info[MODEL_NAME_KEY])

    suggested_policy_index = sorted_policy_list.index(
            equivalence_entry['suggested_protection'])

    # Get the list of policies with lower overhead than the suggested policy
    lower_overhead_policies = sorted_policy_list[:suggested_policy_index]

    # Set the policies with lower overhead as invalid.
    for policy in lower_overhead_policies:
        equivalence_entry['valid_policy'][policy] = False

def set_equivalence_entry_suggested_protection(options, purposedb,
        equivalence_entry):
    info = purposedb.gather_mttdl_calc_info(equivalence_entry)

    try:
        policy, policy_mttdl = reliability.suggested_policy_from_node_config(
                info["num_nodes"], info["hdd_drives"], info["hdd_driveGB"],
                info["ssd_drives"], info["ssd_driveGB"],
                info[PRODUCT_NAME_KEY], model_name=info[MODEL_NAME_KEY])
    except:
        logger().log("Timeout occurred when calculating "
                "the suggested protection.  "
                "Equivalence ID: " + str(options.equivalence_entry_id) + ".  "
                "Entry information: " + str(info))

    if policy is not None and policy_mttdl is not None:
        equivalence_entry['suggested_protection'] = policy
        equivalence_entry['mttdl'] = policy_mttdl
        equivalence_entry['valid_policy'][policy] = True
        set_lower_overhead_policies_as_invalid(purposedb, equivalence_entry)
        write_database(purposedb)
    else:
        logger().log("No policy was suggested for equivalence entry.  "
                "Equivalence ID: " + str(options.equivalence_entry_id) + ".  "
                "Entry information: " + str(info))

def already_have_result(options, equivalence_entry, write_log=True):
    # Check to make sure the policy hasn't already been computed for this
    # equivalence entry.  The protected dictionary will be wiped out when
    # the equivalence entry is modified, so we don't have to worry about
    # the value being out of date (if it has a value, we know it's correct).
    if 'valid_policy' in equivalence_entry and \
            isinstance(equivalence_entry["valid_policy"], dict) and \
            options.protection_policy in equivalence_entry['valid_policy']:
        if write_log:
            logger().log("Already have answer for %s policy for equivalence " \
                    "entry %d." % (options.protection_policy,
                    options.equivalence_entry_id))
        return True

    return False

def input_validation_checks(options):
    if options.equivalence_entry_id is None:
        logger().raise_exception(ValueError, "Must include the equivalence "
                "entry ID option in your list of arguments.")

    purposedb = drive_purposing.DrivePurposeDB()

    if options.equivalence_entry_id not in purposedb.eq_entries():
        logger().raise_exception(ValueError, "Couldn't find specified "
                "equivalence entry.")

    if options.protection_policy is None:
        logger().raise_exception(ValueError, "Must include the protection "
                "policy option in your list of arguments.")

    equivalence_entry = purposedb.eq_entries()[options.equivalence_entry_id]
    num_nodes = len(purposedb.expand_equivalence_to_devids(equivalence_entry))

    # Parse the protection policy to determine if it's a known policy or not.
    if not reliability.protection_from_policy(options.protection_policy,
            num_nodes, reliability.USE_HISTORIC_OVER_B):
        logger().raise_exception(ValueError, "Protection policy specified "
                "is invalid.  Please specify a valid protection policy.")

def log_devid_running_script(options):
    logger().log("isi_evaluate_underprotected running calculation for "
            "equivalence entry %d and policy %s." %
            (options.equivalence_entry_id, options.protection_policy))

def log_result(options, equivalence_entry):
    logger().log("Validity result for %s policy for equivalence entry %d was "
            "%s" % (options.protection_policy, options.equivalence_entry_id,
            equivalence_entry['valid_policy'][options.protection_policy]))

def parse_input_arguments():
    usage = "usage: isi_evaluate_underprotected [--help] -e " \
            "<equivalence_entry_id> -p <protection_policy> [-t]"
    parser = optparse.OptionParser(usage, option_list=ARGS_LIST)
    return parser.parse_args()

def main(argv):
    (options, args) = parse_input_arguments()

    # Raises exceptions for any problem, so script won't continue past here
    # if input is invalid.
    input_validation_checks(options)

    log_devid_running_script(options)

    purposedb = drive_purposing.DrivePurposeDB()
    equivalence_entry = purposedb.eq_entries()[options.equivalence_entry_id]

    # Check valid_policy dictionary to see if we already have the answer
    # for the given policy.
    if already_have_result(options, equivalence_entry):
        return 1

    # If the valid_policy dictionary doesn't exist, isn't initialized properly,
    # or is empty, we want to initialize it, and set the suggested
    # protection for the equivalence entry.
    if equivalence_entry.get("valid_policy", None) is None or \
            not isinstance(equivalence_entry["valid_policy"], dict) or \
            not equivalence_entry["valid_policy"]:
        equivalence_entry["valid_policy"] = dict()
        set_equivalence_entry_suggested_protection(options, purposedb,
                equivalence_entry)

        if already_have_result(options, equivalence_entry, write_log=False):
            log_result(options, equivalence_entry)
            return 1

    policy_mttdl = calculate_policy_mttdl(
            options, purposedb, equivalence_entry)

    # Pause here so the test can modify the equivalence entry before
    # this continues.  The purpose is to make sure
    # equivalence_entry_has_changed(...) can detect a change properly.
    if options.is_test:
        time.sleep(5)

    if equivalence_entry_has_changed(
        options.equivalence_entry_id, equivalence_entry):
        logger().raise_exception(OSError, "Equivalence entry changed while "
                "checking policy validity!")

    if policy_mttdl:
        equivalence_entry['valid_policy'][options.protection_policy] = True
    else:
        equivalence_entry['valid_policy'][options.protection_policy] = False

    log_result(options, equivalence_entry)

    write_database(purposedb)

    return 1

if __name__ == "__main__":
    imisc.setproctitle(os.path.basename(sys.argv[0]))
    error = 0
    try:
        with safelockfile(LOCK_FILE, "w", blocking = False) as lockfile:
            error = main(sys.argv[1:])
    except:
        raise
        error = 1
    sys.exit(error)