akarneliuk / pygnmi

The pure Python implementation of the gNMI client.
https://training.karneliuk.com
BSD 3-Clause "New" or "Revised" License
127 stars 44 forks source link

Info regarding creating the config for delete operation using pyangbind #28

Closed apathak1990 closed 2 years ago

apathak1990 commented 3 years ago

Hi @akarneliuk , I am trying to generate config for delete operation using pyangbind but some how I am not able to do it getting exception as Exception: Conversion of gNMI paths to the Protobuf format failed do you have a working example of delete operation via pygNMI which is working with pyangbind ? pls suggest

In [5]: intf=ifmgr.interface_configurations.interface_configuration.add(active="act",interface_name="Optics0/2/0/0")                                                                                    

In [6]: print(pybindJSON.dumps(ifmgr,mode="ietf"))                                                                                                                                                      
{
    "Cisco-IOS-XR-ifmgr-cfg:interface-configurations": {
        "interface-configuration": [
            {
                "active": "act",
                "interface-name": "Optics0/2/0/0"
            }
        ]
    }
}

In [7]: pybindJSON.dump(ifmgr,"test.json",mode="ietf") 
   ...: fn=open("test.json") 
   ...: data=json.load(fn) 
   ...: set_list = [(k, v) for k, v in data.items()]                                                                                                                                                    

In [8]:     with gNMIclient(target=host, username='test2',password='cisco123', path_cert='/Volumes/avpathak/BossHogg/openconfig/ems_BH_P2A4.pem',override='ems.cisco.com',debug=True) as gc: 
   ...:         result = gc.set(delete=set_list) 
   ...:         print(result) 
   ...:                                                                                                                                                                                                 
Conversion of gNMI paths to the Protobuf format failed
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
~/anaconda3/lib/python3.7/site-packages/pygnmi/client.py in set(self, delete, replace, update, encoding)
    399                 try:
--> 400                     del_protobuf_paths = [gnmi_path_generator(pe) for pe in delete]
    401 

~/anaconda3/lib/python3.7/site-packages/pygnmi/client.py in <listcomp>(.0)
    399                 try:
--> 400                     del_protobuf_paths = [gnmi_path_generator(pe) for pe in delete]
    401 

~/anaconda3/lib/python3.7/site-packages/pygnmi/path_generator.py in gnmi_path_generator(path_in_question)
     16     if path_in_question:
---> 17         if re.match(r'.*?\[.+?=.+?\].*?', path_in_question):
     18             split_list = re.findall(r'.*?\[.+?=.+?\].*?', path_in_question)

~/anaconda3/lib/python3.7/re.py in match(pattern, string, flags)
    172     a Match object, or None if no match was found."""
--> 173     return _compile(pattern, flags).match(string)
    174 

TypeError: expected string or bytes-like object

During handling of the above exception, another exception occurred:

Exception                                 Traceback (most recent call last)
<ipython-input-8-fb313a024e28> in <module>
      1 with gNMIclient(target=host, username='test2',password='cisco123', path_cert='/Volumes/avpathak/BossHogg/openconfig/ems_BH_P2A4.pem',override='ems.cisco.com',debug=True) as gc:
----> 2     result = gc.set(delete=set_list)
      3     print(result)
      4 

~/anaconda3/lib/python3.7/site-packages/pygnmi/client.py in set(self, delete, replace, update, encoding)
    402                 except:
    403                     logger.error(f'Conversion of gNMI paths to the Protobuf format failed')
--> 404                     raise Exception (f'Conversion of gNMI paths to the Protobuf format failed')
    405 
    406             else:

Exception: Conversion of gNMI paths to the Protobuf format failed

In [9]:  
akarneliuk commented 3 years ago

Hello @apathak1990 ,

Thanks a lot for a question. Per gNMI specification, for protobuf Delete() message you shall provide only a path, which in case of pygnmi is provided as a string: https://github.com/openconfig/gnmi/blob/c69a5df04b5329d70e3e76afa773669527cfad9b/proto/gnmi/gnmi.proto#L344

Hence, I'd suggest to rebuild a script in a way you specify that as a sting, rather than a dictionary, sir.

Let me know if that works for you.

Best, Anton

apathak1990 commented 3 years ago

Thanks @akarneliuk Let me try that out and create a script to do this

akarneliuk commented 3 years ago

Hello @apathak1990 ,

hop you are doing well. Did you have a chance to review that?

Thanks, Anton

apathak1990 commented 3 years ago

hi @akarneliuk , I am planning to do this week.

akarneliuk commented 2 years ago

Hello @apathak1990 ,

How was your results, sir?

Best, Anton

apathak1990 commented 2 years ago

Hi @akarneliuk

i tried i am getting below error. I may need to format it more properly

pls give me one more week to knock it off

Exception: <_InactiveRpcError of RPC that terminated with:
    status = StatusCode.ABORTED
    details = "gNMI: set-request delete: unknown-element: Request is not wellformed: invalid character 'i' after object key"
    debug_error_string = "{"created":"@1633191198.184499000","description":"Error received from peer ipv4:10.127.60.177:57400","file":"src/core/lib/surface/call.cc","file_line":1056,"grpc_message":"gNMI: set-request delete: unknown-element: Request is not wellformed: invalid character 'i' after object key","grpc_status":10}"
>

In [44]:                                                                                                                                                                                                

In [44]: f_list                                                                                                                                                                                         
Out[44]: ['Cisco-IOS-XR-ifmgr-cfg:interface-configurations/{"interface-configuration": [{"active": "act", "interface-name": "Optics0/2/0/0"}]}']
akarneliuk commented 2 years ago

@apathak1990 , if you have somewhere in lab ios xr, which we could connect to for gnni tests, we could help, probably. Unfortunately, we don't have it in lab.

akarneliuk commented 2 years ago

Hey @apathak1990 , please, open a new ticket if you still have an issue. Best, Anton