#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Copyright (C) 2019 tribe29 GmbH - License: GNU General Public License v2
# This file is part of Checkmk (https://checkmk.com). It is subject to the terms and
# conditions defined in the file COPYING, which is part of this source code package.

# .1.3.6.1.2.1.47.1.1.1.1.4.10 1
# .1.3.6.1.2.1.47.1.1.1.1.2.10 Chassis Temperature Sensor
# .1.3.6.1.2.1.47.1.1.1.1.5.10 8
# .1.3.6.1.2.1.47.1.1.1.1.7.10 Chassis Temperature Sensor
# .1.3.6.1.2.1.47.1.1.1.1.11.10
# .1.3.6.1.2.1.47.1.1.1.1.13.10

# Note: Some bogus devices such as Cisco 2500 Series WLC or
# Cisco Wide Area Application Services do not have exactly
# one entry == "0" which means device itself or do not provide
# any information about physical class.
# In the first case {{Hardware > System}} will be omitted.
# In the second case we inventorize these components below physical
# components table {{Unknown entities}}.
# In all cases ports are ignored because we have an extra view
# for them.


def inv_snmp_extended_info(info):
    map_type = {
        "1": ("Other", "others"),
        "2": ("Unknown", "unknowns"),
        "3": ("Chassis", "chassis"),
        "4": ("Backplane", "backplanes"),
        "5": ("Container", "containers"),
        "6": ("PSU", "psus"),
        "7": ("Fan", "fans"),
        "8": ("Sensor", "sensors"),
        "9": ("Module", "modules"),
        "10": ("Port", "ports"),
        "11": ("Stack", "stacks"),
    }

    # TODO: clean up call sites of map_type and move parse function to section definition

    parsed = {}
    count_parents = 0
    for child, description, parent, child_type, name, software, \
        serial, manufacturer, model in info:
        if parent == "0":
            count_parents += 1
        if child_type in map_type:
            parsed.setdefault(
                child,
                (parent, description, child_type, name, software, serial, manufacturer, model))
        else:
            parsed.setdefault(
                child, (parent, description, "2", name, software, serial, manufacturer, model))

    node = None
    for index, (parent, description, entity_type, name, software, \
        serial, manufacturer, model) in parsed.items():
        if count_parents == 1 and parent == "0":
            node = inv_tree("hardware.system.")
            if serial:
                node["serial"] = serial
            if model:
                node["model"] = model

        elif entity_type != "10":
            if parsed.get(parent):
                location_info = (map_type[parsed[parent][2]][0], parent)
            elif parent == "0":
                location_info = ("Device", "0")
            else:
                location_info = ("Missing in ENTITY table", parent)
            node = inv_tree_list("hardware.components.%s:" % (map_type[entity_type][1]))
            node.append({
                "index": index,
                "name": name,
                "description": description,
                "software": software,
                "serial": serial,
                "manufacturer": manufacturer,
                "model": model,
                "location": "%s (%s)" % location_info,
            })

    return node


inv_info['snmp_extended_info'] = {
    'inv_function': inv_snmp_extended_info,
}
