#!/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.


def parse_solaris_route(line):
    def parse_network(n):
        if n == "default":
            return "0.0.0.0/0"
        return n

    route = {}
    if len(line) >= 5 and not line[0].startswith("---") and \
       line[0] != "Destination":

        route["target"] = parse_network(line[0])
        route["gateway"] = line[1]
        if len(line) > 5:
            route["device"] = line[-1]

    return route


def inv_solaris_routes(info, params):
    routes = inv_tree_list("networking.routes:")
    for line in info:
        route = parse_solaris_route(line)
        if route:
            routes.append(route)


inv_info['solaris_routes'] = {
    'inv_function': inv_solaris_routes,
}
