python ttp parser and template indentation

Viewed 254

I'm using python ttp parser to parse network device configuration file. For example, I have the following text content of a vprn service ( the indentation of the first line is 8 blank-space characters ):

    vprn 5000122 customer 99999 create
        description "LastMod: Jan 28, 2022 2:33:11 AM"
        vrf-import "VPRN-5000122-IMPORT"
        vrf-export "VPRN-5000122-EXPORT"
        router-id 95.95.95.95
        autonomous-system 5555
        route-distinguisher 4444:8888888
        auto-bind-tunnel
            resolution-filter
                ldp
            exit
            resolution filter
        exit
        interface "*dummy-1* L3 interface"  create
            description "*** single blank-space description ***"
            address 199.199.199.199/32
        exit
        interface "*dummy-2* L3 interface"  create
            description "***  double blank-space description ***"
            address 201.201.201.201/32
        exit
        service-name "dumy    vprn-service"
        no shutdown
    exit

When I create the ttp template, I have to have the exact same indentation to match the line in the config data, link the following:

<group name="service">
<group name="vprn.{{ service_id }}**">
    vprn {{service_id | _start_ }} customer {{customer_id}} create
        description "{{ description | ORPHRASE | default("")}}"
        vrf-import "{{ vrf_import }}"
        vrf-export "{{ vrf_export }}"
        autonomous-system {{ autonous_system }}
        route-distinguisher {{ route_distinguisher }}
    <group name="interface.{{interface_id}}**">
        interface "{{ interface_id | _start_ }}" create
            description "{{ description | ORPHRASE }}"
            address {{ ipv4_address }}/{{ipv4_mask}}
            ip-mtu {{ ip_mtu }}
        exit {{ _end_ }}
    </group>
        service-name "{{ service_name }}"
        no shutdown {{ state | set("enabled") | default("disabled") }}
    exit {{ _end_ }}
</group>

In the config file, if I remove 4 blank-space characters from the beginning of each line, then I will have to make similar change to the template, otherwise the parsed output will be empty.

Is there some settings in the parser that would allow me to use the same template for config data with different indentation size?

Thanks

1 Answers

I use TTP often. In such cases, I add one more of the same data so that it can capture similar data according to the gaps, so that I can provide the data. (Example: Hop Limit, Hop Limit_2)

Example Code:

ttp_template = """
<group name="MPLS_Lsp_Path_Detail">
LSP Name         : {{LSP_Name}}
Path LSP ID      : {{Path_LSP_id}}
From             : {{From}}         To                   : {{To}}
Admin State      : {{Admin_State}}                   Oper State           : {{Oper_State}}
Path Name        : {{Path_Name}} Path Type            : {{Path_Type}}
Path Admin       : {{Path_Admin_State}}                   Path Oper            : {{Path_Oper_State}}
Out Interface    : {{Out_Interface}}            Out Label            : {{Out_Label}}
Path Up Time     : {{Path_Uptime_Date}} {{Path_Uptime_Time}}         Path Down Time       : 0d 00:00:00
Retry Limit      : 0                    Retry Timer          : {{Retry_Time}} sec
Neg MTU          : {{Neg_Mtu}}                 Oper MTU             : {{Oper_Mtu}}
Hop Limit        : {{Hop_Limit}}                  Oper HopLimit        : {{Oper_Hop_Limit}}
Hop Limit_2        :    {{Hop_Limit2}}                     Oper HopLimit        :    {{Oper_Hop_Limit2}}
Hold Priority    : {{Hold_Priority}}                    Oper Hold Priority   : 0
</group>
"""

parser = ttp(data=data_to_parse, template=ttp_template)
parser.parse()

# print result in JSON format
results = parser.result(format='json')[0]
print(results)

Example run:

[
    {
        "MPLS_Lsp_Path_Detail": [
            {
                "Admin_State": "Up",
                "From": "10.95.204.13",
                "Hold_Priority": "0",
                "Hop_Limit": "255",
                "LSP_Name": "lsp_to_A_CUSTOMER",
                "Neg_Mtu": "1982",
                "Oper_Hop_Limit": "255",
                "Oper_Mtu": "1982",
                "Oper_State": "Up",
                "Out_Interface": "1/1/5:481",
                "Out_Label": "7452",
                "Path_Admin_State": "Up",
                "Path_LSP_id": "14922",
                "Path_Name": "lsp_to_B_CUSTOMER",
                "Path_Oper_State": "Up",
                "Path_Type": "Standby",
                "Path_Uptime_Date": "10d",
                "Path_Uptime_Time": "21:12:42",
                "Retry_Time": "30",
                "To": "10.36.111.104"
            },
            {
                "Admin_State": "Up",
                "From": "156.67.255.16",
                "Hold_Priority": "0",
                "Hop_Limit": "255",
                "LSP_Name": "gla126-1-to-lon38-1",
                "Neg_Mtu": "1500",
                "Oper_Hop_Limit": "255",
                "Oper_Mtu": "1500",
                "Oper_State": "Up",
                "Out_Interface": "lag-50",
                "Out_Label": "34631",
                "Path_Admin_State": "Up",
                "Path_Oper_State": "Up",
                "Path_Uptime_Date": "0d",
                "Path_Uptime_Time": "20:36:35",
                "Retry_Time": "30",
                "To": "78.33.254.104"
            }
        ]
    }
]
            
Related