import json
from pprint import pprint
raw_service_objects = [
    {
        "name": "service_tcp_destination",
        "description": "TCP: Only destination",
        "type": "tcp",
        "destination": "1"
    },
    {
        "name": "service_udp_all",
        "description": "UDP: source and destination",
        "type": "udp",
        "source": "2",
        "destination": "1"
    },
    {
        "name": "service_icmp_all",
        "description": "ICMP: type + code",
        "type": "icmp",
        "icmp_type": "1",
        "icmp_code": "2"
    },
    {
        "name": "service_icmp6_all",
        "description": "ICMP6: type + code",
        "type": "icmp",
        "icmp_type": "254",
        "icmp_code": "0"
    },
    {
        "name": "service_protocol_23",
        "description": "Protocol: 23",
        "protocol": "23"
    },
    {
        "name": "service_protocol_name",
        "description": "Protocol: Name \"ip\"",
        "protocol": "ip"
    },
    {
        "name": "service_tcp_source",
        "description": "TCP: Only source",
        "type": "tcp",
        "source": "123"
    },
    {
        "name": "service_tcp_all",
        "description": "TCP: destination and source",
        "type": "tcp",
        "source": "2",
        "destination": "1"
    },
    {
        "name": "service_udp_source",
        "description": "UDP: Only source",
        "type": "udp",
        "source": "1"
    },
    {
        "name": "service_udp_destination",
        "description": "UDP: only destination",
        "type": "udp",
        "destination": "1"
    },
    {
        "name": "service_icmp_type",
        "description": "ICMP: only type",
        "type": "icmp"
    },
    {
        "name": "service_icmp6_type",
        "description": "ICMP6: type only",
        "type": "icmp"
    },
    {
        "name": "service_protocol_12",
        "description": "Protocol: 12",
        "protocol": "12"
    },
    {
        "name": "service_tcp_no_descr",
        "type": "tcp",
        "source": "2",
        "destination": "1"
    },
    {
        "name": "tcp_obj",
        "description": "descr",
        "type": "tcp",
        "destination": "455"
    },
    {
        "name": "udp_obj",
        "description": "descr",
        "type": "udp",
        "destination": "456"
    },
    {
        "name": "icmp_obj",
        "description": "descr",
        "type": "icmp",
        "icmp_type": "250",
        "icmp_code": "5"
    },
    {
        "name": "icmp6_obj",
        "description": "descr",
        "type": "icmp",
        "icmp_type": "250",
        "icmp_code": "5"
    },
    {
        "name": "protocol_obj",
        "description": "descr",
        "protocol": "252"
    }
]

service_objects = {}
for item in raw_service_objects:
    name = item["name"]
    service_objects[name] = item

base_service_groups = {
    "test_service_group": {
        "name": "test_service_group",
        "type": "",
        "description": "",
        "members": [
            {
                "service_object_name": "service_udp_all"
            },
            {
                "service_object_name": "service_tcp_destination"
            }
        ]
    },
    "tcpudp_service_grp": {
        "name": "tcpudp_service_grp",
        "type": "tcp-udp",
        "description": "",
        "members": [
            {
                "port": "discard"
            },
            {
                "port": "nfs"
            }
        ]
    },
    "test_tcp_grp": {
        "name": "test_tcp_grp",
        "type": "tcp",
        "description": "TCP group description",
        "members": [
            {
                "port": "aol"
            },
            {
                "port": "citrix-ica"
            }
        ]
    },
    "udp_test_service_group": {
        "name": "udp_test_service_group",
        "type": "udp",
        "description": "",
        "members": [
            {
                "port": "biff"
            },
            {
                "port": "www"
            }
        ]
    },
    "UDP_grp_test": {
        "name": "UDP_grp_test",
        "type": "udp",
        "description": "Description",
        "members": [
            {
                "group_name": "tcpudp_service_grp"
            },
            {
                "port": "23"
            },
            {
                "port_range_start": "23",
                "port_range_finish": "bootps"
            },
            {
                "port": "www"
            }
        ]
    },
    "TCP_GROUP_all": {
        "name": "TCP_GROUP_all",
        "type": "tcp",
        "description": "description",
        "members": [
            {
                "port_range_start": "1",
                "port_range_finish": "65535"
            },
            {
                "port": "5"
            },
            {
                "port_range_start": "5",
                "port_range_finish": "255"
            },
            {
                "port": "aol"
            },
            {
                "port": "bgp"
            },
            {
                "port": "chargen"
            },
            {
                "port": "cifs"
            },
            {
                "port": "citrix-ica"
            },
            {
                "port": "ctiqbe"
            },
            {
                "port": "daytime"
            },
            {
                "port": "discard"
            },
            {
                "port": "domain"
            },
            {
                "port": "echo"
            },
            {
                "port": "exec"
            },
            {
                "port": "finger"
            },
            {
                "port": "ftp"
            },
            {
                "port": "ftp-data"
            },
            {
                "port": "gopher"
            },
            {
                "port": "h323"
            },
            {
                "port": "hostname"
            },
            {
                "port": "www"
            },
            {
                "port": "https"
            },
            {
                "port": "ident"
            },
            {
                "port": "imap4"
            },
            {
                "port": "irc"
            },
            {
                "port": "kerberos"
            },
            {
                "port": "klogin"
            },
            {
                "port": "kshell"
            },
            {
                "port": "ldap"
            },
            {
                "port": "ldaps"
            },
            {
                "port": "login"
            },
            {
                "port": "lotusnotes"
            },
            {
                "port": "lpd"
            },
            {
                "port": "netbios-ssn"
            },
            {
                "port": "nfs"
            },
            {
                "port": "nntp"
            },
            {
                "port": "pcanywhere-data"
            },
            {
                "port": "pim-auto-rp"
            },
            {
                "port": "pop2"
            },
            {
                "port": "pop3"
            },
            {
                "port": "pptp"
            },
            {
                "port": "rsh"
            },
            {
                "port": "rtsp"
            },
            {
                "port": "sip"
            },
            {
                "port": "smtp"
            },
            {
                "port": "sqlnet"
            },
            {
                "port": "ssh"
            },
            {
                "port": "sunrpc"
            },
            {
                "port": "tacacs"
            },
            {
                "port": "talk"
            },
            {
                "port": "telnet"
            },
            {
                "port": "uucp"
            },
            {
                "port": "whois"
            },
            {
                "group_name": "tcpudp_service_grp"
            },
            {
                "group_name": "test_tcp_grp"
            }
        ]
    },
    "TCP_GROUP1": {
        "name": "TCP_GROUP1",
        "type": "tcp",
        "description": "description",
        "members": [
            {
                "group_name": "TCP_GROUP_all"
            },
            {
                "group_name": "tcpudp_service_grp"
            }
        ]
    },
    "TCP_GROUP2": {
        "name": "TCP_GROUP2",
        "type": "tcp",
        "description": "",
        "members": [
            {
                "port": "54"
            },
            {
                "port_range_start": "54",
                "port_range_finish": "255"
            },
            {
                "port": "aol"
            },
            {
                "port": "bgp"
            }
        ]
    },
    "UDP_GROUP1": {
        "name": "UDP_GROUP1",
        "type": "udp",
        "description": "description",
        "members": [
            {
                "group_name": "tcpudp_service_grp"
            },
            {
                "group_name": "udp_test_service_group"
            }
        ]
    },
    "UDP_GROUP2": {
        "name": "UDP_GROUP2",
        "type": "udp",
        "description": "",
        "members": [
            {
                "port": "356"
            },
            {
                "port": "bootps"
            },
            {
                "port": "cifs"
            },
            {
                "port": "discard"
            }
        ]
    },
    "UDP_GROUP3": {
        "name": "UDP_GROUP3",
        "type": "udp",
        "description": "description",
        "members": [
            {
                "port": "305"
            },
            {
                "port": "45"
            },
            {
                "port": "47"
            }
        ]
    },
    "UDP_GROUP4": {
        "name": "UDP_GROUP4",
        "type": "udp",
        "description": "",
        "members": [
            {
                "port_range_start": "5",
                "port_range_finish": "35"
            },
            {
                "port_range_start": "78",
                "port_range_finish": "6557"
            }
        ]
    },
    "UDP_GROUP_all": {
        "name": "UDP_GROUP_all",
        "type": "udp",
        "description": "description",
        "members": [
            {
                "group_name": "UDP_grp_test"
            },
            {
                "group_name": "tcpudp_service_grp"
            },
            {
                "port": "0"
            },
            {
                "port_range_start": "0",
                "port_range_finish": "2"
            },
            {
                "port_range_start": "54",
                "port_range_finish": "2456"
            },
            {
                "port": "56"
            },
            {
                "port": "biff"
            },
            {
                "port": "bootpc"
            },
            {
                "port": "bootps"
            },
            {
                "port": "cifs"
            },
            {
                "port": "discard"
            },
            {
                "port": "dnsix"
            },
            {
                "port": "domain"
            },
            {
                "port": "echo"
            },
            {
                "port": "www"
            },
            {
                "port": "isakmp"
            },
            {
                "port": "kerberos"
            },
            {
                "port": "mobile-ip"
            },
            {
                "port": "nameserver"
            },
            {
                "port": "netbios-dgm"
            },
            {
                "port": "netbios-ns"
            },
            {
                "port": "nfs"
            },
            {
                "port": "ntp"
            },
            {
                "port": "pcanywhere-status"
            },
            {
                "port": "pim-auto-rp"
            },
            {
                "port": "radius"
            },
            {
                "port": "radius-acct"
            },
            {
                "port": "rip"
            },
            {
                "port": "secureid-udp"
            },
            {
                "port": "sip"
            },
            {
                "port": "snmp"
            },
            {
                "port": "snmptrap"
            },
            {
                "port": "sunrpc"
            },
            {
                "port": "syslog"
            },
            {
                "port": "tacacs"
            },
            {
                "port": "talk"
            },
            {
                "port": "tftp"
            },
            {
                "port": "time"
            },
            {
                "port": "who"
            },
            {
                "port": "xdmcp"
            },
            {
                "group_name": "udp_test_service_group"
            }
        ]
    },
    "TCP_GROUP": {
        "name": "TCP_GROUP",
        "type": "tcp",
        "description": "long descriptiooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooon",
        "members": [
            {
                "port": "aol"
            },
            {
                "port": "bgp"
            },
            {
                "port": "chargen"
            }
        ]
    },
    "TCP-UDP_GROUP1": {
        "name": "TCP-UDP_GROUP1",
        "type": "tcp-udp",
        "description": "",
        "members": [
            {
                "group_name": "tcpudp_service_grp"
            }
        ]
    },
    "TCP-UDP_GROUP2": {
        "name": "TCP-UDP_GROUP2",
        "type": "tcp-udp",
        "description": "",
        "members": [
            {
                "port": "cifs"
            },
            {
                "port": "discard"
            },
            {
                "port": "domain"
            },
            {
                "port": "echo"
            },
            {
                "port": "www"
            },
            {
                "port": "kerberos"
            },
            {
                "port": "nfs"
            }
        ]
    },
    "TCP-UDP_GROUP3": {
        "name": "TCP-UDP_GROUP3",
        "type": "tcp-udp",
        "description": "description",
        "members": [
            {
                "port": "2030"
            },
            {
                "port": "45"
            }
        ]
    },
    "TCP-UDP_GROUP4": {
        "name": "TCP-UDP_GROUP4",
        "type": "tcp-udp",
        "description": "",
        "members": [
            {
                "port_range_start": "43",
                "port_range_finish": "654"
            },
            {
                "port_range_start": "687",
                "port_range_finish": "6554"
            }
        ]
    },
    "TCP-UDP_GROUP_all": {
        "name": "TCP-UDP_GROUP_all",
        "type": "tcp-udp",
        "description": "description",
        "members": [
            {
                "port_range_start": "2000",
                "port_range_finish": "2100"
            },
            {
                "port": "56"
            },
            {
                "port_range_start": "56",
                "port_range_finish": "6574"
            },
            {
                "port": "cifs"
            },
            {
                "port": "discard"
            },
            {
                "port": "domain"
            },
            {
                "port": "echo"
            },
            {
                "port": "www"
            },
            {
                "port": "kerberos"
            },
            {
                "port": "nfs"
            },
            {
                "port": "pim-auto-rp"
            },
            {
                "port": "sip"
            },
            {
                "port": "sunrpc"
            },
            {
                "port": "tacacs"
            },
            {
                "port": "talk"
            },
            {
                "group_name": "tcpudp_service_grp"
            }
        ]
    },
    "SERVICE_GROUP1": {
        "name": "SERVICE_GROUP1",
        "type": "",
        "description": "description",
        "members": [
            {
                "group_name": "test_service_group"
            }
        ]
    },
    "SERVICE_GROUP2": {
        "name": "SERVICE_GROUP2",
        "type": "",
        "description": "description",
        "members": [
            {
                "service_object_name": "service_icmp6_all"
            },
            {
                "service_object_name": "service_icmp_all"
            },
            {
                "service_object_name": "service_protocol_23"
            },
            {
                "service_object_name": "service_protocol_name"
            },
            {
                "service_object_name": "service_tcp_all"
            },
            {
                "service_object_name": "service_tcp_destination"
            },
            {
                "service_object_name": "service_tcp_source"
            },
            {
                "service_object_name": "service_udp_all"
            },
            {
                "service_object_name": "service_udp_destination"
            },
            {
                "service_object_name": "service_udp_source"
            }
        ]
    },
    "SERVICE_GROUP3": {
        "name": "SERVICE_GROUP3",
        "type": "",
        "description": "",
        "members": [
            {
                "protocol": "ip"
            },
            {
                "protocol": "icmp"
            },
            {
                "protocol": "snp"
            },
            {
                "protocol": "udp"
            },
            {
                "protocol": "igmp"
            },
            {
                "protocol": "ipinip"
            },
            {
                "protocol": "gre"
            },
            {
                "protocol": "esp"
            },
            {
                "protocol": "icmp6"
            },
            {
                "protocol": "tcp"
            },
            {
                "protocol": "igrp"
            },
            {
                "protocol": "icmp",
                "icmp_type": "echo"
            },
            {
                "protocol": "icmp",
                "icmp_type": "echo-reply"
            },
            {
                "protocol": "icmp",
                "icmp_type": "information-reply"
            },
            {
                "protocol": "icmp",
                "icmp_type": "information-request"
            },
            {
                "protocol": "icmp",
                "icmp_type": "mask-reply"
            },
            {
                "protocol": "tcp-udp",
                "destination_port": "www"
            },
            {
                "protocol": "tcp-udp",
                "destination_port": "kerberos"
            },
            {
                "protocol": "tcp-udp",
                "destination_port": "sip"
            },
            {
                "protocol": "tcp-udp",
                "destination_port": "sunrpc"
            },
            {
                "protocol": "tcp",
                "destination_port": "bgp"
            },
            {
                "protocol": "tcp",
                "destination_port": "chargen"
            },
            {
                "protocol": "tcp",
                "destination_port": "cifs"
            },
            {
                "protocol": "tcp",
                "destination_port": "citrix-ica"
            },
            {
                "protocol": "tcp",
                "destination_port": "ctiqbe"
            },
            {
                "protocol": "tcp",
                "destination_port": "daytime"
            },
            {
                "protocol": "tcp",
                "destination_port": "discard"
            },
            {
                "protocol": "tcp",
                "destination_port": "domain"
            },
            {
                "protocol": "tcp",
                "destination_port": "echo"
            },
            {
                "protocol": "udp",
                "destination_port": "isakmp"
            },
            {
                "protocol": "udp",
                "destination_port": "kerberos"
            },
            {
                "protocol": "udp",
                "destination_port": "mobile-ip"
            },
            {
                "protocol": "udp",
                "destination_port": "nameserver"
            }
        ]
    },
    "SERVICE_GROUP4": {
        "name": "SERVICE_GROUP4",
        "type": "",
        "description": "description",
        "members": [
            {
                "service_object_name": "icmp6_obj"
            },
            {
                "service_object_name": "icmp_obj"
            },
            {
                "service_object_name": "protocol_obj"
            },
            {
                "protocol": "tcp-udp",
                "destination_port": "456"
            },
            {
                "service_object_name": "tcp_obj"
            },
            {
                "service_object_name": "udp_obj"
            }
        ]
    },
    "SERVICE_GROUP_all": {
        "name": "SERVICE_GROUP_all",
        "type": "",
        "description": "description",
        "members": [
            {
                "protocol": "ip"
            },
            {
                "protocol": "udp"
            },
            {
                "protocol": "ah"
            },
            {
                "protocol": "tcp"
            },
            {
                "protocol": "icmp",
                "icmp_type": "echo"
            },
            {
                "protocol": "icmp6",
                "icmp_type": "router-solicitation"
            },
            {
                "service_object_name": "icmp6_obj"
            },
            {
                "service_object_name": "protocol_obj"
            },
            {
                "service_object_name": "service_icmp_all"
            },
            {
                "service_object_name": "service_tcp_destination"
            },
            {
                "protocol": "tcp-udp",
                "destination_port": "kerberos"
            },
            {
                "protocol": "tcp",
                "destination_port": "chargen"
            },
            {
                "protocol": "tcp",
                "destination_port": "cifs"
            },
            {
                "group_name": "test_service_group"
            },
            {
                "protocol": "udp",
                "destination_port": "nfs"
            },
            {
                "service_object_name": "udp_obj"
            }
        ]
    },
    "ip_serv_group_1": {
        "name": "ip_serv_group_1",
        "type": "",
        "description": "",
        "members": [
            {
                "group_name": "SERVICE_GROUP1"
            },
            {
                "group_name": "SERVICE_GROUP2"
            },
            {
                "service_object_name": "service_icmp_all"
            },
            {
                "service_object_name": "udp_obj"
            }
        ]
    },
    "ip_serv_group2": {
        "name": "ip_serv_group2",
        "type": "",
        "description": "",
        "members": [
            {
                "group_name": "SERVICE_GROUP3"
            },
            {
                "service_object_name": "icmp_obj"
            },
            {
                "group_name": "ip_serv_group_1"
            }
        ]
    },
    "ip_serv_group3": {
        "name": "ip_serv_group3",
        "type": "",
        "description": "",
        "members": [
            {
                "group_name": "ip_serv_group2"
            },
            {
                "service_object_name": "tcp_obj"
            },
            {
                "group_name": "test_service_group"
            }
        ]
    },
    "tcp_serv_group1": {
        "name": "tcp_serv_group1",
        "type": "tcp",
        "description": "",
        "members": [
            {
                "group_name": "TCP_GROUP"
            },
            {
                "group_name": "TCP_GROUP1"
            }
        ]
    },
    "tcp_serv_group2": {
        "name": "tcp_serv_group2",
        "type": "tcp",
        "description": "",
        "members": [
            {
                "port": "bgp"
            },
            {
                "port": "chargen"
            },
            {
                "group_name": "tcp_serv_group1"
            }
        ]
    },
    "tcp_serv_group3": {
        "name": "tcp_serv_group3",
        "type": "tcp",
        "description": "",
        "members": [
            {
                "group_name": "tcp_serv_group1"
            },
            {
                "group_name": "tcp_serv_group2"
            }
        ]
    },
    "tcpudp_serv_group1": {
        "name": "tcpudp_serv_group1",
        "type": "tcp-udp",
        "description": "",
        "members": [
            {
                "group_name": "TCP-UDP_GROUP2"
            }
        ]
    },
    "tcpudp_serv_group2": {
        "name": "tcpudp_serv_group2",
        "type": "tcp-udp",
        "description": "",
        "members": [
            {
                "group_name": "TCP-UDP_GROUP3"
            },
            {
                "port": "kerberos"
            },
            {
                "port": "nfs"
            },
            {
                "group_name": "tcpudp_serv_group1"
            }
        ]
    },
    "tcpudp_serv_group3": {
        "name": "tcpudp_serv_group3",
        "type": "tcp-udp",
        "description": "",
        "members": [
            {
                "group_name": "tcpudp_serv_group1"
            },
            {
                "group_name": "tcpudp_serv_group2"
            }
        ]
    }
}

icmp_service_groups = {
    "icmp_test_grp": {
        "name": "icmp_test_grp",
        "type": "icmp",
        "description": "",
        "members": [
            {
                "icmp_object_name": "mobile-redirect"
            },
            {
                "icmp_object_name": "router-advertisement"
            }
        ]
    },
    "ICMP_GROUP1": {
        "name": "ICMP_GROUP1",
        "type": "icmp",
        "description": "",
        "members": [
            {
                "group_name": "icmp_test_grp"
            }
        ]
    },
    "ICMP_GROUP2": {
        "name": "ICMP_GROUP2",
        "type": "icmp",
        "description": "description",
        "members": [
            {
                "icmp_object_name": "echo"
            },
            {
                "icmp_object_name": "echo-reply"
            },
            {
                "icmp_object_name": "information-reply"
            },
            {
                "icmp_object_name": "information-request"
            },
            {
                "icmp_object_name": "redirect"
            }
        ]
    },
    "ICMP_GROUP3": {
        "name": "ICMP_GROUP3",
        "type": "icmp",
        "description": "description",
        "members": [
            {
                "icmp_object_name": "146"
            },
            {
                "icmp_object_name": "33"
            },
            {
                "icmp_object_name": "43"
            }
        ]
    },
    "ICMP_GROUP4_all": {
        "name": "ICMP_GROUP4_all",
        "type": "icmp",
        "description": "description",
        "members": [
            {
                "group_name": "ICMP_GROUP1"
            },
            {
                "group_name": "ICMP_GROUP2"
            },
            {
                "group_name": "ICMP_GROUP3"
            },
            {
                "icmp_object_name": "74"
            },
            {
                "icmp_object_name": "alternate-address"
            },
            {
                "icmp_object_name": "conversion-error"
            },
            {
                "icmp_object_name": "echo"
            },
            {
                "icmp_object_name": "echo-reply"
            },
            {
                "icmp_object_name": "information-reply"
            },
            {
                "icmp_object_name": "information-request"
            },
            {
                "icmp_object_name": "mask-reply"
            },
            {
                "icmp_object_name": "mask-request"
            },
            {
                "icmp_object_name": "mobile-redirect"
            },
            {
                "icmp_object_name": "parameter-problem"
            },
            {
                "icmp_object_name": "redirect"
            },
            {
                "icmp_object_name": "router-advertisement"
            },
            {
                "icmp_object_name": "router-solicitation"
            },
            {
                "icmp_object_name": "source-quench"
            },
            {
                "icmp_object_name": "time-exceeded"
            },
            {
                "icmp_object_name": "timestamp-reply"
            },
            {
                "icmp_object_name": "timestamp-request"
            },
            {
                "icmp_object_name": "traceroute"
            },
            {
                "icmp_object_name": "unreachable"
            },
            {
                "group_name": "icmp_test_grp"
            }
        ]
    },
    "icmp_serv_group1": {
        "name": "icmp_serv_group1",
        "type": "icmp",
        "description": "",
        "members": [
            {
                "group_name": "ICMP_GROUP1"
            },
            {
                "group_name": "ICMP_GROUP2"
            }
        ]
    },
    "icmp_serv_group2": {
        "name": "icmp_serv_group2",
        "type": "icmp",
        "description": "descr",
        "members": [
            {
                "group_name": "ICMP_GROUP3"
            },
            {
                "icmp_object_name": "echo"
            },
            {
                "icmp_object_name": "echo-reply"
            },
            {
                "group_name": "icmp_serv_group1"
            },
            {
                "group_name": "icmp_test_grp"
            }
        ]
    },
    "icmp_serv_group3": {
        "name": "icmp_serv_group3",
        "type": "icmp",
        "description": "descr",
        "members": [
            {
                "group_name": "ICMP_GROUP3"
            },
            {
                "icmp_object_name": "mask-request"
            },
            {
                "icmp_object_name": "mobile-redirect"
            },
            {
                "group_name": "icmp_serv_group1"
            },
            {
                "group_name": "icmp_serv_group2"
            }
        ]
    }
}

protocol_service_groups = {
    "TCPUDP": {
        "name": "TCPUDP",
        "type": "protocol",
        "description": "",
        "members": [
            {
                "protocol_object": "udp"
            },
            {
                "protocol_object": "tcp"
            }
        ]
    },
    "DM_INLINE_PROTOCOL_1": {
        "name": "DM_INLINE_PROTOCOL_1",
        "type": "protocol",
        "description": "",
        "members": [
            {
                "protocol_object": "icmp"
            },
            {
                "protocol_object": "8"
            }
        ]
    },
    "test_protocol_grp": {
        "name": "test_protocol_grp",
        "type": "protocol",
        "description": "",
        "members": [
            {
                "protocol_object": "esp"
            },
            {
                "protocol_object": "igrp"
            }
        ]
    },
    "PROTO_GROUP": {
        "name": "PROTO_GROUP",
        "type": "protocol",
        "description": "description",
        "members": [
            {
                "protocol_object": "ip"
            },
            {
                "protocol_object": "udp"
            },
            {
                "protocol_object": "5"
            },
            {
                "protocol_object": "icmp6"
            },
            {
                "protocol_object": "78"
            },
            {
                "group_name": "TCPUDP"
            }
        ]
    },
    "PROTO_GROUP2": {
        "name": "PROTO_GROUP2",
        "type": "protocol",
        "description": "",
        "members": [
            {
                "group_name": "TCPUDP"
            }
        ]
    },
    "PROTO_GROUP3": {
        "name": "PROTO_GROUP3",
        "type": "protocol",
        "description": "description",
        "members": [
            {
                "protocol_object": "100"
            },
            {
                "protocol_object": "23"
            },
            {
                "protocol_object": "24"
            }
        ]
    },
    "PROTO_GROUP4": {
        "name": "PROTO_GROUP4",
        "type": "protocol",
        "description": "description",
        "members": [
            {
                "protocol_object": "ip"
            },
            {
                "protocol_object": "icmp"
            },
            {
                "protocol_object": "udp"
            },
            {
                "protocol_object": "gre"
            },
            {
                "protocol_object": "tcp"
            }
        ]
    },
    "PROTO_GROUP5_all": {
        "name": "PROTO_GROUP5_all",
        "type": "protocol",
        "description": "description",
        "members": [
            {
                "protocol_object": "ip"
            },
            {
                "protocol_object": "icmp"
            },
            {
                "protocol_object": "pim"
            },
            {
                "protocol_object": "pcp"
            },
            {
                "protocol_object": "snp"
            },
            {
                "protocol_object": "udp"
            },
            {
                "protocol_object": "igmp"
            },
            {
                "protocol_object": "255"
            },
            {
                "protocol_object": "ipinip"
            },
            {
                "protocol_object": "45"
            },
            {
                "protocol_object": "gre"
            },
            {
                "protocol_object": "esp"
            },
            {
                "protocol_object": "ah"
            },
            {
                "protocol_object": "icmp6"
            },
            {
                "protocol_object": "tcp"
            },
            {
                "protocol_object": "eigrp"
            },
            {
                "protocol_object": "ospf"
            },
            {
                "protocol_object": "igrp"
            },
            {
                "protocol_object": "nos"
            },
            {
                "group_name": "PROTO_GROUP"
            },
            {
                "group_name": "PROTO_GROUP2"
            },
            {
                "group_name": "PROTO_GROUP3"
            },
            {
                "group_name": "PROTO_GROUP4"
            },
            {
                "group_name": "TCPUDP"
            },
            {
                "group_name": "test_protocol_grp"
            }
        ]
    },
    "proto_serv_group1": {
        "name": "proto_serv_group1",
        "type": "protocol",
        "description": "descr",
        "members": [
            {
                "protocol_object": "ip"
            },
            {
                "protocol_object": "icmp"
            },
            {
                "protocol_object": "udp"
            },
            {
                "protocol_object": "gre"
            },
            {
                "protocol_object": "esp"
            },
            {
                "protocol_object": "icmp6"
            },
            {
                "protocol_object": "tcp"
            },
            {
                "group_name": "PROTO_GROUP"
            },
            {
                "group_name": "PROTO_GROUP2"
            }
        ]
    },
    "proto_serv_group2": {
        "name": "proto_serv_group2",
        "type": "protocol",
        "description": "descr",
        "members": [
            {
                "protocol_object": "ip"
            },
            {
                "protocol_object": "ipinip"
            },
            {
                "protocol_object": "igrp"
            },
            {
                "group_name": "PROTO_GROUP"
            },
            {
                "group_name": "TCPUDP"
            },
            {
                "group_name": "proto_serv_group1"
            }
        ]
    }
}

service_groups = base_service_groups.copy()
service_groups.update(icmp_service_groups)
service_groups.update(protocol_service_groups)


def parse_member(service_member, parsed_members):
    """kek"""
    service_object = service_member.get("service_object_name", None)
    group_name = service_member.get("group_name", None)
    if service_object:
        parsed_members.append(service_objects[service_object])
        return parsed_members
    if group_name:
        group_to_reparse = service_groups[service_member["group_name"]]
        inner_service_members = group_to_reparse["members"]
        for in_member in inner_service_members:
            parse_member(in_member, parsed_members)
    else:
        parsed_members.append(service_member)
        return parsed_members


parsed_services = {}
for service_name, service_inners in service_groups.items():
    tmp_members = []
    for member in service_inners["members"]:
        parse_member(member, tmp_members)
    del service_inners["members"]
    parsed_services[service_name] = service_inners
    parsed_services[service_name]["members"] = tmp_members

f = open("services_groups.json", "a")
json.dump(parsed_services, f)
f.close()
