# flake8: noqa
import pytest
import responses
from concurrent.futures import ThreadPoolExecutor
from brian_dashboard_manager.grafana.provision import provision_folder, provision
from brian_dashboard_manager.inventory_provider.interfaces import get_nren_regions
from brian_dashboard_manager.services.api import fetch_services

TEST_INTERFACES = [
    {
        "router": "srx2.ch.office.geant.net",
        "name": "ge-0/0/8",
        "bundle": [],
        "bundle-parents": [],
        "description": "PHY CUSTOMER GEANT CORPORATE SRF000001 | GEANT Corporate to MX1.LON - Via Vodafone",
        "circuits": [
            {
                "id": 679232,
                "name": "GEANT CORPORATE TO MX1.LON - VIA VODAFONE",
                "type": "GEANT IP",
                "status": "non-monitored"
            }
        ],
        "snmp-index": 523,
        "dashboards": [
            "NREN",
            "RE_CUST"
        ],
        "dashboard_info": {
            "name": "GEANT",
            "interface_type": "PHYSICAL"
        },
        "dashboards_info": [{
            "name": "GEANT",
            "interface_type": "PHYSICAL"
        }],
        "ipv4": [],
        "ipv6": []
    },
    {
        "router": "srx2.ch.office.geant.net",
        "name": "ge-0/0/8.10",
        "bundle": [],
        "bundle-parents": [],
        "description": "SRV_GLOBAL CUSTOMER GEANT #GEANT_CORPORATE-ViaVodafone | GEANT Corporate to mx1.lon - Via Vodafone ",
        "circuits": [
            {
                "id": 679360,
                "name": "GEANT_CORPORATE-VIAVODAFONE",
                "type": "GEANT IP",
                "status": "non-monitored"
            }
        ],
        "snmp-index": 543,
        "dashboards": [
            "NREN",
            "RE_CUST"
        ],
        "dashboard_info": {
            "name": "GEANT",
            "interface_type": "LOGICAL"
        },
        "dashboards_info": [{
            "name": "GEANT",
            "interface_type": "LOGICAL"
        }],
        "ipv4": [
            "62.40.127.131/31"
        ],
        "ipv6": [
            "2001:798:99:1::ca/126"
        ]
    },
    {
        "router": "srx2.ch.office.geant.net",
        "name": "ge-0/0/8.11",
        "bundle": [],
        "bundle-parents": [],
        "description": "SRV_GLOBAL CUSTOMER GEANT #GEANT_CORPORATE_ViaVodafone-VRF | GEANT Corporate to mx1.lon - Via Vodafone - for VRF",
        "circuits": [
            {
                "id": 712144,
                "name": "GEANT_CORPORATE_VIAVODAFONE-VRF",
                "type": "GEANT IP",
                "status": "non-monitored"
            }
        ],
        "snmp-index": 545,
        "dashboards": [
            "NREN",
            "RE_CUST"
        ],
        "dashboard_info": {
            "name": "GEANT",
            "interface_type": "LOGICAL"
        },
        "dashboards_info": [{
            "name": "GEANT",
            "interface_type": "LOGICAL"
        }],
        "ipv4": [
            "10.101.0.6/30"
        ],
        "ipv6": []
    },
    {
        "router": "srx2.ch.office.geant.net",
        "name": "ge-0/0/8.12",
        "bundle": [],
        "bundle-parents": [],
        "description": "SRV_GLOBAL CUSTOMER GEANT #GEANT_CORPORATE_ViaVodafone-VRF-TEST | GEANT Corporate to mx1.lon - Via Vodafone - DASHBOARD BGP TEST VLAN",
        "circuits": [
            {
                "id": 678920,
                "name": "GEANT_CORPORATE_VIAVODAFONE-VRF-TEST (DO NOT OPEN A TICKET)",
                "type": "GEANT IP",
                "status": "non-monitored"
            }
        ],
        "snmp-index": 522,
        "dashboards": [
            "NREN",
            "RE_CUST"
        ],
        "dashboard_info": {
            "name": "GEANT",
            "interface_type": "LOGICAL"
        },
        "dashboards_info": [{
            "name": "GEANT",
            "interface_type": "LOGICAL"
        }],
        "ipv4": [
            "62.40.127.141/31"
        ],
        "ipv6": []
    },
    {
        "router": "srx2.ch.office.geant.net",
        "name": "ge-0/0/8.996",
        "bundle": [],
        "bundle-parents": [],
        "description": "SRV_GLOBAL CUSTOMER GEANT #GEANT_OPERATIONS_LabConnectivity | GEANT MX1.LON Infinera VRF to Operations Lab",
        "circuits": [
            {
                "id": 678999,
                "name": "GEANT_OPERATIONS_LABCONNECTIVITY",
                "type": "GEANT IP",
                "status": "non-monitored"
            }
        ],
        "snmp-index": 554,
        "dashboards": [
            "NREN",
            "RE_CUST"
        ],
        "dashboard_info": {
            "name": "GEANT",
            "interface_type": "LOGICAL"
        },
        "dashboards_info": [{
            "name": "GEANT",
            "interface_type": "LOGICAL"
        }],
        "ipv4": [
            "10.1.1.1/30"
        ],
        "ipv6": []
    },
    {
        "router": "mx1.ams.nl.geant.net",
        "name": "xe-0/0/0",
        "bundle": [
            "ae16"
        ],
        "bundle-parents": [],
        "description": "PHY CUSTOMER KIAE P_AE16 SRF9915671 |",
        "circuits": [],
        "snmp-index": 588,
        "dashboards": [
            "NREN",
            "RE_CUST"
        ],
        "dashboard_info": {
            "name": "KIAE",
            "interface_type": "PHYSICAL"
        },
        "dashboards_info": [{
            "name": "KIAE",
            "interface_type": "PHYSICAL"
        }],
        "ipv4": [],
        "ipv6": []
    },
    {
        "router": "mx1.ams.nl.geant.net",
        "name": "xe-0/0/1",
        "bundle": [
            "ae14"
        ],
        "bundle-parents": [],
        "description": "PHY CUSTOMER SWITCH P_AE14 SRF18008 |",
        "circuits": [],
        "snmp-index": 589,
        "dashboards": [
            "NREN",
            "RE_CUST"
        ],
        "dashboard_info": {
            "name": "SWITCH",
            "interface_type": "PHYSICAL"
        },
        "dashboards_info": [{
            "name": "SWITCH",
            "interface_type": "PHYSICAL"
        }],
        "ipv4": [],
        "ipv6": []
    },
    {
        "router": "mx1.ams.nl.geant.net",
        "name": "ge-0/2/1.0",
        "bundle": [],
        "bundle-parents": [],
        "description": "SRV_L2CIRCUIT INFRASTRUCTURE GEANT GEANT #AMS-ZAG OPENFLOW |",
        "circuits": [],
        "snmp-index": 687,
        "dashboards": [
            "L2_CIRCUIT"
        ],
        "dashboard_info": {
            "name": "GEANT",
            "interface_type": "LOGICAL"
        },
        "dashboards_info": [{
            "name": "GEANT",
            "interface_type": "LOGICAL"
        }],
        "ipv4": [],
        "ipv6": []
    },
    {
        "router": "mx1.ams.nl.geant.net",
        "name": "ge-0/2/2.0",
        "bundle": [],
        "bundle-parents": [],
        "description": "SRV_L2CIRCUIT INFRASTRUCTURE GEANT GEANT #AMS-VIE OPENFLOW |",
        "circuits": [],
        "snmp-index": 711,
        "dashboards": [
            "L2_CIRCUIT"
        ],
        "dashboard_info": {
            "name": "GEANT",
            "interface_type": "LOGICAL"
        },
        "dashboards_info": [{
            "name": "GEANT",
            "interface_type": "LOGICAL"
        }],
        "ipv4": [],
        "ipv6": []
    },
    {
        "router": "mx1.ams.nl.geant.net",
        "name": "ge-0/2/4.0",
        "bundle": [],
        "bundle-parents": [],
        "description": "SRV_L2CIRCUIT INFRASTRUCTURE GEANT GEANT #AMS-FRA OPENFLOW |",
        "circuits": [],
        "snmp-index": 718,
        "dashboards": [
            "L2_CIRCUIT"
        ],
        "dashboard_info": {
            "name": "GEANT",
            "interface_type": "LOGICAL"
        },
        "dashboards_info": [{
            "name": "GEANT",
            "interface_type": "LOGICAL"
        }],
        "ipv4": [],
        "ipv6": []
    },
    {
        "router": "rt1.fra.de.geant.net",
        "name": "xe-11/2/5.300",
        "bundle": [],
        "bundle-parents": [],
        "description": "SRV_GLOBAL CUSTOMER AZSCIENCENET #AZSCIENCENET-AP1 $GS-00433 | ASN202993 |",
        "circuits": [
            {
                "id": 723799,
                "name": "AZSCIENCENET-AP1",
                "type": "GEANT IP",
                "status": "operational"
            }
        ],
        "snmp-index": 1033,
        "dashboards": [
            "EAP",
            "RE_CUST"
        ],
        "dashboard_info": {
            "name": "AZSCIENCENET",
            "interface_type": "LOGICAL"
        },
        "dashboards_info": [
            {
                "name": "AZSCIENCENET",
                "interface_type": "LOGICAL"
            }
        ],
        "port_type": "SERVICE",
        "ipv4": [],
        "ipv6": []
    }
]

NREN_INTERFACES = [
    {
        "router": "mx1.dub2.ie.geant.net",
        "name": "xe-0/0/0",
        "bundle": [
            "ae10"
        ],
        "bundle-parents": [],
        "description": "PHY CUSTOMER HEANET P_AE10 SRF9948758 | HEANET-AP2-LL3",
        "circuits": [],
        "snmp-index": 554,
        "dashboards": [
            "NREN",
            "RE_CUST"
        ],
        "dashboard_info": {
            "name": "HEANET",
            "interface_type": "PHYSICAL"
        },
        "dashboards_info": [{
            "name": "HEANET",
            "interface_type": "PHYSICAL"
        }],
        "ipv4": [],
        "ipv6": []
    },
    {
        "router": "mx1.dub2.ie.geant.net",
        "name": "xe-1/0/1",
        "bundle": [
            "ae10"
        ],
        "bundle-parents": [],
        "description": "PHY CUSTOMER HEANET P_AE10 SRF0000001 | HEANET-AP2-LL2",
        "circuits": [],
        "snmp-index": 527,
        "dashboards": [
            "NREN",
            "RE_CUST"
        ],
        "dashboard_info": {
            "name": "HEANET",
            "interface_type": "PHYSICAL"
        },
        "dashboards_info": [{
            "name": "HEANET",
            "interface_type": "PHYSICAL"
        }],
        "ipv4": [],
        "ipv6": []
    },
    {
        "router": "mx1.dub2.ie.geant.net",
        "name": "xe-1/1/0",
        "bundle": [
            "ae10"
        ],
        "bundle-parents": [],
        "description": "PHY CUSTOMER HEANET P_AE10 SRF9925903 | HEANET-AP2-LL1",
        "circuits": [],
        "snmp-index": 528,
        "dashboards": [
            "NREN",
            "RE_CUST"
        ],
        "dashboard_info": {
            "name": "HEANET",
            "interface_type": "PHYSICAL"
        },
        "dashboards_info": [{
            "name": "HEANET",
            "interface_type": "PHYSICAL"
        }],
        "ipv4": [],
        "ipv6": []
    },
    {
        "router": "mx1.dub2.ie.geant.net",
        "name": "ae10",
        "bundle": [],
        "bundle-parents": [
            "xe-0/0/0",
            "xe-1/0/1",
            "xe-1/1/0"
        ],
        "description": "LAG CUSTOMER HEANET SRF9925909 | HEANET-AP2-LAG",
        "circuits": [],
        "snmp-index": 596,
        "dashboards": [
            "NREN",
            "RE_CUST"
        ],
        "dashboard_info": {
            "name": "HEANET",
            "interface_type": "AGGREGATE"
        },
        "dashboards_info": [{
            "name": "HEANET",
            "interface_type": "AGGREGATE"
        }],
        "ipv4": [],
        "ipv6": []
    },
    {
        "router": "mx1.dub2.ie.geant.net",
        "name": "ae10.12",
        "bundle": [],
        "bundle-parents": [
            "xe-0/0/0",
            "xe-1/0/1",
            "xe-1/1/0"
        ],
        "description": "SRV_GLOBAL CUSTOMER HEANET #HEANET-AP2 | ASN1213 | ",
        "circuits": [
            {
                "id": 662976,
                "name": "HEANET-AP2",
                "type": "GEANT IP",
                "status": "operational"
            }
        ],
        "snmp-index": 713,
        "dashboards": [
            "NREN",
            "RE_CUST"
        ],
        "dashboard_info": {
            "name": "HEANET",
            "interface_type": "LOGICAL"
        },
        "dashboards_info": [{
            "name": "HEANET",
            "interface_type": "LOGICAL"
        }],
        "ipv4": [
            "62.40.125.13/30"
        ],
        "ipv6": [
            "2001:0798:0018:10aa::15/126"
        ]
    },
    {
        "router": "mx1.dub2.ie.geant.net",
        "name": "ae10.30",
        "bundle": [],
        "bundle-parents": [
            "xe-0/0/0",
            "xe-1/0/1",
            "xe-1/1/0"
        ],
        "description": "SRV_MDVPN CUSTOMER HEANET AP2 #HEANET-BGP-LU-CoC-1 |",
        "circuits": [
            {
                "id": 663160,
                "name": "HEANET-BGP-LU-COC-1",
                "type": "MD-VPN (NATIVE)",
                "status": "operational"
            }
        ],
        "snmp-index": 712,
        "dashboards": [
            "MDVPN_CUSTOMERS",
            "NREN"
        ],
        "dashboard_info": {
            "name": "HEANET",
            "interface_type": "LOGICAL"
        },
        "dashboards_info": [{
            "name": "HEANET",
            "interface_type": "LOGICAL"
        }],
        "ipv4": [
            "62.40.102.36/31"
        ],
        "ipv6": []
    },
    {
        "router": "mx1.dub2.ie.geant.net",
        "name": "ae10.333",
        "bundle": [],
        "bundle-parents": [
            "xe-0/0/0",
            "xe-1/0/1",
            "xe-1/1/0"
        ],
        "description": "SRV_IAS CUSTOMER HEANET #HEANET-AP2-IAS IASPS | ASN1213 ",
        "circuits": [
            {
                "id": 663214,
                "name": "HEANET-AP2-IAS",
                "type": "GEANT PEERING",
                "status": "operational"
            }
        ],
        "snmp-index": 658,
        "dashboards": [
            "IAS_CUSTOMER",
            "NREN"
        ],
        "dashboard_info": {
            "name": "HEANET",
            "interface_type": "LOGICAL"
        },
        "dashboards_info": [{
            "name": "HEANET",
            "interface_type": "LOGICAL"
        }],
        "ipv4": [
            "83.97.88.77/30"
        ],
        "ipv6": [
            "2001:798:1::61/126"
        ]
    },
    {
        "router": "mx1.dub2.ie.geant.net",
        "name": "ae10.1214",
        "bundle": [],
        "bundle-parents": [
            "xe-0/0/0",
            "xe-1/0/1",
            "xe-1/1/0"
        ],
        "description": "SRV_L2CIRCUIT CUSTOMER HEANET GEANT #ams-dub2-HEANET-RARE-21061 |",
        "circuits": [
            {
                "id": 713335,
                "name": "AMS-DUB2-HEANET-RARE-21061",
                "type": "GEANT PLUS",
                "status": "non-monitored"
            }
        ],
        "snmp-index": 790,
        "dashboards": [
            "L2_CIRCUIT"
        ],
        "dashboard_info": {
            "name": "HEANET",
            "interface_type": "LOGICAL"
        },
        "dashboards_info": [{
            "name": "GEANT",
            "interface_type": "LOGICAL"
        }, {
            "name": "HEANET",
            "interface_type": "LOGICAL"
        }],
        "ipv4": [],
        "ipv6": []
    },
    {"router": "rt1.fra.de.geant.net",
        "name": "xe-11/2/5.300",
        "bundle": [],
        "bundle-parents": [],
        "description": "SRV_GLOBAL CUSTOMER AZSCIENCENET #AZSCIENCENET-AP1 $GS-00433 | ASN202993 |",
        "circuits": [
            {
                "id": 723799,
                "name": "AZSCIENCENET-AP1",
                "type": "GEANT IP",
                "status": "operational"
            }
        ],
        "snmp-index": 1033,
        "dashboards": [
            "EAP",
            "RE_CUST"
        ],
        "dashboard_info": {
            "name": "AZSCIENCENET",
            "interface_type": "LOGICAL"
        },
        "dashboards_info": [
            {
                "name": "AZSCIENCENET",
                "interface_type": "LOGICAL"
            }
        ],
        "port_type": "SERVICE",
        "ipv4": [],
        "ipv6": []
     }
]

EUMETSAT_MULTICAST = [
    {
        'router': 'mx1.ams.nl.geant.net',
        'oid': '1.3.6.1.2.1.83.1.1.2.1.16.232.223.222.1.193.17.9.3.255.255.255.255',
        'community': '0pBiFbD',
        'subscription': '232.223.222.1',
        'endpoint': '193.17.9.3'
    },
    {
        'router': 'mx1.ams.nl.geant.net',
        'oid': '1.3.6.1.2.1.83.1.1.2.1.16.232.223.222.2.193.17.9.3.255.255.255.255',
        'community': '0pBiFbD',
        'subscription': '232.223.222.2',
        'endpoint': '193.17.9.3'
    },
    {
        'router': 'mx1.lon.uk.geant.net',
        'oid': '1.3.6.1.2.1.83.1.1.2.1.16.232.223.222.1.193.17.9.3.255.255.255.255',
        'community': '0pBiFbD',
        'subscription': '232.223.222.1',
        'endpoint': '193.17.9.3'
    },
    {
        'router': 'mx1.lon.uk.geant.net',
        'oid': '1.3.6.1.2.1.83.1.1.2.1.16.232.223.222.2.193.17.9.3.255.255.255.255',
        'community': '0pBiFbD',
        'subscription': '232.223.222.2',
        'endpoint': '193.17.9.3'
    },
    {
        'router': 'mx1.fra.de.geant.net',
        'oid': '1.3.6.1.2.1.83.1.1.2.1.16.232.223.222.1.193.17.9.3.255.255.255.255',
        'community': '0pBiFbD',
        'subscription': '232.223.222.1',
        'endpoint': '193.17.9.3'
    },
    {
        'router': 'mx1.fra.de.geant.net',
        'oid': '1.3.6.1.2.1.83.1.1.2.1.16.232.223.222.2.193.17.9.3.255.255.255.255',
        'community': '0pBiFbD',
        'subscription': '232.223.222.2',
        'endpoint': '193.17.9.3'
    }
]

NREN_REGIONS = [
    {
        "nren": "URAN",
        "region": "EAP"
    },
    {
        "nren": "ASNET-AM",
        "region": "EAP"
    },
    {
        "nren": "AZSCIENCENET",
        "region": "EAP"
    },
    {
        "nren": "GRENA",
        "region": "EAP"
    },
    {
        "nren": "RENAM",
        "region": "EAP"
    }
]


def generate_folder(data):
    return {
        "id": 555,
        "uid": data['uid'],
        "title": data['title'],
        "url": f"/dashboards/f/{data['uid']}/{data['title'].lower()}",
        "hasAcl": False,
        "canSave": True,
        "canEdit": True,
        "canAdmin": True,
        "createdBy": "Anonymous",
        "created": "2021-02-23T15:33:46Z",
        "updatedBy": "Anonymous",
        "updated": "2021-02-23T15:33:46Z",
        "version": 1
    }


@pytest.fixture
def reporting_provider(get_test_data, data_config):
    responses.add(
        method=responses.GET,
        url=f"{data_config['reporting_provider']}/scid/current",
        json=get_test_data("services.json"),
    )


@pytest.fixture
def populate_inventory(get_test_data, data_config):
    """function-fixture for provisioning inventory provider. Call it with a dictionary
    {url_path: contents}. ie {"/poller/interfaces": [...]}
    """

    def _populate(contents_dict):
        for path, contents in contents_dict.items():
            responses.add(
                method=responses.GET,
                url=f"{data_config['inventory_provider']}{path}",
                json=contents,
            )

    return _populate


@responses.activate
@pytest.mark.parametrize(
    "folder_name, excluded_nrens, expected_nrens",
    [
        ("NREN Access", [], {'ASNET-AM', 'CESNET', 'GEANT', 'KIAE', 'LITNET', 'SWITCH'}),
        ("NREN Access", ["GEANT", "KIAE"], {'ASNET-AM', 'CESNET', 'LITNET', 'SWITCH'}),
        (
            "NREN Access",
            [],
            {'ASNET-AM', "LITNET", "CESNET", "GEANT", "KIAE", "SWITCH"},
        ),
        (
            "NREN Access",
            ["GEANT"],
            {'ASNET-AM', "LITNET", "CESNET", "KIAE", "SWITCH"},
        ),
        ("testfolder", ["GEANT"], {"KIAE", "SWITCH"}),
    ],
)
def test_provision_nren_folder(
        folder_name,
        excluded_nrens,
        expected_nrens,
        data_config,
        mock_grafana,
        reporting_provider,
        populate_inventory,
):
    dashboards = {
        "NREN": {
            "tag": ["customers"],
            "folder_name": "NREN Access",
            "interfaces": [
                iface for iface in TEST_INTERFACES if "NREN" in iface["dashboards"]
            ],
        },
        "RE_CUST": {
            "tag": "RE_CUST",
            "folder_name": "RE Customer",
            "interfaces": [
                iface for iface in TEST_INTERFACES if "RE_CUST" in iface["dashboards"]
            ],
        },
    }
    populate_inventory(
        {
            "/poller/interfaces": NREN_INTERFACES,
            "/data/interfaces": NREN_INTERFACES,
            "/poller/eumetsat-multicast": EUMETSAT_MULTICAST,
            "/poller/regions": NREN_REGIONS,
        }
    )

    services = fetch_services(data_config['reporting_provider'])
    regions = get_nren_regions(data_config['inventory_provider'])

    result = [f.result() for f in provision_folder(
        ThreadPoolExecutor(),
        mock_grafana.request,
        folder_name,
        dashboards["NREN"],
        services,
        regions,
        "testdatasource",
        excluded_nrens,
    )]
    nrens = set([r["title"] for r in result])
    assert nrens == expected_nrens
    for i, nren in enumerate(nrens):
        if "NREN" in folder_name:
            # Every NREN dashboard must have at least 3 panels
            # (1 text, 2 graphs at least, some also have rows/dropdowns with panels)
            panel_count = 0
            for panel in result[i]["panels"]:
                if panel["type"] == "row":
                    for panel in panel["panels"]:
                        panel_count += 1
                else:
                    panel_count += 1
            assert panel_count >= 3


@responses.activate
def test_provision(
        data_config, mocker, mock_grafana, reporting_provider, populate_inventory
):
    mock_grafana.create_datasource(
        {
            "name": "brian-influx-datasource",
            "type": "influxdb",
            "access": "proxy",
            "url": "http://test-brian-datasource.geant.org:8086",
            "database": "test-db",
            "basicAuth": False,
            "isDefault": True,
            "readOnly": False,
        }
    )
    populate_inventory(
        {
            "/poller/interfaces": NREN_INTERFACES,
            "/data/interfaces": NREN_INTERFACES,
            "/poller/eumetsat-multicast": EUMETSAT_MULTICAST,
            "/poller/regions": NREN_REGIONS,
        }
    )
    for org in data_config["organizations"][1:]:
        mock_grafana.create_organization(org)

    mock_grafana.create_dashboard({"title": "testdashboard", "version": 1})

    _mocked_gws = mocker.patch(
        'brian_dashboard_manager.grafana.provision.get_gws_direct')
    _mocked_gws.return_value = []

    _mocked_gws_indirect = mocker.patch(
        'brian_dashboard_manager.grafana.provision.get_gws_indirect')
    _mocked_gws_indirect.return_value = []

    provision(data_config)


@responses.activate
def test_provision_re_peer_dashboard(
        mocker, data_config, mock_grafana, reporting_provider, populate_inventory
):
    interfaces = [
        {
            "router": "mx1.dub2.ie.geant.net",
            "name": "xe-0/0/0.1",
            "description": "PHY SVC P_AE10 SRF9948758 | HEANET-AP2-LL3",
            "dashboards": ["RE_PEER"],
            "dashboard_info": {"name": "ESNET", "interface_type": "LOGICAL"},
            "dashboards_info": [{"name": "ESNET", "interface_type": "LOGICAL"}],
            "ipv4": ["1.1.1.1"],
            "ipv6": ["::2"],
        },
    ]
    populate_inventory(
        {
            "/poller/interfaces": interfaces,
            "/data/interfaces": interfaces,
            "/poller/eumetsat-multicast": EUMETSAT_MULTICAST,
            "/poller/regions": NREN_REGIONS,
        }
    )
    _mocked_gws = mocker.patch(
        "brian_dashboard_manager.grafana.provision.get_gws_direct"
    )
    _mocked_gws.return_value = []

    _mocked_gws_indirect = mocker.patch(
        "brian_dashboard_manager.grafana.provision.get_gws_indirect"
    )
    _mocked_gws_indirect.return_value = []
    data_config["organizations"] = [
        {"name": "Testorg1", "excluded_nrens": ["GEANT"], "excluded_dashboards": []},
    ]
    provision(data_config)
    folder_uid = "RE_Peer"
    assert len(mock_grafana.dashboards_by_folder_uid[folder_uid]) == 1
    panels = mock_grafana.dashboards_by_folder_uid[folder_uid][0]["panels"]
    expected_types = ["text", "graph", "graph", "row", "graph", "row"]
    assert [p["type"] for p in panels] == expected_types
    assert "INFO" in panels[0]["options"]["content"]
    assert "ingress" in panels[1]["title"]
    assert "egress" in panels[2]["title"]
    assert "Services" in panels[3]["title"]
    assert "traffic" in panels[4]["title"]
    assert "IPv6" in panels[5]["title"]
    assert len(panels[-1]['panels'][0]) > 0


@responses.activate
def test_vlan_interfaces(mocker, data_config, mock_grafana, reporting_provider, populate_inventory
                         ):
    interfaces = [
        {'bundle': ['et-4/0/0', 'et-5/0/5', 'et-8/1/2', 'et-8/1/5'],
         'bundle-parents': ['et-4/0/0', 'et-5/0/5', 'et-8/1/2', 'et-8/1/5'], 'circuits': [],
         'dashboard_info': {'interface_type': 'AGGREGATE', 'name': 'REDIRIS'}, 'dashboards': ['NREN', 'RE_CUST'],
         'dashboards_info': [{'interface_type': 'AGGREGATE', 'name': 'REDIRIS'}],
         'description': 'LAG CUSTOMER REDIRIS SRF21114 $GA-01800 |', 'ipv4': [], 'ipv6': [], 'name': 'ae16',
         'port_type': 'ACCESS', 'router': 'mx1.mad.es.geant.net', 'snmp-index': 654, 'vlan_type': 'TRUNK'}, {'bundle': [], 'bundle-parents': ['et-4/0/0', 'et-5/0/5', 'et-8/1/2', 'et-8/1/5'], 'circuits': [
             {'id': 739804, 'name': 'UC3M-REDIRIS-BELNET-SLICES-IMEC', 'status': 'non-monitored', 'type': 'GEANT PLUS'}],
            'dashboard_info': {'interface_type': 'LOGICAL', 'name': 'REDIRIS'},
            'dashboards': ["VLAN Interfaces"],
            'dashboards_info': [{'interface_type': 'LOGICAL', 'name': 'REDIRIS'},
                                {'interface_type': 'LOGICAL', 'name': 'BELNET'}],
            'description': 'SRV_L2CIRCUIT CUSTOMER REDIRIS BELNET #UC3M-RedIRIS-BELNET-SLICES-IMEC $GS-02514 |',
            'ipv4': [], 'ipv6': [], 'name': 'ae16.975', 'port_type': 'SERVICE', 'router': 'mx1.mad.es.geant.net',
            'snmp-index': 818, 'vlan_type': 'VLAN'}, {'bundle': [], 'bundle-parents': ['et-4/0/0', 'et-5/0/5', 'et-8/1/2', 'et-8/1/5'], 'circuits': [
                {'id': 732759, 'name': 'FRA-MAD-RARE-REDIRIS-23017-VL201', 'status': 'non-monitored',
                 'type': 'GEANT PLUS'}], 'dashboard_info': {'interface_type': 'LOGICAL', 'name': 'RARE'},
            'dashboards': ['L2_CIRCUIT'], 'dashboards_info': [{'interface_type': 'LOGICAL', 'name': 'RARE'},
                                                              {'interface_type': 'LOGICAL', 'name': 'REDIRIS'}],
            'description': 'SRV_L2CIRCUIT CUSTOMER RARE REDIRIS #fra-mad-RARE-REDIRIS-23017-VL201 $GS-02274', 'ipv4': [],
            'ipv6': [], 'name': 'ae16.201', 'port_type': 'SERVICE', 'router': 'mx1.mad.es.geant.net', 'snmp-index': 642,
            'vlan_type': 'VLAN'}, {'bundle': [], 'bundle-parents': [], 'circuits': [
                {'id': 729417, 'name': 'PAR-LON2-SUPERPOP-QFX-2-GEANT', 'status': 'operational', 'type': 'GEANT - GBS'}],
            'dashboard_info': {'interface_type': 'LOGICAL', 'name': 'GEANT-IT'}, 'dashboards': ['GBS_10G'],
            'dashboards_info': [{'interface_type': 'LOGICAL', 'name': 'GEANT-IT'}],
            'description': 'SRV_10GGBS CUSTOMER GEANT-IT #par-lon2-SUPERPOP-QFX-2-GEANT $GS-00081 |', 'ipv4': [],
            'ipv6': [], 'name': 'xe-2/2/7.0', 'port_type': 'SERVICE', 'router': 'mx1.lon2.uk.geant.net', 'snmp-index': 613,
            'vlan_type': 'VLAN'}, {'bundle': ['xe-0/1/0', 'xe-0/1/1'], 'bundle-parents': ['xe-0/1/0', 'xe-0/1/1'], 'circuits': [],
                                   'dashboard_info': {'interface_type': 'AGGREGATE', 'name': 'LITNET'}, 'dashboards': ['NREN', 'RE_CUST'],
                                   'dashboards_info': [{'interface_type': 'AGGREGATE', 'name': 'LITNET'}],
                                   'description': 'LAG CUSTOMER LITNET AP2 #LITNET-AP2-LAG $GA-02071 |', 'ipv4': [], 'ipv6': [], 'name': 'ae10',
                                   'port_type': 'ACCESS', 'router': 'art1.kau.lt.geant.net', 'snmp-index': 588, 'vlan_type': 'TRUNK'}, {'bundle': [], 'bundle-parents': ['et-0/0/2'], 'circuits': [
                                       {'id': 679356, 'name': 'LAT-AP1-IPV6', 'status': 'operational', 'type': 'GEANT IP'}],
            'dashboard_info': {'interface_type': 'LOGICAL', 'name': 'LAT'},
            'dashboards': ['NREN', 'RE_CUST'],
            'dashboards_info': [{'interface_type': 'LOGICAL', 'name': 'LAT'}],
            'description': 'SRV_GLOBAL CUSTOMER LAT #LAT-AP1 $GS-00484 | ASN5538',
            'ipv4': ['62.40.124.237/30'], 'ipv6': ['2001:798:99:1::51/126'], 'name': 'ae10.83',
            'port_type': 'SERVICE', 'router': 'art1.kau.lt.geant.net', 'snmp-index': 604,
            'vlan_type': 'VLAN'}, {'bundle': [], 'bundle-parents': ['et-2/1/2', 'et-2/1/5', 'et-11/1/0'], 'circuits': [
                {'id': 707643, 'name': 'GARR-UDMILANO_EXPRESSROUTE_VLAN4086', 'status': 'operational',
                 'type': 'EXPRESS ROUTE'}], 'dashboard_info': {'interface_type': 'LOGICAL', 'name': 'GARR'},
            'dashboards': ['GCS'], 'dashboards_info': [{'interface_type': 'LOGICAL', 'name': 'GARR'}],
            'description': 'SRV_GCS CUSTOMER GARR MICROSOFT #GARR-UDMilano_ExpressRoute_Vlan4086 $GS-01148 | UNIT CONFIGURATION HAS BEEN SYSTEM GENERATED',
            'ipv4': [], 'ipv6': [], 'name': 'ae10.4086', 'port_type': 'SERVICE', 'router': 'art1.kau.lt.geant.net',
            'snmp-index': 795, 'vlan_type': 'VLAN'}, {'bundle': [], 'bundle-parents': [], 'circuits': [],
                                                      'dashboard_info': {'interface_type': 'PHYSICAL', 'name': 'BUD-ZAG'}, 'dashboards': ['INFRASTRUCTURE_BACKBONE'],
                                                      'dashboards_info': [{'interface_type': 'PHYSICAL', 'name': 'BUD-ZAG'}],
                                                      'description': 'PHY INFRASTRUCTURE BACKBONE P_ae5 | BUD-ZAG', 'ipv4': [], 'ipv6': [], 'name': 'et-7/0/2',
                                                      'port_type': 'UNKNOWN', 'router': 'mx1.bud.hu.geant.net', 'snmp-index': 1133, 'vlan_type': 'ACCESS'},

    ]
    populate_inventory(
        {
            "/poller/interfaces": interfaces,
            "/data/interfaces": interfaces,
            "/poller/eumetsat-multicast": EUMETSAT_MULTICAST,
            "/poller/regions": NREN_REGIONS,
        }
    )
    _mocked_gws = mocker.patch(
        "brian_dashboard_manager.grafana.provision.get_gws_direct"
    )
    _mocked_gws.return_value = []

    _mocked_gws_indirect = mocker.patch(
        "brian_dashboard_manager.grafana.provision.get_gws_indirect"
    )
    _mocked_gws_indirect.return_value = []

    data_config["organizations"] = [
        {"name": "Testorg1", "excluded_nrens": ["GEANT"], "excluded_dashboards": []},
    ]
    provision(data_config)
    folder_uid = "VLAN_Interfaces"

    result = mock_grafana.dashboards_by_folder_uid[folder_uid]
    assert len(result) == 3

    assert result[0]["tags"][0] == "vlandash"
    dashboards = mock_grafana.dashboards_by_folder_uid[folder_uid]
    panels = []
    for dashboard in dashboards:
        panels.extend(dashboard["panels"])
    expected_types = ["text", "row", "text", "row", "text", "row"]
    assert [p["type"] for p in panels] == expected_types
    assert "INFO" in panels[0]["options"]["content"]
    assert "ae10" in set(p['title'] for p in panels)

    panel_ae10 = [p for p in panels if p['title'] == 'ae10'][0]
    assert len(panel_ae10["panels"]) == 3


@responses.activate
@pytest.mark.parametrize(
    "folder_name, dashboard_id, expected_dashboard_count",
    [
        ('NREN Access', 'NREN', 6),
        ('EAP NREN Access', 'EAP', 1)
    ]
)
def test_provision_nren_category(
        folder_name,
        dashboard_id,
        expected_dashboard_count,
        data_config,
        mock_grafana,
        reporting_provider,
        populate_inventory,
):
    nren_interfaces = [
        iface for iface in TEST_INTERFACES if "NREN" in iface["dashboards"]
    ]
    eap_interfaces = [
        iface for iface in TEST_INTERFACES if "EAP" in iface["dashboards"]
    ]
    dashboards = {
        "NREN": {
            "tag": ["customers"],
            "folder_name": "NREN Access",
            "interfaces": nren_interfaces,
        },
        "EAP": {
            'region': 'EAP',
            "tag": ["eap"],
            "folder_name": "EAP NREN Access",
            "interfaces": eap_interfaces,
        },
    }
    populate_inventory(
        {
            "/poller/interfaces": NREN_INTERFACES,
            "/data/interfaces": NREN_INTERFACES,
            "/poller/eumetsat-multicast": EUMETSAT_MULTICAST,
            "/poller/regions": NREN_REGIONS,
        }
    )

    services = fetch_services(data_config['reporting_provider'])
    regions = get_nren_regions(data_config['inventory_provider'])

    result = list(provision_folder(
        ThreadPoolExecutor(),
        mock_grafana.request,
        folder_name,
        dashboards[dashboard_id],
        services,
        regions,
        "testdatasource",
        [],
    ))
    assert len(result) == expected_dashboard_count