#!/usr/bin/env python

"""
This tool is intended for multiple possible purposes
1. As a Dev, I want to generate a psi.conf file for a given CTO node.
2. As a Tester, I want to generate a psi.conf file for a given CTO node,
   to test that node in a "PSI" mode instead of "CTO mode."
3. Perhaps in Pipeline, post-rc.d init scripts that run isi_inventory_tool
   and generate the hw_inventory.gc file from CTO (Receipts, Probeconfigs,
   SoftAVLs), we can follow with another rc.d script that runs THIS script
   to create an appropriate psi.conf (CTO info) output file, such that
   post-boot, the node actually behaves like a "PSI" node. This would allow
   us to have backwards-compatible knowledge about "CTO" for legacy nodes,
   and yet have OneFS software NOT using our old HAL code, but instead is
   purely PSI-based.
4. If we wanted to get really fancy we could have special builds, such that
   pre-Infinity legacy CTO nodes can run like #3 above, but not in Pipeline,
   earlier, just to do regression testing on earlier platforms than Infinity,
   using PSI mode, in Freight Trains builds.  HOWEVER, this is totally not
   necessary nor in ANY conceivable Freight Trains scope.  It's just a concept.
   And given that most of the regression wheel hardware available for
   automated regression runs (from buildbiox or Jenkins) isn't even useful
   interesting platforms, it ain't worth it.
5. Once HAL CTO-to-PSI code changes are in our CI branch and everyone starts
   using them, we will need something to jump-start CTO Infinity Nodes to
   start using PSI. This script would be part of that process.

This script only works for a CTO node: you cannot convert CTO node information
to PSI information for a SKU node that does not have CTO node information!
Iff (if and only if) we had a desire to support a "conversion script" for the
SKU-to-PSI case, then that could be done -- SKU nodes have all their info in
the various sysconfig table lookups -- but it might be easier to just identify
the 'N' different SKUs that we actually care about and manually generate static
psi-851-xxxx.conf files for each, by hand.  *IFF we actually need to do so.*
Let's wait until the need arises.

NB: This script ONLY converts the CTO information!, such that is defined and
exists already in hwinventory.gcfg and hw_inventory.gc!  Anything else that
has been added to psi.conf that is NOT historical CTO info will NOT be set
to specific non-default values.  That needs to be done by hand!

Additional Caveats:

* Our historical (existing) CTO attributes vs capabilities information is
  not all that consistent or accurate.  E.g., on a given actual node, we may
  have a capability set to TRUE while the attribute remains FALSE -- or even
  vice-versa.  First, this is due to our existing CTO not being consistent
  (isi_inventory_tool, using Receipt, probeconfig, SoftAVL is not generating
  consistent output CTO hw_inventory data).  Second, presumably it was never
  noticed or mattered since the only consumers of that item were using the
  API that was "correct" in CTO ("has" vs "supports" APIs).

* For the CTO Infinity work to date, this is incomplete, ref probeconfig,
  and some of the resulting node CTO information is just wrong. This script
  "cannot" fix this, nor should it -- these are either mapping problems
  or dynamic hardware probing problems. We want this script to be a simple
  1-to-1 mapping/translation script.  If we identify and know of "shortcomings"
  with our Infinity CTO source data, we can address it by hand, or in some
  other "conversion scripts," but not in this isi_cto_to_psi converter.
"""

import argparse
import collections
import os.path
import pprint
import re
import sys

import isi.gconfig as gconfig

# We *ONLY* require importing and using isi.hw.hal because of the C wrapper for
# the gconfig object for CTO hal.gcfg_hwinventory, and we don't even want to do
# it for that -- ideally we would have this script 100% independent of HAL.
# So DO NOT USE hal for anything else! If there were a way to get gcfg_hwinventory
# in another manner, for Python, simply and easily, we would.
# NB: That includes hal.supportsCto(). That means we will get Exceptions on
# SKU nodes, but that's ok (The Exception error message asks if this is a SKU).
import isi.hw.hal as hal

CTO_PSI_CONF_DIR    = '/etc/psi.conf.d'
CTO_PSI_CONF_PREFIX = '001'
CTO_PSI_CONF_FNAME  = 'cto-config' # For single file, default
CTO_PSI_CONF_FINAL  = 'psi.conf'

def get_cto_psi_conf_file_path(splitkey=None):
    if args.output:
        fname = args.output
    else:
        fname = CTO_PSI_CONF_FNAME
    if splitkey is None:
        fpath = os.path.join(
            CTO_PSI_CONF_DIR,
            '-'.join((CTO_PSI_CONF_PREFIX, fname, CTO_PSI_CONF_FINAL)))
        return fpath
    fpath = os.path.join(
        CTO_PSI_CONF_DIR,
        '-'.join((CTO_PSI_CONF_PREFIX, fname, splitkey, CTO_PSI_CONF_FINAL)))
    return fpath

CTO_TYPES = ['hwattributes', 'hwcapabilities']

# Mappings "actions"
VERIFY        = 0 # Make sure this is true (for CTO initted)
IGNORE        = 1
VERBATIM      = 2 # Copy the name verbatim
HWV_ENUM_BOOL = 3 # This is a bool associated with HWV ENUM
HWV_ENUM_TYPE = 4 # This is a enum associated with HWV ENUM

# Mappings "Receipt Grouping (Multiple) Config Files"
CFG_NONE      = None
CFG_COMPUTE   = "compute"
CFG_SLED      = "sled"
CFG_SSD       = "ssd"
CFG_PWRSUPPLY = "power_supply"
CFG_NETWORK   = "network" # Not in Steven's Spec - the CTO Stuff
CFG_FRONTEND  = None # Not our concern
CFG_BACKEND   = None # Not our concern
CFGS = [CFG_COMPUTE, CFG_SLED, CFG_SSD, CFG_PWRSUPPLY, CFG_NETWORK]

# The HWV_ENUM_BOOLs get mapped to a "foo.present" value, so as not
# to stomp on top of the separate "foo.name" string/enum pairing.
# There are only a few CTO Enum Bool cases: NVRAM, Infiniband, batteries.

# The HWV_ENUM_TYPEs get converted, using mappings hardcoded here in this
# script, from enum ints in CTO gconfig to the HWV Name strings for PSI.

# Other mapping actions include
#   string  = literal mapping to use
#   [list]  = similar to VERBATIM, but precede by list str contents
#   (tuple) = similar to [list], except DO NOT group relative w/others
#             (e.g., under 'cto.attributes.' + 'list.contents.' + 'item')
#             but instead the tuple contents are absolute, from top
#             (e.g., under 'tuple.contents.' + 'item')
#             There's only one (tuple) defined, for hw.config.node_class.

# Ref lib/isi_hw/hwinventory.gcfg, copy/paste/edit to gen this mapping table.
# NB: With one twist. It's annoying that hw_inventory attributes and
# capabilities are "ALMOST 1:1 identical", and many of the omissions
# are likely due to human oversight -- and many of the 1:1 equivalences
# aren't even needed (or used). To simplify matters, or at least this
# code (and table maintenance) -- I'm collapsing both into a single
# mapping list, and at runtime I'll handle "missing" gconfig entries
# gracefully (e.g., cff_power_supply not in attributes).
# The first value in the tuples identifies gconfig expectations:
#   1=attributes ONLY
#   2=capabilities ONLY
#   3=1+2=BOTH

mappings = [
    (3, bool, 'initted',                    CFG_NONE     , VERIFY),
    (3, bool, 'i2c',                        CFG_COMPUTE  , 'i2c.chassis'),
    (3, bool, 'expander_i2c',               CFG_COMPUTE  , 'i2c.expander'),
    (3, bool, 'disk_only_node',             CFG_NONE     , IGNORE), # Dropping DONs in Freight Trains
    (3, bool, 'vitesse_exp',                CFG_NONE     , IGNORE), # Dropping Vitesse in Freight Trains
    (3, bool, 'lsi_exp',                    CFG_COMPUTE  , ['dss']),
    #
    (3, bool, 'nvram',                      CFG_COMPUTE  , HWV_ENUM_BOOL),
    (3, bool, 'nvram_flash',                CFG_COMPUTE  , 'nvram.flash'),
    (3, bool, 'nvram_autorestore',          CFG_COMPUTE  , 'nvram.autorestore'),
    (3, bool, 'nvram_exposed_flash',        CFG_COMPUTE  , 'nvram.exposed_flash'),
    #
    (3, bool, 'batteries',                  CFG_COMPUTE  , HWV_ENUM_BOOL),
    (3, bool, 'external_batteries',         CFG_COMPUTE  , 'batteries.external'),
    (3, bool, 'battery_status',             CFG_COMPUTE  , 'batteries.status'),
    (3, bool, 'battery_ship_mode',          CFG_COMPUTE  , 'batteries.ship_mode'),
    #
    (3, bool, 'infiniband',                 CFG_NETWORK  , HWV_ENUM_BOOL),
    (3, bool, 'sas_zoning',                 CFG_COMPUTE  , ['dss']),
    (3, bool, 'sas_phy_monitoring',         CFG_COMPUTE  , ['dss']),
    #
    (3, bool, 'front_panel_display',        CFG_COMPUTE  , ['interfaces']),
    (3, bool, 'isilon_front_panel_display', CFG_COMPUTE  , ['interfaces']),
    (3, bool, 'front_panel_keyboard',       CFG_COMPUTE  , ['interfaces']),
    (3, bool, 'intrusion_switch',           CFG_COMPUTE  , ['interfaces']),
    (3, bool, 'service_light',              CFG_COMPUTE  , ['interfaces']),
    #
    (3, bool, 'addon_gige_nic',             CFG_NETWORK  , 'network.gige.addon'),
    (3, bool, 'addon_10gige_nic',           CFG_NETWORK  , 'network.10gige.addon'),
    (3, bool, 'addon_40gige_nic',           CFG_NETWORK  , 'network.40gige.addon'),
    (3, bool, 'fibre_channel',              CFG_NETWORK  , 'network.fibre_channel'),
    #
    (3, bool, 'chassis_pbb_eeprom',         CFG_COMPUTE  , 'eeprom.chassis_pbb'),
    (3, bool, 'chassis_vpd_eeprom',         CFG_COMPUTE  , 'eeprom.chassis_vpd'),
    (3, bool, 'chassis_resume_eeprom',      CFG_COMPUTE  , 'eeprom.chassis_resume'),
    #
    (3, bool, 'bmc',                        CFG_COMPUTE  , ['ipmi']),
    (3, bool, 'bmc_vpd',                    CFG_COMPUTE  , ['ipmi']),
    (3, bool, 'cmc',                        CFG_COMPUTE  , ['ipmi']),
    #
    (2, bool, 'cff_power_supply',           CFG_PWRSUPPLY, 'power_supply.cff'),
    (3, bool, 'pata_dongle',                CFG_COMPUTE  , ['dongle']),
    (3, bool, 'pseudo_dongle',              CFG_COMPUTE  , ['dongle']),
    (3, bool, 'system_event_log',           CFG_COMPUTE  , VERBATIM),
    (3, bool, 'internal_boot_drives',       CFG_COMPUTE  , ['dss']),
    (3, bool, 'cpu_throttling',             CFG_COMPUTE  , 'cpu.throttling'),
    #
    (3, bool, 'sata_disks',                 CFG_SLED     , ['dss']),
    (3, bool, 'sas_disks',                  CFG_SLED     , ['dss']),
    (3, bool, 'hdd_drives',                 CFG_SLED     , ['dss']),
    (3, bool, 'ssd_drives',                 CFG_SSD      , ['dss']),
    (3, bool, 'self_encrypting_drives',     CFG_SLED     , ['dss']),
    #
    (3, bool, 'dna',                        CFG_COMPUTE  , VERBATIM),
    (3, bool, 'drive_fail_leds',            CFG_COMPUTE  , ['dss']),
    (3, bool, 'bay_grid_mapping',           CFG_COMPUTE  , ['dss']),
    (3, bool, 'adaptive_cooling',           CFG_COMPUTE  , VERBATIM),
    #
    (3, bool, 'cdes',                       CFG_COMPUTE  , ['dss']),
    (3, bool, 'drive_zone_fuse',            CFG_COMPUTE  , ['dss']),
    (2, long, 'hdd_capacity',               CFG_SLED     , ['dss']),
    (2, long, 'ssd_capacity',               CFG_SSD      , ['dss']),
    #
    (3, int , 'infiniband_type',            CFG_NETWORK  , HWV_ENUM_TYPE),
    (3, int , 'chassis_type',               CFG_COMPUTE  , HWV_ENUM_TYPE),
    (3, int , 'lcd_type',                   CFG_COMPUTE  , HWV_ENUM_TYPE),
    (3, int , 'nvram_type',                 CFG_COMPUTE  , HWV_ENUM_TYPE),
    (3, int , 'sefc_type',                  CFG_COMPUTE  , HWV_ENUM_TYPE),
    (3, int , 'cmc_type',                   CFG_COMPUTE  , HWV_ENUM_TYPE),
    (3, int , 'ps_type',                    CFG_PWRSUPPLY, HWV_ENUM_TYPE, 'power_supply'),
    #
    (3, int , 'num_ssds',                   CFG_SSD      , ['dss']),
    (3, int , 'num_hdds',                   CFG_SLED     , ['dss']),
    (3, int , 'num_self_encrypting_drives', CFG_SLED     , ['dss']),
    #
    (2, long, 'num_blocks_ssd',             CFG_SSD      , ['dss']),
    (2, long, 'num_blocks_hdd',             CFG_SLED     , ['dss']),
    (2, int , 'block_size_ssd',             CFG_SSD      , ['dss']),
    (2, int , 'block_size_hdd',             CFG_SLED     , ['dss']),
    #
    (2, str , 'node_class',                 CFG_COMPUTE  , ('hw', 'config')),
    #
    (3, int , 'num_cpus',                   CFG_COMPUTE  , 'cpu.count'),
    (3, int , 'num_batteries',              CFG_COMPUTE  , 'batteries.count'),
    (3, long, 'dram_size',                  CFG_COMPUTE  , 'dram.size'),
    (3, str , 'dram_config',                CFG_COMPUTE  , 'dram.config'),
    #
    (3, int , 'num_addon_gige_nics',        CFG_NETWORK  , 'network.gige.count'),
    (3, int , 'num_addon_10gige_nics',      CFG_NETWORK  , 'network.10gige.count'),
    (3, int , 'num_addon_40gige_nics',      CFG_NETWORK  , 'network.40gige.count'),
    #
    (3, bool, 'linear_journal',             CFG_COMPUTE  , VERBATIM),
    (3, int , 'num_journal_bays',           CFG_COMPUTE  , ['dss']),
    #
    # ignoring list dir netinfo (for now)
]

# Ref hw_version.h

HWV_NAMES = {
    'nvram': {
        0:  ('HWV_NVR_UNKNOWN',          'Unknown'      ),
        1:  ('HWV_NVR_NONE',             'None'         ),
        3:  ('HWV_NVR_MT25208',          'MT25208'      ), # Mellanox based card used in Graham
        4:  ('HWV_NVR_VMWARE',           'VMWARE'       ), # Fake VMware nvram.
        5:  ('HWV_NVR_ROCKETFUEL',       'RF6487'       ), # Isilon NVRAM -- FreeScale SoC based
        6:  ('HWV_NVR_LOX',              'LX4381'       ), # Isilon NVRAM -- IDT NVMe based
        7:  ('HWV_NVR_INFINITY',         'INFINITY'     ), # Isilon NVRAM -- Memory Journal
    },

    'infiniband': {
        0:  ('HWV_IB_UNKNOWN',           'Unknown'      ),
        1:  ('HWV_IB_NONE',              'NONE'         ),
        2:  ('HWV_IB_VMWARE',            'VMWARE'       ), # Fake VMware infiniband.
        3:  ('HWV_IB_MT23108_T',         'MT23108_T'    ), # Mellanox Cougar Cub 128 Tall Bracket
        4:  ('HWV_IB_MT23108_T_ROHS',    'MT23108_TR'   ), # Mellanox Cougar Cub 128 Tall Bracket, ROHS
        5:  ('HWV_IB_ISI25208',          'ISI25208'     ), # Mellanox 25208-based Isilon Belknap card
        6:  ('HWV_IB_MT25208',           'MT25208'      ), # Mellanox 25208-based off the shelf card
        7:  ('HWV_IB_MT25208_DDR',       'MT25208 DDR'  ), # Mellanox 25208-based off the shelf card
        8:  ('HWV_IB_QLE7342',           'QLE7342'      ), # QLogic InfiniPath QLE7342 
        9:  ('HWV_IB_MT26428_QDR',       'MT26428 QDR'  ), # Mellanox 26428 off the shelf card with QDR
        10: ('HWV_IB_MT4099_QDR',        'MT4099 QDR'   ), # Mellanox 4099 off the shelf card with QDR
    },

    'motherboard': {
        0:  ('HWV_MOBO_UNKNOWN',         'Unknown'          ),
        1:  ('HWV_MOBO_FORCE4201',       None               ), # DEPRECATED - original Force board (Cheddar)
        2:  ('HWV_MOBO_FORCE4203',       None               ), # DEPRECATED - 4-nic Force board (Mozzarella).
        3:  ('HWV_MOBO_TYAN2567',        None               ), # DEPRECATED - Original Tyan [pre-force] boards.
        4:  ('HWV_MOBO_TYAN2726',        None               ), # DEPRECATED - Tyan 4-Intel-down
        5:  ('HWV_MOBO_TYAN5360',        None               ), # DEPRECATED - Tyan Lindenhurst-VS 2-Intel-down
        6:  ('HWV_MOBO_INTEL7520',       None               ), # DEPRECATED - Intel "Jarrell" (Lindenhurst, 2nic) 
        7:  ('HWV_MOBO_VMWARE',          'VMware'           ), # VMware virtual motherboard
        8:  ('HWV_MOBO_TYAN5364',        None               ), # DEPRECATED - Tyan Lindenhurst 2-Intel down
        9:  ('HWV_MOBO_SUPERX7DBU',      None               ), # DEPRECATED - SuperMicro Blackford 2-Intel down
        10: ('HWV_MOBO_SUPERX8DT6_F',    'SupMicX8DT6-F'    ), # Supermicro Tylersburg w/  IPMI (WF & Blueflame)
        11: ('HWV_MOBO_SUPERX8DTU',      None               ), # Supermicro Tylersburg w/o IPMI (WF One-Off)
        12: ('HWV_MOBO_GENERIC',         None               ), # DEPRECATED - Generic (via overrides)
        13: ('HWV_MOBO_SUPERX9DR6_F',    None               ), # DEPRECATED - Supermicro Romley w/ IPMI
        14: ('HWV_MOBO_INTELS2600CP',    'IntelS2600CP'     ), # Intel Romley w/ IPMI
        15: ('HWV_MOBO_SUPERX9DR3_F',    None               ), # DEPRECATED - Supermicro Romley w/ IPMI
        16: ('HWV_MOBO_SUPERX9DR7_F',    None               ), # DEPRECATED - Supermicro Romley w/ IPMI
        17: ('HWV_MOBO_INTELS2600GZ',    'IntelS2600GZ'     ), # Intel Grizzly Pass
        18: ('HWV_MOBO_INTELS1400FP',    'IntelS1400FP'     ), # Intel Romley w/ IPMI (Fremont Pass)
        100:('HWV_MOBO_OBERON',          None               ), # DEPRECATED - EMC Oberon motherboard
        101:('HWV_MOBO_INFINITY',        'EMCInfinityMoboEP'), # EMC Infinity motherboard
        # NB there are actually *TWO* different names for ONE MOBO_INFINITY enum (diff PNs)
    },

    'chassis': {
        0:  ('HWV_CHASSIS_UNKNOWN',      'Unknown'      ),
        3:  ('HWV_CHASSIS_CID12BAY',     None           ), # DEPRECATED - 12-bay CIDesigns(or AIC) chassis:Rainier
        4:  ('HWV_CHASSIS_CID4BAY',      None           ), # DEPRECATED - 4-bay CIDesigns chassis:Fuego
        5:  ('HWV_CHASSIS_VMWARE',       'VMW'          ), # "60-bay" vmware chassis
        6:  ('HWV_CHASSIS_ISI12BAY',     None           ), # DEPRECATED - 12-bay Isilon (CID) chassis:Graham
        7:  ('HWV_CHASSIS_ISI4BAY',      None           ), # DEPRECATED - 4-bay Isilon (CID) chassis:Annapurna
        8:  ('HWV_CHASSIS_ISI36BAY',     None           ), # DEPRECATED - 36-bay Isilon (CID) chassis:MaunaLoa
        11: ('HWV_CHASSIS_ISI12BAY_V2',  'ISI12V2'      ), # 12-bay v2 Isilon chassis:Wingfoot
        12: ('HWV_CHASSIS_ISI24BAY',     'ISI24'        ), # 24-bay Isilon chassis:Blue Flame
        14: ('HWV_CHASSIS_GENERIC',      None           ), # DEPRECATED - Generic (via overrides)
        15: ('HWV_CHASSIS_ISI36BAY_V2',  'ISI36V2'      ), # 36-bay Isilon chassis: Bigfoot
        16: ('HWV_CHASSIS_ISI12BAY_V3',  'ISI12V3'      ), # 12-bay V3 Isilon chassis: Ermine
        17: ('HWV_CHASSIS_ISI24BAY_V2',  'ISI24V2'      ), # 24-bay Isilon chassis: S-Series Hotfoot
        18: ('HWV_CHASSIS_ISI36BAY_V3',  'ISI36V3'      ), # 36-bay Isilon chassis: HoneyBadger
        19: ('HWV_CHASSIS_EMC8BAY',      'EMC8'         ), # 8-bay Kylin 1U Foxconn Chassis
        20: ('HWV_CHASSIS_EMC60BAY',     'EMC60'        ), # 60-bay Colossus 4U EMC DPE Chassis
        21: ('HWV_CHASSIS_ISI36BAY_V4',  'ISI36V4'      ), # 36-bay Isilon chassis: Yeti
        22: ('HWV_CHASSIS_VONEFS60BAY',  'vOneFS'       ), # 60-bay vOneFS Chassis
        99: ('HWV_CHASSIS_VOYAGER60BAY', None           ), # DEPRECATED - 60-bay EMC Voyager 4U DAE Chassis
        100:('HWV_CHASSIS_OBERON',       None           ), # DEPRECATED
        101:('HWV_CHASSIS_INFINITY',     'INFINITY'     ),
    },

    'lcd': {
        0:  ('HWV_LCDVER_UNKNOWN',       'Unknown'      ),
        1:  ('HWV_LCDVER_NONE',          'None'         ),
        2:  ('HWV_LCDVER_NORITAKE',      None           ), # DEPRECATED - Noritake VFD used on Rainier
        3:  ('HWV_LCDVER_ISILON_V1',     'IsiVFD1'      ), # Isilon V1 Noritake compatible	
    },

    'sefc': {
        0:  ('HWV_FCB_UNKNOWN',          'Unknown'),
        1:  ('HWV_FCB_NONE',             'None'),
        2:  ('HWV_FCB_FCB',              None               ), # DEPRECATED - Graham Original
        3:  ('HWV_FCB_SEFC_ML',          None               ), # DEPRECATED - 4U 36 Drive Mauna Loa
        4:  ('HWV_FCB_SEFC_WF',          'SEFC-WF'          ), # 2U 12 Drive Wingfoot
        5:  ('HWV_FCB_SEFC_BF',          'SEFC-BF'          ), # 2U 24 Drive Blueflame
        6:  ('HWV_FCB_SEFC_BIGFOOT',     'SEFC-BIGFOOT'     ), # 4U 36 Drive Bigfoot
        7:  ('HWV_FCB_SEFC_HOTFOOT',     'SEFC-HOTFOOT'     ), # 2U 24 Drive Hotfoot w/ CMC
        8:  ('HWV_FCB_SEFC_HONEYBADGER', 'SEFC-HONEYBADGER' ), # 4U 36 Drive HoneyBadger w/ CMC
        9:  ('HWV_EXP_INTP_COLOSSUS',    'XINTP-COLOSSUS'   ), # 4U 60 Drive Colossus w/ CMC
        10: ('HWV_FCB_SEFC_YETI',        'SEFC-YETI'        ), # 4U 36 Drive Yeti w/ CMC
        11: ('HWV_FCB_SEFC_ERMINE',      'SEFC-ERMINE'      ), # 2U 12 Drive Ermine w/ CMC
    },

    'cmc': {
        0:  ('HWV_CMC_UNKNOWN',          'Unknown'       ),
        1:  ('HWV_CMC_NONE',             'None'          ),
        2:  ('HWV_CMC_SIPRESENT',        'CMC_SIPRESENT' ), # KCS system interface
        3:  ('HWV_CMC_BMCONLY',          'BMCONLY'       ), # BMC only, no CMC
        4:  ('HWV_CMC_PRESENT',          'CMC_PRESENT'   ), # BMC with CMC
    },

    'power_supply': {
        0:  ('HWV_PS_UNKNOWN',           'Unknown'       ),
        1:  ('HWV_PS_NONE',              'None'          ),
        2:  ('HWV_PS_ZIPPY',             'ZIPPY'         ),
        3:  ('HWV_PS_PSMI',              'PSMI'          ),
        4:  ('HWV_PS_PMBUS',             'PMBUS'         ),
        5:  ('HWV_PS_CFF',               'CFF'           ),
    },
}

def psi_key_root(cto_type):
    # NB: Possibly overridden (discarded) for action tuples
    if cto_type == "hwattributes":
        return ["hw", "cto", "attributes"]
    elif cto_type == "hwcapabilities":
        return ["hw", "cto", "capabilities"]
    else:
        raise Exception("Unknown CTO type %s" % cto_type)

def convert(mapping, cto_type, gc_obj):
    extra = None
    if len(mapping) == 5:
        (gc_applic, gc_type, entity, config, action) = mapping
    else:
        (gc_applic, gc_type, entity, config, action, extra) = mapping
    if not entity in gc_obj:
        gc_applic_msg = {
            1: "attributes only",
            2: "capabilities only",
            3: "both attributes and capabilities expected",
        }
        if ((cto_type == 'hwattributes' and gc_applic == 2) or
            (cto_type == 'hwcapabilities' and gc_applic == 1)):
            return None
        print("WARNING! Can't find '%s' in mapping for %s! (%s)" %
              (entity, cto_type, gc_applic_msg.get(gc_applic, "??")))
        return None
    if args.debug: print("convert(%s, gc_obj, %s, %s, %s)" %
                         (cto_type, gc_type, entity, action))
    if action == IGNORE:
        return None
    gc_val = gc_obj[entity]
    if gc_type in [bool, int, long, str]:
        psi_val = gc_type(gc_val)
    else:
        raise Exception("Unknown gconfig type %s" % gc_type)
    psi_key = psi_key_root(cto_type)
    if action == VERIFY:
        if psi_val is False:
            raise Exception("Node CTO %s are not initted, cannot convert. "
                            "(Is this a SKU node?)" % cto_type)
        return None
    elif action == VERBATIM:
        psi_key.append(entity)
    elif action == HWV_ENUM_BOOL:
        psi_key.extend([entity, 'present'])
    elif action == HWV_ENUM_TYPE:
        ident = extra and extra or entity.replace('_type', '')
        psi_key.extend([ident, 'name'])
        # We still need to convert given enum value to a name string.
        # Since we are deleting lib/isi_hw/hw_version in Pipeline,
        # port-hardcode these hwv table values into this script.
        name = HWV_NAMES[ident].get(psi_val)
        if name:
            psi_val = name[1] # name is tuple (HWV String, Name String)
            if psi_val is None:
                psi_val = 'DEPRECATED'
        else:
            psi_val = 'INVALID'
    elif isinstance(action, basestring):
        psi_key.append(action)
    elif isinstance(action, list):
        psi_key.extend(action + [entity])
    elif isinstance(action, tuple):
        psi_key = list(action) + [entity]
    psi_key = '.'.join(psi_key)
    if args.debug: print("convert(%s, gc_obj, %s, %s, %s): %s = %s" %
                         (cto_type, gc_type, entity, action, psi_key, psi_val))
    # NB: We don't have any floats/doubles in our hw_inventory gconfig
    if args.split:
        return (psi_key, (config, psi_val))
    else:
        return (psi_key, psi_val)

def get_cto_gconfig_data(gc_ctx, cto_type):
    gc_obj = gc_ctx.read(cto_type)
    return [convert(m, cto_type, gc_obj) for m in mappings]

def get_cto_gconfig():
    gc_ctx = gconfig.open(hal.gcfg_hwinventory).ctx_new()
    return dict([(ct, get_cto_gconfig_data(gc_ctx, ct)) for ct in CTO_TYPES])

def todict(results, resultdict={}):
    # filter results in case of any IGNORE etc.
    for key, val in filter(None, results):
        keycomps = key.split('.')
        keyrange = [(keycomps[:i], keycomps[i]) for i in range(len(keycomps))]
        if args.debug: print; print((key, keyrange[-1], val))
        for kcomps, kfinal in keyrange:
            tempd = resultdict
            for kcomp in kcomps:
                if kcomp not in tempd:
                    if args.debug: print("Not %s in %s, adding" % (kcomp, tempd))
                    tempd[kcomp] = {}
                tempd = tempd[kcomp]
                if args.debug: print("setting tempd=tempd[%s] ==> %s" % (kcomp, tempd))
            if not kfinal in tempd:
                if args.debug: print("assigning tempd[%s] = {}" % (kfinal))
                tempd[kfinal] = {}
        if args.debug: print("Assigning tempd[%s] = >>> %s <<<" % (kfinal, val))
        tempd[kfinal] = val
        if args.debug: print resultdict
    return resultdict

def get_cto_dict(result):
    if args.split:
        cto_dict = dict([(cfg, {}) for cfg in CFGS])
        for ct in CTO_TYPES:
            for cfg in CFGS:
                d = cto_dict[cfg]
                # First flush out all the Nones from IGNORE etc
                r = filter(None, result[ct])
                # Next filter only this cfg type
                r = filter(lambda (k, (c, v)): c==cfg, r)
                # Finally remove the cfg type label
                r = [(k, v) for (k, (c, v)) in r]
                d = todict(r, resultdict=d)
    else:
        d = todict(result['hwattributes'])
        d = todict(result['hwcapabilities'], resultdict=d)
        cto_dict = d
    return cto_dict

# JSON uses ":" name-value separators, and "," to separate name-value pairs and objects.
# PSI conf files use "=" name-value separators, and inconsistently using ";" to separate
# (terminate) some name-value pairs and objects. Here, we will use ";" to terminate all
# values, and separate list elements, but we will NOT "separate" name-value sequences
# (i.e. the dict-like {} objects).

NAME_VAL_SEP  = "="
NAME_VAL_TERM = ";"

# All those "if key is None" cases really shouldn't happen in a valid dict/conf file,
# but for code flexibility I'm leaving them in here. I suppose we can remove them
# if necessary/someone complains.

def psi_rec_dump(output, depth, data, key=None):
    if args.enclose is False and depth:
        tab = (args.indent and args.indent*' ' or '\t') * (depth - 1)
    else:
        tab = (args.indent and args.indent*' ' or '\t') * depth
    nv_sep, nv_term = NAME_VAL_SEP, NAME_VAL_TERM
    if isinstance(data, dict):
        enclose = not (depth == 0 and args.enclose is False)
        if key is None:
            if enclose:
                output.append("%s{" % (tab))
        else:
            output.append("%s%s %s {" % (tab, key, nv_sep))
        keys = sorted(data.keys())
        # keep "psi" at top
        if 'psi' in keys:
            keys.remove('psi')
            keys.insert(0, 'psi')
        for key2 in keys:
            output = psi_rec_dump(output, depth+1, data[key2], key2)
        if enclose:
            output.append("%s}" % (tab))
    elif isinstance(data, list):
        if key is None:
            output.append("%s[" % (tab))
        else:
            output.append("%s%s %s [" % (tab, key, nv_sep))
        for data2 in data:
            output = psi_rec_dump(output, depth+1, data2)
        output.append("%s]" % (tab))
    else:
        if isinstance(data, basestring):
            pdata = '"%s"' % data
        elif isinstance(data, bool):
            pdata = data and 'true' or 'false'
        else:
            pdata = data
        if key is None:
            output.append("%s%s%s" % (tab, pdata, nv_term))
        else:
            output.append("%s%s %s %s%s" % (tab, key, nv_sep, pdata, nv_term))
    return output

def psi_dump_cto_dict(cto_dict):
    return psi_rec_dump([], 0, cto_dict)

def nginx_lines_to_python_dict_lines(lines):
    regex = [
        (re.compile('#.*'),      ''),
        (re.compile(';\s*'),     ','),
        (re.compile('true,'),    'True,'),
        (re.compile('false,'),   'False,'),
        (re.compile('\s*=\s*'),  ' : '),
        (re.compile('(\S+) : '), '"\\1" : '),
        (re.compile('}$'),       '},'),
    ]
    def func(line):
        rline = line
        for reg, repl in regex:
            rline = reg.sub(repl, rline)
        return rline
    return map(func, lines)

def nginx_lines_to_python_dict(lines):
    pydata = nginx_lines_to_python_dict_lines(lines)
    pydata = filter(None, [l.strip() for l in pydata])
    # Is file data enclosed in {} braces or not? Needs to be for python dict.
    if not pydata[0] == '{':
        pydata = ['{'] + pydata + ['}']
    # If --enclosed was used we may have trailing ',', strip for clean dict.
    pydata[-1] = pydata[-1].rstrip(',')
    return eval('\n'.join(pydata))

def nginx_file_to_python_dict(filename):
    with open(filename) as fd:
        lines = fd.readlines()
    return nginx_lines_to_python_dict(lines)

def merge_cto_dict(cto_dict, merge_dict):
    for key, val in merge_dict.iteritems():
        if isinstance(val, collections.Mapping):
            tmp = merge_cto_dict(cto_dict.get(key, {}), val)
            cto_dict[key] = tmp
        elif isinstance(val, list):
            cto_dict[key] = (cto_dict[key] + val)
        else:
            cto_dict[key] = merge_dict[key]
    return cto_dict

def write_file(fname, data):
    if not fname.endswith('.conf'):
        fname = fname + '.conf'
    if os.path.exists(fname):
        if not os.path.isfile(fname):
            print("Failure: %s is not a file" % fname)
            return 1
        if not args.overwrite:
            print("Failure: %s already exists" % fname)
            return 1
    with open(fname, 'w') as f:
        if args.python:
            pprint.pprint(data, stream=f)
        else:
            for d in data: f.write("%s\n" % d)
    return 0

parser = argparse.ArgumentParser(
    description='Convert the current node CTO information to PSI config.')
parser.add_argument('-d', '--debug', action='store_true',
                    help='Enable debug output')
parser.add_argument('-c', '--console', action='store_true',
                    help='Report output to console INSTEAD of file')
parser.add_argument('-e', '--enable', action='store_true',
                    help='Enable PSI in output (psi.supported=true)')
parser.add_argument('-s', '--split', action='store_true',
                    help='Split output into multiple files like Mfg Builds')
parser.add_argument('-i', '--input', type=argparse.FileType('r'), nargs='*',
                    help='One or more optional input files to include in output')
parser.add_argument('-o', '--output',
                    help='Output filename override (otherwise default in %s)' % CTO_PSI_CONF_DIR)
parser.add_argument('--overwrite', action='store_true',
                    help='Allow overwriting existing files')
parser.add_argument('--enclose', action='store_true',
                    help='Enclose data in outer wrapping braces')
parser.add_argument('--indent', type=int,
                    help='Indentation count (spaces) (default is TAB)')
parser.add_argument('--python', action='store_true',
                    help='Display Python dict output')

def main():
    # For simplicity, just make input args global
    global args
    args = parser.parse_args()
    if args.split and args.python:
        raise Exception("Cannot do both split files and Python output.")
    result = get_cto_gconfig()
    cto_dict = get_cto_dict(result)
    if args.enable:
        if args.split:
            cto_dict[CFG_COMPUTE]['psi'] = {'supported': True}
        else:
            cto_dict['psi'] = {'supported': True}
    if args.input:
        for fd in args.input:
            merge_cto_dict(cto_dict, nginx_lines_to_python_dict(fd.readlines()))
    data = psi_dump_cto_dict(cto_dict)
    if not data:
        print("Failure: no converted output?")
        return 1
    if args.python:
        data = nginx_lines_to_python_dict(data)
    if args.console:
        if args.python:
            pprint.pprint(data)
        else:
            for d in data: print d
    else:
        # Save to file(s). args.output override is optional.
        if args.split:
            for key, dict2 in cto_dict.iteritems():
                data = psi_dump_cto_dict(dict2)
                if not data:
                    print("Failure: no converted output? (key=%s)" % key )
                    return 1
                fname = get_cto_psi_conf_file_path(key)
                write_file(fname, data)
        else:
            fname = get_cto_psi_conf_file_path()
            write_file(fname, data)
    return 0

if __name__ == '__main__':
    sys.exit(main())
