#!/usr/bin/python3

# Copyright (c) 2018, NVIDIA CORPORATION.  All rights reserved.
#
# NVIDIA CORPORATION and its licensors retain all intellectual property
# and proprietary rights in and to this software, related documentation
# and any modifications thereto.  Any use, reproduction, disclosure or
# distribution of this software and related documentation without an express
# license agreement from NVIDIA CORPORATION is strictly prohibited.

#
# RAID configurator for Explorer software raid
#

import sys
import subprocess
import md_raid_configurator
from md_raid_configurator import MDRaidConfigurator
from md_raid_configurator import MD_CONST
from md_raid_configurator import get_next_md_name

class ExpMDRaidConfigurator(MDRaidConfigurator):

    def __init__(self, script_name):
        MDRaidConfigurator.__init__(self, script_name)
        self.data_array_name = "md1"
        self.enable_fscache = True

    def is_m2_drive(self, block_dev):
        #
        # Determine if given device is M.2 drive.
        # M.2 is in BDF given in bdf_list
        bdf_list =  [ "0000:01:00.0", "0000:05:00.0" ]

        for bdf in bdf_list:
            if bdf in block_dev.device_files["by-path"]:
                return True

        return False

    def get_eligible_bd_for_data_array(self, block_devs, elig_dev):
        #
        # Override for DGX-2.
        # Return set of devices eligible for raid-0 creation
        # We are only interested in NVMe device that is not M.2,
        # not a boot drive and not part of a raid group.
        for bd in block_devs:
            if not ("NVMe" in bd.__class__.__name__):
                continue
            if self.is_m2_drive(bd):
                continue
            if bd.is_raid_member():
                continue
            if bd.is_boot_device():
                continue
            if bd.is_drive_unhealthy():
                continue
            elig_dev.append(bd)

